logo
Menu
Customizing AI Behavior: System prompts and inference parameters in Bedrock's Converse API

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.

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:
  • 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:

Step 2: Import required modules

In a new JavaScript file, import the required modules:

Step 3: Create a BedrockRuntimeClient instance

Create an instance of the BedrockRuntimeClient, specifying the AWS region where the model is available:

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:
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:
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:
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.

Step 7: Send the request

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:

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:
To run it:
  1. Save the code in a file named bedrock_customized.js
  2. In your terminal, navigate to the directory containing bedrock_customized.js
  3. 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:

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:
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.

1 Comment