
    +#hy	                         d dl mZ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)    )OptionalUnionNdotdict)RAGPretrainedModelzXYou need to install RAGAtouille library, Please use the command: pip install ragatouillec                   p     e Zd ZdZd	dededef fdZdeeee   f   de	e   de
j                  fdZ xZS )
RAGatouilleRMa  A retrieval model that uses RAGatouille library to return the top passages for a given query.

    Assumes that you already have an index created with RAGatouille.
    Reference: https://github.com/bclavie/RAGatouille

    Args:
        index_root (str): Folder path where you index is stored.
        index_name (str): Name of the index you want to retrieve from.
        k (int, optional): The default number of passages to retrieve. Defaults to 3.

    Examples:
        Below is a code snippet that shows how to use RAGatouille index as the default retriver:
        ```python
        llm = dspy.OpenAI(model="gpt-3.5-turbo")
        rm = RAGatouilleRM(index_root="ragatouille/colbert/indexes", index_name="my_index")
        dspy.settings.configure(lm=llm, rm=rm)
        ```
    
index_root
index_namekc                     t         |   |       | d| | _        	 t        j                  | j                        | _        y # t        $ r}t        d|       d }~ww xY w)N)r   /)
index_pathzIndex not found: )super__init__r   r   
from_indexmodelFileNotFoundError)selfr
   r   r   e	__class__s        Y/var/www/html/sandstorm/venv/lib/python3.12/site-packages/dspy/retrieve/ragatouille_rm.pyr   zRAGatouilleRM.__init__"   sc    1'L*6	=+66$//RDJ  	=#&7s$;<<	=s   %A 	A!AA!query_or_queriesreturnc                 "   ||n| j                   }t        |t              r|gn|}|D cg c]  }|s|	 }}g }|D ]7  }| j                  j	                  ||      }|j                  d |D               9 t        j                  |      S c c}w )am  Search with RAGAtouille based index 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.
        )queryr   c              3   <   K   | ]  }t        d |d   i        yw)	long_textcontentNr   ).0ds     r   	<genexpr>z(RAGatouilleRM.forward.<locals>.<genexpr>=   s     RQG[!I,$?@Rs   )passages)r   
isinstancestrr   searchextenddspy
Prediction)r   r   r   queriesqr#   r   resultss           r   forwardzRAGatouilleRM.forward*   s     ADFF *C0 ! 	
 &+1++ 	SEjj''eq'9GOOR'RR	S 11 ,s
   BB)   )__name__
__module____qualname____doc__r%   intr   r   listr   r(   r)   r-   __classcell__)r   s   @r   r	   r	      sT    &=3 =C =C =2c49n(= 2(3- 2TXTcTc 2    r	   )typingr   r   r(   dsp.utils.utilsr   ragatouiller   ImportError	ExceptionRetriever	    r6   r   <module>r>      sG    "  #.02DMM 02  
b s   0 >