-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
108 lines (85 loc) · 2.06 KB
/
main.go
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"context"
"fmt"
"math/rand"
"github.com/jackc/pgx/v5"
"github.com/pgvector/pgvector-go"
pgxvector "github.com/pgvector/pgvector-go/pgx"
)
func main() {
// generate random data
rows := 1000000
dimensions := 128
embeddings := make([][]float32, 0, rows)
for i := 0; i < rows; i++ {
embedding := make([]float32, 0, dimensions)
for j := 0; j < dimensions; j++ {
embedding = append(embedding, rand.Float32())
}
embeddings = append(embeddings, embedding)
}
// enable extension
ctx := context.Background()
conn, err := pgx.Connect(ctx, "postgres://localhost/pgvector_example")
if err != nil {
panic(err)
}
defer conn.Close(ctx)
_, err = conn.Exec(ctx, "CREATE EXTENSION IF NOT EXISTS vector")
if err != nil {
panic(err)
}
err = pgxvector.RegisterTypes(ctx, conn)
if err != nil {
panic(err)
}
// create table
_, err = conn.Exec(ctx, "DROP TABLE IF EXISTS items")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, fmt.Sprintf("CREATE TABLE items (id bigserial, embedding vector(%d))", dimensions))
if err != nil {
panic(err)
}
// load data
fmt.Printf("Loading %d rows\n", rows)
_, err = conn.CopyFrom(
ctx,
pgx.Identifier{"items"},
[]string{"embedding"},
pgx.CopyFromSlice(len(embeddings), func(i int) ([]any, error) {
if i%10000 == 0 {
fmt.Printf(".")
}
return []interface{}{pgvector.NewVector(embeddings[i])}, nil
}),
)
if err != nil {
panic(err)
}
fmt.Println("\nSuccess!")
// create any indexes *after* loading initial data (skipping for this example)
createIndex := false
if createIndex {
fmt.Println("Creating index")
_, err = conn.Exec(ctx, "SET maintenance_work_mem = '8GB'")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "SET max_parallel_maintenance_workers = 7")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops)")
if err != nil {
panic(err)
}
}
// update planner statistics for good measure
_, err = conn.Exec(ctx, "ANALYZE items")
if err != nil {
panic(err)
}
}