Skip to content
Snippets Groups Projects
Unverified Commit bd3adf76 authored by Renu Rozera's avatar Renu Rozera Committed by GitHub
Browse files

Add changes for bedrock documentation and notebook (#12856)

parent b98552f9
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# Bedrock (Knowledge Bases)
> [Knowledge bases for Amazon Bedrock](https://aws.amazon.com/bedrock/knowledge-bases/) is an Amazon Web Services (AWS) offering which lets you quickly build RAG applications by using your private data to customize FM response.
[Knowledge bases for Amazon Bedrock](https://aws.amazon.com/bedrock/knowledge-bases/) is an Amazon Web Services (AWS) offering which lets you quickly build RAG applications by using your private data to customize FM response.
> Implementing `RAG` requires organizations to perform several cumbersome steps to convert data into embeddings (vectors), store the embeddings in a specialized vector database, and build custom integrations into the database to search and retrieve text relevant to the user’s query. This can be time-consuming and inefficient.
Implementing `RAG` requires organizations to perform several cumbersome steps to convert data into embeddings (vectors), store the embeddings in a specialized vector database, and build custom integrations into the database to search and retrieve text relevant to the user’s query. This can be time-consuming and inefficient.
> With `Knowledge Bases for Amazon Bedrock`, simply point to the location of your data in `Amazon S3`, and `Knowledge Bases for Amazon Bedrock` takes care of the entire ingestion workflow into your vector database. If you do not have an existing vector database, Amazon Bedrock creates an Amazon OpenSearch Serverless vector store for you.
With `Knowledge Bases for Amazon Bedrock`, simply point to the location of your data in `Amazon S3`, and `Knowledge Bases for Amazon Bedrock` takes care of the entire ingestion workflow into your vector database. If you do not have an existing vector database, Amazon Bedrock creates an Amazon OpenSearch Serverless vector store for you.
> Knowledge base can be configured through [AWS Console](https://aws.amazon.com/console/) or by using [AWS SDKs](https://aws.amazon.com/developer/tools/).
Knowledge base can be configured through [AWS Console](https://aws.amazon.com/console/) or by using [AWS SDKs](https://aws.amazon.com/developer/tools/).
> In this notebook, we introduce AmazonKnowledgeBasesRetriever - Amazon Bedrock integration in Llama Index via the Retrieve API to retrieve relevant results for a user query from knowledge bases.
In this notebook, we introduce AmazonKnowledgeBasesRetriever - Amazon Bedrock integration in Llama Index via the Retrieve API to retrieve relevant results for a user query from knowledge bases.
%% Cell type:markdown id: tags:
## Using the Knowledge Base Retriever
%% Cell type:code id: tags:
``` python
%pip install --upgrade --quiet boto3 botocore
%pip install llama-index
%pip install llama-index-retrievers-bedrock
```
%% Cell type:markdown id: tags:
For more information about the supported parameters for `retrieval_config`, please check the boto3 documentation: [link](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/bedrock-agent-runtime/client/retrieve.html)
To use filters in the `retrieval_config` you will need to set up metadata.json file for your data source. For more info, see: [link](https://aws.amazon.com/blogs/machine-learning/knowledge-bases-for-amazon-bedrock-now-supports-metadata-filtering-to-improve-retrieval-accuracy/)
%% Cell type:code id: tags:
``` python
from llama_index.retrievers.bedrock import AmazonKnowledgeBasesRetriever
retriever = AmazonKnowledgeBasesRetriever(
knowledge_base_id="<knowledge-base-id>",
retrieval_config={
"vectorSearchConfiguration": {
"numberOfResults": 4,
"overrideSearchType": "HYBRID",
"filter": {"equals": {"key": "tag", "value": "space"}},
}
},
)
```
%% Cell type:code id: tags:
``` python
query = "How big is Milky Way as compared to the entire universe?"
retrieved_results = retriever._retrieve(query)
# Prints the first retrieved result
print(retrieved_results[0].get_content())
```
%% Cell type:markdown id: tags:
The Milky Way is a large spiral galaxy, but in the grand scheme of the universe, it's relatively small. The observable universe is estimated to be about 93 billion light-years in diameter. In comparison, the Milky Way galaxy has a diameter of about 100,000 light-years. So, the Milky Way is just a tiny speck within the vastness of the observable universe. Keep in mind, however, that the universe may extend beyond the observable universe, but our ability to observe it is limited by the speed of light and the age of the universe.
%% Cell type:markdown id: tags:
## Use the retriever to query with Bedrock LLMs
%% Cell type:code id: tags:
``` python
%pip install llama-index-llms-bedrock
```
%% Cell type:code id: tags:
``` python
from llama_index.core import get_response_synthesizer
from llama_index.llms.bedrock.base import Bedrock
llm = Bedrock(model="anthropic.claude-v2", temperature=0, max_tokens=3000)
response_synthesizer = get_response_synthesizer(
response_mode="compact", llm=llm
)
response_obj = response_synthesizer.synthesize(query, retrieved_results)
print(response_obj)
```
......
......@@ -60,6 +60,7 @@ hierarchical retrieval and query decomposition.
- [Vectara](../../../examples/managed/vectaraDemo.ipynb)
- [VideoDB](../../../examples/retrievers/videodb_retriever.ipynb)
- [Zilliz](../../../examples/managed/zcpDemo.ipynb)
- [Amazon Bedrock](../../../examples/retrievers/bedrock_retriever.ipynb)
### Other Retrievers
......
......@@ -8,11 +8,11 @@ check-hidden = true
skip = "*.csv,*.html,*.json,*.jsonl,*.pdf,*.txt,*.ipynb"
[tool.llamahub]
contains_example = true
contains_example = false
import_path = "llama_index.retrievers.bedrock"
[tool.llamahub.class_authors]
BedrockRetriever = "AmazonBedrock"
AmazonKnowledgeBasesRetriever = "llama-index"
[tool.mypy]
disallow_untyped_defs = true
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment