
Customizing AI Behavior: System prompts and inference parameters in Bedrock's Converse API
In this tutorial, you'll learn how to configure a generative AI model with a system prompt and additional inference parameters, using the Bedrock Converse API and the AWS SDK for JavaScript.
Series: Streamlining AI Development: A Practical Guide to Bedrock's Unified Messages API (3 articles)
- 3Customizing AI Behavior: System prompts and inference parameters in Bedrock's Converse API This article
[ Jump directly to the code 💻 or switch to the Java version ]
Welcome to Part 3 of my tutorial series on Amazon Bedrock's Converse API!
In previous parts, we covered the basics of sending requests to the API and implementing conversational turns. If you haven't gone through those tutorials yet, I recommend starting with Part 1 to get a solid foundation.
In this tutorial, we'll explore how to customize the behavior of AI models using system prompts and inference parameters.
Note: The examples in this edition use JavaScript. I've also prepared a Java edition , and many more examples in Python, C#, etc.
Series overview
This series guides you through Amazon Bedrock's Converse API:
- In Part 1: Getting Started , you learned how to send your first request.
- In Part 2: Conversational AI , I'll show you how to implement conversational turns.
- In Part 3: Customizing AI Behavior (this post), we'll configure the model with a system prompt and additional inference parameters.
Future posts will cover extracting invocation metrics and metadata, sending and receiving model-specific parameters and fields, processing model responses in real-time, the new tool-use feature, and more.
Let's dive in and start building! 💻
Step-by-step: Customize the AI response with system prompts and inference parameters
Prerequisites
Before you begin, ensure all prerequisites are in place. You should have:
- An AWS account
- The AWS CLI installed and configured with your credentials
- The latest stable version of Node.js and npm installed
- Requested access to the model you want to use
Step 1: Install the Bedrock Runtime client
First, install the Bedrock Runtime client from the AWS SDK for JavaScript v3:
1
npm install @aws-sdk/client-bedrock-runtimeStep 2: Import required modules
In a new JavaScript file, import the required modules:
1
2
3
4
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";Step 3: Create a BedrockRuntimeClient instance
Create an instance of the
BedrockRuntimeClient, specifying the AWS region where the model is available :1
const client = new BedrockRuntimeClient({ region: "us-east-1" });Step 4: Specify the model ID
Specify the ID of the model you want to use. In this example, we'll use Claude 3 Haiku:
1
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";You can find the complete list of models supporting the Converse API and a list of all available model IDs in the documentation.
Step 5: Prepare a message to send
Prepare the first user message and add it to a new conversation array:
1
2
3
4
5
6
7
const userMessage = "What is 'rubber duck debugging'";
const conversation = [
{
role: "user",
content: [{ text: userMessage }]
}
];The conversation array represents the conversation history, with each element containing a role (e.g., "user" or "assistant") and at least one content block with the message text.
Step 6: Define a system prompt and additional inference parameters
A system prompt is a special type of message that provides additional context or instructions to the AI model. Unlike user messages, which represent the user's input, a system prompt is used to guide the model's behavior and set expectations for its responses.
In this example, we'll use a system prompt to instruct the model to respond in rhymes:
1
const systemPrompt = [{ text: "You must always respond in rhymes." }];In addition to the system prompt, we can also specify inference parameters to further customize the model's behavior. Inference parameters allow us to control various aspects of the generated response, such as its length and randomness.
In this example, we will configure the following additional parameters:
1
2
3
4
const additionalParameters = {
maxTokens: 100,
temperature: 0.5
};These parameters will limit the response to a maximum of 100 tokens and maintain a balance between creativity and coherence with a temperature of 0.5.
By combining the system prompt and inference parameters, you can fine-tune the model's behavior to suit your specific use case. Experiment with different values to observe their impact on the generated responses.
Step 7: Send the request
Send the conversation, the system prompt, and the additional parameters to the model using a
ConverseCommand:1
2
3
4
5
6
7
8
const response = await client.send(
new ConverseCommand({
modelId,
messages: conversation,
system: systemPrompt,
inferenceConfig: additionalParameters
})
);The ConverseCommand sends the conversation to the specified model and returns its response.
Print out the model's response:
1
2
const responseText = response.output.message.content[0].text;
console.log(responseText);Let's run the program
With the code complete, let's run it and see the AI engage in a multi-turn conversation!
Here's the full code for reference:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
const client = new BedrockRuntimeClient({ region: "us-east-1" });
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";
const userMessage = "What is 'rubber duck debugging'";
const conversation = [
{
role: "user",
content: [{ text: userMessage }]
}
];
const systemPrompt = [{ text: "You must always respond in rhymes." }];
const additionalParameters = {
maxTokens: 100,
temperature: 0.5
};
const response = await client.send(
new ConverseCommand({
modelId,
messages: conversation,
system: systemPrompt,
inferenceConfig: additionalParameters
})
);
const responseText = response.output.message.content[0].text;
console.log(responseText);
To run it:
- Save the code in a file named
bedrock_customized.js - In your terminal, navigate to the directory containing
bedrock_customized.js - Run the following command:
1
node bedrock_customized.jsIf everything is set up correctly, you should see the model's response printed in the console:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Rubber duck debugging, a clever way,
To solve problems without delay.
You talk to a duck, real or toy,
And the solution you will enjoy.
By explaining the issue out loud,
Your mind becomes less clouded.
The duck listens, never judges,
And the answer often just nudges.
It's a technique that's quite profound,
When you're stuck, it helps you rebound.
So next time you're in a bind,
Grab a duck and let your thoughts unwind.If you encounter any errors, double check that you have:
- Configured your AWS credentials
- Requested access to the model you are using
- Installed the required dependencies
- Specified the correct region and model ID
Next steps
Congratulations on influencing the model's responses with a system prompt and additional inference parameters.
Ready for more? Here are some ideas to keep exploring:
- Experiment with different models and compare their responses. Here's the list of all models supporting the Converse API.
- Challenge yourself to rewrite this program in another language. Here are examples in Python, Java, C#, and more .
- Dive deeper into the Converse API in the Amazon Bedrock User Guide .
In future posts you will learn how to extract invocation metrics and metadata from the model response, how to send and receive model-specific parameters and fields, how to retrieve, process, and print the model response in real-time, and a lot more.
I'd love to see what you build with Amazon Bedrock! Feel free to share your projects or ask questions in the comments.
Thanks for following along and happy building! 💻🤖
Series: Streamlining AI Development: A Practical Guide to Bedrock's Unified Messages API (3 articles)
- 3Customizing AI Behavior: System prompts and inference parameters in Bedrock's Converse API This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article