a
    bg                      @  sd   d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZ ddlmZ G dd	 d	eZdS )
z-Wrapper around Elasticsearch vector database.    )annotationsN)AnyIterableList)CallbackManagerForRetrieverRun)Document)BaseRetrieverc                   @  sd   e Zd ZU dZded< ded< edddddd d	d
dZdddddddZddddddZdS )ElasticSearchBM25Retrievera?  `Elasticsearch` retriever that uses `BM25`.

    To connect to an Elasticsearch instance that requires login credentials,
    including Elastic Cloud, use the Elasticsearch URL format
    https://username:password@es_host:9243. For example, to connect to Elastic
    Cloud, create the Elasticsearch URL with the required authentication details and
    pass it to the ElasticVectorSearch constructor as the named parameter
    elasticsearch_url.

    You can obtain your Elastic Cloud URL and login credentials by logging in to the
    Elastic Cloud console at https://cloud.elastic.co, selecting your deployment, and
    navigating to the "Deployments" page.

    To obtain your Elastic Cloud password for the default "elastic" user:

    1. Log in to the Elastic Cloud console at https://cloud.elastic.co
    2. Go to "Security" > "Users"
    3. Locate the "elastic" user and click "Edit"
    4. Click "Reset password"
    5. Follow the prompts to reset the password

    The format for Elastic Cloud URLs is
    https://username:password@cluster_id.region_id.gcp.cloud.es.io:9243.
    r   clientstr
index_name       @      ?float)elasticsearch_urlr   k1breturnc           	      C  sf   ddl m} ||}ddddiiidd||d	id
}dddddii}|jj|||d | ||dS )aD  
        Create a ElasticSearchBM25Retriever from a list of texts.

        Args:
            elasticsearch_url: URL of the Elasticsearch instance to connect to.
            index_name: Name of the index to use in Elasticsearch.
            k1: BM25 parameter k1.
            b: BM25 parameter b.

        Returns:

        r   )ElasticsearchZanalyzerdefaulttypestandardZcustom_bm25ZBM25)r   r   r   )Zanalysis
similarity
propertiescontenttext)r   r   )indexmappingssettings)r
   r   )Zelasticsearchr   indicescreate)	clsr   r   r   r   r   esr   r    r#   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/elastic_search_bm25.pyr    ,   s&    
z!ElasticSearchBM25Retriever.createTzIterable[str]boolz	List[str])textsrefresh_indicesr   c           
      C  s   zddl m} W n ty*   tdY n0 g }g }t|D ]8\}}tt }d| j||d}	|| ||	 q<|| j	| |r| j	j
j| jd |S )a.  Run more texts through the embeddings and add to the retriever.

        Args:
            texts: Iterable of strings to add to the retriever.
            refresh_indices: bool to refresh ElasticSearch indices

        Returns:
            List of ids from adding the texts into the retriever.
        r   )bulkzbCould not import elasticsearch python package. Please install it with `pip install elasticsearch`.r   )Z_op_type_indexr   _id)r   )Zelasticsearch.helpersr(   ImportError	enumerater   uuiduuid4r   appendr
   r   refresh)
selfr&   r'   r(   requestsZidsir   r*   requestr#   r#   r$   	add_textsY   s*    

z$ElasticSearchBM25Retriever.add_textsr   zList[Document])queryrun_managerr   c                C  sT   ddd|iii}| j j| j|d}g }|d d D ]}|t|d d d q2|S )Nr6   matchr   )r   bodyhits_source)Zpage_content)r
   searchr   r/   r   )r1   r6   r7   Z
query_dictresdocsrr#   r#   r$   _get_relevant_documents   s    z2ElasticSearchBM25Retriever._get_relevant_documentsN)r   r   )T)	__name__
__module____qualname____doc____annotations__classmethodr    r5   r@   r#   r#   r#   r$   r	      s   
 / 'r	   )rD   
__future__r   r-   typingr   r   r   Zlangchain_core.callbacksr   Zlangchain_core.documentsr   Zlangchain_core.retrieversr   r	   r#   r#   r#   r$   <module>   s   