From 5272e42b0de2a056271ebea8245cba1e29863621 Mon Sep 17 00:00:00 2001 From: Ahmad Bunni <48261284+Ahmad-Bunni@users.noreply.github.com> Date: Tue, 19 Sep 2023 17:39:10 +0200 Subject: [PATCH] Add namespace to pinecone hybrid search (#10677) **Description:** Pinecone hybrid search is now limited to default namespace. There is no option for the user to provide a namespace to partition an index, which is one of the most important features of pinecone. **Resource:** https://docs.pinecone.io/docs/namespaces --------- Co-authored-by: Harrison Chase --- .../langchain/retrievers/pinecone_hybrid_search.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/libs/langchain/langchain/retrievers/pinecone_hybrid_search.py b/libs/langchain/langchain/retrievers/pinecone_hybrid_search.py index 068a5a53c19..7b41616feff 100644 --- a/libs/langchain/langchain/retrievers/pinecone_hybrid_search.py +++ b/libs/langchain/langchain/retrievers/pinecone_hybrid_search.py @@ -28,6 +28,7 @@ def create_index( sparse_encoder: Any, ids: Optional[List[str]] = None, metadatas: Optional[List[dict]] = None, + namespace: Optional[str] = None, ) -> None: """Create an index from a list of contexts. @@ -91,7 +92,7 @@ def create_index( ) # upload the documents to the new hybrid index - index.upsert(vectors) + index.upsert(vectors, namespace=namespace) class PineconeHybridSearchRetriever(BaseRetriever): @@ -108,6 +109,8 @@ class PineconeHybridSearchRetriever(BaseRetriever): """Number of documents to return.""" alpha: float = 0.5 """Alpha value for hybrid search.""" + namespace: Optional[str] = None + """Namespace value for index partition.""" class Config: """Configuration for this pydantic object.""" @@ -120,6 +123,7 @@ class PineconeHybridSearchRetriever(BaseRetriever): texts: List[str], ids: Optional[List[str]] = None, metadatas: Optional[List[dict]] = None, + namespace: Optional[str] = None, ) -> None: create_index( texts, @@ -128,6 +132,7 @@ class PineconeHybridSearchRetriever(BaseRetriever): self.sparse_encoder, ids=ids, metadatas=metadatas, + namespace=namespace, ) @root_validator() @@ -162,6 +167,7 @@ class PineconeHybridSearchRetriever(BaseRetriever): sparse_vector=sparse_vec, top_k=self.top_k, include_metadata=True, + namespace=self.namespace, ) final_result = [] for res in result["matches"]: