a
    bg                     @  s|   d dl mZ d dlmZmZmZmZm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 G dd	 d	eZd
S )    )annotations)AnyDictListOptionalcast)uuid4)CallbackManagerForRetrieverRun)Document)BaseRetriever)
ConfigDictmodel_validatorc                   @  s   e Zd ZU dZdZded< ded< ded< dZd	ed
< dZded< ded< dZded< e	dde
dddddZeddZddddddZdddddd d!dd!dd"d#d$ZdS )%WeaviateHybridSearchRetrieverz{`Weaviate hybrid search` retriever.

    See the documentation:
      https://weaviate.io/blog/hybrid-search-explained
    Nr   clientstr
index_nametext_keyg      ?floatalpha   intkz	List[str]
attributesTboolcreate_schema_if_missingbefore)modezDict[str, Any])valuesreturnc                 C  s   zdd l }W n ty&   tdY n0 t|d |jsR|d }tdt| |dd u rhg |d< tt|d 	|d  |ddr|d	 |d d
gdgdd}|d j
|d	 s|d j
| |S )Nr   z_Could not import weaviate python package. Please install it with `pip install weaviate-client`.r   z5client should be an instance of weaviate.Client, got r   r   r   Tr   text)nameZdataTypeztext2vec-openai)class
propertiesZ
vectorizer)weaviateImportError
isinstanceZClient
ValueErrortypegetr   r   appendZschemaexistsZcreate_class)clsr   r#   r   Z	class_obj r,   /var/www/html/cobodadashboardai.evdpl.com/venv/lib/python3.9/site-packages/langchain_community/retrievers/weaviate_hybrid_search.pyvalidate_client"   s,    
z-WeaviateHybridSearchRetriever.validate_client)Zarbitrary_types_allowedzList[Document])docskwargsr   c                 K  s   ddl m} | jjz}g }t|D ]\\}}|jp2i }| j|ji|}	d|v rZ|d | }
n
|t }
|	|	| j
|
 ||
 q"W d   n1 s0    Y  |S )zUpload documents to Weaviate.r   )get_valid_uuidZuuidsN)Zweaviate.utilr1   r   batch	enumeratemetadatar   page_contentr   Zadd_data_objectr   r)   )selfr/   r0   r1   r2   Zidsidocr4   Zdata_propertiesZ_idr,   r,   r-   add_documentsJ   s    


*z+WeaviateHybridSearchRetriever.add_documentsF)where_filterscorehybrid_search_kwargsr	   zOptional[Dict[str, object]])queryrun_managerr:   r;   r<   r   c                C  s   | j j| j| j}|r"||}|r4|ddg}|du r@i }|j|fd| ji|	| j
 }d|v r~td|d  g }|d d | j D ]"}	|	| j}
|t|
|	d	 q|S )
aD  Look up similar documents in Weaviate.

        query: The query to search for relevant documents
         of using weviate hybrid search.

        where_filter: A filter to apply to the query.
            https://weaviate.io/developers/weaviate/guides/querying/#filtering

        score: Whether to include the score, and score explanation
            in the returned Documents meta_data.

        hybrid_search_kwargs: Used to pass additional arguments
         to the .with_hybrid() method.
            The primary uses cases for this are:
            1)  Search specific properties only -
                specify which properties to be used during hybrid search portion.
                Note: this is not the same as the (self.attributes) to be returned.
                Example - hybrid_search_kwargs={"properties": ["question", "answer"]}
            https://weaviate.io/developers/weaviate/search/hybrid#selected-properties-only

            2) Weight boosted searched properties -
                Boost the weight of certain properties during the hybrid search portion.
                Example - hybrid_search_kwargs={"properties": ["question^2", "answer"]}
            https://weaviate.io/developers/weaviate/search/hybrid#weight-boost-searched-properties

            3) Search with a custom vector - Define a different vector
                to be used during the hybrid search portion.
                Example - hybrid_search_kwargs={"vector": [0.1, 0.2, 0.3, ...]}
            https://weaviate.io/developers/weaviate/search/hybrid#with-a-custom-vector

            4) Use Fusion ranking method
                Example - from weaviate.gql.get import HybridFusion
                hybrid_search_kwargs={"fusion": fusion_type=HybridFusion.RELATIVE_SCORE}
            https://weaviate.io/developers/weaviate/search/hybrid#fusion-ranking-method
        r;   ZexplainScoreNr   errorszError during query: dataZGet)r5   r4   )r   r=   r(   r   r   Z
with_whereZwith_additionalZwith_hybridr   Z
with_limitr   dor&   popr   r)   r
   )r6   r=   r>   r:   r;   r<   Z	query_objresultr/   resr   r,   r,   r-   _get_relevant_documents_   s$    ,
z5WeaviateHybridSearchRetriever._get_relevant_documents)__name__
__module____qualname____doc__r   __annotations__r   r   r   r   classmethodr.   r   Zmodel_configr9   rE   r,   r,   r,   r-   r      s&   
!r   N)
__future__r   typingr   r   r   r   r   uuidr   Zlangchain_core.callbacksr	   Zlangchain_core.documentsr
   Zlangchain_core.retrieversr   Zpydanticr   r   r   r,   r,   r,   r-   <module>   s   