-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.jl
50 lines (42 loc) · 1.49 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
48
49
50
using HTTP, JSON, LibPQ, SparseArrays, 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 sparsevec(30522))")
module Pgvector
using SparseArrays
function convert(vec::AbstractSparseVector{Tv,Ti}) where {Tv<:Real,Ti<:Integer}
elements = [string(i, ":", v) for (i, v) in zip(vec.nzind, vec.nzval)]
string("{", join(elements, ","), "}/", vec.n)
end
end
function embed(inputs)
url = "http://localhost:3000/embed_sparse"
data = Dict(
"inputs" => inputs
)
headers = [
"content-type" => "application/json"
]
r = HTTP.request("POST", url, headers, JSON.json(data))
[sparsevec(map(e -> e["index"] + 1, v), map(e -> e["value"], v), 30522) for v in JSON.parse(String(r.body))]
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)