
    Ng                        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	e          Zd
S )    )annotations)AnyDictListOptionalcast)uuid4)CallbackManagerForRetrieverRun)Document)BaseRetriever)
ConfigDictmodel_validatorc                      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          e
d(d                        Z ed          Zd)dZ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k	List[str]
attributesTboolcreate_schema_if_missingbefore)modevaluesDict[str, Any]returnc                R   	 dd l }n# t          $ r t          d          w xY wt          |d         |j                  s'|d         }t	          dt          |                     |                    d          g |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)namedataTypeztext2vec-openai)class
properties
vectorizer)weaviateImportError
isinstanceClient
ValueErrortypegetr   r   appendschemaexistscreate_class)clsr    r*   r   	class_objs        q/var/www/html/ai-engine/env/lib/python3.11/site-packages/langchain_community/retrievers/weaviate_hybrid_search.pyvalidate_clientz-WeaviateHybridSearchRetriever.validate_client"   s^   	OOOO 	 	 	H  	
 &*HO<< 	H%FVVVV   ::l##+#%F< T6,'((//z0BCCC::0$77 	@-(.z(:QQR/ I (#*11&2FGG @x '44Y???s    !)arbitrary_types_alloweddocsList[Document]kwargsc                r   ddl m} | j        j        5 }g }t	          |          D ]y\  }}|j        pi }| j        |j        i|}	d|v r|d         |         }
n |t                                }
|	                    |	| j
        |
           |                    |
           z	 ddd           n# 1 swxY w Y   |S )zUpload documents to Weaviate.r   )get_valid_uuiduuidsN)weaviate.utilr>   r   batch	enumeratemetadatar   page_contentr	   add_data_objectr   r1   )selfr:   r<   r>   rA   idsidocrC   data_properties_ids              r7   add_documentsz+WeaviateHybridSearchRetriever.add_documentsJ   s   000000[ 	 %C#D//    3<-2#'=#2B"Oh"O f$$ /!,CC(.11C%%otLLL

3 	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  
s   BB,,B03B0F)where_filterscorehybrid_search_kwargsqueryrun_managerr
   rM   Optional[Dict[str, object]]rN   rO   c               0   | j         j                            | j        | j                  }|r|                    |          }|r|                    ddg          }|i } |j        |fd| j        i|	                    | j
                                                  }d|v rt          d|d                    g }|d         d         | j                 D ]@}	|	                    | j                  }
|                    t!          |
|		                     A|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
        rN   explainScoreNr   errorszError during query: dataGet)rD   rC   )r   rP   r0   r   r   
with_wherewith_additionalwith_hybridr   
with_limitr   dor.   popr   r1   r   )rF   rP   rQ   rM   rN   rO   	query_objresultr:   resr$   s              r7   _get_relevant_documentsz5WeaviateHybridSearchRetriever._get_relevant_documents_   s3   X K%))$/4?KK	 	;!,,\::I 	M!117N2KLLI'#%  "I!%RRtzR=QRRZRTT 	
 vFF84DFFGGG&>%(9 	C 	CC774=))DKKdSAAABBBB    )r    r!   r"   r   )r:   r;   r<   r   r"   r   )rP   r   rQ   r
   rM   rR   rN   r   rO   rR   r"   r;   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   classmethodr8   r   model_configrL   ra    rb   r7   r   r      s-          F;OOO'MMM*E:AJJJJ*2%)))));_(###   [ $#B : $  L
   4 59<@C C C C C C C Crb   r   N)
__future__r   typingr   r   r   r   r   uuidr	   langchain_core.callbacksr
   langchain_core.documentsr   langchain_core.retrieversr   pydanticr   r   r   rj   rb   r7   <module>rr      s    " " " " " " 2 2 2 2 2 2 2 2 2 2 2 2 2 2       C C C C C C - - - - - - 3 3 3 3 3 3 0 0 0 0 0 0 0 0V V V V VM V V V V Vrb   