-
-
Notifications
You must be signed in to change notification settings - Fork 697
/
Copy pathpinecone.go
247 lines (208 loc) · 6.23 KB
/
pinecone.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package pinecone
import (
"context"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/pinecone-io/go-pinecone/pinecone"
"github.com/tmc/langchaingo/embeddings"
"github.com/tmc/langchaingo/schema"
"github.com/tmc/langchaingo/vectorstores"
"google.golang.org/protobuf/types/known/structpb"
)
var (
// ErrMissingTextKey is returned in SimilaritySearch if a vector
// from the query is missing the text key.
ErrMissingTextKey = errors.New("missing text key in vector metadata")
// ErrEmbedderWrongNumberVectors is returned when if the embedder returns a number
// of vectors that is not equal to the number of documents given.
ErrEmbedderWrongNumberVectors = errors.New(
"number of vectors from embedder does not match number of documents",
)
// ErrEmptyResponse is returned if the API gives an empty response.
ErrEmptyResponse = errors.New("empty response")
ErrInvalidScoreThreshold = errors.New(
"score threshold must be between 0 and 1")
)
// Store is a wrapper around the pinecone rest API and grpc client.
type Store struct {
embedder embeddings.Embedder
client *pinecone.Client
host string
apiKey string
textKey string
nameSpace string
}
// New creates a new Store with options. Options for WithAPIKey, WithHost and WithEmbedder must be set.
func New(opts ...Option) (Store, error) {
s, err := applyClientOptions(opts...)
if err != nil {
return Store{}, err
}
s.client, err = pinecone.NewClient(pinecone.NewClientParams{ApiKey: s.apiKey})
if err != nil {
return Store{}, err
}
return s, nil
}
// AddDocuments creates vector embeddings from the documents using the embedder
// and upsert the vectors to the pinecone index and returns the ids of the added documents.
func (s Store) AddDocuments(ctx context.Context,
docs []schema.Document,
options ...vectorstores.Option,
) ([]string, error) {
opts := s.getOptions(options...)
nameSpace := s.getNameSpace(opts)
indexConn, err := s.client.IndexWithNamespace(s.host, nameSpace)
if err != nil {
return nil, err
}
defer indexConn.Close()
texts := make([]string, 0, len(docs))
for _, doc := range docs {
texts = append(texts, doc.PageContent)
}
vectors, err := s.embedder.EmbedDocuments(ctx, texts)
if err != nil {
return nil, err
}
if len(vectors) != len(docs) {
return nil, ErrEmbedderWrongNumberVectors
}
metadatas := make([]map[string]any, 0, len(docs))
for i := 0; i < len(docs); i++ {
metadata := make(map[string]any, len(docs[i].Metadata))
for key, value := range docs[i].Metadata {
metadata[key] = value
}
metadata[s.textKey] = texts[i]
metadatas = append(metadatas, metadata)
}
pineconeVectors := make([]*pinecone.Vector, 0, len(vectors))
ids := make([]string, len(vectors))
for i := 0; i < len(vectors); i++ {
metadataStruct, err := structpb.NewStruct(metadatas[i])
if err != nil {
return nil, err
}
id := uuid.New().String()
ids[i] = id
pineconeVectors = append(
pineconeVectors,
&pinecone.Vector{
Id: id,
Values: vectors[i],
Metadata: metadataStruct,
},
)
}
_, err = indexConn.UpsertVectors(&ctx, pineconeVectors)
if err != nil {
return nil, err
}
return ids, nil
}
// SimilaritySearch creates a vector embedding from the query using the embedder
// and queries to find the most similar documents.
func (s Store) SimilaritySearch(ctx context.Context, query string, numDocuments int, options ...vectorstores.Option) ([]schema.Document, error) { //nolint:lll
opts := s.getOptions(options...)
nameSpace := s.getNameSpace(opts)
indexConn, err := s.client.IndexWithNamespace(s.host, nameSpace)
if err != nil {
return nil, err
}
defer indexConn.Close()
var protoFilterStruct *structpb.Struct
filters := s.getFilters(opts)
if filters != nil {
protoFilterStruct, err = s.createProtoStructFilter(filters)
if err != nil {
return nil, err
}
}
scoreThreshold, err := s.getScoreThreshold(opts)
if err != nil {
return nil, err
}
vector, err := s.embedder.EmbedQuery(ctx, query)
if err != nil {
return nil, err
}
queryResult, err := indexConn.QueryByVectorValues(
&ctx,
&pinecone.QueryByVectorValuesRequest{
Vector: vector,
TopK: uint32(numDocuments),
Filter: protoFilterStruct,
IncludeMetadata: true,
IncludeValues: true,
},
)
if err != nil {
return nil, err
}
if len(queryResult.Matches) == 0 {
return nil, ErrEmptyResponse
}
return s.getDocumentsFromMatches(queryResult, scoreThreshold)
}
func (s Store) getDocumentsFromMatches(queryResult *pinecone.QueryVectorsResponse, scoreThreshold float32) ([]schema.Document, error) {
resultDocuments := make([]schema.Document, 0)
for _, match := range queryResult.Matches {
metadata := match.Vector.Metadata.AsMap()
pageContent, ok := metadata[s.textKey].(string)
if !ok {
return nil, ErrMissingTextKey
}
delete(metadata, s.textKey)
doc := schema.Document{
PageContent: pageContent,
Metadata: metadata,
Score: match.Score,
}
// If scoreThreshold is not 0, we only return matches with a score above the threshold.
if scoreThreshold != 0 && match.Score >= scoreThreshold {
resultDocuments = append(resultDocuments, doc)
} else if scoreThreshold == 0 { // If scoreThreshold is 0, we return all matches.
resultDocuments = append(resultDocuments, doc)
}
}
return resultDocuments, nil
}
func (s Store) getNameSpace(opts vectorstores.Options) string {
if opts.NameSpace != "" {
return opts.NameSpace
}
return s.nameSpace
}
func (s Store) getScoreThreshold(opts vectorstores.Options) (float32, error) {
if opts.ScoreThreshold < 0 || opts.ScoreThreshold > 1 {
return 0, ErrInvalidScoreThreshold
}
return opts.ScoreThreshold, nil
}
func (s Store) getFilters(opts vectorstores.Options) any {
if opts.Filters != nil {
return opts.Filters
}
return nil
}
func (s Store) getOptions(options ...vectorstores.Option) vectorstores.Options {
opts := vectorstores.Options{}
for _, opt := range options {
opt(&opts)
}
return opts
}
func (s Store) createProtoStructFilter(filter any) (*structpb.Struct, error) {
filterBytes, err := json.Marshal(filter)
if err != nil {
return nil, err
}
var filterStruct structpb.Struct
err = json.Unmarshal(filterBytes, &filterStruct)
if err != nil {
return nil, err
}
return &filterStruct, nil
}