o
    Zh                      @  s   d dl mZ d dlZd dlZd dlmZmZmZ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 d dlmZmZmZmZ d dlmZmZmZmZ ee Z!dddZ"dddZ#dddZ$eddddG dd
 d
eeZ%dS )    )annotationsN)AnyCallableDictListOptionalTupleUnioncast)
deprecated)
Embeddings)convert_to_secret_strget_from_dict_or_env)	BaseModel
ConfigDict	SecretStrmodel_validator)before_sleep_logretrystop_after_attemptwait_exponential
embeddingsVoyageEmbeddingsreturnCallable[[Any], Any]c                 C  s0   d}d}t dt| jtd||dtttjdS )N   
   T   )
multiplierminmax)reraisestopwaitbefore_sleep)r   r   max_retriesr   r   loggerloggingWARNING)r   Zmin_secondsZmax_seconds r)   ^/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/voyageai.py_create_retry_decorator   s   
r+   responsedictc                 C  s    d| vrt dt|  | S )NdatazVoyage API Error. Message: )RuntimeErrorjsondumps)r,   r)   r)   r*   _check_response,   s   r2   kwargsr   c                 K  s$   t | }|ddd}|di |S )	z)Use tenacity to retry the embedding call.r3   r   r   c                  [  s   t jdi | }t| S )Nr)   )requestspostr2   r0   )r3   r,   r)   r)   r*   _embed_with_retry6   s   z+embed_with_retry.<locals>._embed_with_retryN)r3   r   r   r   r)   )r+   )r   r3   Zretry_decoratorr6   r)   r)   r*   embed_with_retry2   s   r7   z0.0.29z1.0z%langchain_voyageai.VoyageAIEmbeddings)ZsinceZremovalZalternative_importc                   @  s   e Zd ZU dZded< dZded< dZded< d	ed
< 	 dZd	ed< 	 dZded< 	 dZ	ded< 	 dZ
ded< 	 eddZedded2ddZ	d3d4d"d#Z		d5d6d'd(Zd7d)d*Zd8d-d.Zdd/d9d0d1ZdS ):r   a  Voyage embedding models.

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

    Example:
        .. code-block:: python

            from langchain_community.embeddings import VoyageEmbeddings

            voyage = VoyageEmbeddings(voyage_api_key="your-api-key", model="voyage-2")
            text = "This is a test query."
            query_result = voyage.embed_query(text)
    strmodelz&https://api.voyageai.com/v1/embeddingsvoyage_api_baseNzOptional[SecretStr]voyage_api_keyint
batch_size   r%   z+Optional[Union[float, Tuple[float, float]]]request_timeoutFboolshow_progress_barT
truncationZforbid)extrabefore)modevaluesr   r   r   c                 C  sZ   t t|dd|d< d|vrd|d< td d|vr+d|v r'|d dv r'dnd	|d< |S )
z?Validate that api key and python package exists in environment.r;   ZVOYAGE_API_KEYr9   z	voyage-01zmodel will become a required arg for VoyageAIEmbeddings, we recommend to specify it when using this class. Currently the default is set to voyage-01.r=   )zvoyage-2z	voyage-02H      )r   r   r&   warning)clsrF   r)   r)   r*   validate_environmentj   s   
z%VoyageEmbeddings.validate_environmentinput	List[str]
input_typeOptional[str]c                 C  s>   t t| j }| jdd| i| j||| jd| jd}|S )NAuthorizationzBearer )r9   rL   rN   rB   )urlheadersr0   timeout)r
   r   r;   Zget_secret_valuer:   r9   rB   r?   )selfrL   rN   Zapi_keyparamsr)   r)   r*   _invocation_params   s   z#VoyageEmbeddings._invocation_paramstextsOptional[int]List[List[float]]c           
   
   C  s   g }|d u r	| j }| jr1zddlm} W n ty% } ztd|d }~ww |tdt||}ntdt||}|rG|dvrGtd| d|D ]!}t| fi | j	||||  |d}	|
dd	 |	d
 D  qI|S )Nr   )tqdmzgMust have tqdm installed if `show_progress_bar` is set to True. Please install with `pip install tqdm`.)querydocumentzinput_type z0 is invalid. Options: None, 'query', 'document'.)rL   rN   c                 s  s    | ]}|d  V  qdS )Z	embeddingNr)   ).0rr)   r)   r*   	<genexpr>   s    z3VoyageEmbeddings._get_embeddings.<locals>.<genexpr>r.   )r=   rA   Z	tqdm.autorZ   ImportErrorrangelen
ValueErrorr7   rV   extend)
rT   rW   r=   rN   r   rZ   eZ_iterir,   r)   r)   r*   _get_embeddings   s<   
z VoyageEmbeddings._get_embeddingsc                 C  s   | j || jddS )zCall out to Voyage Embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        r\   r=   rN   rg   r=   )rT   rW   r)   r)   r*   embed_documents   s   	z VoyageEmbeddings.embed_documentstextList[float]c                 C  s   | j |g| jddd S )zCall out to Voyage Embedding endpoint for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        r[   rh   r   ri   )rT   rk   r)   r)   r*   embed_query   s
   	
zVoyageEmbeddings.embed_query)rN   c                C  s   | j || j|dS )aP  Call out to Voyage Embedding endpoint for embedding general text.

        Args:
            texts: The list of texts to embed.
            input_type: Type of the input text. Default to None, meaning the type is
                unspecified. Other options: query, document.

        Returns:
            Embedding for the text.
        rh   ri   )rT   rW   rN   r)   r)   r*   embed_general_texts   s   z$VoyageEmbeddings.embed_general_texts)rF   r   r   r   )N)rL   rM   rN   rO   r   r   )NN)rW   rM   r=   rX   rN   rO   r   rY   )rW   rM   r   rY   )rk   r8   r   rl   )rW   rM   rN   rO   r   rY   )__name__
__module____qualname____doc____annotations__r:   r;   r%   r?   rA   rB   r   Zmodel_configr   classmethodrK   rV   rg   rj   rm   rn   r)   r)   r)   r*   r   >   s<   
 
)
)r   r   r   r   )r,   r-   r   r-   )r   r   r3   r   r   r   )&
__future__r   r0   r'   typingr   r   r   r   r   r   r	   r
   r4   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr   r   Zpydanticr   r   r   r   tenacityr   r   r   r   	getLoggerro   r&   r+   r2   r7   r   r)   r)   r)   r*   <module>   s(    (



