a
    bg                     @   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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)run_in_executor)get_from_dict_or_envpre_init)	BaseModel
ConfigDictz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dd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 )Zprotected_namespaces)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   r   r/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/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   
_embedding5   s    
zErnieEmbeddings._embedding)r   c                 C   sv   | j \ td | j d}tj|dddd| j| jdd}t|	 
d| _W d    n1 sh0    Y  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_lockC   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>a   s   z3ErnieEmbeddings.embed_documents.<locals>.<listcomp>r   inputc                 S   s   g | ]}|qS r   r   r/   textr   r   r   r2   g       
error_codeo   c                 S   s   g | ]}|qS r   r   r4   r   r   r   r2   k   r6   Error from Ernie: c                 S   s   g | ]}|d  qS )	embeddingr   r.   r   r   r   r2   n   r6   data)	r   r,   rangelenr   r%   r+   
ValueErrorextend)r"   r-   Ztext_in_chunkslstchunkr$   r   r1   r   embed_documentsU   s    

zErnieEmbeddings.embed_documents)r5   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.
        r3   r7   r8   r9   r;   r   r:   )r   r,   r%   r+   r>   )r"   r5   r$   r   r   r   embed_queryq   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   rC   )r"   r5   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   )rD   r4   r"   r   r   r2      r6   z4ErnieEmbeddings.aembed_documents.<locals>.<listcomp>N)asynciogatherlist)r"   r-   resultr   rE   r   aembed_documents   s    
z ErnieEmbeddings.aembed_documents) __name__
__module____qualname____doc__r   r   r*   __annotations__r   r   r   r   intr   	threadingLockr'   r   Zmodel_configr	   r   r   objectdictr%   r,   r   floatrB   rC   rD   rJ   r   r   r   r   r      s"   

r   )rF   loggingrQ   typingr   r   r   r    Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.runnables.configr   Zlangchain_core.utilsr   r	   Zpydanticr
   r   	getLoggerrK   r(   r   r   r   r   r   <module>   s   
