From 4035a1d234c7fb28e6321ecdf7d39c6ff907b3cc Mon Sep 17 00:00:00 2001 From: Renu Rozera <166179060+rozerarenu@users.noreply.github.com> Date: Thu, 9 May 2024 08:06:22 -0700 Subject: [PATCH] Add source metadata to bedrock retriever response (#21349) Thank you for contributing to LangChain! - [X] **PR title**: "community: Add source metadata to bedrock retriever response" - [X] **PR message**: - **Description:** Bedrock retrieve API returns extra metadata in the response which is currently not returned in the retriever response - **Issue:** The change adds the metadata from bedrock retrieve API response to the bedrock retriever in a backward compatible way. Renamed metadata to sourceMetadata as metadata term is being used in the Document already. This is in sync with what we are doing in llama-index as well. - **Dependencies:** No - [X] **Add tests and docs**: 1. Added unit tests 2. Notebook already exists and does not need any change 3. Response from end to end testing, just to ensure backward compatibility: `[Document(page_content='Exoplanets.', metadata={'location': {'s3Location': {'uri': 's3://bucket/file_name.txt'}, 'type': 'S3'}, 'score': 0.46886647, 'source_metadata': {'x-amz-bedrock-kb-source-uri': 's3://bucket/file_name.txt', 'tag': 'space', 'team': 'Nasa', 'year': 1946.0}})]` - [X] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, hwchase17. --------- Co-authored-by: Piyush Jain --- .../langchain_community/retrievers/bedrock.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/retrievers/bedrock.py b/libs/community/langchain_community/retrievers/bedrock.py index 1c9fd71aff..85718c79c9 100644 --- a/libs/community/langchain_community/retrievers/bedrock.py +++ b/libs/community/langchain_community/retrievers/bedrock.py @@ -115,13 +115,16 @@ class AmazonKnowledgeBasesRetriever(BaseRetriever): results = response["retrievalResults"] documents = [] for result in results: + content = result["content"]["text"] + result.pop("content") + if "score" not in result: + result["score"] = 0 + if "metadata" in result: + result["source_metadata"] = result.pop("metadata") documents.append( Document( - page_content=result["content"]["text"], - metadata={ - "location": result["location"], - "score": result["score"] if "score" in result else 0, - }, + page_content=content, + metadata=result, ) )