-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
60 lines (54 loc) · 1.61 KB
/
Copy pathmain.go
File metadata and controls
60 lines (54 loc) · 1.61 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
// vec-keyed: vector search keyed by a string primary key (UUID / slug) instead
// of an int64 rowid, via vec.KeyedTable[string]. The KNN results come back keyed
// by your own string IDs.
//
// Run with:
//
// just example vec-keyed
package main
import (
"context"
"database/sql"
"fmt"
"log"
_ "gosqlite.org"
"gosqlite.org/vec"
)
func main() {
ctx := context.Background()
db, err := sql.Open("sqlite", ":memory:")
if err != nil {
log.Fatal(err)
}
defer db.Close()
// A vec0 table with an explicit `id text primary key` column.
tbl, err := vec.CreateKeyed[string](ctx, db, "docs", 4, vec.Options{Metric: vec.Cosine})
if err != nil {
log.Fatal(err)
}
if err := tbl.BatchInsert(ctx, []vec.KeyedRow[string]{
{Key: "doc-apple", Embedding: []float32{1, 0, 0, 0}},
{Key: "doc-banana", Embedding: []float32{0.9, 0.1, 0, 0}},
{Key: "doc-cherry", Embedding: []float32{0, 1, 0, 0}},
{Key: "doc-date", Embedding: []float32{0, 0, 1, 0}},
}); err != nil {
log.Fatal(err)
}
fmt.Println("3 nearest to [1,0,0,0] (results keyed by string ID):")
hits, err := tbl.KNNSlice(ctx, []float32{1, 0, 0, 0}, 3)
if err != nil {
log.Fatal(err)
}
for _, h := range hits {
fmt.Printf(" %-12s distance %.4f\n", h.Key, h.Distance)
}
// Update and delete address rows by their string key, just like the rowid API.
if err := tbl.Update(ctx, "doc-cherry", []float32{1, 0, 0, 0}); err != nil {
log.Fatal(err)
}
if err := tbl.Delete(ctx, "doc-date"); err != nil {
log.Fatal(err)
}
hits, _ = tbl.KNNSlice(ctx, []float32{1, 0, 0, 0}, 5)
fmt.Printf("\nAfter update+delete, %d rows; nearest = %q\n", len(hits), hits[0].Key)
}