mirror of
https://github.com/asg017/sqlite-vec.git
synced 2026-05-04 21:32:41 +02:00
go rename
This commit is contained in:
parent
a0bc9404ce
commit
5be4c980ca
5 changed files with 0 additions and 0 deletions
1
examples/simple-go-cgo/.gitignore
vendored
Normal file
1
examples/simple-go-cgo/.gitignore
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
demo
|
||||
2
examples/simple-go-cgo/Makefile
Normal file
2
examples/simple-go-cgo/Makefile
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
demo: demo.go go.mod go.sum
|
||||
go build -o $@
|
||||
86
examples/simple-go-cgo/demo.go
Normal file
86
examples/simple-go-cgo/demo.go
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
sqlite_vec "github.com/asg017/sqlite-vec-go-bindings/cgo"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
sqlite_vec.Auto()
|
||||
db, err := sql.Open("sqlite3", ":memory:")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
var sqliteVersion string
|
||||
var vecVersion string
|
||||
err = db.QueryRow("select sqlite_version(), vec_version()").Scan(&sqliteVersion, &vecVersion)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("sqlite_version=%s, vec_version=%s\n", sqliteVersion, vecVersion)
|
||||
|
||||
_, err = db.Exec("CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[4])")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
items := map[int][]float32{
|
||||
1: {0.1, 0.1, 0.1, 0.1},
|
||||
2: {0.2, 0.2, 0.2, 0.2},
|
||||
3: {0.3, 0.3, 0.3, 0.3},
|
||||
4: {0.4, 0.4, 0.4, 0.4},
|
||||
5: {0.5, 0.5, 0.5, 0.5},
|
||||
}
|
||||
q := []float32{0.3, 0.3, 0.3, 0.3}
|
||||
|
||||
for id, values := range items {
|
||||
v, err := sqlite_vec.SerializeFloat32(values)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, err = db.Exec("INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)", id, v)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
query, err := sqlite_vec.SerializeFloat32(q)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT
|
||||
rowid,
|
||||
distance
|
||||
FROM vec_items
|
||||
WHERE embedding MATCH ?
|
||||
ORDER BY distance
|
||||
LIMIT 3
|
||||
`, query)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for rows.Next() {
|
||||
var rowid int64
|
||||
var distance float64
|
||||
err = rows.Scan(&rowid, &distance)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
fmt.Printf("rowid=%d, distance=%f\n", rowid, distance)
|
||||
}
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
log.Fatal((err))
|
||||
}
|
||||
|
||||
}
|
||||
7
examples/simple-go-cgo/go.mod
Normal file
7
examples/simple-go-cgo/go.mod
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
module github.com/asg017/sqlite-vec/examples/go
|
||||
|
||||
go 1.22.5
|
||||
|
||||
require github.com/mattn/go-sqlite3 v1.14.22
|
||||
|
||||
require github.com/asg017/sqlite-vec-go-bindings v0.0.1-alpha.36 // indirect
|
||||
6
examples/simple-go-cgo/go.sum
Normal file
6
examples/simple-go-cgo/go.sum
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
github.com/asg017/sqlite-vec-go-bindings v0.0.1-alpha.36 h1:FMGkKAA7nZL8gr/dvIx1uc54J3v2gbLVa+mLqZDCvjk=
|
||||
github.com/asg017/sqlite-vec-go-bindings v0.0.1-alpha.36/go.mod h1:A8+cTt/nKFsYCQF6OgzSNpKZrzNo5gQsXBTfsXHXY0Q=
|
||||
github.com/asg017/sqlite-vec/bindings/go/cgo v0.0.0-20240511043328-3d763f499859 h1:6jeFy/tSnyNJUrTHoIaFTYkjrHtwVAojvCGkr9G8d4o=
|
||||
github.com/asg017/sqlite-vec/bindings/go/cgo v0.0.0-20240511043328-3d763f499859/go.mod h1:Go89G54PaautWRwxvAa1fmKeYoSuUyIvSYpvlfXQaNU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
|
||||
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
|
||||
Loading…
Add table
Add a link
Reference in a new issue