
    Ngڽ                        d dl Z d dlZd dlmZmZ d dlmZmZmZm	Z	m
Z
mZmZmZmZm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lmZmZ erd d	lmZ  e j        e           Z! G d
 de          Z" eddd           G d de"                      Z# eddd           G d de"                      Z$ eddd           G d de"                      Z% eddd           G d de                      Z&dS )    N)ABCabstractmethod)
TYPE_CHECKINGAnyCallableDictIterableListLiteralOptionalTupleUnion)
deprecated)Document)
Embeddings)VectorStore)DistanceStrategymaximal_marginal_relevance)Elasticsearchc                      e Zd ZdZedeee         df         deedf         de	de	deded	ee
         d
eedf         defd            Zedee	df         ded
eedf         defd            ZdddededdfdZdefdZdS )BaseRetrievalStrategyz4Base class for `Elasticsearch` retrieval strategies.query_vectorNquerykfetch_kvector_query_field
text_fieldfilter
similarityreturnc                    dS )a  
        Executes when a search is performed on the store.

        Args:
            query_vector: The query vector,
                          or None if not using vector-based query.
            query: The text query, or None if not using text-based query.
            k: The total number of results to retrieve.
            fetch_k: The number of results to fetch initially.
            vector_query_field: The field containing the vector
                                representations in the index.
            text_field: The field containing the text data in the index.
            filter: List of filter clauses to apply to the query.
            similarity: The similarity strategy to use, or None if not using one.

        Returns:
            Dict: The Elasticsearch query body.
        N 	selfr   r   r   r   r   r   r   r   s	            j/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/vectorstores/elasticsearch.pyr   zBaseRetrievalStrategy.query%             dims_lengthc                     dS )a  
        Executes when the index is created.

        Args:
            dims_length: Numeric length of the embedding vectors,
                        or None if not using vector-based query.
            vector_query_field: The field containing the vector
                                representations in the index.
            similarity: The similarity strategy to use,
                        or None if not using one.

        Returns:
            Dict: The Elasticsearch settings and mappings for the strategy.
        Nr"   r$   r(   r   r   s       r%   indexzBaseRetrievalStrategy.indexE   r&   r'   clientr   c                     dS )a  
        Executes before the index is created. Used for setting up
        any required Elasticsearch resources like a pipeline.

        Args:
            client: The Elasticsearch client.
            text_field: The field containing the text data in the index.
            vector_query_field: The field containing the vector
                                representations in the index.
        Nr"   r$   r,   r   r   s       r%   before_index_setupz(BaseRetrievalStrategy.before_index_setup[   r&   r'   c                     dS )a)  
        Returns whether or not the strategy requires inference
        to be performed on the text before it is added to the index.

        Returns:
            bool: Whether or not the strategy requires inference
            to be performed on the text before it is added to the index.
        Tr"   r$   s    r%   require_inferencez'BaseRetrievalStrategy.require_inferencei   s	     tr'   )__name__
__module____qualname____doc__r   r   r
   floatstrintdictr   r   r   r+   r/   boolr2   r"   r'   r%   r   r   "   s_       >>DK-. S$Y
      T
 *D01 
   ^> 39%   *D01	
 
   ^*%36LO	   	4 	 	 	 	 	 	r'   r   z0.0.27z,Use class in langchain-elasticsearch packageT)alternativependingc                      e Zd ZdZ	 	 	 ddee         dee         deeeef                  fdZ	d	ee
e         df         d
eedf         dedededede
e         deedf         defdZdeedf         dedeedf         defdZdS )ApproxRetrievalStrategyz:Approximate retrieval strategy using the `HNSW` algorithm.NFTquery_model_idhybridrrfc                 0    || _         || _        || _        d S Nr@   rA   rB   )r$   r@   rA   rB   s       r%   __init__z ApproxRetrievalStrategy.__init__{   s      -
 r'   r   r   r   r   r   r   r   r   r    c	                 \   ||||d}	|r| j         s||	d<   n(|r| j         rd| j         |di|	d<   nt          d          | j        rd|	dd|d	|iiig|d
id}
t          | j        t
                    rd| j        i|
d<   n*t          | j        t                    r| j        du rdi i|
d<   |
S d|	iS )N)r   fieldr   num_candidatesr   text_embeddingmodel_id
model_textquery_vector_builderzZYou must provide an embedding function or a query_model_id to perform a similarity search.r;   matchr   mustr   )knnr   rB   rankTrR   )r@   
ValueErrorrA   
isinstancerB   r:   r;   )r$   r   r   r   r   r   r   r   r   rR   
query_bodys              r%   r   zApproxRetrievalStrategy.query   sL    '%	
 
  	 3 	".C  	t* 	  $ 3"'# #+C&'' B   ; 	  !($.(/1&*"! #)  J$ $(D)) 1&+TX%6
6""DHd++ 1D0@0@&+R[
6"3<r'   r(   c                     |t           j        u rd}nF|t           j        u rd}n5|t           j        u rd}n$|t           j        u rd}nt          d| d          dd|d	|d
|diiiS )/Create the mapping for the Elasticsearch index.cosinel2_normdot_productmax_inner_productSimilarity  not supported.mappings
propertiesdense_vectorT)typedimsr+   r   )r   COSINEEUCLIDEAN_DISTANCEDOT_PRODUCTMAX_INNER_PRODUCTrT   )r$   r(   r   r   similarityAlgos        r%   r+   zApproxRetrievalStrategy.index   s     )000%NN+>>>&NN+777*NN+===0NNF:FFFGGG & . +!%&4	) )	
 	
r'   NFT)r3   r4   r5   r6   r   r8   r;   r   r:   rF   r
   r7   r9   r   r   r   r+   r"   r'   r%   r?   r?   u   sa        ED )-!&+/	    eD$J'(	   D DK-.D  S$YD  	D 
 D   D  D  T
D  *D01D  
D  D  D  D L
39%
  
 *D01	

 

 
 
 
 
 
r'   r?   c                       e Zd ZdZdeee         df         deedf         dedededed	eee	         df         d
ee
df         defdZdeedf         ded
ee
df         defdZdS )ExactRetrievalStrategyz8Exact retrieval strategy using the `script_score` query.r   Nr   r   r   r   r   r   r   r    c	                     |t           j        u rd| d}	n=|t           j        u rd| d}	n(|t           j        u rd| d}	nt	          d| d          d	i i}
|rd
d|ii}
dd|
|	d|iddiiS )Nz'cosineSimilarity(params.query_vector, 'z') + 1.0z&1 / (1 + l2norm(params.query_vector, 'z'))z=
            double value = dotProduct(params.query_vector, 'z?');
            return sigmoid(1, Math.E, -value);
            r]   r^   	match_allr;   r   r   script_scorer   )sourceparams)r   script)r   rd   re   rf   rT   )r$   r   r   r   r   r   r   r   r   rh   	queryBools              r%   r   zExactRetrievalStrategy.query   s     )000V:LVVV N +>>>P9KPPP N +777=O  NN
 F:FFFGGG&+	 	5(F!34I &"0#1<"@ ! !

 
	
r'   r(   c                     dd|d|ddiiiS )rX   r_   r`   ra   F)rb   rc   r+   r"   r*   s       r%   r+   zExactRetrievalStrategy.index!  s4     & . +!&) )

 
	
r'   )r3   r4   r5   r6   r   r
   r7   r8   r9   r:   r   r   r   r+   r"   r'   r%   rk   rk      s        CB)
DK-.)
 S$Y)
 	)

 )
  )
 )
 d4j$&')
 *D01)
 
)
 )
 )
 )
V
39%
  
 *D01	

 

 
 
 
 
 
r'   rk   c                      e Zd ZdZddee         fdZdeee	         df         deedf         de
de
d	ed
edee         deedf         defdZdefdZddd
ed	eddfdZdee
df         d	edeedf         defdZdefdZdS )SparseRetrievalStrategyz?Sparse retrieval strategy using the `text_expansion` processor.NrL   c                     |pd| _         d S )Nz.elser_model_1rL   )r$   rL   s     r%   rF   z SparseRetrievalStrategy.__init__<  s     4$4r'   r   r   r   r   r   r   r   r   r    c	                 4    ddd| d| j         |diig|diiS )Nr   r;   text_expansionz.tokensrK   rP   rw   r#   s	            r%   r   zSparseRetrievalStrategy.query?  s\      -#5 > > >0427A" A"/	 % 
 	
r'   c                     | j          dS )N_sparse_embeddingrw   r1   s    r%   _get_pipeline_namez*SparseRetrievalStrategy._get_pipeline_name\  s    -2222r'   r,   r   c                     | j         rD|j                            |                                 dd| j         ||didddiidig           d S d S )	Nz,Embedding pipeline for langchain vectorstore	inferencer   ry   results_fieldtokens)rL   target_field	field_mapinference_config)iddescription
processors)rL   ingestput_pipeliner|   r.   s       r%   r/   z*SparseRetrievalStrategy.before_index_setup_  s     = 	M&&**,,J $(,,>*4l)C 0?H2M1	& &	 '     	 	r'   r(   c                 H    d|ddddiiiiid|                                  idS )Nr`   r   rb   rank_featuresdefault_pipeline)r_   settings)r|   r*   s       r%   r+   zSparseRetrievalStrategy.indexu  sM     &$x&/1J&K) ,T-D-D-F-FG	
 	
 		
r'   c                     dS )NFr"   r1   s    r%   r2   z)SparseRetrievalStrategy.require_inference  s    ur'   rD   )r3   r4   r5   r6   r   r8   rF   r   r
   r7   r9   r:   r   r   r   r|   r/   r+   r;   r2   r"   r'   r%   ru   ru   6  s        JI5 5# 5 5 5 5
DK-.
 S$Y
 	

 
  
 
 T

 *D01
 

 
 
 
:3C 3 3 3 3%36LO	   ,
39%
  
 *D01	

 

 
 
 
"4      r'   ru   c                      e Zd ZdZdddddddddd e            dddedee         ded	         d
ee         dee         dee         dee         dee         dededeee	j
        e	j        e	j        e	j        f                  dedeeeef                  fdZedefd            Zedddddddd
ee         dee         dee         dee         dee         deeeef                  dd	fd            Zedee         fd            Z	 	 	 dRd ed!ed"ed#eee                  d$edee         fd%Z	 	 	 	 dSd ed!ed"ed(ed)eee                  d$edee         fd*Zed+edefd,            Zdeegef         fd-Z 	 dTd ed!ed#eee                  d$edee!eef                  f
d.Z"	 	 dTdee         d!ed#eee                  d$edee!eef                  f
d/Z#	 	 	 	 	 	 	 	 dUd ee         d!ed0e$ee         df         d"ed)eee                  d#eee                  d1eeee$edf         gef                  d2eeegef                  d$edee!eef                  fd3Z%	 	 dVd5eee                  d6ee&         d$edee&         fd7Z'	 dWded8ee         ddfd9Z(	 	 	 	 	 dXd:e)e         d;eeee                           d<eeeeef                           d5eee                  d6e&d=e&d>ee         d$edee         fd?Z*	 	 	 	 	 dXd:e)e         d<eeeeef                           d5eee                  d6e&d=e&d>ee         d$edee         fd@Z+	 	 	 	 	 dXdAe)e!eee         f                  d<eee                  d5eee                  d6e&d=e&d>ee         d$edee         fdBZ,e-	 	 	 dYd:ee         dee         d<eeeeef                           d>ee         d$edd fdC            Z.e	 dWdee         d$edd fdD            Z/e-	 	 dZdEee         dee         d>ee         d$edd f
dF            Z0ed[dH            Z1e	 	 	 d\dJee         dKee&         dLee$ee&f                  ddMfdN            Ze	 dWdOee         ddPfdQ            Z2dS )]ElasticsearchStorea  `Elasticsearch` vector store.

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import ElasticsearchStore
            from langchain_community.embeddings.openai import OpenAIEmbeddings

            vectorstore = ElasticsearchStore(
                embedding=OpenAIEmbeddings(),
                index_name="langchain-demo",
                es_url="http://localhost:9200"
            )

    Args:
        index_name: Name of the Elasticsearch index to create.
        es_url: URL of the Elasticsearch instance to connect to.
        cloud_id: Cloud ID of the Elasticsearch instance to connect to.
        es_user: Username to use when connecting to Elasticsearch.
        es_password: Password to use when connecting to Elasticsearch.
        es_api_key: API key to use when connecting to Elasticsearch.
        es_connection: Optional pre-existing Elasticsearch connection.
        vector_query_field: Optional. Name of the field to store
                            the embedding vectors in.
        query_field: Optional. Name of the field to store the texts in.
        strategy: Optional. Retrieval strategy to use when searching the index.
                 Defaults to ApproxRetrievalStrategy. Can be one of
                 ExactRetrievalStrategy, ApproxRetrievalStrategy,
                 or SparseRetrievalStrategy.
        distance_strategy: Optional. Distance strategy to use when
                            searching the index.
                            Defaults to COSINE. Can be one of COSINE,
                            EUCLIDEAN_DISTANCE, MAX_INNER_PRODUCT or DOT_PRODUCT.

    If you want to use a cloud hosted Elasticsearch instance, you can pass in the
    cloud_id argument instead of the es_url argument.

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import ElasticsearchStore
            from langchain_community.embeddings.openai import OpenAIEmbeddings

            vectorstore = ElasticsearchStore(
                embedding=OpenAIEmbeddings(),
                index_name="langchain-demo",
                es_cloud_id="<cloud_id>"
                es_user="elastic",
                es_password="<password>"
            )

    You can also connect to an existing Elasticsearch instance by passing in a
    pre-existing Elasticsearch connection via the es_connection argument.

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import ElasticsearchStore
            from langchain_community.embeddings.openai import OpenAIEmbeddings

            from elasticsearch import Elasticsearch

            es_connection = Elasticsearch("http://localhost:9200")

            vectorstore = ElasticsearchStore(
                embedding=OpenAIEmbeddings(),
                index_name="langchain-demo",
                es_connection=es_connection
            )

    ElasticsearchStore by default uses the ApproxRetrievalStrategy, which uses the
    HNSW algorithm to perform approximate nearest neighbor search. This is the
    fastest and most memory efficient algorithm.

    If you want to use the Brute force / Exact strategy for searching vectors, you
    can pass in the ExactRetrievalStrategy to the ElasticsearchStore constructor.

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import ElasticsearchStore
            from langchain_community.embeddings.openai import OpenAIEmbeddings

            vectorstore = ElasticsearchStore(
                embedding=OpenAIEmbeddings(),
                index_name="langchain-demo",
                es_url="http://localhost:9200",
                strategy=ElasticsearchStore.ExactRetrievalStrategy()
            )

    Both strategies require that you know the similarity metric you want to use
    when creating the index. The default is cosine similarity, but you can also
    use dot product or euclidean distance.

    Example:
        .. code-block:: python

            from langchain_community.vectorstores import ElasticsearchStore
            from langchain_community.embeddings.openai import OpenAIEmbeddings
            from langchain_community.vectorstores.utils import DistanceStrategy

            vectorstore = ElasticsearchStore(
                "langchain-demo",
                embedding=OpenAIEmbeddings(),
                es_url="http://localhost:9200",
                distance_strategy="DOT_PRODUCT"
            )

    Nvectortext)	embeddinges_connectiones_urles_cloud_ides_user
es_api_keyes_passwordr   query_fielddistance_strategystrategy	es_params
index_namer   r   r   r   r   r   r   r   r   r   r   r   r   c                   || _         || _        |
| _        |	| _        |t          j        nt          |         | _        || _        |Zt          |j	                  }|
                    d|                                 i           |                    |          | _        d S ||'t                              ||||||          | _        d S t!          d          )N
user-agent)headers)r   usernamepasswordcloud_idapi_keyr   z{Either provide a pre-existing Elasticsearch connection,                 or valid credentials for creating a new connection.)r   r   r   r   r   rd   r   r   r:   _headersupdateget_user_agentoptionsr,   r   connect_to_elasticsearchrT   )r$   r   r   r   r   r   r   r   r   r   r   r   r   r   r   s                  r%   rF   zElasticsearchStore.__init__  s    0 #$&"4 !( ##!"34 	
 !$=122GNNL$*=*=*?*?@AAA'///@@DKKK;#:,EE $$"# F  DKKK G  r'   r    c                      ddl m}  d|  S )Nr   __version__zlangchain-py-vs/)langchain_communityr   r   s    r%   r   z!ElasticsearchStore.get_user_agent2  s"    333333/+///r'   )r   r   r   r   r   r   r   r   r   r   c                    	 dd l }n# t          $ r t          d          w xY w| r|rt          d          i }| r| g|d<   n|r||d<   nt          d          |r||d<   n|r	|r||f|d<   ||                    |            |j        di |d	d
t
                                          ii}	 |                                 n1# t          $ r$}	t          
                    d|	            |	d }	~	ww xY w|S )Nr   bCould not import elasticsearch python package. Please install it with `pip install elasticsearch`.z>Both es_url and cloud_id are defined. Please provide only one.hostsr   z4Please provide either elasticsearch_url or cloud_id.r   
basic_authr   r   z#Error connecting to Elasticsearch: r"   )elasticsearchImportErrorrT   r   r   r   r   info	Exceptionloggererror)
r   r   r   r   r   r   r   connection_params	es_clientes
             r%   r   z+ElasticsearchStore.connect_to_elasticsearch8  s   	      	 	 	F  	  	h 	P   -/ 	U*0g&& 	U,4j))STTT 	C+2i(( 	C( 	C/7.Bl+ $$Y////M/ 
 

 
!#5#D#D#F#FG
 
 
		NN 	 	 	LLBqBBCCCG	 s     !.C 
C1C,,C1c                     | j         S rD   )r   r1   s    r%   
embeddingszElasticsearchStore.embeddingsl  s
    ~r'      2   r   r   r   r   kwargsc                 >     | j         d||||d|}d |D             S )a  Return Elasticsearch documents most similar to query.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            fetch_k (int): Number of Documents to fetch to pass to knn num_candidates.
            filter: Array of Elasticsearch filter clauses to apply to the query.

        Returns:
            List of Documents most similar to the query,
            in descending order of similarity.
        )r   r   r   r   c                     g | ]\  }}|S r"   r"   ).0doc_s      r%   
<listcomp>z8ElasticsearchStore.similarity_search.<locals>.<listcomp>  s    ***Q***r'   r"   )_search)r$   r   r   r   r   r   resultss          r%   similarity_searchz$ElasticsearchStore.similarity_searchp  sH    * $, 
1gf
 
@F
 
 +*'****r'            ?lambda_multfieldsc                      j         t          d          d}|	 j        g}n& j        |vr|                     j                   nd} j                             |          }  j        d	|||d| fdD             }	t          t          j        |          |	||          }
fd|
D             }|r|D ]}|j	         j        = |S )
a  Return docs selected using the maximal marginal relevance.

        Maximal marginal relevance optimizes for similarity to query AND diversity
            among selected documents.

        Args:
            query (str): Text to look up documents similar to.
            k (int): Number of Documents to return. Defaults to 4.
            fetch_k (int): Number of Documents to fetch to pass to MMR algorithm.
            lambda_mult (float): Number between 0 and 1 that determines the degree
                of diversity among the results with 0 corresponding
                to maximum diversity and 1 to minimum diversity.
                Defaults to 0.5.
            fields: Other fields to get from elasticsearch source. These fields
                will be added to the document metadata.

        Returns:
            List[Document]: A list of Documents selected by maximal marginal relevance.
        Nz5You must provide an embedding function to perform MMRTF)r   r   r   c                 :    g | ]\  }}|j         j                 S r"   )metadatar   )r   r   r   r$   s      r%   r   zDElasticsearchStore.max_marginal_relevance_search.<locals>.<listcomp>  s'    WWWFC#,t'>?WWWr'   )r   r   c                 ,    g | ]}|         d          S )r   r"   )r   igot_docss     r%   r   zDElasticsearchStore.max_marginal_relevance_search.<locals>.<listcomp>  s!    BBBA!QBBBr'   r"   )
r   rT   r   appendembed_queryr   r   nparrayr   )r$   r   r   r   r   r   r   'remove_vector_query_field_from_metadataquery_embeddinggot_embeddingsselected_indicesselected_docsr   r   s   `            @r%   max_marginal_relevance_searchz0ElasticsearchStore.max_marginal_relevance_search  s7   8 >!TUUU26/>-.FF$F22MM$122226;3 .44U;;  4< 
(GF
 
FL
 

 XWWWhWWW 6H_%%~;RS
 
 
 CBBB1ABBB2 	:$ : :L!899r'   scorec                     | S rD   r"   )r   s    r%   _identity_fnzElasticsearchStore._identity_fn  s    r'   c                     | j         S )a  
        The 'correct' relevance function
        may differ depending on a few things, including:
        - the distance / similarity metric used by the VectorStore
        - the scale of your embeddings (OpenAI's are unit normed. Many others are not!)
        - embedding dimensionality
        - etc.

        Vectorstores should define their own selection based method of relevance.
        )r   r1   s    r%   _select_relevance_score_fnz-ElasticsearchStore._select_relevance_score_fn  s       r'   c                     t          | j        t                    r| j        j        rt	          d           | j        d|||d|S )a  Return Elasticsearch documents most similar to query, along with scores.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Array of Elasticsearch filter clauses to apply to the query.

        Returns:
            List of Documents most similar to the query and score for each
        1scores are currently not supported in hybrid mode)r   r   r   r"   rU   r   r?   rA   rT   r   )r$   r   r   r   r   s        r%   similarity_search_with_scorez/ElasticsearchStore.similarity_search_with_score  sW     dm%<== 	R$-BV 	RPQQQt|F%1VFFvFFFr'   c                     t          | j        t                    r| j        j        rt	          d           | j        d|||d|S )a  Return Elasticsearch documents most similar to query, along with scores.

        Args:
            embedding: Embedding to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            filter: Array of Elasticsearch filter clauses to apply to the query.

        Returns:
            List of Documents most similar to the embedding and score for each
        r   )r   r   r   r"   r   )r$   r   r   r   r   s        r%   1similarity_search_by_vector_with_relevance_scoreszDElasticsearchStore.similarity_search_by_vector_with_relevance_scores  sW    " dm%<== 	R$-BV 	RPQQQt|QaQQ&QQQr'   r   custom_querydoc_builderc	           
      >    |g }d|vr|                     d            j        |vr|                      j                    j        r|| j                            |          } j                            |||| j         j        |pg  j                  }
t          	                    d|
            |) ||
|          }
t          	                    d|
              j
        j        dd j        i|
||d}dt          d	t          f fd
}|p|}g }|d         d         D ]t}|D ]I}||d         v r=|d j        fvr2d|d         vri |d         d<   |d         |         |d         d         |<   J|                      ||          |d         f           u|S )a8  Return Elasticsearch documents most similar to query, along with scores.

        Args:
            query: Text to look up documents similar to.
            k: Number of Documents to return. Defaults to 4.
            query_vector: Embedding to look up documents similar to.
            fetch_k: Number of candidates to fetch from each shard.
                    Defaults to 50.
            fields: List of fields to return from Elasticsearch.
                    Defaults to only returning the text field.
            filter: Array of Elasticsearch filter clauses to apply to the query.
            custom_query: Function to modify the Elasticsearch
                         query body before it is sent to Elasticsearch.

        Returns:
            List of Documents most similar to the query and score for each
        Nr   )r   r   r   r   r   r   r   r   zQuery body: z&Calling custom_query, Query body now: r+   )sizero   hitr    c                 |    t          | d                             j        d          | d         d                   S )N_source r   )page_contentr   )r   getr   )r   r$   s    r%   default_doc_builderz7ElasticsearchStore._search.<locals>.default_doc_builderC  s@     ^//0@"EEY
3   r'   hitsr   _scorer"   )r   r   r   r   r   r   r   r   r   debugr,   searchr   r   r   )r$   r   r   r   r   r   r   r   r   r   rV   responser   docs_and_scoresr   rH   s   `               r%   r   zElasticsearchStore._search  sW   : >FV##MM*%%%6))MM$*+++> 	=e/L4H>55e<<L]((%#6'<R- ) 	
 	

 	0J00111#%j%88JLLN*NNOOO%4;% 
 
/

 	
 
 
 
	T 	h 	 	 	 	 	 	 "8%8F#F+ 	 	C N NC	N**u$= 0 0 "Y7757Iz28;Iu8MC	N:.u5""K$$M    r'   Tidsrefresh_indicesc                    	 ddl m}m} n# t          $ r t          d          w xY wg }|t	          d          |D ] }|                    d| j        |d           !t          |          dk    r	  || j        ||d	           t          
                    d
t          |           d           dS # |$ r}t                              d|            |j        d                             di                               di           }	t                              d|	                    d                      |d}~ww xY wt          
                    d           dS )zDelete documents from the Elasticsearch index.

        Args:
            ids: List of ids of documents to delete.
            refresh_indices: Whether to refresh the index
                            after deleting documents. Defaults to True.
        r   BulkIndexErrorbulkr   Nzids must be provided.delete)_op_type_index_idi  )refreshignore_statuszDeleted z texts from indexTzError deleting texts: r+   r   First error reason: reasonzNo texts to delete from indexF)elasticsearch.helpersr   r   r   rT   r   r   lenr,   r   r   r   errorsr   )
r$   r   r   r   r   r   bodyr  r   
firstErrors
             r%   r   zElasticsearchStore.delete^  s   	BBBBBBBBB 	 	 	F  	 ;4555 	W 	WCKKXQTUUVVVVt99q==	T[$sSSSSDD		DDDEEEt!   9a99:::Xa[__Wb99==grJJ
NJNN84L4LNNOOO	 LL89995s!    %2?B3 3E8BD<<Er(   c                    | j         j                            |          r t                              d| d           dS |(| j                                        rt          d          | j                            | j         | j	        | j
                   | j                            | j
        || j                  }t                              d| d	|d
                      | j         j        j        dd|i| dS )zCreate the Elasticsearch index if it doesn't already exist.

        Args:
            index_name: Name of the Elasticsearch index to create.
            dims_length: Length of the embedding vectors.
        )r+   zIndex z# already exists. Skipping creation.NzCannot create index without specifying dims_length when the index doesn't already exist. We infer dims_length from the first embedding. Check that you have provided an embedding function.)r,   r   r   )r   r(   r   zCreating index z with mappings r_   r+   r"   )r,   indicesexistsr   r   r   r2   rT   r/   r   r   r+   r   create)r$   r   r(   indexSettingss       r%   _create_index_if_not_existsz.ElasticsearchStore._create_index_if_not_exists  s3    ;%%J%77 	JLLQ*QQQRRRRR "t}'F'F'H'H" ?   M,,{+#'#: -    !M//#'#:'1 0  M
 LLX*XX]:=VXX   'DK&IIZI=IIIIIr'   textsr   	metadatascreate_index_if_not_existsbulk_kwargsc                    	 ddl m}	m}
 n# t          $ r t          d          w xY w|pi }|pd |D             }g }|r6|rt	          |d                   }nd }|                     | j        |           t          |          D ]T\  }}|r||         ni }ddd| j        | j        |d	|d
||         i}|r||         || j	        <   |
                    |           Ut	          |          dk    r	  |
| j        |fd|d|\  }}t                              d| d| d           t                              d| d           |S # |	$ r}t                              d|            |j        d                             di                               di           }t                              d|                    d                      |d }~ww xY wt                              d           g S )Nr   r   r   c                 N    g | ]"}t          t          j                              #S r"   )r8   uuiduuid4)r   r   s     r%   r   z,ElasticsearchStore.__add.<locals>.<listcomp>  s&    777Ac$*,,''777r'   )r   r(   r   r+   r  r   r  T)
stats_onlyr  zAdded z and failed to add z texts to indexzadded texts z	 to indexzError adding texts: r   r  r  zNo texts to add to index)r  r   r   r   r  r  r   	enumerater   r   r   r,   r   r   r   r	  r   )r$   r  r   r  r   r   r  r  r   r   r   requestsr(   r   r   r   requestsuccessfailedr   r  s                        r%   __addzElasticsearchStore.__add  s   	BBBBBBBBB 	 	 	F  	
 "'R777777% 	 #!*Q-00",,? -    !'' 	% 	%GAt'08y||bH G$/ $Hs1vG  A3=a=/0OOG$$$$x==1"&$K#  $+	# #
 "# # PWPPPPP   :C:::;;;
!   7A77888Xa[__Wb99==grJJ
NJNN84L4LNNOOO	 LL3444Is"    %,AE GBGGc           
          | j         (| j                             t          |                    }nd}|                     ||||||||          S )a`  Run more texts through the embeddings and add to the vectorstore.

        Args:
            texts: Iterable of strings to add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of ids to associate with the texts.
            refresh_indices: Whether to refresh the Elasticsearch indices
                            after adding the texts.
            create_index_if_not_exists: Whether to create the Elasticsearch
                                        index if it doesn't already exist.
            *bulk_kwargs: Additional arguments to pass to Elasticsearch bulk.
                - chunk_size: Optional. Number of texts to add to the
                    index at a time. Defaults to 500.

        Returns:
            List of ids from adding the texts into the vectorstore.
        Nr  r   r   r  r  r   )r   embed_documentslist_ElasticsearchStore__add)	r$   r  r  r   r   r  r  r   r   s	            r%   	add_textszElasticsearchStore.add_texts  se    6 >% 77UDDJJ Jzz+'A#  	
 	
 		
r'   text_embeddingsc           
          t          | \  }}	|                     t          |          t          |	          ||||||          S )am  Add the given texts and embeddings to the vectorstore.

        Args:
            text_embeddings: Iterable pairs of string and embedding to
                add to the vectorstore.
            metadatas: Optional list of metadatas associated with the texts.
            ids: Optional list of unique IDs.
            refresh_indices: Whether to refresh the Elasticsearch indices
                            after adding the texts.
            create_index_if_not_exists: Whether to create the Elasticsearch
                                        index if it doesn't already exist.
            *bulk_kwargs: Additional arguments to pass to Elasticsearch bulk.
                - chunk_size: Optional. Number of texts to add to the
                    index at a time. Defaults to 500.

        Returns:
            List of ids from adding the texts into the vectorstore.
        r"  )zipr%  r$  )
r$   r'  r  r   r   r  r  r   r  r   s
             r%   add_embeddingsz!ElasticsearchStore.add_embeddings(  sT    8  1zzzKK+'A#  	
 	
 		
r'   c                 \    t          j        dd|i|}|                    |||           |S )a  Construct ElasticsearchStore wrapper from raw documents.

        Example:
            .. code-block:: python

                from langchain_community.vectorstores import ElasticsearchStore
                from langchain_community.embeddings.openai import OpenAIEmbeddings

                db = ElasticsearchStore.from_texts(
                    texts,
                    // embeddings optional if using
                    // a strategy that doesn't require inference
                    embeddings,
                    index_name="langchain-demo",
                    es_url="http://localhost:9200"
                )

        Args:
            texts: List of texts to add to the Elasticsearch index.
            embedding: Embedding function to use to embed the texts.
            metadatas: Optional list of metadatas associated with the texts.
            index_name: Name of the Elasticsearch index to create.
            es_url: URL of the Elasticsearch instance to connect to.
            cloud_id: Cloud ID of the Elasticsearch instance to connect to.
            es_user: Username to use when connecting to Elasticsearch.
            es_password: Password to use when connecting to Elasticsearch.
            es_api_key: API key to use when connecting to Elasticsearch.
            es_connection: Optional pre-existing Elasticsearch connection.
            vector_query_field: Optional. Name of the field to
                                store the embedding vectors in.
            query_field: Optional. Name of the field to store the texts in.
            distance_strategy: Optional. Name of the distance
                                strategy to use. Defaults to "COSINE".
                                can be one of "COSINE",
                                "EUCLIDEAN_DISTANCE", "DOT_PRODUCT",
                                "MAX_INNER_PRODUCT".
            bulk_kwargs: Optional. Additional arguments to pass to
                        Elasticsearch bulk.
        r   )r  r  r"   )r   _create_cls_from_kwargsr&  )clsr  r   r  r  r   elasticsearchStores          r%   
from_textszElasticsearchStore.from_textsP  s[    b 0G 
 

#)
 

 	$$YK 	% 	
 	
 	
 "!r'   c                 p   |                     d          }|t          d          |                     d          }|                     d          }|                     d          }|                     d          }|                     d          }|                     d          }|                     d	          }	|                     d
          }
|                     d          }|                     dt                                                    }i }|	|	|d	<   |
|
|d
<   t          d|| ||||||||d
|S )Nr   zPlease provide an index_name.r   r   r   r   r   r   r   r   r   r   )
r   r   r   r   r   r   r   r   r   r   r"   )r   rT   r   r?   )r   r   r   r   r   r   r   r   r   r   r   r   r   optional_argss                 r%   r,  z*ElasticsearchStore._create_cls_from_kwargs  sQ    ZZ--
<===

?33jj//H%%**Y''jj//ZZ--
#ZZ(<==jj//"JJ':;;::j*<*T*T*V*VWW)2DM./"+6M-(! 
!'##!/
 
 
 
 	
r'   	documentsc                 Z    t          j        dd|i|}|                    ||           |S )a  Construct ElasticsearchStore wrapper from documents.

        Example:
            .. code-block:: python

                from langchain_community.vectorstores import ElasticsearchStore
                from langchain_community.embeddings.openai import OpenAIEmbeddings

                db = ElasticsearchStore.from_documents(
                    texts,
                    embeddings,
                    index_name="langchain-demo",
                    es_url="http://localhost:9200"
                )

        Args:
            texts: List of texts to add to the Elasticsearch index.
            embedding: Embedding function to use to embed the texts.
                      Do not provide if using a strategy
                      that doesn't require inference.
            metadatas: Optional list of metadatas associated with the texts.
            index_name: Name of the Elasticsearch index to create.
            es_url: URL of the Elasticsearch instance to connect to.
            cloud_id: Cloud ID of the Elasticsearch instance to connect to.
            es_user: Username to use when connecting to Elasticsearch.
            es_password: Password to use when connecting to Elasticsearch.
            es_api_key: API key to use when connecting to Elasticsearch.
            es_connection: Optional pre-existing Elasticsearch connection.
            vector_query_field: Optional. Name of the field
                                to store the embedding vectors in.
            query_field: Optional. Name of the field to store the texts in.
            bulk_kwargs: Optional. Additional arguments to pass to
                        Elasticsearch bulk.
        r   )r  r"   )r   r,  add_documents)r-  r2  r   r  r   r.  s         r%   from_documentsz!ElasticsearchStore.from_documents  sN    V 0G 
 

#)
 
 	(((LLL!!r'   rk   c                      t                      S )zUUsed to perform brute force / exact
        nearest neighbor search via script_score.)rk   r"   r'   r%   rk   z)ElasticsearchStore.ExactRetrievalStrategy  s     &'''r'   Fr@   rA   rB   r?   c                 &    t          | ||          S )a(  Used to perform approximate nearest neighbor search
        using the HNSW algorithm.

        At build index time, this strategy will create a
        dense vector field in the index and store the
        embedding vectors in the index.

        At query time, the text will either be embedded using the
        provided embedding function or the query_model_id
        will be used to embed the text using the model
        deployed to Elasticsearch.

        if query_model_id is used, do not provide an embedding function.

        Args:
            query_model_id: Optional. ID of the model to use to
                            embed the query text within the stack. Requires
                            embedding model to be deployed to Elasticsearch.
            hybrid: Optional. If True, will perform a hybrid search
                    using both the knn query and a text query.
                    Defaults to False.
            rrf: Optional. rrf is Reciprocal Rank Fusion.
                 When `hybrid` is True,
                    and `rrf` is True, then rrf: {}.
                    and `rrf` is False, then rrf is omitted.
                    and isinstance(rrf, dict) is True, then pass in the dict values.
                 rrf could be passed for adjusting 'rank_constant' and 'window_size'.
        rE   )r?   rE   s      r%   r?   z*ElasticsearchStore.ApproxRetrievalStrategy  s#    D ')&c
 
 
 	
r'   rL   ru   c                 "    t          |           S )a  Used to perform sparse vector search via text_expansion.
        Used for when you want to use ELSER model to perform document search.

        At build index time, this strategy will create a pipeline that
        will embed the text using the ELSER model and store the
        resulting tokens in the index.

        At query time, the text will be embedded using the ELSER
        model and the resulting tokens will be used to
        perform a text_expansion query.

        Args:
            model_id: Optional. Default is ".elser_model_1".
                    ID of the model to use to embed the query text
                    within the stack. Requires embedding model to be
                    deployed to Elasticsearch.
        rw   )ru   rw   s    r%   SparseVectorRetrievalStrategyz0ElasticsearchStore.SparseVectorRetrievalStrategy  s    * '9999r'   )r   r   N)r   r   r   N)r   N)Nr   Nr   NNNN)NTrD   )NNTTN)NNN)NN)r    rk   ri   )3r3   r4   r5   r6   r?   r8   r   r   r   r   rd   rf   re   rg   r   r   r   rF   staticmethodr   r   propertyr   r9   r
   r:   r   r   r7   r   r   r   r   r   r   r   r   r   r;   r   r  r	   r%  r&  r*  classmethodr/  r,  r5  rk   r9  r"   r'   r%   r   r     s
       l ld +/37 $%)!%$(%)"*! *A*A*C*C.2-4 4 44 J'	4
  04 4 c]4 #4 SM4 c]4  4 4 $ ' , 3 24
4* (+4, DcN+-4 4 4 4l 0C 0 0 0 \0
  !%"&!%"&"&.21 1 11 3-1 #	1
 3-1 3-1 DcN+1 
1 1 1 \1f HZ0    X '++ ++ + 	+
 d$+ + 
h+ + + +:  &*; ;; ; 	;
 ; c#; ; 
h; ; ; ;z E e    \!HeWe^,D ! ! ! !  FJG GG G.6tDz.BGUXG	eHeO$	%G G G G* '+	R R;R R d$	R
 R 
eHeO$	%R R R R0  $15&*'+KO<@Z Z}Z Z DK-.	Z
 Z c#Z d$Z xuS$Y/?(@$(FGHZ hvx'789Z Z 
eHeO$	%Z Z Z Z| $(*.+ +d3i + "$+ 	+
 
$+ + + +\ =A$J $J$J,4SM$J	$J $J $J $JT 59#' $+/&*F F}F T$u+./F Dc3h01	F
 d3i F F %)F d^F F 
cF F F FV 59#' $+/&*-
 -
}-
 Dc3h01-
 d3i 	-

 -
 %)-
 d^-
 -
 
c-
 -
 -
 -
d +/#' $+/&*&
 &
!%T%[(8"9:&
 DJ'&
 d3i 	&

 &
 %)&
 d^&
 &
 
c&
 &
 &
 &
P  +/48&*9" 9"Cy9" J'9" Dc3h01	9"
 d^9" 9" 
9" 9" 9" [9"v *.'
 '
J''
:='
	'
 '
 '
 \'
R  +/&*	0" 0">0" J'0" d^	0"
 0" 
0" 0" 0" [0"d ( ( ( \(
 (,!&+/#
 #
 #
#
 eD$J'(#
 
#	#
 #
 #
 \#
J "&: :3-:	": : : \: : :r'   r   )'loggingr  abcr   r   typingr   r   r   r   r	   r
   r   r   r   r   numpyr   langchain_core._apir   langchain_core.documentsr   langchain_core.embeddingsr   langchain_core.vectorstoresr   &langchain_community.vectorstores.utilsr   r   r   r   	getLoggerr3   r   r   r?   rk   ru   r   r"   r'   r%   <module>rG     s     # # # # # # # #                            * * * * * * - - - - - - 0 0 0 0 0 0 3 3 3 3 3 3       
  ,++++++		8	$	$P P P P PC P P Pf HRV  u
 u
 u
 u
 u
3 u
 u
 u
p HRV  @
 @
 @
 @
 @
2 @
 @
 @
F HRV  N N N N N3 N N Nb HRV  ]: ]: ]: ]: ]: ]: ]: ]: ]: ]:r'   