Skip to content

Commit

Permalink
community: ✨ Use new OVHcloud batch embedding (#26209)
Browse files Browse the repository at this point in the history
- **Description:** change to do the batch embedding server side and not
client side
- **Twitter handle:** @wildagsx

---------

Co-authored-by: ccurme <[email protected]>
  • Loading branch information
philippart-s and ccurme authored Nov 4, 2024
1 parent a54f390 commit 4b8cd7a
Showing 1 changed file with 42 additions and 22 deletions.
64 changes: 42 additions & 22 deletions libs/community/langchain_community/embeddings/ovhcloud.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import time
from typing import Any, List
Expand Down Expand Up @@ -41,17 +42,55 @@ def _generate_embedding(self, text: str) -> List[float]:
Returns:
List[float]: Embeddings for the text.
"""

return self._send_request_to_ai_endpoints("text/plain", text, "text2vec")

def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Embed a list of documents.
Args:
texts (List[str]): The list of texts to embed.
Returns:
List[List[float]]: List of embeddings, one for each input text.
"""

return self._send_request_to_ai_endpoints(
"application/json", json.dumps(texts), "batch_text2vec"
)

def embed_query(self, text: str) -> List[float]:
"""Embed a single query text.
Args:
text (str): The text to embed.
Returns:
List[float]: Embeddings for the text.
"""
return self._generate_embedding(text)

def _send_request_to_ai_endpoints(
self, contentType: str, payload: str, route: str
) -> Any:
"""Send a HTTPS request to OVHcloud AI Endpoints
Args:
contentType (str): The content type of the request, application/json or text/plain.
payload (str): The payload of the request.
route (str): The route of the request, batch_text2vec or text2vec.
""" # noqa: E501
headers = {
"content-type": "text/plain",
"content-type": contentType,
"Authorization": f"Bearer {self.access_token}",
}

session = requests.session()
while True:
response = session.post(
f"https://{self.model_name}.endpoints.{self.region}.ai.cloud.ovh.net/api/text2vec",
(
f"https://{self.model_name}.endpoints.{self.region}"
f".ai.cloud.ovh.net/api/{route}"
),
headers=headers,
data=text,
data=payload,
)
if response.status_code != 200:
if response.status_code == 429:
Expand All @@ -74,22 +113,3 @@ def _generate_embedding(self, text: str) -> List[float]:
)
)
return response.json()

def embed_documents(self, texts: List[str]) -> List[List[float]]:
"""Create a retry decorator for PremAIEmbeddings.
Args:
texts (List[str]): The list of texts to embed.
Returns:
List[List[float]]: List of embeddings, one for each input text.
"""
return [self._generate_embedding(text) for text in texts]

def embed_query(self, text: str) -> List[float]:
"""Embed a single query text.
Args:
text (str): The text to embed.
Returns:
List[float]: Embeddings for the text.
"""
return self._generate_embedding(text)

0 comments on commit 4b8cd7a

Please sign in to comment.