| Thông tin | Chi tiết |
|---|---|
| Mục tiêu | Tạo Knowledge Base, upload documents, query với RAG |
| Thời gian | ~30 phút |
| Domain liên quan | Domain 1 (Task 1.4, 1.5) |
| Prerequisites | AWS Account, Bedrock model access, S3 bucket |
# Tạo S3 bucket
aws s3 mb s3://workshop-rag-kb-$(aws sts get-caller-identity --query Account --output text) --region us-east-1
# Tạo sample document
echo "Amazon Bedrock is a fully managed service that offers a choice of high-performing foundation models from leading AI companies. It provides a single API to access models from Anthropic, Meta, Amazon, Cohere, and Mistral. Bedrock supports features like Knowledge Bases for RAG, Guardrails for content safety, and Agents for autonomous workflows." > bedrock-overview.txt
echo "Retrieval Augmented Generation (RAG) is a technique that enhances LLM responses by retrieving relevant information from external data sources. RAG reduces hallucination by grounding responses in factual data. Amazon Bedrock Knowledge Bases provides a managed RAG solution with automatic chunking, embedding, and vector storage." > rag-overview.txt
# Upload documents
aws s3 cp bedrock-overview.txt s3://workshop-rag-kb-$(aws sts get-caller-identity --query Account --output text)/
aws s3 cp rag-overview.txt s3://workshop-rag-kb-$(aws sts get-caller-identity --query Account --output text)/
workshop-knowledge-bases3://workshop-rag-kb-<account-id>/Knowledge Base sẽ tự động: chunking documents → generate embeddings → store trong vector database. Quá trình sync có thể mất vài phút.
Qua Console:
Qua Python SDK:
import boto3
import json
client = boto3.client('bedrock-agent-runtime', region_name='us-east-1')
response = client.retrieve_and_generate(
input={'text': 'What is RAG and how does Bedrock support it?'},
retrieveAndGenerateConfiguration={
'type': 'KNOWLEDGE_BASE',
'knowledgeBaseConfiguration': {
'knowledgeBaseId': '<YOUR_KB_ID>',
'modelArn': 'arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-3-5-sonnet-20241022-v2:0'
}
}
)
print("Response:", response['output']['text'])
print("\nCitations:")
for citation in response.get('citations', []):
for ref in citation.get('retrievedReferences', []):
print(f" - {ref['location']['s3Location']['uri']}")
Exam Tip: Hiểu flow: Documents → Chunking → Embeddings → Vector Store → Query → Retrieve → Augment Prompt → Generate Response. Đây là core RAG pipeline.