Skip to content

Commit

Permalink
Improved example
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 31, 2023
1 parent e742e7e commit ae9bf0d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ Insert vectors
```nim
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)
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 embedding = %* @[1, 1, 1];
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding)
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
```
Expand Down
12 changes: 6 additions & 6 deletions example.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ db.exec(sql"DROP TABLE IF EXISTS items")

db.exec(sql"CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))")

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 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 embedding = %* @[1, 1, 1];
let rows = db.getAllRows(sql"SELECT * FROM items ORDER BY embedding <-> ? LIMIT 5", embedding)
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[0], ": ", parseJson(row[1])

Expand Down

0 comments on commit ae9bf0d

Please sign in to comment.