generated from langchain-ai/integration-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from rozerarenu/main
Add source metadata from API response to Document metadata
- Loading branch information
Showing
4 changed files
with
82 additions
and
8 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# type: ignore | ||
|
||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from langchain_core.documents import Document | ||
|
||
from langchain_aws.retrievers import AmazonKnowledgeBasesRetriever | ||
|
||
|
||
@pytest.fixture | ||
def mock_client(): | ||
return MagicMock() | ||
|
||
|
||
@pytest.fixture | ||
def mock_retriever_config(): | ||
return {"vectorSearchConfiguration": {"numberOfResults": 4}} | ||
|
||
|
||
@pytest.fixture | ||
def amazon_retriever(mock_client, mock_retriever_config): | ||
return AmazonKnowledgeBasesRetriever( | ||
knowledge_base_id="test_kb_id", | ||
retrieval_config=mock_retriever_config, | ||
client=mock_client, | ||
) | ||
|
||
|
||
def test_retriever_invoke(amazon_retriever, mock_client): | ||
query = "test query" | ||
mock_client.retrieve.return_value = { | ||
"retrievalResults": [ | ||
{"content": {"text": "result1"}, "metadata": {"key": "value1"}}, | ||
{ | ||
"content": {"text": "result2"}, | ||
"metadata": {"key": "value2"}, | ||
"score": 1, | ||
"location": "testLocation", | ||
}, | ||
{"content": {"text": "result3"}}, | ||
] | ||
} | ||
documents = amazon_retriever.invoke(query, run_manager=None) | ||
|
||
assert len(documents) == 3 | ||
assert isinstance(documents[0], Document) | ||
assert documents[0].page_content == "result1" | ||
assert documents[0].metadata == {"score": 0, "source_metadata": {"key": "value1"}} | ||
assert documents[1].page_content == "result2" | ||
assert documents[1].metadata == { | ||
"score": 1, | ||
"source_metadata": {"key": "value2"}, | ||
"location": "testLocation", | ||
} | ||
assert documents[2].page_content == "result3" | ||
assert documents[2].metadata == {"score": 0} |