Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

retrievers import patches

Arnav Singhvi91ef3ccc3f6c
diff --git a/dspy/retrieve/mongodb_atlas_rm.py b/dspy/retrieve/mongodb_atlas_rm.py
index 41542a507..89be15dda 100644
--- a/dspy/retrieve/mongodb_atlas_rm.py
+++ b/dspy/retrieve/mongodb_atlas_rm.py
@@ -11,6 +11,7 @@ from openai import (
)
from dsp.utils.settings import settings
+from dspy import Retrieve, Prediction
try:
from pymongo import MongoClient
@@ -68,7 +69,7 @@ class Embedder:
return [result.embedding for result in embedding.data]
-class MongoDBAtlasRM(dspy.Retrieve):
+class MongoDBAtlasRM(Retrieve):
def __init__(
self,
db_name: str,
@@ -107,7 +108,7 @@ class MongoDBAtlasRM(dspy.Retrieve):
self.embedder = Embedder(provider=embedding_provider, model=embedding_model)
- def forward(self, query_or_queries: str) -> dspy.Prediction:
+ def forward(self, query_or_queries: str) -> Prediction:
query_vector = self.embedder([query_or_queries])
pipeline = build_vector_search_pipeline(
index_name=self.index_name,
@@ -116,4 +117,4 @@ class MongoDBAtlasRM(dspy.Retrieve):
limit=self.k,
)
contents = self.client[self.db_name][self.collection_name].aggregate(pipeline)
- return dspy.Prediction(passages=list(contents))
+ return Prediction(passages=list(contents))
diff --git a/dspy/retrieve/neo4j_rm.py b/dspy/retrieve/neo4j_rm.py
index 43c9024da..f71dbb1cb 100644
--- a/dspy/retrieve/neo4j_rm.py
+++ b/dspy/retrieve/neo4j_rm.py
@@ -10,6 +10,7 @@ from openai import (
UnprocessableEntityError,
)
+from dspy import Retrieve, Prediction
from dsp.utils.settings import settings
from dsp.utils import dotdict
@@ -52,7 +53,7 @@ class Embedder:
DEFAULT_INDEX_QUERY = "CALL db.index.vector.queryNodes($index, $k, $embedding) YIELD node, score "
-class Neo4jRM(dspy.Retrieve):
+class Neo4jRM(Retrieve):
"""
Implements a retriever that utilizes Neo4j for retrieving passages.
This class manages a connection to a Neo4j database using official Neo4j Python drivers and requires
@@ -137,7 +138,7 @@ class Neo4jRM(dspy.Retrieve):
self.embedder = Embedder(provider=embedding_provider, model=embedding_model)
- 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]) -> Prediction:
if not isinstance(query_or_queries, list):
query_or_queries = [query_or_queries]
query_vectors = self.embedder(query_or_queries)
diff --git a/dspy/retrieve/pinecone_rm.py b/dspy/retrieve/pinecone_rm.py
index fe65bf23a..0b5518e96 100644
--- a/dspy/retrieve/pinecone_rm.py
+++ b/dspy/retrieve/pinecone_rm.py
@@ -7,6 +7,7 @@ from typing import List, Optional, Union
import backoff
+from dspy import Retrieve, Prediction
from dsp.utils.settings import settings
from dsp.utils import dotdict
@@ -33,7 +34,7 @@ try:
except Exception:
ERRORS = (openai.RateLimitError, openai.APIError)
-class PineconeRM(dspy.Retrieve):
+class PineconeRM(Retrieve):
"""
A retrieval module that uses Pinecone to return the top passages for a given query.
@@ -54,7 +55,7 @@ class PineconeRM(dspy.Retrieve):
dspy.Prediction: An object containing the retrieved passages.
Examples:
- Below is a code snippet that shows how to use this as the default retriver:
+ Below is a code snippet that shows how to use this as the default retriever:
```python
llm = dspy.OpenAI(model="gpt-3.5-turbo")
retriever_model = PineconeRM(openai.api_key)
@@ -222,7 +223,7 @@ class PineconeRM(dspy.Retrieve):
# we need a pooling strategy to get a single vector representation of the input
# so the default is to take the mean of the hidden states
- def forward(self, query_or_queries: Union[str, List[str]]) -> dspy.Prediction:
+ def forward(self, query_or_queries: Union[str, List[str]]) -> Prediction:
"""Search with pinecone for self.k top passages for query
Args:
@@ -251,7 +252,7 @@ class PineconeRM(dspy.Retrieve):
)
passages = [result["metadata"]["text"] for result in sorted_results]
passages = [dotdict({"long_text": passage for passage in passages})]
- return dspy.Prediction(passages=passages)
+ return Prediction(passages=passages)
# For multiple queries, query each and return the highest scoring passages
# If a passage is returned multiple times, the score is accumulated. For this reason we increase top_k by 3x
@@ -269,4 +270,4 @@ class PineconeRM(dspy.Retrieve):
sorted_passages = sorted(
passage_scores.items(), key=lambda x: x[1], reverse=True,
)[: self.k]
- return dspy.Prediction(passages=[dotdict({"long_text": passage}) for passage, _ in sorted_passages])
+ return Prediction(passages=[dotdict({"long_text": passage}) for passage, _ in sorted_passages])