Skip to content
Snippets Groups Projects
Commit da9fbd97 authored by Luca Mannini's avatar Luca Mannini
Browse files

refactor: enhance PineconeIndex API key handling and request headers

- Updated the constructor to retrieve the API key from environment variables if not provided.
- Added a validation step to ensure the API key is present, raising a ValueError if missing.
- Consolidated header management by using the instance's headers attribute for API requests, improving code clarity and maintainability.
parent 787943cb
Branches
Tags
No related merge requests found
...@@ -132,7 +132,9 @@ class PineconeIndex(BaseIndex): ...@@ -132,7 +132,9 @@ class PineconeIndex(BaseIndex):
init_async_index: bool = False, init_async_index: bool = False,
): ):
super().__init__() super().__init__()
self.api_key = api_key self.api_key = api_key or os.getenv("PINECONE_API_KEY")
if not self.api_key:
raise ValueError("Pinecone API key is required.")
self.headers = { self.headers = {
"Api-Key": self.api_key, "Api-Key": self.api_key,
"Content-Type": "application/json", "Content-Type": "application/json",
...@@ -161,7 +163,6 @@ class PineconeIndex(BaseIndex): ...@@ -161,7 +163,6 @@ class PineconeIndex(BaseIndex):
self.client = self._initialize_client(api_key=self.api_key) self.client = self._initialize_client(api_key=self.api_key)
# try initializing index # try initializing index
self.index = self._init_index() self.index = self._init_index()
...@@ -186,7 +187,6 @@ class PineconeIndex(BaseIndex): ...@@ -186,7 +187,6 @@ class PineconeIndex(BaseIndex):
return Pinecone(**pinecone_args) return Pinecone(**pinecone_args)
def _init_index(self, force_create: bool = False) -> Union[Any, None]: def _init_index(self, force_create: bool = False) -> Union[Any, None]:
"""Initializing the index can be done after the object has been created """Initializing the index can be done after the object has been created
to allow for the user to set the dimensions and other parameters. to allow for the user to set the dimensions and other parameters.
...@@ -884,12 +884,10 @@ class PineconeIndex(BaseIndex): ...@@ -884,12 +884,10 @@ class PineconeIndex(BaseIndex):
elif self.namespace: elif self.namespace:
params["namespace"] = [self.namespace] params["namespace"] = [self.namespace]
headers = {
"Api-Key": self.api_key,
}
async with aiohttp.ClientSession() as session: async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, headers=headers) as response: async with session.get(
url, params=params, headers=self.headers
) as response:
if response.status != 200: if response.status != 200:
error_text = await response.text() error_text = await response.text()
logger.error(f"Error fetching metadata: {error_text}") logger.error(f"Error fetching metadata: {error_text}")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment