-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Added support for LlamaIndex Retrievers as a DSPy Retriever Module #911
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
16c464d
feat(llama_index_rm): Llama Index retreivers
no-dice-io b3b47eb
feat(llamaindex): added llamaindex support
no-dice-io 1bebd46
feat(llamaindex): added llamaindex rm
no-dice-io d630d8e
feat(llamaindex): refactored tests
no-dice-io a2f2bd2
refactor(llama_index_rm): per pr comments
no-dice-io f070e05
Merge branch 'stanfordnlp:main' into li-bridge
no-dice-io 5d1989a
refactor(llama_index_rm): simplified class
no-dice-io 216546b
fix(conflicts): resolved li dependency conflicts
no-dice-io 269d9e8
fix(dependencies): updated LI tests to be optional
no-dice-io 259843d
Merge branch 'main' into li-bridge
no-dice-io 9ab749e
Merge branch 'main' into li-bridge
no-dice-io 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 hidden or 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,90 @@ | ||
| import logging | ||
| from typing import Optional | ||
|
|
||
| import dspy | ||
|
|
||
| try: | ||
| from llama_index.core.base.base_retriever import BaseRetriever | ||
| except ImportError: | ||
| err = "The 'llama_index' package is required to use LlamaIndexRM. Install it with 'pip install llama_index'." | ||
| raise ImportError(err) from None | ||
|
|
||
| NO_TOP_K_WARNING = "The underlying LlamaIndex retriever does not support top k retrieval. Ignoring k value." | ||
|
|
||
|
|
||
| class LlamaIndexRM(dspy.Retrieve): | ||
| """Implements a retriever which wraps over a LlamaIndex retriever. | ||
|
|
||
| This is done to bridge LlamaIndex and DSPy and allow the various retrieval | ||
| abstractions in LlamaIndex to be used in DSPy. | ||
|
|
||
| To-do (maybe): | ||
| - Async support (DSPy lacks this entirely it seems, so not a priority until the rest of the repo catches on) | ||
| - Text/video retrieval (Available in LI, not sure if this will be a priority in DSPy) | ||
|
|
||
| Args: | ||
| retriever (BaseRetriever): A LlamaIndex retriever object - text based only | ||
| k (int): Optional; the number of examples to retrieve (similarity_top_k) | ||
|
|
||
| If the underlying LI retriever does not have the property similarity_top_k, k will be ignored. | ||
|
|
||
| Returns: | ||
| DSPy RM Object - this is a retriever object that can be used in DSPy | ||
| """ | ||
|
|
||
| retriever: BaseRetriever | ||
|
|
||
| def __init__( | ||
| self, | ||
| retriever: BaseRetriever, | ||
| k: Optional[int] = None, | ||
| ): | ||
| self.retriever = retriever | ||
|
|
||
| if k: | ||
| self.k = k | ||
|
|
||
| @property | ||
| def k(self) -> Optional[int]: | ||
| """Get similarity top k of retriever.""" | ||
| if not hasattr(self.retriever, "similarity_top_k"): | ||
| logging.warning(NO_TOP_K_WARNING) | ||
| return None | ||
|
|
||
| return self.retriever.similarity_top_k | ||
|
|
||
| @k.setter | ||
| def k(self, k: int) -> None: | ||
| """Set similarity top k of retriever.""" | ||
| if hasattr(self.retriever, "similarity_top_k"): | ||
| self.retriever.similarity_top_k = k | ||
| else: | ||
| logging.warning(NO_TOP_K_WARNING) | ||
|
|
||
| def forward(self, query: str, k: Optional[int] = None) -> list[dspy.Example]: | ||
| """Forward function for the LI retriever. | ||
|
|
||
| This is the function that is called to retrieve the top k examples for a given query. | ||
| Top k is set via the setter similarity_top_k or at LI instantiation. | ||
|
|
||
| Args: | ||
| query (str): The query to retrieve examples for | ||
| k (int): Optional; the number of examples to retrieve (similarity_top_k) | ||
|
|
||
| If the underlying LI retriever does not have the property similarity_top_k, k will be ignored. | ||
|
|
||
| Returns: | ||
| List[dspy.Example]: A list of examples retrieved by the retriever | ||
| """ | ||
| if k: | ||
| self.k = k | ||
|
|
||
| raw = self.retriever.retrieve(query) | ||
|
|
||
| return [ | ||
| dspy.Example( | ||
| text=result.text, | ||
| score=result.score, | ||
| ) | ||
| for result in raw | ||
| ] | ||
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.
not every retriever in llama-index will have this property 👀 It kind of depends. The
BaseRetrieverclass is fairly generic, and just exposesretrieve()/aretrieve()methodsThere 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.
Ooh thank you for pointing this out - I'll make an adjustment shortly!
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.
Thanks @logan-markewich for looking into this!
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.
I believe I've resolved this - please review!
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.
I think this looks good, but the output should be typed as
Optional[int]thoughKind of curious what the motivation is to expose this on the class? Is this something that dspy can optimize 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.
I don't recall if it can be optimized, but its a parameter that is often used with their
forwardmethod so I was looking for a way to pass it to the underlying LI retriever.