Skip to content

The agent engineering platform.

community: ✨ Use new OVHcloud batch embedding (#26209)

Stéphane Philippart4b8cd7a09a6a
diff --git a/libs/community/langchain_community/embeddings/ovhcloud.py b/libs/community/langchain_community/embeddings/ovhcloud.py
index 5786a761f4..49b9cbfa21 100644
--- a/libs/community/langchain_community/embeddings/ovhcloud.py
+++ b/libs/community/langchain_community/embeddings/ovhcloud.py
@@ -1,3 +1,4 @@
+import json
import logging
import time
from typing import Any, List
@@ -41,17 +42,55 @@ class OVHCloudEmbeddings(BaseModel, Embeddings):
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:
@@ -74,22 +113,3 @@ class OVHCloudEmbeddings(BaseModel, Embeddings):
)
)
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)