U
    h                     @  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	m
Z
 d dlmZmZ d dlmZ d dlmZ d dlmZmZmZmZmZ eeZd	d
dddZd	dddddZG dd	 d	ee
ZdS )    )annotationsN)AnyCallableDictListOptional)
Embeddings)	BaseModelroot_validator)get_from_dict_or_env)	HTTPError)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponentialDashScopeEmbeddingszCallable[[Any], Any])
embeddingsreturnc                 C  s:   d}d}d}t dt| jt|||dtttttj	dS )N      T)minmax)reraisestopwaitr   Zbefore_sleep)
r   r   max_retriesr   r   r   r   loggerloggingWARNING)r   Z
multiplierZmin_secondsZmax_seconds r    L/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/dashscope.py_create_retry_decorator   s    
r"   r   )r   kwargsr   c                   s*   t  }|ddd fdd}|f |S )z)Use tenacity to retry the embedding call.r   )r#   r   c                    s   g }d}| d }t |tr"t|nd}||k rt |trH|||d  n|| d<  jjf | }|jdkrx||jd 7 }nP|jdkrtd|j d	|j d
|j	 n$t
d|j d	|j d
|j	 |d|d7 }q&|S )Nr   inputr         r   )i  i  zstatus_code: z	 
 code: z 
 message: z"HTTP error occurred: status_code: )response)
isinstancelistlenclientcallstatus_codeoutput
ValueErrorcodemessager   )r#   resultiZ
input_dataZ	input_lenrespr   r    r!   _embed_with_retry.   s(    


z+embed_with_retry.<locals>._embed_with_retry)r"   )r   r#   Zretry_decoratorr6   r    r5   r!   embed_with_retry*   s    r7   c                   @  s   e Zd ZU dZded< dZded< dZded	< d
Zded< G dd dZe	dddddddZ
dddddZdddddZdS )r   a`  DashScope embedding models.

    To use, you should have the ``dashscope`` python package installed, and the
    environment variable ``DASHSCOPE_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 DashScopeEmbeddings
            embeddings = DashScopeEmbeddings(dashscope_api_key="my-api-key")

    Example:
        .. code-block:: python

            import os
            os.environ["DASHSCOPE_API_KEY"] = "your DashScope API KEY"

            from langchain_community.embeddings.dashscope import DashScopeEmbeddings
            embeddings = DashScopeEmbeddings(
                model="text-embedding-v1",
            )
            text = "This is a test query."
            query_result = embeddings.embed_query(text)

    r   r+   ztext-embedding-v1strmodelNzOptional[str]dashscope_api_key   intr   c                   @  s   e Zd ZdZdS )zDashScopeEmbeddings.ConfigZforbidN)__name__
__module____qualname__extrar    r    r    r!   Configo   s   rA   T)prer   )valuesr   c                 C  sZ   dd l }t|dd|d< |d |_zdd l }|j|d< W n tk
rT   tdY nX |S )Nr   r:   ZDASHSCOPE_API_KEYr+   zZCould not import dashscope python package. Please install it with `pip install dashscope`.)	dashscoper   Zapi_keyZTextEmbeddingImportError)clsrC   rD   r    r    r!   validate_environmentr   s      

z(DashScopeEmbeddings.validate_environmentz	List[str]zList[List[float]])textsr   c                 C  s$   t | |d| jd}dd |D }|S )aP  Call out to DashScope's embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.
            chunk_size: The chunk size of embeddings. If None, will use the chunk size
                specified by the class.

        Returns:
            List of embeddings, one for each text.
        documentr$   	text_typer9   c                 S  s   g | ]}|d  qS )	embeddingr    ).0itemr    r    r!   
<listcomp>   s     z7DashScopeEmbeddings.embed_documents.<locals>.<listcomp>r7   r9   )selfrH   r   Zembedding_listr    r    r!   embed_documents   s       z#DashScopeEmbeddings.embed_documentszList[float])textr   c                 C  s   t | |d| jdd d }|S )zCall out to DashScope's embedding endpoint for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        queryrJ   r   rL   rP   )rQ   rS   rL   r    r    r!   embed_query   s    	   zDashScopeEmbeddings.embed_query)r=   r>   r?   __doc____annotations__r9   r:   r   rA   r
   rG   rR   rU   r    r    r    r!   r   L   s   
)
__future__r   r   typingr   r   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r	   r
   Zlangchain_core.utilsr   Zrequests.exceptionsr   Ztenacityr   r   r   r   r   	getLoggerr=   r   r"   r7   r   r    r    r    r!   <module>   s   
"