From 261e68a3a1228b5e3185c5b2f8e4c782a3b128a8 Mon Sep 17 00:00:00 2001 From: Ismail Ashraq Date: Tue, 14 May 2024 17:46:04 +0500 Subject: [PATCH 1/2] use api_key if explicitly passed --- semantic_router/index/pinecone.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index 136422d1..c685f7f9 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -38,6 +38,7 @@ def to_dict(self): class PineconeIndex(BaseIndex): index_prefix: str = "semantic-router--" + api_key: Optional[str] = None index_name: str = "index" dimensions: Union[int, None] = None metric: str = "cosine" @@ -69,7 +70,12 @@ def __init__( self.host = host self.namespace = namespace self.type = "pinecone" - self.client = self._initialize_client(api_key=api_key) + self.api_key = api_key or os.getenv("PINECONE_API_KEY") + + if self.api_key is None: + raise ValueError("Pinecone API key is required.") + + self.client = self._initialize_client(api_key=self.api_key) def _initialize_client(self, api_key: Optional[str] = None): try: @@ -82,9 +88,6 @@ def _initialize_client(self, api_key: Optional[str] = None): "You can install it with: " "`pip install 'semantic-router[pinecone]'`" ) - api_key = api_key or os.getenv("PINECONE_API_KEY") - if api_key is None: - raise ValueError("Pinecone API key is required.") pinecone_args = {"api_key": api_key, "source_tag": "semantic-router"} if self.namespace: pinecone_args["namespace"] = self.namespace @@ -190,7 +193,7 @@ def _get_all(self, prefix: Optional[str] = None, include_metadata: bool = False) params: Dict = {} if self.namespace: params["namespace"] = self.namespace - headers = {"Api-Key": os.environ["PINECONE_API_KEY"]} + headers = {"Api-Key": self.api_key} metadata = [] while True: From 1810b24d5de49c2944db35bb8b4a40015ca77d25 Mon Sep 17 00:00:00 2001 From: Ismail Ashraq Date: Tue, 14 May 2024 17:52:17 +0500 Subject: [PATCH 2/2] linting --- semantic_router/index/pinecone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index c685f7f9..de288524 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -71,7 +71,7 @@ def __init__( self.namespace = namespace self.type = "pinecone" self.api_key = api_key or os.getenv("PINECONE_API_KEY") - + if self.api_key is None: raise ValueError("Pinecone API key is required.")