o
    Zh                     @  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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urlstrheadersOptional[dict]payloadr   returnboolc              
   C  s^   zt jd| ||d}|jdkrW dS td|j  t jjy. } ztd| d}~ww )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   Z/var/www/html/lang_env/lib/python3.10/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Z	d&ddZ
d'ddZd(ddZd)ddZd)dd Z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_urlvaluesr   r   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_environmentsessionr   textr.   List[float]c              	     s   ddi}|j | j|| j|d|d4 I dH #}|  | I dH }t|}|d d d W  d  I dH  S 1 I dH s@w   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_statusr4   r/   loads)selfr3   r4   r.   r   r   Zanswerr   r   r   _aembedding_funcS   s   
0zNeMoEmbeddings._aembedding_funcc                 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   r6   )r/   r0   r&   r   r   r'   r9   r4   )r:   r4   r.   r   r   r   Zresponse_jsonr6   r   r   r   _embedding_funck   s   

zNeMoEmbeddings._embedding_func	documents	List[str]List[List[float]]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<   .0r4   r:   r   r   
<listcomp>   s    z2NeMoEmbeddings.embed_documents.<locals>.<listcomp>r   )r:   r=   r   rE   r   embed_documents   s   	zNeMoEmbeddings.embed_documentsc                 C  s   | j |ddS )Nr+   rA   rB   )r:   r4   r   r   r   embed_query   s   zNeMoEmbeddings.embed_queryc              	     sZ   t  4 I dH }| ||dI dH }|W  d  I dH  S 1 I dH s&w   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ClientSessionr;   )r:   r4   r3   r6   r   r   r   aembed_query   s
   
0zNeMoEmbeddings.aembed_querytextsc              	     s   g }t  4 I dH =tdt| jD ]'}||| j  }|D ]} fdd|D }tj| I dH }|| q"qW d  I dH  |S 1 I dH sNw   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@   )r;   rC   r:   r3   r   r   rF      s    z3NeMoEmbeddings.aembed_documents.<locals>.<listcomp>)rI   rJ   rangelenr%   asynciogatherextend)r:   rL   Z
embeddingsbatchZ
text_batchr4   tasksZbatch_resultsr   rM   r   aembed_documents   s"   	zNeMoEmbeddings.aembed_documentsN)r(   r   r   r   )r3   r   r4   r   r.   r   r   r5   )r4   r   r.   r   r   r5   )r=   r>   r   r?   )r4   r   r   r5   )rL   r>   r   r?   )__name__
__module____qualname____doc__r%   __annotations__r&   r'   r	   r2   r;   r<   rG   rH   rK   rU   r   r   r   r   r"   .   s   
 





r"   )r   r   r   r   r   r   r   r   )
__future__r   rP   r/   typingr   r   r   r   rI   r   Zlangchain_core._api.deprecationr   Zlangchain_core.embeddingsr   Zlangchain_core.utilsr	   Zpydanticr
   r    r"   r   r   r   r   <module>   s"    
	