Skip to content

The agent engineering platform.

community[minor]: Add support for OVHcloud AI Endpoints Embedding (#22667)

Mathis Joffreea43f40dafb3
diff --git a/docs/docs/integrations/text_embedding/ovhcloud.ipynb b/docs/docs/integrations/text_embedding/ovhcloud.ipynb
new file mode 100644
index 0000000000..f60e328ef2
--- /dev/null
+++ b/docs/docs/integrations/text_embedding/ovhcloud.ipynb
@@ -0,0 +1,61 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "6a3268e4",
+ "metadata": {},
+ "source": [
+ "# OVHcloud\n",
+ "\n",
+ "This notebook explains how to use OVHCloudEmbeddings, which is included in the langchain_community package, to embed texts in langchain."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "id": "3da0fce0",
+ "metadata": {},
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Embedding generated by OVHCloudEmbeddings: [0.013393167, 0.03732136, -0.0042296825, 0.042756177, 0.022484222, -0.0045062024, -0.039632313, -0.016722068, 0.035480227, 0.021987874, -0.007831321, -0.002374333, 0.14827625, 0.044154406, -0.038416132, -0.051982213, 0.0054677227, -0.030913774, 0.03872776, 0.00080121466, 0.06914864, -0.01889285, 0.051963896, -0.049868602, 0.032817934, -0.012699772, 0.029378777, -0.019135607, -0.014866451, 0.010978144, 0.036102116, -0.024129838, 0.0059551136, 0.025592148, 0.0330448, 0.05326782, -0.008618608, -0.04626763, 0.046697352, 0.007115799, 0.025649866, 0.02479821, -0.011125191, -0.011960746, 0.027974335, -0.008805084, -0.0014219242, -0.016421443, -0.042280562, -0.027239632, 0.013829875, 0.008757527, 0.024184346, 0.01559286, -0.05868668, -0.058345765, 0.048597623, 0.027461633, -0.03701432, 0.014960674, 0.0055076545, 0.011801827, 0.003185198, 0.024615683, 0.04377218, -0.02428408, -0.01129621, -0.008451086, -0.024531357, -0.029689042, 0.019617215, -0.009499967, 0.034481872, -0.022548521, -0.027568225, -0.027564893, -0.035448182, 0.042752527, -0.017667985, -0.006429351, 0.028172225, 0.021843985, 0.05518236, 0.026108254, -0.01864022, -0.021649806, -0.042575743, 0.043298125, 0.032832835, 0.05166516, 0.011787296, 0.013220338, -0.028256241, 0.028401077, -0.0023671573, 0.005722865, 0.0051106545, 0.02541163, 0.033874203, -0.038719863, 0.010436916, -0.043236364, -0.023228278, -0.03358519, -0.024077564, 8.423713e-05, -0.010243255, -0.0191856, 0.013305917, -0.020698957, -0.04052005, 0.026274782, 0.054635983, -0.057551324, 0.034959268, -0.025090441, 0.02773644, -0.031936403, 0.008175286, -0.05568246, -0.013420259, 0.02285795, -0.012539113, -0.018429684, 0.061069433, -0.026720565, -0.0008156537, 0.0014032789, 0.043951243, -0.0065814317, 0.011629428, -0.06288238, -0.011015484, 0.02559024, -0.041962583, 0.037935987, 0.027115924, 0.0132008735, 0.010924172, 0.010795455, 0.010284355, -0.060599748, 0.037463877, 0.051818028, 0.027654605, -0.054811046, 0.019782307, 0.003971803, -0.028695354, 0.086431496, 0.038262486, -0.039732426, -0.0045595192, -0.016946813, 0.047315836, -0.02716498, -0.07109412, -0.01315921, -0.010889335, 0.043004725, 0.060072046, 0.013834794, 0.014518575, -0.013399902, 0.050983083, -0.0066714315, -0.0225039, 0.01304092, -0.009736523, -0.038328674, -0.02377573, 0.010796089, -0.058065176, 0.008482149, 0.016022328, -0.023443135, 0.008705595, -0.03286985, -0.028836675, -0.05872433, -0.056536626, -0.04216553, 0.00037138985, 0.0410015, -0.0035549132, -0.01765879, 0.0072336365, -0.02078376, 0.018013686, 0.004380657, 0.009426806, 0.04496503, 0.013016738, 0.041157607, 0.011397934, 0.018421972, 0.032070283, 0.023589233, -0.031510174, -0.03177538, 0.019968262, 0.0031847644, 0.03337904, 0.04362253, -0.032639876, -0.078984305, 0.022978902, 0.022103759, 0.03148644, 0.040037263, -0.013258827, 0.04515543, -0.045400217, 0.031163648, -0.038804565, -0.031007605, 0.023038942, -0.0011629256, -0.0042446335, 0.038747445, 0.039732117, -0.052249946, 0.003977225, -0.005010845, -0.025907055, 0.0050243125, 0.042312477, -0.014793418, 0.03156775, 0.015165637, 0.03156905, 0.025180826, -0.04529393, 0.029273387, -0.033187654, -0.022706378, 0.030855985, -0.023539526, -0.030362226, -0.10679687, -0.030312601, 0.019773543, 0.0250672, -0.019900797, 0.027153352, -0.0407523, -0.053737476, 0.037743907, -0.03822609, 0.004701334, 0.018653622, -0.029043352, 0.013663203, -0.0109640695, 0.013055596, 0.0071652965, -0.045625277, 0.008842614, -0.010782444, 0.029200679, 0.0108302515, -0.041484382, -0.027877813, -0.0050561433, 0.001051684, 0.028695228, 0.111581974, -0.033906773, -0.005789528, -0.01691454, -0.057842113, -0.017875662, -0.063073546, 0.04463758, 0.014070596, -0.02932851, -0.06885991, 0.022553641, -0.009132225, -0.0017104117, -0.02573207, 0.07448002, -0.03711256, -0.051314738, -0.034982048, 0.027141424, -0.06874479, -0.02388671, -0.06571741, 0.013449982, 0.0638684, 0.040945932, -0.026943449, 0.03134517, 0.0344114, 0.01026371, 0.038227987, -0.027564207, 0.016802547, 0.0010686627, -0.030647304, -0.042039596, 0.054497436, -0.040464804, -0.03338543, 0.02260303, 0.09915977, 0.022541435, -0.039202042, 0.050876632, -0.03609107, -0.034563474, -0.06706467, 0.02145668, 0.03366738, -0.03411402, 0.012604448, 0.08058551, -0.007932673, 0.06885105, -0.030523479, 0.027278991, -0.046861324, -0.093850814, 0.030921744, -0.0001421079, 0.030683668, 0.004855684, 0.076923594, 0.010595104, -0.027388366, 0.015755313, -0.008487381, 0.06655968, -0.08168016, -0.0018116324, -0.012591006, -0.027123308, 0.07031095, 0.04333807, -0.03915123, 0.044605616, -0.022781648, 0.0093986215, 0.08933287, -0.055434275, -0.0102571435, -0.013561327, -0.027190993, 0.036250908, 0.034637712, -0.023693744, 0.028632542, 0.0072604613, -0.027789399, -0.012132866, 0.062264692, -0.038561203, -0.0028939024, -0.052279804, -0.012950528, 0.029368099, -0.01727593, -0.009667129, 0.031548638, 0.042698074, -0.015050672, -0.03120724, 0.024719479, -0.01405534, 0.100237176, 0.03711969, -0.025223013, -0.015259215, 0.04748944, -0.025767656, 0.047430392, -0.03020851, -0.015670097, 0.07102661, 0.065665215, 0.019847495, 0.029551532, -0.0025012908, -0.036311474, 0.07141015, 0.009710868, -0.03509336, 0.033391234, -0.03209491, -0.0020321321, -0.022850651, -0.015315501, 0.006990975, 0.028062506, -0.017586777, 0.034145683, -0.0033781836, 0.004156304, -0.032210104, 0.010601066, 0.0048922407, -0.01577382, 0.048883524, 0.028200202, -0.0072908103, -0.035968613, 0.0346475, -0.027046643, -0.020911021, 0.04035775, -0.027658584, 0.051322017, 0.02296099, -0.023528099, -0.022320902, -0.027127141, 0.031832132, 0.05151557, 0.02551352, -0.03645325, 0.025603358, 0.009815077, 0.005696306, 0.037177127, -0.055942845, -0.01414183, -0.032122746, 0.008900472, -0.04909773, -0.025821397, 0.008453814, -0.00066156825, 0.00021046218, 0.033632305, -0.037483413, 0.035067603, -0.0068717552, -0.012747838, 0.033523142, -0.018556861, -0.0013153392, 0.00041785053, 0.0056918347, -0.047996394, -0.0121070435, -0.016490333, 0.021066675, -0.013169224, -0.004945434, 0.018413335, 0.021549506, 0.010436816, -0.005243072, 0.037264977, -0.05419949, 0.051790405, -0.02995299, 0.00830641, -0.09983456, 0.035929922, 0.046650928, -0.02201099, -0.015233417, -0.03984102, -0.02588652, 0.014141513, -0.032033846, -0.00088278914, -0.017325765, -0.041028026, -0.04869172, 0.032118957, -0.0049999636, 0.031933237, 0.03276276, -0.013989229, 0.049034916, 0.026281273, -0.046090055, -0.010919109, -0.026676904, -0.025712457, 0.03475912, 0.025149662, 0.0555755, -0.06140382, 0.061161127, 0.0030567874, -0.0365629, -0.01902828, 0.01566279, 0.026230726, -0.016239835, -0.004278232, 0.033120655, -0.03901436, -0.091688305, 0.02096186, 0.04452546, -0.02552988, -0.025540574, -0.048035506, 0.00352354, -0.042762343, 0.030095248, -0.043667283, -0.028081333, 0.03475098, -0.038900424, 0.11706695, -0.06385194, -0.01991146, -0.042574335, 0.013697014, 0.014061967, -0.027709525, 0.015497077, 0.0059057726, -0.017043548, 0.016371854, 0.011076671, -0.043543536, -0.021559414, 0.014312511, -0.059308562, 0.027888788, -0.06514841, 0.026988931, -0.021745581, 0.04002255, -0.043083742, 0.023495318, -0.08972084, -0.061623067, 0.02676458, -0.016847553, -0.017119622, 0.038647383, -0.008218997, 0.0025763474, -0.028317345, 0.014624456, -0.013914129, 0.02815451, -0.036198556, 0.009238764, -0.015274136, 0.015579736, 0.0032229964, 0.006329244, -0.013670273, 0.00665867, 0.05292342, -0.03484488, 0.0024167884, -0.0057902746, 0.05282686, 0.0005000245, -0.017153509, -0.028036479, -0.027311599, -0.009889913, 0.02431086, -0.0038315805, 0.03333115, 0.02464384, -0.025810424, -0.062422995, 0.060066104, -0.092039555, 0.014482284, 0.02608793, -0.021194633, 0.061447322, 0.04130138, -0.03332812, -0.042047437, 0.05123424, -0.07453314, -0.028080523, 0.0046392465, -0.029648444, 0.028595202, -0.013599953, 0.014632302, 0.056478836, -0.058442805, -0.1930449, 0.021608872, -0.015712231, -0.06269486, 0.020336937, 0.011157828, -0.0015483286, 0.007450834, 0.019511258, 0.0021432014, 0.039572544, -0.06631538, 0.061495554, -0.027214238, -0.03019643, 0.021765657, -0.018758483, -0.059369385, 0.0104420185, 0.04438893, 0.011059414, 0.07466604, -0.026724849, 0.0012903131, -0.046258427, -0.044896547, -0.027910942, 0.026439613, 0.02349909, 0.047514588, 0.0037869548, 0.014885506, 0.04653929, 0.03331027, -0.02470549, 0.006271202, -0.026514992, 0.02139273, -0.07263723, 0.016668767, -0.013725435, -0.06854902, -0.0118108615, 0.021851629, -0.03325869, 0.05457194, 0.026849618, 0.014450217, -0.04094698, -0.025690362, 0.08235594, 0.01301538, -0.08728046, -0.022502083, -0.047967393, 0.053036716, 0.03064825, 0.016691066, 0.05352114, -0.028005075, 0.029265208, 0.03578327, -0.0068425513, 0.028201371, -0.022989936, -0.0082469685, 0.014952235, -0.02482578, -0.001407646, 0.032534935, 0.029727332, -0.01972456, -0.05026965, 0.037474174, 0.041771494, -0.032914527, 0.023217645, -0.025604516, -0.076627776, 0.020639537, -0.02778371, -0.03310011, 0.008182295, 0.01675386, -0.031687014, -0.023041338, 0.031311274, 0.004942907, 0.032867808, 0.03349065, -0.010631844, -0.023672776, 0.011756044, 0.016093759, 0.035696387, -0.011801524, -0.047790904, 0.04692089, -0.004650366, -0.03204725, 0.0035557016, -0.025661316, -0.013585784, 0.034187928, 0.023118816, -0.021673787, -0.05335118, 0.027510943, -0.009999087, -0.03761488, 0.0060729063, 0.025209012, 0.021224871, -0.014786133, -0.013114826, -0.017566234, 0.007534834, -0.043879513, -0.037670616, -0.05683524, -0.00272405, -0.012196419, 0.04363327, 0.00737273, 0.05148426, 0.0055279597, -0.015904596, 0.047310952, 0.03045511, 0.030634195, -0.009361755, -0.039683104, 0.06814403, -0.051854312, -0.011992879, 0.06865308, 0.041739926, -0.017870232, 0.032383803, 0.05271035, 0.05132444, -0.021921795, -0.004006664, 0.013597788, -0.040238887, 0.030131282, 0.02308868, -0.04397555, 0.03917002, -0.011500181, -0.037450697, -0.09086526, -0.031017756, -0.016214617, -0.028405681, 0.009200299, 0.0074356734, -0.0019842882, -0.049663708, 0.035164695, -0.044908658, -0.05643445, 0.02870157, 0.03714114, 0.033664975, -0.013390485, -0.0004991374, 0.02155712, 0.021897627, 0.047690097, -0.01916393, -0.01362889, 0.025618015, 0.018122612, 0.036994122, -0.01447489, 0.040231027, -0.047183823, -0.020032013, 0.027981037]\n"
+ ]
+ }
+ ],
+ "source": [
+ "from langchain_community.embeddings.ovhcloud import OVHCloudEmbeddings\n",
+ "\n",
+ "embedder = OVHCloudEmbeddings(\n",
+ " model_name=\"multilingual-e5-base\", region=\"kepler\", access_token=\"MyAccessToken\"\n",
+ ")\n",
+ "embed = embedder.embed_query(\"Hello World!\")\n",
+ "\n",
+ "\"\"\" verify \"\"\"\n",
+ "print(f\"Embedding generated by OVHCloudEmbeddings: {embed}\")"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Python 3 (ipykernel)",
+ "language": "python",
+ "name": "python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.8.10"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/libs/community/langchain_community/embeddings/__init__.py b/libs/community/langchain_community/embeddings/__init__.py
index 4905f879cd..bef8ec4b1e 100644
--- a/libs/community/langchain_community/embeddings/__init__.py
+++ b/libs/community/langchain_community/embeddings/__init__.py
@@ -176,6 +176,9 @@ if TYPE_CHECKING:
from langchain_community.embeddings.oracleai import (
OracleEmbeddings,
)
+ from langchain_community.embeddings.ovhcloud import (
+ OVHCloudEmbeddings,
+ )
from langchain_community.embeddings.premai import (
PremAIEmbeddings,
)
@@ -283,6 +286,7 @@ __all__ = [
"OpenVINOBgeEmbeddings",
"OpenVINOEmbeddings",
"OracleEmbeddings",
+ "OVHCloudEmbeddings",
"PremAIEmbeddings",
"QianfanEmbeddingsEndpoint",
"QuantizedBgeEmbeddings",
@@ -365,6 +369,7 @@ _module_lookup = {
"QuantizedBgeEmbeddings": "langchain_community.embeddings.itrex",
"QuantizedBiEncoderEmbeddings": "langchain_community.embeddings.optimum_intel",
"OracleEmbeddings": "langchain_community.embeddings.oracleai",
+ "OVHCloudEmbeddings": "langchain_community.embeddings.ovhcloud",
"SagemakerEndpointEmbeddings": "langchain_community.embeddings.sagemaker_endpoint",
"SambaStudioEmbeddings": "langchain_community.embeddings.sambanova",
"SelfHostedEmbeddings": "langchain_community.embeddings.self_hosted",
diff --git a/libs/community/langchain_community/embeddings/ovhcloud.py b/libs/community/langchain_community/embeddings/ovhcloud.py
new file mode 100644
index 0000000000..7c90686246
--- /dev/null
+++ b/libs/community/langchain_community/embeddings/ovhcloud.py
@@ -0,0 +1,101 @@
+import logging
+import time
+from typing import Any, List, Optional
+
+import requests
+from langchain_core.embeddings import Embeddings
+from langchain_core.pydantic_v1 import BaseModel, Extra
+
+logger = logging.getLogger(__name__)
+
+
+class OVHCloudEmbeddings(BaseModel, Embeddings):
+ """
+ Usage:
+ OVH_AI_ENDPOINTS_ACCESS_TOKEN="your-token" python3 langchain_embedding.py
+ NB: Make sure you are using a valid token.
+ In the contrary, document indexing will be long due to rate-limiting.
+ """
+
+ """ OVHcloud AI Endpoints Access Token"""
+ access_token: Optional[str] = None
+
+ """ OVHcloud AI Endpoints model name for embeddings generation"""
+ model_name: str = ""
+
+ """ OVHcloud AI Endpoints region"""
+ region: str = "kepler"
+
+ class Config:
+ """Configuration for this pydantic object."""
+
+ extra = Extra.forbid
+
+ def __init__(self, **kwargs: Any):
+ super().__init__(**kwargs)
+ if self.access_token is None:
+ logger.warning(
+ "No access token provided indexing will be slow due to rate limiting."
+ )
+ if self.model_name == "":
+ raise ValueError("Model name is required for OVHCloud embeddings.")
+ if self.region == "":
+ raise ValueError("Region is required for OVHCloud embeddings.")
+
+ def _generate_embedding(self, text: str) -> List[float]:
+ """Generate embeddings from OVHCLOUD AIE.
+ Args:
+ text (str): The text to embed.
+ Returns:
+ List[float]: Embeddings for the text.
+ """
+ headers = {
+ "content-type": "text/plain",
+ "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",
+ headers=headers,
+ data=text,
+ )
+ if response.status_code != 200:
+ if response.status_code == 429:
+ """Rate limit exceeded, wait for reset"""
+ reset_time = int(response.headers.get("RateLimit-Reset", 0))
+ logger.info("Rate limit exceeded. Waiting %d seconds.", reset_time)
+ if reset_time > 0:
+ time.sleep(reset_time)
+ continue
+ else:
+ """Rate limit reset time has passed, retry immediately"""
+ continue
+
+ """ Handle other non-200 status codes """
+ raise ValueError(
+ "Request failed with status code: {status_code}, {text}".format(
+ status_code=response.status_code, text=response.text
+ )
+ )
+ 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)
diff --git a/libs/community/tests/unit_tests/embeddings/test_imports.py b/libs/community/tests/unit_tests/embeddings/test_imports.py
index 3dd123ae79..85b8a37e21 100644
--- a/libs/community/tests/unit_tests/embeddings/test_imports.py
+++ b/libs/community/tests/unit_tests/embeddings/test_imports.py
@@ -60,6 +60,7 @@ EXPECTED_ALL = [
"JavelinAIGatewayEmbeddings",
"OllamaEmbeddings",
"OracleEmbeddings",
+ "OVHCloudEmbeddings",
"QianfanEmbeddingsEndpoint",
"JohnSnowLabsEmbeddings",
"VoyageEmbeddings",
diff --git a/libs/community/tests/unit_tests/embeddings/test_ovhcloud.py b/libs/community/tests/unit_tests/embeddings/test_ovhcloud.py
new file mode 100644
index 0000000000..c456238e69
--- /dev/null
+++ b/libs/community/tests/unit_tests/embeddings/test_ovhcloud.py
@@ -0,0 +1,32 @@
+import pytest
+
+from langchain_community.embeddings.ovhcloud import OVHCloudEmbeddings
+
+
+def test_ovhcloud_correct_instantiation() -> None:
+ llm = OVHCloudEmbeddings(model_name="multilingual-e5-base")
+ assert isinstance(llm, OVHCloudEmbeddings)
+
+
+def test_ovhcloud_empty_model_name_should_raise_error() -> None:
+ with pytest.raises(ValueError):
+ OVHCloudEmbeddings(model_name="")
+
+
+def test_ovhcloud_empty_region_should_raise_error() -> None:
+ with pytest.raises(ValueError):
+ OVHCloudEmbeddings(model_name="multilingual-e5-base", region="")
+
+
+def test_ovhcloud_empty_access_token_should_not_raise_error() -> None:
+ llm = OVHCloudEmbeddings(
+ model_name="multilingual-e5-base", region="kepler", access_token=""
+ )
+ assert isinstance(llm, OVHCloudEmbeddings)
+
+
+def test_ovhcloud_embed_documents() -> None:
+ llm = OVHCloudEmbeddings(model_name="multilingual-e5-base")
+ docs = ["Hello", "World"]
+ output = llm.embed_documents(docs)
+ assert len(output) == len(docs)