Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.lua
More file actions
Latest commit
71 lines (63 loc) · 2.05 KB
/
Copy pathexample.lua
File metadata and controls
71 lines (63 loc) · 2.05 KB
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
67
68
69
70
71
-- good resources
-- https://opensearch.org/blog/improving-document-retrieval-with-sparse-semantic-encoders/
-- https://huggingface.co/opensearch-project/opensearch-neural-sparse-encoding-v1
--
-- run with
-- text-embeddings-router --model-id opensearch-project/opensearch-neural-sparse-encoding-v1 --pooling splade
localcjson=require("cjson")
localhttp=require("socket.http")
localltn12=require("ltn12")
localpgmoon=require("pgmoon")
localpgvector=require("./src/pgvector")
localpg=pgmoon.new({
database="pgvector_example",
user=os.getenv("USER")
})
assert(pg:connect())
assert(pg:query("CREATE EXTENSION IF NOT EXISTS vector"))
assert(pg:query("DROP TABLE IF EXISTS documents"))
assert(pg:query("CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding sparsevec(30522))"))
functionembed(inputs)
localurl="http://localhost:3000/embed_sparse"
localdata= {
inputs=inputs,
}
localheaders= {
["Content-Type"] ="application/json"
}
localchunks= {}
localr, c, h=http.request {
method="POST",
url=url,
headers=headers,
source=ltn12.source.string(cjson.encode(data)),
sink=ltn12.sink.table(chunks)
}
assert(c==200)
localres=cjson.decode(table.concat(chunks))
localembeddings= {}
fori, iteminipairs(res) do
localembedding= {}
fori, vinipairs(item) do
embedding[v["index"] +1] =v["value"]
end
embeddings[i] =embedding
end
returnembeddings
end
localdocuments= {
"The dog is barking",
"The cat is purring",
"The bear is growling"
}
localembeddings=embed(documents)
fori, contentinipairs(documents) do
localembedding=embeddings[i]
assert(pg:query("INSERT INTO documents (content, embedding) VALUES ($1, $2)", content, pgvector.sparsevec(embedding, 30522)))
end
localquery="forest"
localembedding=embed({query})[1]
localres=assert(pg:query("SELECT content FROM documents ORDER BY embedding <#> $1 LIMIT 5", pgvector.sparsevec(embedding, 30522)))
fori, rowinipairs(res) do
print(row["content"])
end