a
    bgy                     @  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 d dl	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	d
ddddZeddddG dd deeZdS )    )annotationsN)AnyDictListOptional)
deprecated)
Embeddings)pre_init)	BaseModelstrzOptional[dict]r   bool)urlheaderspayloadreturnc              
   C  sr   z6t jd| ||d}|jdkr$W dS td|j W n6 t jjyl } ztd| W Y d}~n
d}~0 0 dS )a  
    Check if an endpoint is live by sending a GET request to the specified URL.

    Args:
        url (str): The URL of the endpoint to check.

    Returns:
        bool: True if the endpoint is live (status code 200), False otherwise.

    Raises:
        Exception: If the endpoint returns a non-successful status code or if there is
            an error querying the endpoint.
    POSTr   data   Tz0Endpoint returned a non-successful status code: zError querying the endpoint: N)requestsrequeststatus_code	Exception
exceptionsRequestException)r   r   r   responsee r   q/var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/embeddings/nemo.pyis_endpoint_live   s    
r   z0.0.37z1.0.0zDirectly instantiating a NeMoEmbeddings from langchain-community is deprecated. Please use langchain-nvidia-ai-endpoints NVIDIAEmbeddings interface.)ZsinceZremovalmessagec                   @  s   e Zd ZU dZdZded< dZded< dZded	< ed
d
dddZ	dddddddZ
ddddddZdddddZdddddZdddddZddd d!d"Zd#S )$NeMoEmbeddingszNeMo embedding models.   int
batch_sizezNV-Embed-QA-003r   modelz#http://localhost:8088/v1/embeddingsapi_endpoint_urlr   )valuesr   c                 C  s:   |d }|d }ddi}t d|dd}t||| |S )zHValidate that the end point is alive using the values that are provided.r&   r%   Content-Typeapplication/jsonzHello Worldqueryinputr%   
input_type)jsondumpsr   )clsr'   r   r%   r   r   r   r   r   validate_environment>   s    
z#NeMoEmbeddings.validate_environmentr   zList[float])sessiontextr-   r   c              	     s   ddi}|j | j|| j|d|d4 I dH F}|  | I dH }t|}|d d d W  d  I dH  S 1 I dH s~0    Y  dS )	zAsync call out to embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r(   r)   r+   )r.   r   Nr   r   	embedding)postr&   r%   raise_for_statusr3   r.   loads)selfr2   r3   r-   r   r   Zanswerr   r   r   _aembedding_funcO   s    
zNeMoEmbeddings._aembedding_func)r3   r-   r   c                 C  sP   t || j|d}ddi}tjd| j||d}t |j}|d d d }|S )	zCall out to Cohere's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r+   r(   r)   r   r   r   r   r4   )r.   r/   r%   r   r   r&   r7   r3   )r8   r3   r-   r   r   r   Zresponse_jsonr4   r   r   r   _embedding_funcg   s    

zNeMoEmbeddings._embedding_funcz	List[str]zList[List[float]])	documentsr   c                   s    fdd|D S )zEmbed a list of document texts.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        c                   s   g | ]} j |d dqS )passager-   r:   .0r3   r8   r   r   
<listcomp>       z2NeMoEmbeddings.embed_documents.<locals>.<listcomp>r   )r8   r;   r   rA   r   embed_documents~   s    	zNeMoEmbeddings.embed_documents)r3   r   c                 C  s   | j |ddS )Nr*   r=   r>   )r8   r3   r   r   r   embed_query   s    zNeMoEmbeddings.embed_queryc              	     sX   t  4 I dH .}| ||dI dH }|W  d  I dH  S 1 I dH sJ0    Y  dS )zCall out to NeMo's embedding endpoint async for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        Nr<   )aiohttpClientSessionr9   )r8   r3   r2   r4   r   r   r   aembed_query   s    
zNeMoEmbeddings.aembed_query)textsr   c              	     s   g }t  4 I dH xtdt| jD ]N}||| j  }|D ]2} fdd|D }tj| I dH }|| qBq(W d  I dH  q1 I dH s0    Y  |S )zCall out to NeMo's embedding endpoint async for embedding search docs.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        Nr   c                   s   g | ]}  |d qS )r<   )r9   r?   r8   r2   r   r   rB      s   z3NeMoEmbeddings.aembed_documents.<locals>.<listcomp>)rF   rG   rangelenr$   asynciogatherextend)r8   rI   Z
embeddingsbatchZ
text_batchr3   tasksZbatch_resultsr   rJ   r   aembed_documents   s    	8zNeMoEmbeddings.aembed_documentsN)__name__
__module____qualname____doc__r$   __annotations__r%   r&   r	   r1   r9   r:   rD   rE   rH   rR   r   r   r   r   r!   .   s   

r!   )
__future__r   rM   r.   typingr   r   r   r   rF   r   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr	   Zpydanticr
   r   r!   r   r   r   r   <module>   s    	