Conversational AI: Add chatting capabilities to your app
Discover how to implement multi-turn conversations and maintain context using Amazon Bedrock's Converse API and the AWS SDK for JavaScript.
- In Part 1: Getting Started, you learned how to send your first request.
- In Part 2: Conversational AI (this post), I'll show you how to implement conversational turns.
- In Part 3: Customizing AI Behavior, we'll configure the model with a system prompt and additional inference parameters.
- 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
1
npm install @aws-sdk/client-bedrock-runtime
1
2
3
4
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
BedrockRuntimeClient
, specifying the AWS region where the model is available:1
const client = new BedrockRuntimeClient({ region: "us-east-1" });
1
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";
1
2
3
4
5
6
7
const firstUserMessage = "What is the capital of Australia?";
const conversation = [
{
role: "user",
content: [{ text: firstUserMessage }]
}
];
ConverseCommand
:1
2
3
const firstResponse = await client.send(
new ConverseCommand({ modelId, messages: conversation })
);
1
2
const firstResponseText = firstResponse.output.message.content[0].text;
console.log(`First response: ${firstResponseText}`);
1
conversation.push(firstResponse.output.message);
1
2
3
4
5
const secondUserMessage = "What was my first question?";
conversation.push({
role: "user",
content: [{ text: secondUserMessage }]
});
1
2
3
const secondResponse = await client.send(
new ConverseCommand({ modelId, messages: conversation })
);
1
2
const secondResponseText = secondResponse.output.message.content[0].text;
console.log(`Second response: ${secondResponseText}`);
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
36
37
38
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 firstUserMessage = "What is the capital of Australia?";
const conversation = [
{
role: "user",
content: [{ text: firstUserMessage }],
},
];
const firstResponse = await client.send(
new ConverseCommand({ modelId, messages: conversation }),
);
const firstResponseText = firstResponse.output.message.content[0].text;
console.log(`First response: ${firstResponseText}`);
conversation.push(firstResponse.output.message);
const secondUserMessage = "What was my first question?";
conversation.push({
role: "user",
content: [{ text: secondUserMessage }],
});
const secondResponse = await client.send(
new ConverseCommand({ modelId, messages: conversation }),
);
const secondResponseText = secondResponse.output.message.content[0].text;
console.log(`Second response: ${secondResponseText}`);
- Save the code in a file named
bedrock_conversation.js
- In your terminal, navigate to the directory containing
bedrock_conversation.js
- Run the following command:
1
node bedrock_conversation.js
1
2
First response: The capital of Australia is Canberra.
Second response: Your first question was "What is the capital of Australia?".
- Configured your AWS credentials
- Requested access to the model you are using
- Installed the required dependencies
- Write a chat loop that repeatedly prompts the user for input and prints the model's responses.
- 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.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.