Skip to content

Commit

Permalink
Merge pull request #278 from aurelio-labs/pc-index-api-key
Browse files Browse the repository at this point in the history
fix: Use 'api_key' parameter if passed explicitly
  • Loading branch information
jamescalam authored May 14, 2024
2 parents 423e1a3 + 1810b24 commit 62aca47
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions semantic_router/index/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 62aca47

Please sign in to comment.