
    dhT!                        S SK Jr  S SKJrJrJr  S SKJr  S SKJ	r	  \(       a  S SK
Jr  S SKJr  S SKJr  \" SS	S
S9 " S S\5      5       rg)    )annotations)TYPE_CHECKINGListOptional)
deprecated)get_from_envElasticsearchMlClient)
Embeddingsz0.1.11z,Use class in langchain-elasticsearch packageT)alternativependingc                      \ rS rSrSrSS.     SS jjr\SSSSS.           SS jj5       r\ S       SS	 jj5       rSS
 jr	SS jr
SS jrSrg)ElasticsearchEmbeddings   a  Elasticsearch embedding models.

This class provides an interface to generate embeddings using a model deployed
in an Elasticsearch cluster. It requires an Elasticsearch connection object
and the model_id of the model deployed in the cluster.

In Elasticsearch you need to have an embedding model loaded and deployed.
- https://www.elastic.co/guide/en/elasticsearch/reference/current/infer-trained-model.html
- https://www.elastic.co/guide/en/machine-learning/current/ml-nlp-deploy-models.html

text_fieldinput_fieldc               (    Xl         X l        X0l        g)aG  
Initialize the ElasticsearchEmbeddings instance.

Args:
    client (MlClient): An Elasticsearch ML client object.
    model_id (str): The model_id of the model deployed in the Elasticsearch
        cluster.
    input_field (str): The name of the key for the input text field in the
        document. Defaults to 'text_field'.
N)clientmodel_idr   )selfr   r   r   s       d/var/www/html/shao/venv/lib/python3.13/site-packages/langchain_community/embeddings/elasticsearch.py__init__ ElasticsearchEmbeddings.__init__   s    "  &    N)es_cloud_ides_useres_passwordr   c                    SSK Jn  SSKJn  U=(       d    [        SS5      nU=(       d    [        SS5      nU=(       d    [        S	S
5      nU" X#U4S9nU" U5      n	U " XUS9$ ! [         a    [	        S5      ef = f)a  Instantiate embeddings from Elasticsearch credentials.

Args:
    model_id (str): The model_id of the model deployed in the Elasticsearch
        cluster.
    input_field (str): The name of the key for the input text field in the
        document. Defaults to 'text_field'.
    es_cloud_id: (str, optional): The Elasticsearch cloud ID to connect to.
    es_user: (str, optional): Elasticsearch username.
    es_password: (str, optional): Elasticsearch password.

Example:
    .. code-block:: python

        from langchain_community.embeddings import ElasticsearchEmbeddings

        # Define the model ID and input field name (if different from default)
        model_id = "your_model_id"
        # Optional, only if different from 'text_field'
        input_field = "your_input_field"

        # Credentials can be passed in two ways. Either set the env vars
        # ES_CLOUD_ID, ES_USER, ES_PASSWORD and they will be automatically
        # pulled in, or pass them in directly as kwargs.
        embeddings = ElasticsearchEmbeddings.from_credentials(
            model_id,
            input_field=input_field,
            # es_cloud_id="foo",
            # es_user="bar",
            # es_password="baz",
        )

        documents = [
            "This is an example document.",
            "Another example document to generate embeddings for.",
        ]
        embeddings_generator.embed_documents(documents)
r   r	   r   zPelasticsearch package not found, please install with 'pip install elasticsearch'r   ES_CLOUD_IDr   ES_USERr    ES_PASSWORD)cloud_id
basic_authr   )elasticsearchr
   elasticsearch.clientr   ImportErrorr   )
clsr   r   r   r    r   r
   r   es_connectionr   s
             r   from_credentials(ElasticsearchEmbeddings.from_credentials3   s    `	35 "O\-%O?\)Y?!O\-%O & {-C
 -(6==  	! 	s   A$ $A:c                ,    SSK Jn  U" U5      nU " XQUS9$ )a  
Instantiate embeddings from an existing Elasticsearch connection.

This method provides a way to create an instance of the ElasticsearchEmbeddings
class using an existing Elasticsearch connection. The connection object is used
to create an MlClient, which is then used to initialize the
ElasticsearchEmbeddings instance.

Args:
model_id (str): The model_id of the model deployed in the Elasticsearch cluster.
es_connection (elasticsearch.Elasticsearch): An existing Elasticsearch
connection object. input_field (str, optional): The name of the key for the
input text field in the document. Defaults to 'text_field'.

Returns:
ElasticsearchEmbeddings: An instance of the ElasticsearchEmbeddings class.

Example:
    .. code-block:: python

        from elasticsearch import Elasticsearch

        from langchain_community.embeddings import ElasticsearchEmbeddings

        # Define the model ID and input field name (if different from default)
        model_id = "your_model_id"
        # Optional, only if different from 'text_field'
        input_field = "your_input_field"

        # Create Elasticsearch connection
        es_connection = Elasticsearch(
            hosts=["localhost:9200"], http_auth=("user", "password")
        )

        # Instantiate ElasticsearchEmbeddings using the existing connection
        embeddings = ElasticsearchEmbeddings.from_es_connection(
            model_id,
            es_connection,
            input_field=input_field,
        )

        documents = [
            "This is an example document.",
            "Another example document to generate embeddings for.",
        ]
        embeddings_generator.embed_documents(documents)
r   r   r   )r(   r   )r*   r   r+   r   r   r   s         r   from_es_connection*ElasticsearchEmbeddings.from_es_connectionw   s#    p 	2 -( 6==r   c                    U R                   R                  U R                  U Vs/ sH  o R                  U0PM     snS9nUS    Vs/ sH  oDS   PM	     nnU$ s  snf s  snf )z
Generate embeddings for the given texts using the Elasticsearch model.

Args:
    texts (List[str]): A list of text strings to generate embeddings for.

Returns:
    List[List[float]]: A list of embeddings, one for each text in the input
        list.
)r   docsinference_resultspredicted_value)r   infer_trained_modelr   r   )r   textstextresponsedoc
embeddingss         r   _embedding_func'ElasticsearchEmbeddings._embedding_func   ss     ;;22]]u)Uut+;+;T*Bu)U 3 
 9AAT8UV8U+,8U
V	 *V Ws   A
Ac                $    U R                  U5      $ )z
Generate embeddings for a list of documents.

Args:
    texts (List[str]): A list of document text strings to generate embeddings
        for.

Returns:
    List[List[float]]: A list of embeddings, one for each document in the input
        list.
r;   )r   r6   s     r   embed_documents'ElasticsearchEmbeddings.embed_documents   s     ##E**r   c                ,    U R                  U/5      S   $ )z
Generate an embedding for a single query text.

Args:
    text (str): The query text to generate an embedding for.

Returns:
    List[float]: The embedding for the input query text.
r   r>   )r   r7   s     r   embed_query#ElasticsearchEmbeddings.embed_query   s     ##TF+A..r   )r   r   r   )r   r   r   strr   rD   )r   rD   r   Optional[str]r   rE   r    rE   r   rD   returnr   )r   )r   rD   r+   r
   r   rD   rF   r   )r6   z	List[str]rF   zList[List[float]])r7   rD   rF   zList[float])__name__
__module____qualname____firstlineno____doc__r   classmethodr,   r/   r;   r?   rB   __static_attributes__ r   r   r   r      s    	  ('' '
 '* 
 &*!%%)'A>A> #	A>
 A> #A> A> 
!A> A>F 
 (	>>>> %>> 	>>
 
!>> >>@$+
/r   r   N)
__future__r   typingr   r   r   langchain_core._apir   langchain_core.utilsr   r'   r
   r(   r   langchain_core.embeddingsr   r   rN   r   r   <module>rT      sL    " 0 0 * -+- 0 HRVP/j P/P/r   