o
    Zh(                     @  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 d dlmZmZ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ZdddZdddZG dd dee
ZdS )    )annotationsN)AnyCallableDictListOptional)
Embeddings)get_from_dict_or_env)	BaseModel
ConfigDictmodel_validator)	HTTPError)before_sleep_logretryretry_if_exception_typestop_after_attemptwait_exponential      )text-embedding-v1ztext-embedding-v2ztext-embedding-v3
embeddingsDashScopeEmbeddingsreturnCallable[[Any], Any]c                 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   before_sleep)
r   r   max_retriesr   r   r   r   loggerloggingWARNING)r   
multiplierZmin_secondsZmax_seconds r'   _/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/embeddings/dashscope.py_create_retry_decorator   s   
r)   kwargsr   c                   s(   t  }|d fdd}|di |S )	z)Use tenacity to retry the embedding call.r*   r   r   c                    s   g }d}| d }t |trt|nd}t| d d}||k rut |tr,||||  n|| d<  jjdi | }|jdkrF||jd 7 }n'|jdv r[t	d	|j d
|j
 d|j td|j d
|j
 d|j |d||7 }||k s|S )Nr   inputr   modelr      r   )i  i  zstatus_code: z	 
 code: z 
 message: z"HTTP error occurred: status_code: )responser'   )
isinstancelistlen
BATCH_SIZEgetclientcallstatus_codeoutput
ValueErrorcodemessager   )r*   resultiZ
input_dataZ	input_lenZ
batch_sizerespr   r'   r(   _embed_with_retry0   s@   



z+embed_with_retry.<locals>._embed_with_retryN)r*   r   r   r   r'   )r)   )r   r*   Zretry_decoratorr?   r'   r>   r(   embed_with_retry,   s   r@   c                   @  s|   e Zd ZU dZ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dddZd ddZ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)

    Nr   r4   r   strr,   zOptional[str]dashscope_api_key   intr"   Zforbid)extrabefore)modevaluesr   r   c                 C  sT   dd l }	 t|dd|d< |d |_zdd l }|j|d< W |S  ty)   tdw )Nr   rB   ZDASHSCOPE_API_KEYr4   zZCould not import dashscope python package. Please install it with `pip install dashscope`.)	dashscoper	   Zapi_keyZTextEmbeddingImportError)clsrH   rI   r'   r'   r(   validate_environmentx   s   
z(DashScopeEmbeddings.validate_environmenttexts	List[str]List[List[float]]c                 C  s$   t | |d| jd}dd |D }|S )zCall out to DashScope's embedding endpoint for embedding search docs.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        Zdocumentr+   	text_typer,   c                 S  s   g | ]}|d  qS )	embeddingr'   ).0itemr'   r'   r(   
<listcomp>   s    z7DashScopeEmbeddings.embed_documents.<locals>.<listcomp>r@   r,   )selfrM   r   Zembedding_listr'   r'   r(   embed_documents   s
   	
z#DashScopeEmbeddings.embed_documentstextList[float]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.
        queryrP   r   rR   rV   )rW   rY   rR   r'   r'   r(   embed_query   s   	
zDashScopeEmbeddings.embed_query)rH   r   r   r   )rM   rN   r   rO   )rY   rA   r   rZ   )__name__
__module____qualname____doc__r4   __annotations__r,   rB   r"   r   Zmodel_configr   classmethodrL   rX   r\   r'   r'   r'   r(   r   Q   s    
 
)r   r   r   r   )r   r   r*   r   r   r   ) 
__future__r   r$   typingr   r   r   r   r   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr	   Zpydanticr
   r   r   Zrequests.exceptionsr   tenacityr   r   r   r   r   	getLoggerr]   r#   r2   r)   r@   r   r'   r'   r'   r(   <module>   s    


%