-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding in a rockset retriever class #1146
Open
danpechi
wants to merge
2
commits into
stanfordnlp:main
Choose a base branch
from
danpechi:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
from typing import List, Optional, Union | ||
|
||
import dspy | ||
import pandas as pd | ||
from dsp.utils import dotdict | ||
|
||
try: | ||
import rockset | ||
from rockset import ApiException, Regions, RocksetClient | ||
except ImportError: | ||
raise ImportError( | ||
"The 'rockset' extra is required to use RocksetRM. Install it with `pip install dspy-ai[rockset]`", | ||
) | ||
|
||
|
||
class RocksetRM(dspy.Retrieve): | ||
""" | ||
A retrieval module that uses Rockset to return the top passages for a given query. | ||
|
||
Assumes that a Rockset collection has been created and populated with the following payload: | ||
- content: The text of the passage | ||
|
||
Args: | ||
rockset_collection_name (str): The name of the Rockset collection. | ||
rockset_client (RocksetClient): An instance of the Rockset client. | ||
k (int, optional): The default number of top passages to retrieve. Defaults to 3. | ||
rockset_collection_text_key (str, optional): The key in the collection with the content. Defaults to | ||
page_content. | ||
rockset_collection_source_key (str, optional): The key in the collection with the source. Defaults to | ||
source. | ||
rockset_collection_embeddings_key (str, optional): The key in the collection with the embeddings. Defaults to | ||
embeddings. | ||
|
||
Examples: | ||
Below is a code snippet that shows how to use Rockset as the default retriver: | ||
```python | ||
import rockset | ||
|
||
llm = dspy.OpenAI(model="gpt-3.5-turbo") | ||
rockset_client = rockset.Client("your-path-here") | ||
retriever_model = RocksetRM(rockset_collection_name="my_collection_name", | ||
rockset_collection_text_key="content", | ||
rockset_client=rockset_client) | ||
dspy.settings.configure(lm=llm, rm=retriever_model) | ||
``` | ||
|
||
Below is a code snippet that shows how to use Rockset in the forward() function of a module | ||
```python | ||
self.retrieve = RocksetRM("my_collection_name", rockset_client=rockset_client, k=num_passages) | ||
``` | ||
""" | ||
|
||
def __init__(self, | ||
rockset_collection_name: str, | ||
rockset_client: rockset.RocksetClient, | ||
k: int = 3, | ||
rockset_collection_text_key: Optional[str] = "page_content", | ||
rockset_collection_source_key: Optional[str] = "source", | ||
rockset_collection_embeddings_key: Optional[str] = "embeddings", | ||
): | ||
self._rockset_collection_name = rockset_collection_name | ||
self._rockset_client = rockset_client | ||
self._rockset_collection_text_key = rockset_collection_text_key | ||
self._rockset_collection_source_key = rockset_collection_source_key | ||
self._rockset_collection_embeddings_key = rockset_collection_embeddings_key | ||
super().__init__(k=k) | ||
|
||
def forward(self, query_or_queries: Union[str, List[str]], metadata_filter: Optional[str] = None, | ||
k: Optional[int] = None, **kwargs) -> dspy.Prediction: | ||
"""Search with Rockset for self.k top passages for query | ||
|
||
Args: | ||
query_or_queries (Union[str, List[str]]): The query or queries to search for. | ||
metadata_filter (Optional[str]): a filter on the metadata within the vectorstore. | ||
k (Optional[int]): The number of top passages to retrieve. Defaults to self.k. | ||
Returns: | ||
dspy.Prediction: An object containing the retrieved passages. | ||
""" | ||
|
||
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: | ||
sim_query = f""" | ||
with base as | ||
(select {self._rockset_collection_text_key}, {self._rockset_collection_source_key}, | ||
COSINE_SIM(v.{self._rockset_collection_embedding_key}, {query.strip("'")}) as sim_search | ||
from {self._rockset_collection_name} v | ||
where v.{self._rockset_collection_embedding_key} is not null | ||
{metadata_filter} | ||
order by sim_search desc limit {k}) | ||
select {self._rockset_collection_text_key}, {self._rockset_collection_source_key}, | ||
sim_search from base | ||
""" | ||
|
||
results = self._rockset_client.sql(query=sim_query) | ||
|
||
if not pd.DataFrame(results["results"], dtype='str').empty: | ||
data = pd.DataFrame(results["results"], dtype='str')[[self._rockset_collection_text_key, | ||
self._rockset_collection_source_key]].values | ||
passages.extend(data) | ||
|
||
# Return type not changed, needs to be a Prediction object. But other code will break if we change it. | ||
return passages |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comment to be addressed? This can be a dspy.Prediction(passages=...).