Điều chỉnh mức sáng tạo bằng tham số temperature
Bạn đang làm việc tại RoboChef Labs, một startup xây dựng robot nấu ăn dùng AI. Nhóm đang thử nghiệm Amazon Bedrock để tạo truyện quảng bá, hướng dẫn sử dụng, và nội dung tiếp thị sản phẩm.
Đầu tiên, họ cần các truyện ngắn để quảng bá dòng robot nấu ăn. Hãy dùng tham số temperature để điều chỉnh mức sáng tạo của Claude và tạo ra các phiên bản khác nhau.
Trong bài tập này, các thư viện boto3 và json, cùng với client bedrock, đã được nhập sẵn.
Bài tập này là một phần của khóa học
Giới thiệu về Amazon Bedrock
Hướng dẫn bài tập
- Tạo hai câu chuyện về robot: dùng nhiệt độ thấp cho đầu ra thứ nhất và nhiệt độ cao cho đầu ra thứ hai, rồi quan sát sự khác biệt giữa các kết quả.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
def generate_story_with_temperature(bedrock, temperature):
messages = [{"role": "user",
"content": "Write a short story about a cooking robot teaching other robots to cook."}]
request_body=json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
"temperature": temperature, "messages": messages})
response = bedrock.invoke_model(body=request_body, modelId='us.anthropic.claude-sonnet-4-5-20250929-v1:0')
response_body = json.loads(response.get('body').read().decode())
return response_body["content"][0]["text"]
# Test low and high temperature
low_temp = generate_story_with_temperature(bedrock, ____)
high_temp = generate_story_with_temperature(bedrock, ____)
print("Low temperature (more focused):", low_temp, "High temperature (more creative):", high_temp)