Skip to content

Commit

Permalink
Adds Bedrock KB metadata to Document metadata
Browse files Browse the repository at this point in the history
AWS Bedrock supports custom user metadata per source document.
This change adds that metadata to AmazonKnowledgeBasesRetriever
Documents metadata as "source_metadata"
  • Loading branch information
karlnewell committed May 3, 2024
1 parent 123c720 commit ecb8e1f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion libs/aws/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ retriever = AmazonKendraRetriever(
retriever.get_relevant_documents(query="What is the meaning of life?")
```

`AmazonKnowlegeBasesRetriever` class provides a retriever to connect with Amazon Knowledge Bases.
`AmazonKnowledgeBasesRetriever` class provides a retriever to connect with Amazon Knowledge Bases.

```python
from langchain_aws import AmazonKnowledgeBasesRetriever
Expand Down
3 changes: 3 additions & 0 deletions libs/aws/langchain_aws/retrievers/bedrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def _get_relevant_documents(
metadata={
"location": result["location"],
"score": result["score"] if "score" in result else 0,
"source_metadata": (
result["metadata"] if "metadata" in result else None
),
},
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ def test_get_relevant_documents(retriever, mock_client) -> None: # type: ignore
"score": 0.8,
},
{"content": {"text": "This is the third result."}, "location": "location3"},
{
"content": {"text": "This is the fourth result."},
"location": "location4",
"score": 0.4,
"metadata": {"url": "http://example.com", "title": "Example Title"},
},
]
}
mock_client.retrieve.return_value = response
Expand All @@ -43,19 +49,30 @@ def test_get_relevant_documents(retriever, mock_client) -> None: # type: ignore
expected_documents = [
Document(
page_content="This is the first result.",
metadata={"location": "location1", "score": 0.9},
metadata={"location": "location1", "score": 0.9, "source_metadata": None},
),
Document(
page_content="This is the second result.",
metadata={"location": "location2", "score": 0.8},
metadata={"location": "location2", "score": 0.8, "source_metadata": None},
),
Document(
page_content="This is the third result.",
metadata={"location": "location3", "score": 0.0},
metadata={"location": "location3", "score": 0.0, "source_metadata": None},
),
Document(
page_content="This is the fourth result.",
metadata={
"location": "location4",
"score": 0.4,
"source_metadata": {
"url": "http://example.com",
"title": "Example Title",
},
},
),
]

documents = retriever.get_relevant_documents(query)
documents = retriever.invoke(query)

assert documents == expected_documents

Expand Down

0 comments on commit ecb8e1f

Please sign in to comment.