1
1
/*
2
- * Copyright 2023-2023 the original author or authors.
2
+ * Copyright 2023-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
47
47
* Pinecone index.
48
48
*
49
49
* @author Christian Tzolov
50
+ * @author Adam Bchouti
50
51
*/
51
52
public class PineconeVectorStore implements VectorStore {
52
53
@@ -60,18 +61,9 @@ public class PineconeVectorStore implements VectorStore {
60
61
61
62
private final PineconeConnection pineconeConnection ;
62
63
63
- private final ObjectMapper objectMapper ;
64
-
65
- private String pineconeNamespace ;
64
+ private final String pineconeNamespace ;
66
65
67
- /**
68
- * Change the Index Name.
69
- * @param pineconeNamespace The Azure VectorStore index name to use.
70
- */
71
- public void setPineconeNamespace (String pineconeNamespace ) {
72
- Assert .hasText (pineconeNamespace , "The Pinecone namespace can not be empty." );
73
- this .pineconeNamespace = pineconeNamespace ;
74
- }
66
+ private final ObjectMapper objectMapper ;
75
67
76
68
/**
77
69
* Configuration class for the PineconeVectorStore.
@@ -232,11 +224,11 @@ public PineconeVectorStore(PineconeVectorStoreConfig config, EmbeddingClient emb
232
224
}
233
225
234
226
/**
235
- * Adds a list of documents to the vector store.
227
+ * Adds a list of documents to the vector store based on the namespace .
236
228
* @param documents The list of documents to be added.
229
+ * @param namespace The namespace to add the documents to
237
230
*/
238
- @ Override
239
- public void add (List <Document > documents ) {
231
+ public void add (List <Document > documents , String namespace ) {
240
232
241
233
List <Vector > upsertVectors = documents .stream ().map (document -> {
242
234
// Compute and assign an embedding to the document.
@@ -251,12 +243,21 @@ public void add(List<Document> documents) {
251
243
252
244
UpsertRequest upsertRequest = UpsertRequest .newBuilder ()
253
245
.addAllVectors (upsertVectors )
254
- .setNamespace (this . pineconeNamespace )
246
+ .setNamespace (namespace )
255
247
.build ();
256
248
257
249
this .pineconeConnection .getBlockingStub ().upsert (upsertRequest );
258
250
}
259
251
252
+ /**
253
+ * Adds a list of documents to the vector store.
254
+ * @param documents The list of documents to be added.
255
+ */
256
+ @ Override
257
+ public void add (List <Document > documents ) {
258
+ add (documents , this .pineconeNamespace );
259
+ }
260
+
260
261
/**
261
262
* Converts the document metadata to a Protobuf Struct.
262
263
* @param document The document containing metadata.
@@ -286,15 +287,15 @@ private Value contentValue(Document document) {
286
287
}
287
288
288
289
/**
289
- * Deletes a list of documents by their IDs.
290
+ * Deletes a list of documents by their IDs based on the namespace .
290
291
* @param documentIds The list of document IDs to be deleted.
292
+ * @param namespace The namespace of the document IDs.
291
293
* @return An optional boolean indicating the deletion status.
292
294
*/
293
- @ Override
294
- public Optional <Boolean > delete (List <String > documentIds ) {
295
+ public Optional <Boolean > delete (List <String > documentIds , String namespace ) {
295
296
296
297
DeleteRequest deleteRequest = DeleteRequest .newBuilder ()
297
- .setNamespace (this . pineconeNamespace ) // ignored for free tier.
298
+ .setNamespace (namespace ) // ignored for free tier.
298
299
.addAllIds (documentIds )
299
300
.setDeleteAll (false )
300
301
.build ();
@@ -305,8 +306,17 @@ public Optional<Boolean> delete(List<String> documentIds) {
305
306
return Optional .of (true );
306
307
}
307
308
309
+ /**
310
+ * Deletes a list of documents by their IDs.
311
+ * @param documentIds The list of document IDs to be deleted.
312
+ * @return An optional boolean indicating the deletion status.
313
+ */
308
314
@ Override
309
- public List <Document > similaritySearch (SearchRequest request ) {
315
+ public Optional <Boolean > delete (List <String > documentIds ) {
316
+ return delete (documentIds , this .pineconeNamespace );
317
+ }
318
+
319
+ public List <Document > similaritySearch (SearchRequest request , String namespace ) {
310
320
311
321
String nativeExpressionFilters = (request .getFilterExpression () != null )
312
322
? this .filterExpressionConverter .convertExpression (request .getFilterExpression ()) : "" ;
@@ -317,7 +327,7 @@ public List<Document> similaritySearch(SearchRequest request) {
317
327
.addAllVector (toFloatList (queryEmbedding ))
318
328
.setTopK (request .getTopK ())
319
329
.setIncludeMetadata (true )
320
- .setNamespace (this . pineconeNamespace );
330
+ .setNamespace (namespace );
321
331
322
332
if (StringUtils .hasText (nativeExpressionFilters )) {
323
333
queryRequestBuilder .setFilter (metadataFiltersToStruct (nativeExpressionFilters ));
@@ -339,6 +349,11 @@ public List<Document> similaritySearch(SearchRequest request) {
339
349
.toList ();
340
350
}
341
351
352
+ @ Override
353
+ public List <Document > similaritySearch (SearchRequest request ) {
354
+ return similaritySearch (request , this .pineconeNamespace );
355
+ }
356
+
342
357
private Struct metadataFiltersToStruct (String metadataFilters ) {
343
358
try {
344
359
var structBuilder = Struct .newBuilder ();
0 commit comments