
    Ngy                        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Z eddd           G 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                    	 t          j        d| ||          }|j        dk    rdS t          d|j                   # t           j        j        $ r}t          d|           d}~ww xY 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es        _/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/embeddings/nemo.pyis_endpoint_liver!      s    =#FCwOOO 3&&4 *'* *   / = = =;;;<<<=s   #> > A)A$$A)z0.0.37z1.0.0zDirectly instantiating a NeMoEmbeddings from langchain-community is deprecated. Please use langchain-nvidia-ai-endpoints NVIDIAEmbeddings interface.)sinceremovalmessagec                      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            Z	ddZ
d dZd!dZd"dZd"dZ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                    |d         }|d         }ddi}t          j        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   s         r    validate_environmentz#NeMoEmbeddings.validate_environment>   s\     '(w "#56*#e7KK
 
 	gw///    sessionr   textr3   List[float]c                `  K   ddi}|                     | j        || j        |d|          4 d{V 	 }|                                 |                                 d{V }t          j        |          }|d         d         d         cddd          d{V  S # 1 d{V swxY w Y   dS )	zAsync call out to embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r.   r/   r1   )r4   r   Nr   r   	embedding)postr+   r*   raise_for_statusr:   r4   loads)selfr9   r:   r3   r   r   answers          r    _aembedding_funczNeMoEmbeddings._aembedding_funcO   sX      "#56<<!$*JOO   
 
 	2 	2 	2 	2 	2 	2 	2 	2 %%'''#==??******FZ''F&>!$[1	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   AB
B'*B'c                    t          j        || j        |d          }ddi}t          j        d| j        ||          }t          j        |j                  }|d         d         d         }|S )	zCall out to Cohere's embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r1   r.   r/   r   r   r   r   r=   )r4   r5   r*   r   r   r+   r@   r:   )rA   r:   r3   r   r   r   response_jsonr=   s           r    _embedding_funczNeMoEmbeddings._embedding_funcg   s     *TZzJJ
 
 "#56#D)7
 
 
 
8=11!&)!,[9	r8   	documents	List[str]List[List[float]]c                       f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                >    g | ]}                     |d           S )passager3   rF   ).0r:   rA   s     r    
<listcomp>z2NeMoEmbeddings.embed_documents.<locals>.<listcomp>   s,    WWWT$$Ti$@@WWWr8    )rA   rG   s   ` r    embed_documentszNeMoEmbeddings.embed_documents~   s      XWWWYWWWWr8   c                0    |                      |d          S )Nr0   rM   rN   )rA   r:   s     r    embed_queryzNeMoEmbeddings.embed_query   s    ##DW#===r8   c                   K   t          j                    4 d{V }|                     ||d           d{V }|cddd          d{V  S # 1 d{V swxY 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.
        NrL   )aiohttpClientSessionrC   )rA   r:   r9   r=   s       r    aembed_queryzNeMoEmbeddings.aembed_query   s       (** 	 	 	 	 	 	 	g"33GT9MMMMMMMMI	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   A
AAtextsc                x   K   g }t          j                    4 d{V t          dt          |           j                  D ]Q}||| j        z            }|D ]:} fd|D             }t          j        |  d{V }|                    |           ;R	 ddd          d{V  n# 1 d{V swxY w 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                >    g | ]}                     |d           S )rL   )rC   )rO   r:   rA   r9   s     r    rP   z3NeMoEmbeddings.aembed_documents.<locals>.<listcomp>   s;         --gtYGG  r8   )rV   rW   rangelenr)   asynciogatherextend)	rA   rY   
embeddingsbatch
text_batchr:   tasksbatch_resultsr9   s	   `       @r    aembed_documentszNeMoEmbeddings.aembed_documents   s      
(** 	5 	5 	5 	5 	5 	5 	5gq#e**do>> 5 5"554?+B#BC
& 5 5D    $.  E +2.%*@$@$@$@$@$@$@M %%m444455	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5 	5" s   A6B))
B36B3N)r,   r   r   r   )r9   r   r:   r   r3   r   r   r;   )r:   r   r3   r   r   r;   )rG   rH   r   rI   )r:   r   r   r;   )rY   rH   r   rI   )__name__
__module____qualname____doc__r)   __annotations__r*   r+   r
   r7   rC   rF   rR   rT   rX   rf   rQ   r8   r    r&   r&   .   s          ! J"E""""AAAAA   X 2 2 2 20   .	X 	X 	X 	X> > > >        r8   r&   )r   r   r   r   r   r   r   r   )
__future__r   r^   r4   typingr   r   r   r   rV   r   langchain_core._api.deprecationr   langchain_core.embeddingsr	   langchain_core.utilsr
   pydanticr   r!   r&   rQ   r8   r    <module>rr      s&   " " " " " "   , , , , , , , , , , , ,   6 6 6 6 6 6 0 0 0 0 0 0 ) ) ) ) ) )      = = = => 
		      Y
     r8   