Skip to content

Commit

Permalink
refactor: streamline PineconeIndex initialization and remove async_cl…
Browse files Browse the repository at this point in the history
…ient

- Removed the async_client attribute and its related initialization logic.
- Consolidated API key and headers setup into the constructor for clarity.
- Enhanced code readability by eliminating redundant code and improving structure.
  • Loading branch information
italianconcerto committed Jan 23, 2025
1 parent c8e7a62 commit 787943c
Showing 1 changed file with 10 additions and 22 deletions.
32 changes: 10 additions & 22 deletions semantic_router/index/pinecone.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ class PineconeIndex(BaseIndex):
region: str = "us-east-1"
host: str = ""
client: Any = Field(default=None, exclude=True)
async_client: Any = Field(default=None, exclude=True)
index: Optional[Any] = Field(default=None, exclude=True)
ServerlessSpec: Any = Field(default=None, exclude=True)
namespace: Optional[str] = ""
base_url: Optional[str] = "https://api.pinecone.io"
headers: dict[str, str] = {}

def __init__(
self,
Expand All @@ -132,6 +132,13 @@ def __init__(
init_async_index: bool = False,
):
super().__init__()
self.api_key = api_key
self.headers = {
"Api-Key": self.api_key,
"Content-Type": "application/json",
"X-Pinecone-API-Version": "2024-07",
"User-Agent": "source_tag=semanticrouter",
}
self.index_name = index_name
self.dimensions = dimensions
self.metric = metric
Expand All @@ -154,13 +161,7 @@ def __init__(

self.client = self._initialize_client(api_key=self.api_key)

self.api_key = api_key
self.headers = {
"Api-Key": self.api_key,
"Content-Type": "application/json",
"X-Pinecone-API-Version": "2024-07",
"User-Agent": "source_tag=semanticrouter",
}

# try initializing index
self.index = self._init_index()

Expand All @@ -185,19 +186,6 @@ def _initialize_client(self, api_key: Optional[str] = None):

return Pinecone(**pinecone_args)

def _initialize_async_client(self, api_key: Optional[str] = None):
api_key = api_key or self.api_key
if api_key is None:
raise ValueError("Pinecone API key is required.")
async_client = aiohttp.ClientSession(
headers={
"Api-Key": api_key,
"Content-Type": "application/json",
"X-Pinecone-API-Version": "2024-07",
"User-Agent": "source_tag=semanticrouter",
}
)
return async_client

def _init_index(self, force_create: bool = False) -> Union[Any, None]:
"""Initializing the index can be done after the object has been created
Expand Down Expand Up @@ -874,7 +862,7 @@ async def _async_fetch_metadata(
namespace: str | None = None,
) -> dict:
"""Fetch metadata for a single vector ID asynchronously using the
async_client.
ClientSession.
:param vector_id: The ID of the vector to fetch metadata for.
:type vector_id: str
Expand Down

0 comments on commit 787943c

Please sign in to comment.