Get startedGet started for free

Adjusting creativity with temperature control

You work for RoboChef Labs, a startup that builds AI-powered cooking robots. The team is experimenting with Amazon Bedrock to generate promotional stories, user guides, and product-marketing copy.

To start with, they need short stories to promote their cooking-robot line. Use the temperature parameter to tune Claude’s creativity and generate different versions.

In this exercise, the boto3 and json libraries, and the bedrock client, have been pre-imported.

This exercise is part of the course

Introduction to Amazon Bedrock

View Course

Exercise instructions

  • Generate two robot stories using a low temperature for the first output and a high temperature for the second output and observe how the outputs differ.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

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='anthropic.claude-3-5-sonnet-20240620-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)
Edit and Run Code