Skip to content

Commit 36278af

Browse files
adambchoutitzolov
authored andcommitted
Add Pinecone namespace support for add, search and delete operations
1 parent 41885a8 commit 36278af

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

vector-stores/spring-ai-pinecone/src/main/java/org/springframework/ai/vectorstore/PineconeVectorStore.java

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2023 the original author or authors.
2+
* Copyright 2023-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@
4747
* Pinecone index.
4848
*
4949
* @author Christian Tzolov
50+
* @author Adam Bchouti
5051
*/
5152
public class PineconeVectorStore implements VectorStore {
5253

@@ -60,18 +61,9 @@ public class PineconeVectorStore implements VectorStore {
6061

6162
private final PineconeConnection pineconeConnection;
6263

63-
private final ObjectMapper objectMapper;
64-
65-
private String pineconeNamespace;
64+
private final String pineconeNamespace;
6665

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;
7567

7668
/**
7769
* Configuration class for the PineconeVectorStore.
@@ -232,11 +224,11 @@ public PineconeVectorStore(PineconeVectorStoreConfig config, EmbeddingClient emb
232224
}
233225

234226
/**
235-
* Adds a list of documents to the vector store.
227+
* Adds a list of documents to the vector store based on the namespace.
236228
* @param documents The list of documents to be added.
229+
* @param namespace The namespace to add the documents to
237230
*/
238-
@Override
239-
public void add(List<Document> documents) {
231+
public void add(List<Document> documents, String namespace) {
240232

241233
List<Vector> upsertVectors = documents.stream().map(document -> {
242234
// Compute and assign an embedding to the document.
@@ -251,12 +243,21 @@ public void add(List<Document> documents) {
251243

252244
UpsertRequest upsertRequest = UpsertRequest.newBuilder()
253245
.addAllVectors(upsertVectors)
254-
.setNamespace(this.pineconeNamespace)
246+
.setNamespace(namespace)
255247
.build();
256248

257249
this.pineconeConnection.getBlockingStub().upsert(upsertRequest);
258250
}
259251

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+
260261
/**
261262
* Converts the document metadata to a Protobuf Struct.
262263
* @param document The document containing metadata.
@@ -286,15 +287,15 @@ private Value contentValue(Document document) {
286287
}
287288

288289
/**
289-
* Deletes a list of documents by their IDs.
290+
* Deletes a list of documents by their IDs based on the namespace.
290291
* @param documentIds The list of document IDs to be deleted.
292+
* @param namespace The namespace of the document IDs.
291293
* @return An optional boolean indicating the deletion status.
292294
*/
293-
@Override
294-
public Optional<Boolean> delete(List<String> documentIds) {
295+
public Optional<Boolean> delete(List<String> documentIds, String namespace) {
295296

296297
DeleteRequest deleteRequest = DeleteRequest.newBuilder()
297-
.setNamespace(this.pineconeNamespace) // ignored for free tier.
298+
.setNamespace(namespace) // ignored for free tier.
298299
.addAllIds(documentIds)
299300
.setDeleteAll(false)
300301
.build();
@@ -305,8 +306,17 @@ public Optional<Boolean> delete(List<String> documentIds) {
305306
return Optional.of(true);
306307
}
307308

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+
*/
308314
@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) {
310320

311321
String nativeExpressionFilters = (request.getFilterExpression() != null)
312322
? this.filterExpressionConverter.convertExpression(request.getFilterExpression()) : "";
@@ -317,7 +327,7 @@ public List<Document> similaritySearch(SearchRequest request) {
317327
.addAllVector(toFloatList(queryEmbedding))
318328
.setTopK(request.getTopK())
319329
.setIncludeMetadata(true)
320-
.setNamespace(this.pineconeNamespace);
330+
.setNamespace(namespace);
321331

322332
if (StringUtils.hasText(nativeExpressionFilters)) {
323333
queryRequestBuilder.setFilter(metadataFiltersToStruct(nativeExpressionFilters));
@@ -339,6 +349,11 @@ public List<Document> similaritySearch(SearchRequest request) {
339349
.toList();
340350
}
341351

352+
@Override
353+
public List<Document> similaritySearch(SearchRequest request) {
354+
return similaritySearch(request, this.pineconeNamespace);
355+
}
356+
342357
private Struct metadataFiltersToStruct(String metadataFilters) {
343358
try {
344359
var structBuilder = Struct.newBuilder();

0 commit comments

Comments
 (0)