
    +#h                         d dl mZ d dlmZmZ d dlZd dlmZ 	 d dlZ G d dej                        Zy# e	$ r	  e	d      w xY w)    )defaultdict)ListUnionN)dotdictzZThe 'marqo' extra is required to use MarqoRM. Install it with `pip install dspy-ai[marqo]`c                        e Zd ZdZ	 	 	 ddedej                  j                  dededef
 fdZ	dde
eee   f   d	ej                  fd
Z xZS )MarqoRMar  
    A retrieval module that uses Marqo to return the top passages for a given query.

    Assumes that a Marqo index has been created and populated with the following payload:
        - document: The text of the passage

    Args:
        marqo_index_name (str): The name of the marqo index.
        marqo_client (marqo.client.Client): A marqo client instance.
        k (int, optional): The number of top passages to retrieve. Defaults to 3.
        page_content (str, optional): The name of the field in the marqo index that contains the text of the passage. Defaults to 'document'.
        filter_string (str, optional): A filter string to use when searching. Defaults to None.
        **kwargs: Additional keyword arguments to pass to the marqo search function.

    Examples:
        Below is a code snippet that shows how to use Marqo as the default retriver:
        ```python
        import marqo
        marqo_client = marqo.Client(url="http://0.0.0.0:8882")

        llm = dspy.OpenAI(model="gpt-3.5-turbo")
        retriever_model = MarqoRM("my_index_name", marqo_client=marqo_client)
        dspy.settings.configure(lm=llm, rm=retriever_model)
        ```

        Below is a code snippet that shows how to use Marqo in the forward() function of a module
        ```python
        self.retrieve = MarqoRM("my_index_name", marqo_client=marqo_client, k=num_passages)
        ```
    marqo_index_namemarqo_clientkpage_contentfilter_stringc                 ^    || _         || _        || _        || _        t        |   |       y )N)r   )_marqo_index_name_marqo_clientr   r   super__init__)selfr	   r
   r   r   r   	__class__s         S/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/retrieve/marqo_rm.pyr   zMarqoRM.__init__.   s5     "2)(*1    query_or_queriesreturnc                 :   t        |t              r|gn|}|D cg c]  }|s|	 }}|r|n| j                  }g }|D ]V  } | j                  j	                  | j
                        j                  d	||| j                  d|}	|j                  |	       X t        t              }
|D ])  }|d   D ]  }|
|| j                     xx   |d   z  cc<   ! + t        |
j                         d d      d| }|D cg c]  \  }}t        d|i       c}}S c c}w c c}}w )
a  Search with Marqo for self.k top passages for query

        Args:
            query_or_queries (Union[str, List[str]]): The query or queries to search for.

        Returns:
            dspy.Prediction: An object containing the retrieved passages.
        )qlimitr   hits_scorec                     | d   S )N    )xs    r   <lambda>z!MarqoRM.forward.<locals>.<lambda>_   s
    AaD r   T)keyreverseN	long_textr    )
isinstancestrr   r   indexr   searchr   appendr   floatr   sorteditemsr   )r   r   r   kwargsqueriesr   r   all_query_resultsquery_resultpassagesresult_dictresultsorted_passagespassage_s                   r   forwardzMarqoRM.forward=   sR    *C0 ! 	
 &+1++DFF 	.EMd((..t/E/EFMM "00 	G $$W-	. u%, 	HK%f- H 1 123vh7GG3H	H !NN.$@@FHBQRJGQg./RR+ ,* Ss   DD7D)   documentN)N)__name__
__module____qualname____doc__r'   marqoclientClientintr   r   r   dspy
Predictionr9   __classcell__)r   s   @r   r   r      s}    F &! ll)) 	
  #Sc49n(= #STXTcTc #Sr   r   )collectionsr   typingr   r   rD   	dsp.utilsr   r@   ImportErrorRetriever   r    r   r   <module>rL      sN    #   RSdmm RS  
d s	   4 A