Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Add WeaviateRM

Connor Shorten71d2dcfee967
diff --git a/dspy/retrieve/weaviate_rm.py b/dspy/retrieve/weaviate_rm.py
new file mode 100644
index 000000000..f9271719a
--- /dev/null
+++ b/dspy/retrieve/weaviate_rm.py
@@ -0,0 +1,78 @@
+from collections import defaultdict
+from typing import List, Union
+import dspy
+from typing import Optional
+
+# Note, using Python v3 Weaviate Client -- need to make sure this is tied to `pip install dspy-ai[weaviate]`
+
+try:
+ import weaviate
+except ImportError:
+ raise ImportError(
+ "The 'weaviate' extra is required to use WeaviateRM. Install it with `pip install dspy-ai[weaviate]`"
+ )
+
+
+class WeaviateRM(dspy.Retrieve):
+ """
+ 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:
+ - document: 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.
+
+ 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 = WeaviatetRM("my_collection_name", 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)
+ ```
+ """
+
+ def __init__(self,
+ weaviate_collection_name: str,
+ weaviate_content_key: str,
+ weaviate_client: weaviate.Client,
+ k: int = 3
+ ):
+ self._weaviate_collection_name = weaviate_collection_name
+ self._weaviate_content_key = weaviate_content_key
+ self._weaviate_client = weaviate_client
+
+ super().__init__(k=k)
+
+ # ToDo -- interface query batching
+ def forward(self, query: str, k: Optional[int]) -> dspy.Prediction:
+ """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.
+ """
+
+ # Please note, as stated above the WeaviateRM assumes you have a text key payload named: `document`
+ k = k if k is not None else self.k
+ results = self._weaviate_client.query\
+ .get(self._weaviate_collection_name, ["document"])\
+ .with_near_text({"concepts": [query]})\
+ .with_limit(k)\
+ .do()
+
+ results = results["data"]["Get"][self._weaviate_collection_name]
+
+ return dspy.Prediction(passages=results)