Skip to content

Commit

Permalink
🦍
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurme committed Dec 17, 2024
1 parent 897fd22 commit a9816f7
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions libs/partners/pinecone/langchain_pinecone/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,25 @@ class PineconeEmbeddings(BaseModel, Embeddings):
protected_namespaces=(),
)

async def _initialize_async_client(self) -> aiohttp.ClientSession:
return aiohttp.ClientSession(
headers={
"Api-Key": self.pinecone_api_key.get_secret_value(),
"Content-Type": "application/json",
"X-Pinecone-API-Version": "2024-10",
}
)

@property
def async_client(self) -> aiohttp.ClientSession:
async def _get_async_client(self) -> aiohttp.ClientSession:
"""Lazily initialize the async client."""
if self._async_client is None:
self._async_client = asyncio.run(self._initialize_async_client())
self._async_client = aiohttp.ClientSession(
headers={
"Api-Key": self.pinecone_api_key.get_secret_value(),
"Content-Type": "application/json",
"X-Pinecone-API-Version": "2024-10",
}
)
return self._async_client

# @property
# def async_client(self) -> aiohttp.ClientSession:
# """Lazily initialize the async client."""
# if self._async_client is None:
# self._async_client = asyncio.run(self._get_async_client())
# return self._async_client

@model_validator(mode="before")
@classmethod
def set_default_config(cls, values: dict) -> Any:
Expand Down Expand Up @@ -110,7 +113,7 @@ def validate_environment(self) -> Self:
self._client = client

# Ensure async_client is lazily initialized
_ = self.async_client
# _ = asyncio.run(self._get_async_client())
return self

def _get_batch_iterator(self, texts: List[str]) -> Iterable:
Expand Down Expand Up @@ -184,8 +187,7 @@ async def _aembed_texts(
"inputs": [{"text": text} for text in texts],
"parameters": parameters,
}
async with self.async_client.post(
"https://api.pinecone.io/embed", json=data
) as response:
client = await self._get_async_client()
async with client.post("https://api.pinecone.io/embed", json=data) as response:
response_data = await response.json(content_type=None)
return response_data

0 comments on commit a9816f7

Please sign in to comment.