a
    bgg                     @  s   d dl mZ d dlZd dlmZmZmZmZmZ d dl	Z	d dl
mZ d dlmZ d dlmZmZmZ d dlmZmZmZ d dlmZmZmZmZ eeZd	d
ddZdddddddZeddddG dd deeZ dS )    )annotationsN)AnyCallableDictListOptional)
deprecated)
Embeddings)convert_to_secret_strget_from_dict_or_envpre_init)	BaseModel
ConfigDict	SecretStr)before_sleep_logretrystop_after_attemptwait_exponentialzCallable[[Any], Any])returnc                  C  s6   d} d}d}d}t dt|t| ||dtttjdS )z#Returns a tenacity retry decorator.         T)
multiplierminmax)reraisestopwaitbefore_sleep)r   r   r   r   loggerloggingWARNING)r   Zmin_secondsZmax_secondsmax_retries r#   r/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/solar.py_create_retry_decorator   s    
r%   SolarEmbeddingsr   )
embeddingsargskwargsr   c                   s.   t  }|dddd fdd}||i |S )z*Use tenacity to retry the completion call.r   )r(   r)   r   c                    s    j | i |S )N)embed)r(   r)   r'   r#   r$   _embed_with_retry)   s    z+embed_with_retry.<locals>._embed_with_retry)r%   )r'   r(   r)   Zretry_decoratorr,   r#   r+   r$   embed_with_retry%   s    r-   z0.0.34z1.0zlangchain_upstage.ChatUpstage)ZsinceZremovalZalternative_importc                   @  s   e Zd ZU dZdZded< dZded< dZded	< ed
dZ	e
dddddZdddddZdddddZdddddZdS )r&   aJ  Solar's embedding service.

    To use, you should have the environment variable``SOLAR_API_KEY`` set
    with your API token, or pass it as a named parameter to the constructor.

    Example:
        .. code-block:: python

            from langchain_community.embeddings import SolarEmbeddings
            embeddings = SolarEmbeddings()

            query_text = "This is a test query."
            query_result = embeddings.embed_query(query_text)

            document_text = "This is a test document."
            document_result = embeddings.embed_documents([document_text])

    z*https://api.upstage.ai/v1/solar/embeddingsstrendpoint_urlzembedding-querymodelNzOptional[SecretStr]solar_api_keyZforbid)extrar   )valuesr   c                 C  s   t t|dd}||d< |S )z'Validate api key exists in environment.r1   ZSOLAR_API_KEY)r
   r   )clsr3   r1   r#   r#   r$   validate_environmentR   s
    
z$SolarEmbeddings.validate_environmentzList[List[float]])textr   c                 C  sr   | j |d}d| j  dd}tj| j||d}| }t|d dkr^td|d	  |d d d
 }|S )N)r0   inputzBearer zapplication/json)AuthorizationzContent-Type)headersjsondatar   zSolar API returned an error: Z	base_resp	embedding)	r0   r1   Zget_secret_valuerequestspostr/   r:   len
ValueError)selfr6   payloadr9   responseZparsed_responser<   r#   r#   r$   r*   [   s    zSolarEmbeddings.embedz	List[str])textsr   c                   s    fdd|D }|S )zEmbed documents using a Solar embedding endpoint.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                   s   g | ]}t  |d qS )r6   r-   ).0r6   rA   r#   r$   
<listcomp>       z3SolarEmbeddings.embed_documents.<locals>.<listcomp>r#   )rA   rD   r'   r#   rH   r$   embed_documentsx   s    	zSolarEmbeddings.embed_documentszList[float]c                 C  s   t | |d}|S )zEmbed a query using a Solar embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        rE   rF   )rA   r6   r<   r#   r#   r$   embed_query   s    	zSolarEmbeddings.embed_query)__name__
__module____qualname____doc__r/   __annotations__r0   r1   r   Zmodel_configr   r5   r*   rK   rL   r#   r#   r#   r$   r&   0   s   
)!
__future__r   r    typingr   r   r   r   r   r=   Zlangchain_core._apir   Zlangchain_core.embeddingsr	   Zlangchain_core.utilsr
   r   r   Zpydanticr   r   r   tenacityr   r   r   r   	getLoggerrM   r   r%   r-   r&   r#   r#   r#   r$   <module>   s   
