From a9816f75458987be2e2461c7016a5b1b6c4e1394 Mon Sep 17 00:00:00 2001 From: Chester Curme Date: Tue, 17 Dec 2024 15:21:48 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=A6=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pinecone/langchain_pinecone/embeddings.py | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/libs/partners/pinecone/langchain_pinecone/embeddings.py b/libs/partners/pinecone/langchain_pinecone/embeddings.py index 86411d2511d21..0389899224f22 100644 --- a/libs/partners/pinecone/langchain_pinecone/embeddings.py +++ b/libs/partners/pinecone/langchain_pinecone/embeddings.py @@ -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: @@ -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: @@ -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