-
Notifications
You must be signed in to change notification settings - Fork 15.7k
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
Fix multi vector retriever subclassing #14350
Merged
Merged
Changes from 11 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ec30fb4
Fix multivector retriever subclassing
jacoblee93 7b56d4d
Merge branch 'master' of https://github.com/hwchase17/langchain into …
jacoblee93 4f85c1e
Notebook tweak
jacoblee93 7b4f473
Fix lint
jacoblee93 80d52c0
Add unit tests
jacoblee93 0126d66
Update tests
jacoblee93 5a1fd1a
Typing
jacoblee93 e1649cf
Merge branch 'master' into jacob/hotfix
efriis 55a2add
Merge branch 'master' into jacob/hotfix
efriis 84376a9
lint 1
efriis b0e9887
lint 2
efriis f39c981
Rename parameter
jacoblee93 e3c1431
Rename
jacoblee93 4932fe6
Fix typo
jacoblee93 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
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
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
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
30 changes: 30 additions & 0 deletions
30
libs/langchain/tests/unit_tests/retrievers/test_multi_vector.py
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,30 @@ | ||
from typing import Any, List | ||
|
||
from langchain_core.documents import Document | ||
|
||
from langchain.retrievers.multi_vector import MultiVectorRetriever | ||
from langchain.storage import InMemoryStore | ||
from tests.unit_tests.indexes.test_indexing import InMemoryVectorStore | ||
|
||
|
||
class InMemoryVectorstoreWithSearch(InMemoryVectorStore): | ||
def similarity_search( | ||
self, query: str, k: int = 4, **kwargs: Any | ||
) -> List[Document]: | ||
res = self.store.get(query) | ||
if res is None: | ||
return [] | ||
return [res] | ||
|
||
|
||
def test_multi_vector_retriever_initialization() -> None: | ||
vectorstore = InMemoryVectorstoreWithSearch() | ||
retriever = MultiVectorRetriever( | ||
vectorstore=vectorstore, docstore=InMemoryStore(), doc_id="doc_id" | ||
) | ||
documents = [Document(page_content="test document", metadata={"doc_id": "1"})] | ||
retriever.vectorstore.add_documents(documents, ids=["1"]) | ||
retriever.docstore.mset(list(zip(["1"], documents))) | ||
results = retriever.invoke("1") | ||
assert len(results) > 0 | ||
assert results[0].page_content == "test document" |
40 changes: 40 additions & 0 deletions
40
libs/langchain/tests/unit_tests/retrievers/test_parent_document.py
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,40 @@ | ||
from typing import Any, List, Sequence | ||
|
||
from langchain_core.documents import Document | ||
|
||
from langchain.retrievers import ParentDocumentRetriever | ||
from langchain.storage import InMemoryStore | ||
from langchain.text_splitter import CharacterTextSplitter | ||
from tests.unit_tests.indexes.test_indexing import InMemoryVectorStore | ||
|
||
|
||
class InMemoryVectorstoreWithSearch(InMemoryVectorStore): | ||
def similarity_search( | ||
self, query: str, k: int = 4, **kwargs: Any | ||
) -> List[Document]: | ||
res = self.store.get(query) | ||
if res is None: | ||
return [] | ||
return [res] | ||
|
||
def add_documents(self, documents: Sequence[Document], **kwargs: Any) -> List[str]: | ||
print(documents) | ||
return super().add_documents( | ||
documents, ids=[f"{i}" for i in range(len(documents))] | ||
) | ||
|
||
|
||
def test_parent_document_retriever_initialization() -> None: | ||
vectorstore = InMemoryVectorstoreWithSearch() | ||
store = InMemoryStore() | ||
child_splitter = CharacterTextSplitter(chunk_size=400) | ||
documents = [Document(page_content="test document")] | ||
retriever = ParentDocumentRetriever( | ||
vectorstore=vectorstore, | ||
docstore=store, | ||
child_splitter=child_splitter, | ||
) | ||
retriever.add_documents(documents) | ||
results = retriever.invoke("0") | ||
assert len(results) > 0 | ||
assert results[0].page_content == "test document" |
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.
should we call this
byte_store
instead?If this was breaking in last release anyways, I think it's ok to rename (unless
base_store
in this is older than that)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.
Sure, down for that
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.
Just confirming - this
base_store
param is brand new. Nobody would be using it?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.
Can also use the
ByteStore
type instead ofBaseStore[str, bytes]
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.
Yes, base store is brand new in last release