diff --git a/README.md b/README.md index b435d0d..1532853 100644 --- a/README.md +++ b/README.md @@ -29,13 +29,19 @@ db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))") Insert vectors ```nim -db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", "[1,1,1]", "[2,2,2]", "[1,1,2]") +import std/json + +let embedding1 = %* @[1, 1, 1]; +let embedding2 = %* @[1, 1, 2]; +let embedding3 = %* @[2, 2, 2]; +db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", embedding1, embedding2, embedding3) ``` Get the nearest neighbors ```nim -let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", "[1,1,1]") +let embedding = %* @[1, 1, 1]; +let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding) for row in rows: echo row ``` diff --git a/example.nim b/example.nim index 83a76ea..25aece1 100644 --- a/example.nim +++ b/example.nim @@ -1,4 +1,5 @@ import db_connector/db_postgres +import std/json let db = db_postgres.open("localhost", "", "", "pgvector_nim_test") @@ -7,11 +8,15 @@ db.exec(sql"DROP TABLE IF EXISTS items") db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))") -db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", "[1,1,1]", "[2,2,2]", "[1,1,2]") +let embedding1 = %* @[1, 1, 1]; +let embedding2 = %* @[1, 1, 2]; +let embedding3 = %* @[2, 2, 2]; +db.exec(sql"INSERT INTO items (embedding) VALUES (?), (?), (?)", embedding1, embedding2, embedding3) -let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", "[1,1,1]") +let embedding = %* @[1, 1, 1]; +let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding) for row in rows: - echo row + echo row[0], ": ", parseJson(row[1]) db.exec(sql"CREATE INDEX ON items USING hnsw (embedding vector_l2_ops)")