community: Fix OVHcloud 401 Unauthorized on embedding. (#23260)

They are now rejecting with code 401 calls from users with expired or
invalid tokens (while before they were being considered anonymous).
Thus, the authorization header has to be removed when there is no token.

Related to: #23178

---------

Signed-off-by: Joffref <mariusjoffre@gmail.com>
This commit is contained in:
Mathis Joffre
2024-06-24 18:58:32 +02:00
committed by GitHub
parent 4964ba74db
commit 60103fc4a5
4 changed files with 39 additions and 28 deletions

View File

@@ -1,6 +1,6 @@
import logging
import time
from typing import Any, List, Optional
from typing import Any, List
import requests
from langchain_core.embeddings import Embeddings
@@ -11,14 +11,11 @@ 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 Embeddings.
"""
""" OVHcloud AI Endpoints Access Token"""
access_token: Optional[str] = None
access_token: str = ""
""" OVHcloud AI Endpoints model name for embeddings generation"""
model_name: str = ""
@@ -33,10 +30,8 @@ class OVHCloudEmbeddings(BaseModel, Embeddings):
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.access_token == "":
raise ValueError("Access token is required for OVHCloud embeddings.")
if self.model_name == "":
raise ValueError("Model name is required for OVHCloud embeddings.")
if self.region == "":
@@ -72,7 +67,9 @@ class OVHCloudEmbeddings(BaseModel, Embeddings):
else:
"""Rate limit reset time has passed, retry immediately"""
continue
if response.status_code == 401:
""" Unauthorized, retry with new token """
raise ValueError("Unauthorized, retry with new token")
""" Handle other non-200 status codes """
raise ValueError(
"Request failed with status code: {status_code}, {text}".format(