
    7|h                        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y)    )annotationsN)AnyDictListOptional)
deprecated)
Embeddings)pre_init)	BaseModelc                    	 t        j                  d| ||      }|j                  dk(  ry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headersdata   Tz0Endpoint returned a non-successful status code: zError querying the endpoint: N)requestsrequeststatus_code	Exception
exceptionsRequestException)urlr   payloadresponsees        b/var/www/html/test/engine/venv/lib/python3.12/site-packages/langchain_community/embeddings/nemo.pyis_endpoint_liver      s    =##FCwO 3& B''(*  // =7s;<<=s   (A A A3 A..A3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y)NeMoEmbeddingszNeMo embedding models.   int
batch_sizezNV-Embed-QA-003strmodelz#http://localhost:8088/v1/embeddingsapi_endpoint_urlc                n    |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   )clsvaluesr   r'   r   r   s         r   validate_environmentz#NeMoEmbeddings.validate_environment>   sT     '(w "#56**&%
 	gw/    c                d  K   ddi}|j                  | j                  || j                  |d|      4 d{   }|j                          |j	                          d{   }t        j                  |      }|d   d   d   cddd      d{    S 7 ]7 77 	# 1 d{  7  sw Y   yxY ww)	zAsync call out to embedding endpoint.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        r*   r+   r-   )r0   r   Nr   r   	embedding)postr(   r'   raise_for_statustextr0   loads)selfsessionr:   r/   r   r   answers          r   _aembedding_funczNeMoEmbeddings._aembedding_funcS   s      "#56<<!!$**JO   
 	2 	2 %%'#==?*FZZ'F&>!$[1	2 	2 	2 +	2 	2 	2 	2sV   5B0BB0$BB #BB0BB0BB0B-!B$"B-)B0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.
        r-   r*   r+   r   r   r   r   r7   )r0   r1   r'   r   r   r(   r;   r:   )r<   r:   r/   r   r   r   response_jsonr7   s           r   _embedding_funczNeMoEmbeddings._embedding_funck   s~     **(
 "#56##D))7
 

8==1!&)!,[9	r5   c                N    |D cg c]  }| j                  |d       c}S c c}w )zEmbed a list of document texts.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        passager/   rB   )r<   	documentsr:   s      r   embed_documentszNeMoEmbeddings.embed_documents   s*     NWWT$$Ti$@WWWs   "c                (    | j                  |d      S )Nr,   rE   rF   )r<   r:   s     r   embed_queryzNeMoEmbeddings.embed_query   s    ##DW#==r5   c                   K   t        j                         4 d{   }| j                  ||d       d{   }|cddd      d{    S 7 27 7 	# 1 d{  7  sw Y   yxY ww)zCall out to NeMo's embedding endpoint async for embedding query text.

        Args:
            text: The text to embed.

        Returns:
            Embedding for the text.
        NrD   )aiohttpClientSessionr?   )r<   r:   r=   r7   s       r   aembed_queryzNeMoEmbeddings.aembed_query   sc      ((* 	 	g"33GT9MMI	 	 	M	 	 	 	sS   A)AA)AAAA)A	A)AA)A&AA&"A)c                  K   g }t        j                         4 d{   }t        dt        |      | j                        D ]f  }|||| j                  z    }|D ]M  }|D cg c]  }| j                  ||d       }}t        j                  |  d{   }|j                  |       O h ddd      d{    |S 7 c c}w 7 27 # 1 d{  7  sw Y   |S xY ww)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   rD   )	rL   rM   rangelenr%   r?   asynciogatherextend)	r<   texts
embeddingsr=   batch
text_batchr:   tasksbatch_resultss	            r   aembed_documentszNeMoEmbeddings.aembed_documents   s      
((* 	5 	5gq#e*doo> 5"554??+BC
& 5D %/  --gtYGE  +2..%*@$@M %%m455	5 	5" #	5 %A	5 	5 	5 	5" sb   CB=C?C B?
:CC
C+C6C7C?CCCCCCN)r3   r   returnr   )r=   r   r:   r&   r/   r&   r\   List[float])r:   r&   r/   r&   r\   r]   )rG   	List[str]r\   List[List[float]])r:   r&   r\   r]   )rU   r^   r\   r_   )__name__
__module____qualname____doc__r%   __annotations__r'   r(   r
   r4   r?   rB   rH   rJ   rN   r[    r5   r   r"   r"   .   sw     !J"E3"AcA (22"%2362	206	X>r5   r"   )r   r&   r   zOptional[dict]r   r   r\   bool)
__future__r   rR   r0   typingr   r   r   r   rL   r   langchain_core._api.deprecationr   langchain_core.embeddingsr	   langchain_core.utilsr
   pydanticr   r   r"   re   r5   r   <module>rm      s[    "   , ,   6 0 ) => 
		GY
 GGr5   