| Thông tin | Chi tiết |
|---|---|
| Mục tiêu | Invoke FM qua Bedrock Console, AWS CLI, và Python SDK |
| Thời gian | ~15 phút |
| Domain liên quan | Domain 1 (Task 1.2), Domain 2 (Task 2.4) |
| Prerequisites | AWS Account, Bedrock model access enabled |
Sau lab này, bạn sẽ biết cách:
Giải thích Retrieval Augmented Generation (RAG) trong 3 câu.
aws bedrock-runtime invoke-model \
--model-id anthropic.claude-3-5-sonnet-20241022-v2:0 \
--region us-east-1 \
--content-type application/json \
--accept application/json \
--body '{
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 256,
"messages": [
{"role": "user", "content": "What is Amazon Bedrock? Answer in 2 sentences."}
]
}' \
output.json
type output.json
Tạo file invoke_bedrock.py:
import boto3
import json
# Tạo Bedrock Runtime client
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Invoke model
response = client.invoke_model(
modelId='anthropic.claude-3-5-sonnet-20241022-v2:0',
contentType='application/json',
accept='application/json',
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 256,
"temperature": 0.7,
"messages": [
{"role": "user", "content": "Explain RAG in 3 sentences."}
]
})
)
# Parse response
result = json.loads(response['body'].read())
print(result['content'][0]['text'])
Chạy:
python invoke_bedrock.py
import boto3
import json
client = boto3.client('bedrock-runtime', region_name='us-east-1')
# Invoke with streaming
response = client.invoke_model_with_response_stream(
modelId='anthropic.claude-3-5-sonnet-20241022-v2:0',
contentType='application/json',
body=json.dumps({
"anthropic_version": "bedrock-2023-05-31",
"max_tokens": 512,
"messages": [
{"role": "user", "content": "List 5 benefits of using Amazon Bedrock."}
]
})
)
# Stream response
for event in response['body']:
chunk = json.loads(event['chunk']['bytes'])
if chunk['type'] == 'content_block_delta':
print(chunk['delta'].get('text', ''), end='', flush=True)
print()
output.jsonExam Tip: Hiểu sự khác biệt giữa invoke_model (synchronous) và invoke_model_with_response_stream (streaming). Streaming là best practice cho user-facing applications.