DSPy: The framework for programming—not prompting—language models
Python
custom rm for ragatouille
Hasala Marakkalage0beaf62f68a4
diff --git a/docs/api/retrieval_model_clients/RAGatouilleRM.md b/docs/api/retrieval_model_clients/RAGatouilleRM.mdnew file mode 100644index 000000000..e1d9c528b--- /dev/null+++ b/docs/api/retrieval_model_clients/RAGatouilleRM.md@@ -0,0 +1,38 @@+---+sidebar_position: 1+---++# dspy.RAGatouilleRM++### Constructor++The constructor initializes the `RAGatouille` class instance and sets up the required parameters for interacting with the index created using [RAGatouille library](https://github.com/bclavie/RAGatouille).++```python+class RAGatouilleRM(dspy.Retrieve):+ def __init__(+ self,+ index_root: str,+ index_name: str,+ k: int = 3,+ ):+```++**Parameters:**+- `index_root` (_str_): Folder path where you index is stored.+- `index_name` (_str_): Name of the index you want to retrieve from.+- `k` (_int_): The default number of passages to retrieve. Defaults to `3`.++### Methods++#### `forward(self, query_or_queries: Union[str, List[str]], k:Optional[int]) -> dspy.Prediction`++Enables making queries to the RAGatouille made index for retrieval. Internally, the method handles the specifics of preparing the query to obtain the response. The function handles the retrieval of the top-k passages based on the provided query.++**Parameters:**+- `query_or_queries` (Union[str, List[str]]): Query string used for retrieval.+- `k` (_int_, _optional_): Number of passages to retrieve. Defaults to 3.++**Returns:**+- `dspy.Prediction`: List of K passages+diff --git a/dspy/retrieve/ragatouille_rm.py b/dspy/retrieve/ragatouille_rm.pynew file mode 100644index 000000000..259104480--- /dev/null+++ b/dspy/retrieve/ragatouille_rm.py@@ -0,0 +1,58 @@+from dsp.utils.utils import dotdict+import dspy+from typing import List, Optional, Union+++try:+ from ragatouille import RAGPretrainedModel+except ImportError:+ raise ImportError(+ "You need to install RAGAtouille library"+ "Please use the command: pip install ragatouille"+ )+++class RAGatouilleRM(dspy.Retrieve):+ """+ 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)+ ```+ """++ def __init__(self, index_root:str, index_name:str, k:int = 3):+ super.__init__(k=k)+ self.index_path = f"{index_root}/{index_name}"+ try:+ self.model = RAGPretrainedModel.from_index(index_path=self.index_path)+ except FileNotFoundError as e:+ raise FileNotFoundError(f"Index not found: {e}")++ def forward(self, query_or_queries: Union[str, List[str]], k:Optional[int]) -> dspy.Prediction:+ k = k if k is not None else self.k+ queries = (+ [query_or_queries]+ if isinstance(query_or_queries, str)+ else query_or_queries+ )+ queries = [q for q in queries if q]+ passages = []+ for query in queries:+ results = self.model.search(query=query, k=k)+ passages.extend(dotdict({"long_text":d["content"]}) for d in results)+ return dspy.Prediction(passages=passages)++\ No newline at end of file