Enhancing prompts with role assignment
In this exercise, you'll improve a basic question about Amazon Bedrock by assigning Claude the role of an AWS expert. You'll learn to enhance your prompt and handle the model's response to ensure the output is correctly formatted and error-free.
The json
library and bedrock
client have been pre-imported.
This exercise is part of the course
Introduction to Amazon Bedrock
Exercise instructions
- Specify the role of an AWS expert as part of the prompt.
- Parse the response body to extract the output.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Format the prompt with role and required Claude format
prompt = '____'
request_body = json.dumps({"anthropic_version": "bedrock-2023-05-31", "max_tokens": 100,
"messages": [{"role": "user", "content": prompt}]})
response = bedrock.invoke_model(modelId='anthropic.claude-3-5-sonnet-20240620-v1:0',
body=request_body)
# Extract completion from response
output = json.loads(response['body'].____)['content'][0]['text'] # Parse JSON and get completion
print(output)