![Customizing AI Behavior: System prompts and inference parameters in Bedrock's Converse API](https://assets.community.aws/a/2i3piRkyS3ynODuM2RTH6D3JO8m/imag.webp)
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.
Dennis Traub
Amazon Employee
Published May 30, 2024
Last Modified Jun 27, 2024
[ 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.
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! 💻
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
First, install the Bedrock Runtime client from the AWS SDK for JavaScript v3:
In a new JavaScript file, import the required modules:
Create an instance of the
BedrockRuntimeClient
, specifying the AWS region where the model is available:Specify the ID of the model you want to use. In this example, we'll use Claude 3 Haiku:
You can find the complete list of models supporting the Converse API and a list of all available model IDs in the documentation.
Prepare the first user message and add it to a new conversation array:
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.
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:
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:
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.
Send the conversation, the system prompt, and the additional parameters to the model using a
ConverseCommand
:The ConverseCommand sends the conversation to the specified model and returns its response.
Print out the model's response:
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:
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:
If everything is set up correctly, you should see the model's response printed in the console:
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
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! 💻🤖
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.