U
    h|                     @   s   d dl Z d dlZd dlZd dl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 d dlmZ d dlmZmZ eeZe	dd	d
G dd deeZdS )    N)DictListOptional)
deprecated)
Embeddings)	BaseModel)run_in_executor)get_from_dict_or_envpre_initz0.0.13z8langchain_community.embeddings.QianfanEmbeddingsEndpoint)Zsincealternativec                   @   s  e Zd ZU dZdZee ed< dZee ed< dZ	ee ed< dZ
ee ed< dZeed< d	Zeed
< e ZeeedddZeedddZddddZee eee  dddZeee dddZeee dddZee eee  dddZdS )ErnieEmbeddingsz'`Ernie Embeddings V1` embedding models.Nernie_api_baseernie_client_idernie_client_secretaccess_token   
chunk_sizezErnieBot-Embedding-V1
model_name)valuesreturnc                 C   s6   t |ddd|d< t |dd|d< t |dd|d< |S )Nr   ZERNIE_API_BASEzhttps://aip.baidubce.comr   ZERNIE_CLIENT_IDr   ZERNIE_CLIENT_SECRET)r	   )clsr    r   H/tmp/pip-unpacked-wheel-9gdii04g/langchain_community/embeddings/ernie.pyvalidate_environment"   s"       z$ErnieEmbeddings.validate_environment)jsonr   c                 C   s6   | j  d}tj| dddid| ji|d}| S )Nz//rpc/2.0/ai_custom/v1/wenxinworkshop/embeddingsz/embedding-v1Content-Typeapplication/jsonr   )headersparamsr   )r   requestspostr   r   )selfr   base_urlrespr   r   r   
_embedding3   s    
 zErnieEmbeddings._embedding)r   c              	   C   sb   | j R td | j d}tj|dddd| j| jdd}t|	 
d| _W 5 Q R X d S )	NzRefreshing access tokenz/oauth/2.0/tokenr   )r   AcceptZclient_credentials)Z
grant_typeZ	client_idZclient_secret)r   r   r   )_lockloggerdebugr   r   r    r   r   strr   getr   )r!   r"   r#   r   r   r   _refresh_access_token_with_lockA   s    
z/ErnieEmbeddings._refresh_access_token_with_lock)textsr   c                    s    j s    fddtdt jD }g }|D ]|} ddd |D i}|dr|ddkr    ddd |D i}ntd	| |d
d |d D  q8|S )zEmbed search docs.

        Args:
            texts: The list of texts to embed

        Returns:
            List[List[float]]: List of embeddings, one for each text.
        c                    s   g | ]}|| j   qS r   )r   .0ir!   r,   r   r   
<listcomp>_   s   z3ErnieEmbeddings.embed_documents.<locals>.<listcomp>r   inputc                 S   s   g | ]}|qS r   r   r.   textr   r   r   r1   e   s     
error_codeo   c                 S   s   g | ]}|qS r   r   r3   r   r   r   r1   i   s     Error from Ernie: c                 S   s   g | ]}|d  qS )	embeddingr   r-   r   r   r   r1   l   s     data)	r   r+   rangelenr   r$   r*   
ValueErrorextend)r!   r,   Ztext_in_chunkslstchunkr#   r   r0   r   embed_documentsS   s    

zErnieEmbeddings.embed_documents)r4   r   c                 C   sn   | j s|   | d|gi}|dr^|ddkrP|   | d|gi}ntd| |d d d S )zEmbed query text.

        Args:
            text: The text to embed.

        Returns:
            List[float]: Embeddings for the text.
        r2   r5   r6   r7   r9   r   r8   )r   r+   r$   r*   r<   )r!   r4   r#   r   r   r   embed_queryo   s    

zErnieEmbeddings.embed_queryc                    s   t d| j|I dH S )zAsynchronous Embed query text.

        Args:
            text: The text to embed.

        Returns:
            List[float]: Embeddings for the text.
        N)r   rA   )r!   r4   r   r   r   aembed_query   s    
zErnieEmbeddings.aembed_queryc                    s&   t j fdd|D  I dH }t|S )zAsynchronous Embed search docs.

        Args:
            texts: The list of texts to embed

        Returns:
            List[List[float]]: List of embeddings, one for each text.
        c                    s   g | ]}  |qS r   )rB   r3   r!   r   r   r1      s     z4ErnieEmbeddings.aembed_documents.<locals>.<listcomp>N)asyncioZgatherlist)r!   r,   resultr   rC   r   aembed_documents   s    
z ErnieEmbeddings.aembed_documents)__name__
__module____qualname____doc__r   r   r)   __annotations__r   r   r   r   intr   	threadingLockr&   r
   r   r   objectdictr$   r+   r   floatr@   rA   rB   rG   r   r   r   r   r      s    
r   )rD   loggingrN   typingr   r   r   r   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.pydantic_v1r   Zlangchain_core.runnables.configr   Zlangchain_core.utilsr	   r
   	getLoggerrH   r'   r   r   r   r   r   <module>   s   
