2023-06-30 21:44:03 +00:00
|
|
|
"""Test Base Retriever logic."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-07-02 14:22:24 +00:00
|
|
|
from typing import Dict, List, Optional
|
2023-06-30 21:44:03 +00:00
|
|
|
|
|
|
|
import pytest
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_core.callbacks import (
|
2023-06-30 21:44:03 +00:00
|
|
|
AsyncCallbackManagerForRetrieverRun,
|
|
|
|
CallbackManagerForRetrieverRun,
|
|
|
|
)
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_core.retrievers import BaseRetriever, Document
|
|
|
|
|
2023-06-30 21:44:03 +00:00
|
|
|
from tests.unit_tests.callbacks.fake_callback_handler import FakeCallbackHandler
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_retriever_v1() -> BaseRetriever:
|
|
|
|
with pytest.warns(
|
|
|
|
DeprecationWarning,
|
|
|
|
match="Retrievers must implement abstract "
|
|
|
|
"`_get_relevant_documents` method instead of `get_relevant_documents`",
|
|
|
|
):
|
|
|
|
|
|
|
|
class FakeRetrieverV1(BaseRetriever):
|
|
|
|
def get_relevant_documents( # type: ignore[override]
|
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV1)
|
|
|
|
return [
|
|
|
|
Document(page_content=query, metadata={"uuid": "1234"}),
|
|
|
|
]
|
|
|
|
|
|
|
|
async def aget_relevant_documents( # type: ignore[override]
|
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV1)
|
|
|
|
return [
|
|
|
|
Document(
|
|
|
|
page_content=f"Async query {query}", metadata={"uuid": "1234"}
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
return FakeRetrieverV1() # type: ignore[abstract]
|
|
|
|
|
|
|
|
|
|
|
|
def test_fake_retriever_v1_upgrade(fake_retriever_v1: BaseRetriever) -> None:
|
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v1._new_arg_supported is False
|
|
|
|
assert fake_retriever_v1._expects_other_args is False
|
|
|
|
results: List[Document] = fake_retriever_v1.get_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks]
|
|
|
|
)
|
|
|
|
assert results[0].page_content == "Foo"
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_fake_retriever_v1_upgrade_async(
|
|
|
|
fake_retriever_v1: BaseRetriever,
|
|
|
|
) -> None:
|
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v1._new_arg_supported is False
|
|
|
|
assert fake_retriever_v1._expects_other_args is False
|
|
|
|
results: List[Document] = await fake_retriever_v1.aget_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks]
|
|
|
|
)
|
|
|
|
assert results[0].page_content == "Async query Foo"
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fake_retriever_v1_with_kwargs() -> BaseRetriever:
|
|
|
|
# Test for things like the Weaviate V1 Retriever.
|
|
|
|
with pytest.warns(
|
|
|
|
DeprecationWarning,
|
|
|
|
match="Retrievers must implement abstract "
|
|
|
|
"`_get_relevant_documents` method instead of `get_relevant_documents`",
|
|
|
|
):
|
|
|
|
|
|
|
|
class FakeRetrieverV1(BaseRetriever):
|
|
|
|
def get_relevant_documents( # type: ignore[override]
|
|
|
|
self, query: str, where_filter: Optional[Dict[str, object]] = None
|
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV1)
|
|
|
|
return [
|
|
|
|
Document(page_content=query, metadata=where_filter or {}),
|
|
|
|
]
|
|
|
|
|
|
|
|
async def aget_relevant_documents( # type: ignore[override]
|
|
|
|
self, query: str, where_filter: Optional[Dict[str, object]] = None
|
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV1)
|
|
|
|
return [
|
|
|
|
Document(
|
|
|
|
page_content=f"Async query {query}", metadata=where_filter or {}
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
return FakeRetrieverV1() # type: ignore[abstract]
|
|
|
|
|
|
|
|
|
|
|
|
def test_fake_retriever_v1_with_kwargs_upgrade(
|
|
|
|
fake_retriever_v1_with_kwargs: BaseRetriever,
|
|
|
|
) -> None:
|
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v1_with_kwargs._new_arg_supported is False
|
|
|
|
assert fake_retriever_v1_with_kwargs._expects_other_args is True
|
|
|
|
results: List[Document] = fake_retriever_v1_with_kwargs.get_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks], where_filter={"foo": "bar"}
|
|
|
|
)
|
|
|
|
assert results[0].page_content == "Foo"
|
|
|
|
assert results[0].metadata == {"foo": "bar"}
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
|
|
|
|
|
|
|
|
|
|
|
async def test_fake_retriever_v1_with_kwargs_upgrade_async(
|
|
|
|
fake_retriever_v1_with_kwargs: BaseRetriever,
|
|
|
|
) -> None:
|
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v1_with_kwargs._new_arg_supported is False
|
|
|
|
assert fake_retriever_v1_with_kwargs._expects_other_args is True
|
|
|
|
results: List[
|
|
|
|
Document
|
|
|
|
] = await fake_retriever_v1_with_kwargs.aget_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks], where_filter={"foo": "bar"}
|
|
|
|
)
|
|
|
|
assert results[0].page_content == "Async query Foo"
|
|
|
|
assert results[0].metadata == {"foo": "bar"}
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
|
|
|
|
|
|
|
|
2023-07-02 14:22:24 +00:00
|
|
|
class FakeRetrieverV2(BaseRetriever):
|
Add serialized object to retriever start callback (#7074)
<!-- Thank you for contributing to LangChain!
Replace this comment with:
- Description: a description of the change,
- Issue: the issue # it fixes (if applicable),
- Dependencies: any dependencies required for this change,
- Tag maintainer: for a quicker response, tag the relevant maintainer
(see below),
- Twitter handle: we announce bigger features on Twitter. If your PR
gets announced and you'd like a mention, we'll gladly shout you out!
If you're adding a new integration, please include:
1. a test for the integration, preferably unit tests that do not rely on
network access,
2. an example notebook showing its use.
Maintainer responsibilities:
- General / Misc / if you don't know who to tag: @dev2049
- DataLoaders / VectorStores / Retrievers: @rlancemartin, @eyurtsev
- Models / Prompts: @hwchase17, @dev2049
- Memory: @hwchase17
- Agents / Tools / Toolkits: @vowelparrot
- Tracing / Callbacks: @agola11
- Async: @agola11
If no one reviews your PR within a few days, feel free to @-mention the
same people again.
See contribution guidelines for more information on how to write/run
tests, lint, etc:
https://github.com/hwchase17/langchain/blob/master/.github/CONTRIBUTING.md
-->
2023-07-05 17:04:43 +00:00
|
|
|
throw_error: bool = False
|
2023-07-02 14:22:24 +00:00
|
|
|
|
|
|
|
def _get_relevant_documents(
|
2023-10-30 23:30:10 +00:00
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
*,
|
|
|
|
run_manager: Optional[CallbackManagerForRetrieverRun] = None,
|
2023-07-02 14:22:24 +00:00
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV2)
|
|
|
|
assert run_manager is not None
|
|
|
|
assert isinstance(run_manager, CallbackManagerForRetrieverRun)
|
|
|
|
if self.throw_error:
|
|
|
|
raise ValueError("Test error")
|
|
|
|
return [
|
|
|
|
Document(page_content=query),
|
|
|
|
]
|
|
|
|
|
|
|
|
async def _aget_relevant_documents(
|
2023-10-30 23:30:10 +00:00
|
|
|
self,
|
|
|
|
query: str,
|
|
|
|
*,
|
|
|
|
run_manager: Optional[AsyncCallbackManagerForRetrieverRun] = None,
|
2023-07-02 14:22:24 +00:00
|
|
|
) -> List[Document]:
|
|
|
|
assert isinstance(self, FakeRetrieverV2)
|
|
|
|
assert run_manager is not None
|
|
|
|
assert isinstance(run_manager, AsyncCallbackManagerForRetrieverRun)
|
|
|
|
if self.throw_error:
|
|
|
|
raise ValueError("Test error")
|
|
|
|
return [
|
|
|
|
Document(page_content=f"Async query {query}"),
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2023-06-30 21:44:03 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def fake_retriever_v2() -> BaseRetriever:
|
|
|
|
return FakeRetrieverV2() # type: ignore[abstract]
|
|
|
|
|
|
|
|
|
2023-07-02 14:22:24 +00:00
|
|
|
@pytest.fixture
|
|
|
|
def fake_erroring_retriever_v2() -> BaseRetriever:
|
|
|
|
return FakeRetrieverV2(throw_error=True) # type: ignore[abstract]
|
|
|
|
|
|
|
|
|
|
|
|
def test_fake_retriever_v2(
|
|
|
|
fake_retriever_v2: BaseRetriever, fake_erroring_retriever_v2: BaseRetriever
|
|
|
|
) -> None:
|
2023-06-30 21:44:03 +00:00
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v2._new_arg_supported is True
|
|
|
|
results = fake_retriever_v2.get_relevant_documents("Foo", callbacks=[callbacks])
|
|
|
|
assert results[0].page_content == "Foo"
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
2023-07-02 14:22:24 +00:00
|
|
|
fake_retriever_v2.get_relevant_documents("Foo", callbacks=[callbacks])
|
2023-06-30 21:44:03 +00:00
|
|
|
|
|
|
|
with pytest.raises(ValueError, match="Test error"):
|
2023-07-02 14:22:24 +00:00
|
|
|
fake_erroring_retriever_v2.get_relevant_documents("Foo", callbacks=[callbacks])
|
2023-06-30 21:44:03 +00:00
|
|
|
assert callbacks.retriever_errors == 1
|
|
|
|
|
|
|
|
|
2023-07-02 14:22:24 +00:00
|
|
|
async def test_fake_retriever_v2_async(
|
|
|
|
fake_retriever_v2: BaseRetriever, fake_erroring_retriever_v2: BaseRetriever
|
|
|
|
) -> None:
|
2023-06-30 21:44:03 +00:00
|
|
|
callbacks = FakeCallbackHandler()
|
|
|
|
assert fake_retriever_v2._new_arg_supported is True
|
|
|
|
results = await fake_retriever_v2.aget_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks]
|
|
|
|
)
|
|
|
|
assert results[0].page_content == "Async query Foo"
|
|
|
|
assert callbacks.retriever_starts == 1
|
|
|
|
assert callbacks.retriever_ends == 1
|
|
|
|
assert callbacks.retriever_errors == 0
|
2023-07-02 14:22:24 +00:00
|
|
|
await fake_retriever_v2.aget_relevant_documents("Foo", callbacks=[callbacks])
|
2023-06-30 21:44:03 +00:00
|
|
|
with pytest.raises(ValueError, match="Test error"):
|
2023-07-02 14:22:24 +00:00
|
|
|
await fake_erroring_retriever_v2.aget_relevant_documents(
|
|
|
|
"Foo", callbacks=[callbacks]
|
2023-06-30 21:44:03 +00:00
|
|
|
)
|