
    dh                         S SK r S SKrS SKrS SKJrJr  S SKrS SKJr  S SK	J
r
Jr  \R                  " \5      r " S S\
\5      rg)    N)AnyList)
Embeddings)	BaseModel
ConfigDictc                      ^  \ rS rSr% Sr Sr\\S'    Sr\\S'    Sr	\\S'   \
" SS	S
9rS\4U 4S jjrS\S\\   4S jrS\\   S\\\      4S jrS\S\\   4S jrS\S\S\S\4S jrS	rU =r$ )OVHCloudEmbeddings   z#
OVHcloud AI Endpoints Embeddings.
 access_token
model_namekeplerregionforbid )extraprotected_namespaceskwargsc                    > [         TU ]  " S0 UD6  U R                  S:X  a  [        S5      eU R                  S:X  a  [        S5      eU R
                  S:X  a  [        S5      eg )Nr   z1Access token is required for OVHCloud embeddings.z/Model name is required for OVHCloud embeddings.z+Region is required for OVHCloud embeddings.r   )super__init__r   
ValueErrorr   r   )selfr   	__class__s     _/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/ovhcloud.pyr   OVHCloudEmbeddings.__init__   sb    "6""PQQ??b NOO;;"JKK     textreturnc                 (    U R                  SUS5      $ )zGenerate embeddings from OVHCLOUD AIE.
Args:
    text (str): The text to embed.
Returns:
    List[float]: Embeddings for the text.
z
text/plaintext2vec)_send_request_to_ai_endpointsr   r   s     r   _generate_embedding&OVHCloudEmbeddings._generate_embedding&   s     11,jQQr   textsc                 P    U R                  S[        R                  " U5      S5      $ )zEmbed 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.

zapplication/jsonbatch_text2vec)r"   jsondumps)r   r&   s     r   embed_documents"OVHCloudEmbeddings.embed_documents0   s(     11

5 13C
 	
r   c                 $    U R                  U5      $ )zwEmbed a single query text.
Args:
    text (str): The text to embed.
Returns:
    List[float]: Embeddings for the text.
)r$   r#   s     r   embed_queryOVHCloudEmbeddings.embed_query>   s     ''--r   contentTypepayloadroutec                 \   USU R                    3S.n[        R                  " 5       n UR                  SU R                   SU R
                   SU 3UUS9nUR                  S:w  a  UR                  S:X  a]   [        UR                  R                  S	S
5      5      n[        R                  SU5        US
:  a  [        R                  " U5        M   M  UR                  S:X  a   [        S5      e [        SR                  UR                  UR                   S95      eUR#                  5       $ )a  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.
zBearer )zcontent-typeAuthorizationzhttps://z.endpoints.z.ai.cloud.ovh.net/api/)headersdata   i  zRateLimit-Resetr   z(Rate limit exceeded. Waiting %d seconds.i  z"Unauthorized, retry with new tokenz6Request failed with status code: {status_code}, {text})status_coder   )r   requestssessionpostr   r   r8   intr5   getloggerinfotimesleepr   formatr   r)   )r   r0   r1   r2   r5   r:   response
reset_times           r   r"   0OVHCloudEmbeddings._send_request_to_ai_endpointsG   s7    (&t'8'8&9:

 ""$||t/{4;;-,UG5   $ H ##s*''3.=!$X%5%5%9%9:KQ%O!PJKK JJW!A~

:. Q ''3.>$%IJJ9 LSS$,$8$8x}} T  
 ==?"r   )__name__
__module____qualname____firstlineno____doc__r   str__annotations__r   r   r   model_configr   r   r   floatr$   r+   r.   r"   __static_attributes____classcell__)r   s   @r   r	   r	      s     .L#EJ'FCH2FLL LR RU R
T#Y 
4U3D 
. .U .,#,#),,#58,#	,# ,#r   r	   )r)   loggingr@   typingr   r   r9   langchain_core.embeddingsr   pydanticr   r   	getLoggerrF   r>   r	   r   r   r   <module>rV      s:         0 *			8	$f#J f#r   