o
    Zh                     @  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 d dlmZmZ ed	d
ddG dd deZdS )    )annotations)AnyDictListOptionalcast)uuid4)
deprecated)CallbackManagerForRetrieverRun)Document)BaseRetriever)
ConfigDictmodel_validatorz0.3.18z1.0z&langchain_weaviate.WeaviateVectorStore)ZsinceZremovalZalternative_importc                   @  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ZeddZd,ddZ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k	List[str]
attributesTboolcreate_schema_if_missingbefore)modevaluesDict[str, Any]returnc                 C  s   zdd l }W n ty   tdw t|d |js'|d }tdt| |dd u r2g |d< tt|d 	|d  |ddre|d	 |d d
gdgdd}|d j
|d	 se|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/   l/var/www/html/lang_env/lib/python3.10/site-packages/langchain_community/retrievers/weaviate_hybrid_search.pyvalidate_client(   s.   z-WeaviateHybridSearchRetriever.validate_client)Zarbitrary_types_alloweddocsList[Document]kwargsc                 K  s   ddl m} | jj>}g }t|D ].\}}|jpi }| j|ji|}	d|v r-|d | }
n|t }
|	|	| j
|
 ||
 qW d   |S 1 sKw   Y  |S )zUpload documents to Weaviate.r   )get_valid_uuidZuuidsN)Zweaviate.utilr5   r   batch	enumeratemetadatar   page_contentr   Zadd_data_objectr   r,   )selfr2   r4   r5   r6   Zidsidocr8   Zdata_propertiesZ_idr/   r/   r0   add_documentsP   s    



z+WeaviateHybridSearchRetriever.add_documentsF)where_filterscorehybrid_search_kwargsqueryrun_managerr
   r>   Optional[Dict[str, object]]r?   r@   c                C  s   | j j| j| j}|r||}|r|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	 qJ|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)r9   r8   )r   rA   r+   r   r   Z
with_whereZwith_additionalZwith_hybridr   Z
with_limitr   dor)   popr   r,   r   )r:   rA   rB   r>   r?   r@   Z	query_objresultr2   resr"   r/   r/   r0   _get_relevant_documentse   s$   ,
z5WeaviateHybridSearchRetriever._get_relevant_documents)r   r    r!   r   )r2   r3   r4   r   r!   r   )rA   r   rB   r
   r>   rC   r?   r   r@   rC   r!   r3   )__name__
__module____qualname____doc__r   __annotations__r   r   r   r   classmethodr1   r   Zmodel_configr=   rJ   r/   r/   r/   r0   r      s6   
 !
r   N)
__future__r   typingr   r   r   r   r   uuidr   Zlangchain_core._apir	   Zlangchain_core.callbacksr
   Zlangchain_core.documentsr   Zlangchain_core.retrieversr   Zpydanticr   r   r   r/   r/   r/   r0   <module>   s    