-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample.exs
66 lines (53 loc) · 1.66 KB
/
example.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Postgrex.Types.define(Example.PostgrexTypes, Pgvector.extensions(), [])
{:ok, pid} = Postgrex.start_link(database: "pgvector_example", types: Example.PostgrexTypes)
Postgrex.query!(pid, "CREATE EXTENSION IF NOT EXISTS vector", [])
Postgrex.query!(pid, "DROP TABLE IF EXISTS documents", [])
Postgrex.query!(
pid,
"CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding bit(1024))",
[]
)
defmodule Example do
def embed(texts, input_type) do
api_key = System.fetch_env!("CO_API_KEY")
url = "https://api.cohere.com/v1/embed"
data = %{
"texts" => texts,
"model" => "embed-english-v3.0",
"input_type" => input_type,
"embedding_types" => ["ubinary"]
}
response =
%HTTPoison.Response{status_code: 200} =
HTTPoison.post!(url, Jason.encode!(data), [
{"Authorization", "Bearer #{api_key}"},
{"Content-Type", "application/json"}
])
for e <- Jason.decode!(response.body)["embeddings"]["ubinary"] do
for v <- e, into: "", do: <<v::unsigned-size(8)>>
end
end
end
input = [
"The dog is barking",
"The cat is purring",
"The bear is growling"
]
embeddings = Example.embed(input, "search_document")
for {content, embedding} <- Enum.zip(input, embeddings) do
Postgrex.query!(pid, "INSERT INTO documents (content, embedding) VALUES ($1, $2)", [
content,
embedding
])
end
query = "forest"
query_embedding = Example.embed([query], "search_query") |> List.first()
result =
Postgrex.query!(
pid,
"SELECT id, content FROM documents ORDER BY embedding <~> $1 LIMIT 5",
[query_embedding]
)
for [id, content] <- result.rows do
IO.puts("#{id}: #{content}")
end