-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.go
166 lines (143 loc) · 3.61 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/jackc/pgx/v5"
"github.com/pgvector/pgvector-go"
pgxvector "github.com/pgvector/pgvector-go/pgx"
)
func main() {
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)
}
_, err = conn.Exec(ctx, "DROP TABLE IF EXISTS documents")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "CREATE TABLE documents (id bigserial PRIMARY KEY, content text, embedding vector(768))")
if err != nil {
panic(err)
}
_, err = conn.Exec(ctx, "CREATE INDEX ON documents USING GIN (to_tsvector('english', content))")
if err != nil {
panic(err)
}
input := []string{
"The dog is barking",
"The cat is purring",
"The bear is growling",
}
embeddings, err := FetchEmbeddings(input)
if err != nil {
panic(err)
}
for i, content := range input {
_, err := conn.Exec(ctx, "INSERT INTO documents (content, embedding) VALUES ($1, $2)", content, pgvector.NewVector(embeddings[i]))
if err != nil {
panic(err)
}
}
sql := `
WITH semantic_search AS (
SELECT id, RANK () OVER (ORDER BY embedding <=> $2) AS rank
FROM documents
ORDER BY embedding <=> $2
LIMIT 20
),
keyword_search AS (
SELECT id, RANK () OVER (ORDER BY ts_rank_cd(to_tsvector('english', content), query) DESC)
FROM documents, plainto_tsquery('english', $1) query
WHERE to_tsvector('english', content) @@ query
ORDER BY ts_rank_cd(to_tsvector('english', content), query) DESC
LIMIT 20
)
SELECT
COALESCE(semantic_search.id, keyword_search.id) AS id,
COALESCE(1.0 / ($3 + semantic_search.rank), 0.0) +
COALESCE(1.0 / ($3 + keyword_search.rank), 0.0) AS score
FROM semantic_search
FULL OUTER JOIN keyword_search ON semantic_search.id = keyword_search.id
ORDER BY score DESC
LIMIT 5
`
query := "growling bear"
queryEmbedding, err := FetchEmbeddings([]string{query})
if err != nil {
panic(err)
}
k := 60
rows, err := conn.Query(ctx, sql, query, pgvector.NewVector(queryEmbedding[0]), k)
if err != nil {
panic(err)
}
defer rows.Close()
for rows.Next() {
var id int64
var score float64
err = rows.Scan(&id, &score)
if err != nil {
panic(err)
}
fmt.Println("document:", id, "| RRF score:", score)
}
if rows.Err() != nil {
panic(rows.Err())
}
}
type apiRequest struct {
Input []string `json:"input"`
Model string `json:"model"`
}
func FetchEmbeddings(input []string) ([][]float32, error) {
url := "http://localhost:11434/api/embed"
data := &apiRequest{
Input: input,
Model: "nomic-embed-text",
}
b, err := json.Marshal(data)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(b))
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Bad status code: %d", resp.StatusCode)
}
var result map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
var embeddings [][]float32
for _, item := range result["embeddings"].([]interface{}) {
var embedding []float32
for _, v := range item.([]interface{}) {
embedding = append(embedding, float32(v.(float64)))
}
embeddings = append(embeddings, embedding)
}
return embeddings, nil
}