-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.jl
47 lines (40 loc) · 1.38 KB
/
example.jl
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
using HTTP, JSON, LibPQ, Tables
conn = LibPQ.Connection("dbname=pgvector_example host=localhost")
execute(conn, "CREATE EXTENSION IF NOT EXISTS vector")
execute(conn, "DROP TABLE IF EXISTS documents")
execute(conn, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(1536))")
module Pgvector
convert(v::AbstractVector{T}) where T<:Real = string("[", join(v, ","), "]")
end
function embed(input)
url = "https://api.openai.com/v1/embeddings"
data = Dict(
"input" => input,
"model" => "text-embedding-3-small"
)
headers = [
"authorization" => string("Bearer ", ENV["OPENAI_API_KEY"]),
"content-type" => "application/json"
]
r = HTTP.request("POST", url, headers, JSON.json(data))
[map(identity, v["embedding"]) for v in JSON.parse(String(r.body))["data"]]
end
input = [
"The dog is barking",
"The cat is purring",
"The bear is growling"
]
embeddings = embed(input)
LibPQ.load!(
(content = input, embedding = map(Pgvector.convert, embeddings)),
conn,
"INSERT INTO documents (content, embedding) VALUES (\$1, \$2)",
)
query = "forest"
embedding = embed([query])[1]
result = execute(conn, "SELECT content FROM documents ORDER BY embedding <=> \$1 LIMIT 5", [Pgvector.convert(embedding)])
rows = Tables.rows(columntable(result))
for row in rows
println(Tables.getcolumn(row, 1))
end
close(conn)