Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

Add WeaviateRM

Connor Shortend5d6fa9fa849
diff --git a/dspy/retrieve/weaviate_rm.py b/dspy/retrieve/weaviate_rm.py
index 24daffd84..763ef5a5b 100644
--- a/dspy/retrieve/weaviate_rm.py
+++ b/dspy/retrieve/weaviate_rm.py
@@ -53,7 +53,7 @@ class WeaviateRM(dspy.Retrieve):
super().__init__(k=k)
# ToDo -- interface query batching
- def forward(self, query: str, k: Optional[int]) -> dspy.Prediction:
+ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int]) -> dspy.Prediction:
"""Search with Weaviate for self.k top passages for query
Args:
@@ -65,12 +65,29 @@ class WeaviateRM(dspy.Retrieve):
# Please note, as stated above the WeaviateRM assumes you have a text key payload named: `content`
k = k if k is not None else self.k
- results = self._weaviate_client.query\
- .get(self._weaviate_collection_name, ["content"])\
- .with_near_text({"concepts": [query]})\
- .with_limit(k)\
- .do()
-
- results = results["data"]["Get"][self._weaviate_collection_name]
-
- return dspy.Prediction(passages=results)
+ queries = (
+ [query_or_queries]
+ if isinstance(query_or_queries, str)
+ else query_or_queries
+ )
+ queries = [q for q in queries if q] # Filter empty queries
+ passages = []
+ # Note this assumes you are using a text2vec embedding service orchestrated in Weaviate
+ for query in queries:
+ # We are currently hard-coding the payload / text key `content`
+ results = self._weaviate_client.query\
+ .get(self._weaviate_collection_name, ["content"])\
+ .with_near_text({"concepts": [query]})\
+ .with_limit(k)\
+ .do()
+
+ results = results["data"]["Get"][self._weaviate_collection_name]
+ parsed_results = [result["content"] for result in results]
+ # This assumes you will call `dspy.utils.deduplicate` in the forward pass
+ # I am skeptical how often you will have duplicate documents,
+ # but an idea could be to extend this with the distance scores.
+ # However, it probably makes more sense for us (Weaviate) to cook this into our
+ # batch query API (which will be in the v4 client, expected to go GA in 1.24).
+ passages.extend(parsed_results)
+
+ return dspy.Prediction(passages=passages)