Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix for weaviate outdated client version. Issue 699

Jettro Coenradiee02211d86482
diff --git a/dspy/retrieve/weaviate_rm.py b/dspy/retrieve/weaviate_rm.py
index 61e8d1ef0..bb1d712fa 100644
--- a/dspy/retrieve/weaviate_rm.py
+++ b/dspy/retrieve/weaviate_rm.py
@@ -5,6 +5,7 @@ from dsp.utils import dotdict
try:
import weaviate
+ import weaviate.classes as wvc
except ImportError:
raise ImportError(
"The 'weaviate' extra is required to use WeaviateRM. Install it with `pip install dspy-ai[weaviate]`",
@@ -22,6 +23,9 @@ class WeaviateRM(dspy.Retrieve):
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.
+ weaviate_collection_text_key (str, optional): The key in the collection with the content. Defaults to content.
+ weaviate_alpha (float, optional): The alpha value for the hybrid query. Defaults to 0.5.
+ weaviate_fusion_type (wvc.HybridFusion, optional): The fusion type for the query. Defaults to RELATIVE_SCORE.
Examples:
Below is a code snippet that shows how to use Weaviate as the default retriver:
@@ -47,13 +51,17 @@ class WeaviateRM(dspy.Retrieve):
weaviate_client: weaviate.Client,
k: int = 3,
weaviate_collection_text_key: Optional[str] = "content",
+ weaviate_alpha: Optional[float] = 0.5,
+ weaviate_fusion_type: Optional[wvc.HybridFusion] = wvc.HybridFusion.RELATIVE_SCORE
):
self._weaviate_collection_name = weaviate_collection_name
self._weaviate_client = weaviate_client
self._weaviate_collection_text_key = weaviate_collection_text_key
+ self._weaviate_alpha = weaviate_alpha
+ self._weaviate_fusion_type = weaviate_fusion_type
super().__init__(k=k)
- def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int]) -> dspy.Prediction:
+ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int] = None) -> dspy.Prediction:
"""Search with Weaviate for self.k top passages for query
Args:
@@ -72,14 +80,17 @@ class WeaviateRM(dspy.Retrieve):
queries = [q for q in queries if q]
passages = []
for query in queries:
- results = self._weaviate_client.query\
- .get(self._weaviate_collection_name, [self._weaviate_collection_text_key])\
- .with_hybrid(query=query)\
- .with_limit(k)\
- .do()
+ collection = self._weaviate_client.collections.get(self._weaviate_collection_name)
+ results = collection.query.hybrid(query=query,
+ limit=k,
+ alpha=self._weaviate_alpha,
+ fusion_type=self._weaviate_fusion_type,
+ return_metadata=wvc.query.MetadataQuery(
+ distance=True, score=True)
+ )
- results = results["data"]["Get"][self._weaviate_collection_name]
- parsed_results = [result[self._weaviate_collection_text_key] for result in results]
+ parsed_results = [result.properties[self._weaviate_collection_text_key] for result in results.objects]
passages.extend(dotdict({"long_text": d}) for d in parsed_results)
+ # Return type not changed, needs to be a Prediction object. But other code will break if we change it.
return passages
diff --git a/pyproject.toml b/pyproject.toml
index d7bc3528f..d55bb54b3 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -96,7 +96,7 @@ fastembed = { version = "^0.1.0", optional = true }
marqo = { version = "*", optional = true }
qdrant-client = { version = "^1.6.2", optional = true }
pinecone-client = { version = "^2.2.4", optional = true }
-weaviate-client = { version = "^3.26.1", optional = true }
+weaviate-client = { version = "^4.5.4", optional = true }
sphinx = { version = ">=4.3.0", optional = true }
furo = { version = ">=2023.3.27", optional = true }
docutils = { version = "<0.17", optional = true }