
    +#h                         d dl mZmZmZ d dlZd dlmZ 	 d dlZd dlm	Z
 d dlmZ  G d dej                        Zy# e$ r	  ed      w xY w)    )ListOptionalUnionNdotdict)HybridFusionzcThe 'weaviate' extra is required to use WeaviateRM. Install it with `pip install dspy-ai[weaviate]`c                        e Zd ZdZdddej
                  fdedej                  de	de
e   d	e
e   d
e
e   f fdZddeeee   f   de
e	   dej                   fdZ xZS )
WeaviateRMaV  
    A retrieval module that uses Weaviate to return the top passages for a given query.

    Assumes that a Weaviate collection has been created and populated with the following payload:
        - content: The text of the passage

    Args:
        weaviate_collection_name (str): The name of the Weaviate collection.
        weaviate_client (WeaviateClient): An instance of the Weaviate client.
        k (int, optional): The default number of top passages to retrieve. Defaults to 3.
        weaviate_collection_text_key (str, optional): The key in the collection with the content. Defaults to content.
        weaviate_alpha (float, optional): The alpha value for the hybrid query. Defaults to 0.5.
        weaviate_fusion_type (wvc.HybridFusion, optional): The fusion type for the query. Defaults to RELATIVE_SCORE.

    Examples:
        Below is a code snippet that shows how to use Weaviate as the default retriver:
        ```python
        import weaviate

        llm = dspy.OpenAI(model="gpt-3.5-turbo")
        weaviate_client = weaviate.Client("your-path-here")
        retriever_model = WeaviateRM(weaviate_collection_name="my_collection_name",
                                     weaviate_collection_text_key="content", 
                                     weaviate_client=weaviate_client)
        dspy.settings.configure(lm=llm, rm=retriever_model)
        ```

        Below is a code snippet that shows how to use Weaviate in the forward() function of a module
        ```python
        self.retrieve = WeaviateRM("my_collection_name", weaviate_client=weaviate_client, k=num_passages)
        ```
       contentg      ?weaviate_collection_nameweaviate_clientkweaviate_collection_text_keyweaviate_alphaweaviate_fusion_typec                 l    || _         || _        || _        || _        || _        t
        |   |       y )N)r   )_weaviate_collection_name_weaviate_client_weaviate_collection_text_key_weaviate_alpha_weaviate_fusion_typesuper__init__)selfr   r   r   r   r   r   	__class__s          V/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/retrieve/weaviate_rm.pyr   zWeaviateRM.__init__2   s>     *B& /-I*-%9"1    query_or_queriesreturnc                 2   ||n| j                   }t        |t              r|gn|}|D cg c]  }|s|	 }}g }|D ]  }| j                  j                  j                  | j                        } |j                  j                  d||| j                  | j                  t        j                  j                  dd      d|}	|	j                  D 
cg c]  }
|
j                  | j                      }}
|j!                  d |D                |S c c}w c c}
w )a^  Search with Weaviate for self.k top passages for query

        Args:
            query_or_queries (Union[str, List[str]]): The query or queries to search for.
            k (Optional[int]): The number of top passages to retrieve. Defaults to self.k.
        Returns:
            dspy.Prediction: An object containing the retrieved passages.
        T)distancescore)querylimitalphafusion_typereturn_metadatac              3   6   K   | ]  }t        d |i        yw)	long_textNr   ).0ds     r   	<genexpr>z%WeaviateRM.forward.<locals>.<genexpr>_   s     N!G[!$45Ns    )r   
isinstancestrr   collectionsgetr   r$   hybridr   r   wvcMetadataQueryobjects
propertiesr   extend)r   r   r   kwargsqueriesqpassagesr$   
collectionresultsresultparsed_resultss               r   forwardzWeaviateRM.forwardA   s.    ADFF *C0 ! 	
 &+1++ 	OE..::>>t?]?]^J-j&&-- 0E45484H4H:>:T:T>Aii>U>U;?t ?V ?M	0 170G cjbqbqrX^f//0R0RSrNrOON~NN	O # , ss   DD D)N)__name__
__module____qualname____doc__r   RELATIVE_SCOREr0   weaviateWeaviateClientintr   floatr   r   r   dspy
PredictionrA   __classcell__)r   s   @r   r
   r
      s    H ?H36@L@[@[+."*"9"9  08}	
 "*% (0'=!c49n(= !(3- !eietet !r   r
   )typingr   r   r   rK   	dsp.utilsr   rG   weaviate.classesclassesr4   !weaviate.collections.classes.grpcr   ImportErrorRetriever
   r.   r   r   <module>rU      sR    ( (  ">R R  
m s	   < A
