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
30 lines (23 loc) · 997 Bytes
/
Copy pathexample.lua
File metadata and controls
30 lines (23 loc) · 997 Bytes
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
localpgmoon=require("pgmoon")
localpgvector=require("./src/pgvector")
localpg=pgmoon.new({
database="pgvector_lua_test",
user=os.getenv("USER")
})
assert(pg:connect())
assert(pg:query("CREATE EXTENSION IF NOT EXISTS vector"))
assert(pg:query("DROP TABLE IF EXISTS items"))
assert(pg:query("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"))
localembedding1=pgvector.new({1, 1, 1})
localembedding2=pgvector.new({2, 2, 2})
localembedding3=pgvector.new({1, 1, 2})
assert(pg:query("INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", embedding1, embedding2, embedding3))
-- optional: automatically deserialize vector types
pgvector.setup_vector(pg)
localembedding=pgvector.new({1, 1, 1})
localres=assert(pg:query("SELECT * FROM items ORDER BY embedding <-> $1 LIMIT 5", embedding))
fori, rowinipairs(res) do
print(row["id"])
print(row["embedding"])
end
assert(pg:query("CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)"))