o
    Zh                     @   sl   d dl Z d dlZd dlZd dlmZmZ d dlZd dlmZ d dl	m
Z
mZ eeZG dd de
eZdS )    N)AnyList)
Embeddings)	BaseModel
ConfigDictc                       s   e Zd ZU dZ	 dZeed< 	 dZeed< 	 dZeed< e	ddd	Z
d
ef fddZdedee fddZdee deee  fddZdedee fddZdedededefddZ  ZS )OVHCloudEmbeddingsz+
    OVHcloud AI Endpoints Embeddings.
     access_token
model_nameZkeplerregionZforbid )extraZprotected_namespaceskwargsc                    sL   t  jdi | | jdkrtd| jdkrtd| jdkr$tdd S )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__r   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/ovhcloud.pyr      s   


zOVHCloudEmbeddings.__init__textreturnc                 C   s   |  d|dS )zGenerate embeddings from OVHCLOUD AIE.
        Args:
            text (str): The text to embed.
        Returns:
            List[float]: Embeddings for the text.
        z
text/plainZtext2vec)_send_request_to_ai_endpointsr   r   r   r   r   _generate_embedding&   s   z&OVHCloudEmbeddings._generate_embeddingtextsc                 C   s   |  dt|dS )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/jsonZbatch_text2vec)r   jsondumps)r   r   r   r   r   embed_documents0   s   
z"OVHCloudEmbeddings.embed_documentsc                 C   s
   |  |S )zEmbed a single query text.
        Args:
            text (str): The text to embed.
        Returns:
            List[float]: Embeddings for the text.
        )r   r   r   r   r   embed_query>   s   
zOVHCloudEmbeddings.embed_querycontentTypepayloadroutec                 C   s   |d| j  d}t }	 |jd| j d| j d| ||d}|jdkr]|jd	krG	 t|j	d
d}t
d| |dkrEt| q	 q|jdkrQ	 td	 tdj|j|jd| S )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AuthorizationTz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   r'   intr$   getloggerinfotimesleepr   formatr   r   )r   r    r!   r"   r$   r)   responseZ
reset_timer   r   r   r   G   s@   





z0OVHCloudEmbeddings._send_request_to_ai_endpoints)__name__
__module____qualname____doc__r	   str__annotations__r
   r   r   Zmodel_configr   r   r   floatr   r   r   r   __classcell__r   r   r   r   r      s,   
 	
	r   )r   loggingr/   typingr   r   r(   Zlangchain_core.embeddingsr   Zpydanticr   r   	getLoggerr3   r-   r   r   r   r   r   <module>   s    
