Since you've already synced your AWS Knowledge Base with Bedrock, you're ready to query it using an Amazon Bedrock Runtime API with a RAG (Retrieval-Augmented Generation) setup. Here's how you can get started programmatically:
Make sure you're using AWS SDK v3 for JavaScript, or boto3 if using Python
Configure IAM credentials with access to bedrock:InvokeModelWithResponseStream
RetrieveAndGenerate
APIHere’s an example in Python using boto3
:
Replace YOUR_KB_ID
with the actual Knowledge Base ID
Replace modelArn
with the model you want to use (e.g., Claude 3, Titan, etc.)
import boto3
bedrock_agent_runtime = boto3.client('bedrock-agent-runtime')
response = bedrock_agent_runtime.retrieve_and_generate(
input={
"text": "What are the benefits of using Amazon SageMaker?"
},
retrieveAndGenerateConfiguration={
"knowledgeBaseConfiguration": {
"knowledgeBaseId": "YOUR_KB_ID"
},
"modelArn": "arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-sonnet-20240229-v1:0"
}
)
print(response['output']['text'])
More details and examples on Cloudoku.training here - https://cloudoku.training/blog/aws-knowledge-base-with-bedrock
Good Luck! let me know how it goes.