Use System Prompts with Anthropic Claude on Amazon Bedrock
System prompts are a great way to predefine how a generative AI model should respond to subsequent user input. In this post you'll learn what they are and how you can use them for your own applications with Anthropic Claude 2.x and 3.
Dennis Traub
Amazon Employee
Published Mar 6, 2024
Last Modified Mar 20, 2024
Did you know that you can use system prompts when interacting with Anthropic's Claude models on Amazon Bedrock?
In this post I will briefly explain what system prompts are, before showing you how to use them with the Text Completions API for Claude 2.x and the new Messages API for Claude 3.
⚠ Warning: This post contains code!
Speaking of which, if you already know about system prompts and Claude's two types of APIs, and want to get right to the code, I've added two fully functional examples at the bottom of this post!
Since we're talking about Claude today, let's start by having a look at what Anthropic has to say:
A system prompt is a way to provide context, instructions, and guidelines to Claude before presenting it with a question or task. By using a system prompt, you can set the stage for the conversation, specifying Claude's role, personality, tone, or any other relevant information that will help it better understand and respond to the user's input.
In other words, a system prompts is a form of in-context learning, providing an effective way to pre-define the context, scope, guardrails, or output format for the model to use during an interaction.
System prompts try to ensure that the AI's output aligns with specific goals or tasks across various domains. Some typical use cases include:
- Pizza Order Processing: Reduce the scope of the model to focus on taking orders based on the pizza service's menu and location.
- Technical Support Troubleshooting: Inform the model about product details, FAQs, and decision trees to help users solve technical issues with a product.
- Code Debugging: Inject information about libraries, frameworks, and programming language versions to identify and suggest fixes for software bugs.
Now that we know about system prompts, let's have a look at the two different APIs provided by the different versions of Claude on Amazon Bedrock:
- The text completion API, used by Claude versions 1 and 2.x.
- The messages API introduced by the new Claude version 3.
With the release of Amazon Bedrock, AWS customers have gained access to Anthropic's Claude versions 1 and 2, followed by the release of Claude 2.1 during re:Invent 2023. All Claude models up to 2.1 offer a text completions API, which optimize for single-turn text generation based on a user-provided prompt with the following template:
Note: You can find fully functional code examples at the end of this post.
While I ran this prompt with Claude v2.1, I received the following completion:
The young boy wandered into the dark, mysterious forest, hoping to find the rare flower his mother needed to recover from her illness, but instead encountered a wise old owl who offered him cryptic advice about believing in himself.
To add a system prompt, all you need to do is add it to the beginning of the prompt. Let's try:
Sending this prompt to Claude 2.1 created the following completion:
Yarrr, 'twas a dark 'n stormy night when Blackbeard 'n his scurvy crew set sail on the seven seas, plunderin' merchant ships fer pieces of eight 'n fine silks, before returnin' to Tortuga fer a night of rum-filled debauchery!
With the addition of Claude 3 Sonnet in March 2024, Amazon Bedrock introduced a messages API, optimized for conversational exchanges, e.g. for chat bots or virtual assistants, and multimodal requests, e.g. sending an image along with a text prompt to ask questions about the image.
Claude 3 has been trained to operate on alternating conversational turns between user and assistant. When creating a new message, you specify the prior conversational turns with the messages parameter and the model will generate the next message in the conversation.
Each input message must be an object with a role and content. You can specify a single user-role message, or you can include multiple user and assistant messages.
Note: You can find fully functional code examples at the end of this post.
Here's the example from above, wrapped in a request object for the messages API:
As you can see, there's only one message, it's role is the user, and Claude will return a new message containing the response:
To add a system prompt, all you have to do is add the
"system"
parameter to the request object like this:Which produces the following result:
And that's it!
If you enjoyed this post or learned something new, please hit the like button or let me know what you think in the comments below.
If you want to learn more, have a look at the following resources:
- Amazon Bedrock code examples - Our constantly growing list of examples across models and programming languages.
- The inference parameter reference for Claude and all other models on Amazon Bedrock.
- And, of course, the Generative AI Space here on community.aws with a curated list of articles all around Amazon Bedrock and Generative AI.
If you're anything like me, you probably want to go ahead and experiment with it yourself. So, as promised, here are two fully functional scripts in Python for both APIs. Happy coding!
Have fun!
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.