generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 118
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added tests for knowlegebases retriever
- Loading branch information
Showing
3 changed files
with
75 additions
and
2 deletions.
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 |
---|---|---|
@@ -1,4 +1,11 @@ | ||
from langchain_aws.llms import SagemakerEndpoint | ||
from langchain_aws.retrievers import AmazonKendraRetriever | ||
from langchain_aws.retrievers import ( | ||
AmazonKendraRetriever, | ||
AmazonKnowledgeBasesRetriever, | ||
) | ||
|
||
__all__ = ["SagemakerEndpoint", "AmazonKendraRetriever"] | ||
__all__ = [ | ||
"SagemakerEndpoint", | ||
"AmazonKendraRetriever", | ||
"AmazonKnowledgeBasesRetriever", | ||
] |
Empty file.
66 changes: 66 additions & 0 deletions
66
libs/aws/tests/integration_tests/retrievers/test_amazon_knowledgebases_retriever.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,66 @@ | ||
from unittest.mock import Mock | ||
|
||
import pytest | ||
from langchain_core.documents import Document | ||
|
||
from langchain_aws import AmazonKnowledgeBasesRetriever | ||
|
||
|
||
@pytest.fixture | ||
def mock_client() -> Mock: | ||
return Mock() | ||
|
||
|
||
@pytest.fixture | ||
def retriever(mock_client: Mock) -> AmazonKnowledgeBasesRetriever: | ||
return AmazonKnowledgeBasesRetriever( | ||
knowledge_base_id="test-knowledge-base", | ||
client=mock_client, | ||
retrieval_config={"vectorSearchConfiguration": {"numberOfResults": 4}}, # type: ignore[arg-type] | ||
) | ||
|
||
|
||
def test_get_relevant_documents(retriever, mock_client) -> None: # type: ignore[no-untyped-def] | ||
response = { | ||
"retrievalResults": [ | ||
{ | ||
"content": {"text": "This is the first result."}, | ||
"location": "location1", | ||
"score": 0.9, | ||
}, | ||
{ | ||
"content": {"text": "This is the second result."}, | ||
"location": "location2", | ||
"score": 0.8, | ||
}, | ||
{"content": {"text": "This is the third result."}, "location": "location3"}, | ||
] | ||
} | ||
mock_client.retrieve.return_value = response | ||
|
||
query = "test query" | ||
|
||
expected_documents = [ | ||
Document( | ||
page_content="This is the first result.", | ||
metadata={"location": "location1", "score": 0.9}, | ||
), | ||
Document( | ||
page_content="This is the second result.", | ||
metadata={"location": "location2", "score": 0.8}, | ||
), | ||
Document( | ||
page_content="This is the third result.", | ||
metadata={"location": "location3", "score": 0.0}, | ||
), | ||
] | ||
|
||
documents = retriever.get_relevant_documents(query) | ||
|
||
assert documents == expected_documents | ||
|
||
mock_client.retrieve.assert_called_once_with( | ||
retrievalQuery={"text": "test query"}, | ||
knowledgeBaseId="test-knowledge-base", | ||
retrievalConfiguration={"vectorSearchConfiguration": {"numberOfResults": 4}}, | ||
) |