Amazon Bedrock Runtime examples using SDK for JavaScript (v3) (original) (raw)
The primary execution of the scenario flow. This scenario orchestrates the conversation between the user, the Amazon Bedrock Converse API, and a weather tool.
/* Before running this JavaScript code example, set up your development environment, including your credentials.
This demo illustrates a tool use scenario using Amazon Bedrock's Converse API and a weather tool.
The script interacts with a foundation model on Amazon Bedrock to provide weather information based on user
input. It uses the Open-Meteo API (https://open-meteo.com) to retrieve current weather data for a given location.*/
import {
Scenario,
ScenarioAction,
ScenarioInput,
ScenarioOutput,
} from "@aws-doc-sdk-examples/lib/scenario/index.js";
import {
BedrockRuntimeClient,
ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
import { parseArgs } from "node:util";
import { fileURLToPath } from "node:url";
import { dirname } from "node:path";
const __filename = fileURLToPath(import.meta.url);
import data from "./questions.json" with { type: "json" };
import toolConfig from "./tool_config.json" with { type: "json" };
const systemPrompt = [
{
text:
"You are a weather assistant that provides current weather data for user-specified locations using only\n" +
"the Weather_Tool, which expects latitude and longitude. Infer the coordinates from the location yourself.\n" +
"If the user provides coordinates, infer the approximate location and refer to it in your response.\n" +
"To use the tool, you strictly apply the provided tool specification.\n" +
"If the user specifies a state, country, or region, infer the locations of cities within that state.\n" +
"\n" +
"- Explain your step-by-step process, and give brief updates before each step.\n" +
"- Only use the Weather_Tool for data. Never guess or make up information. \n" +
"- Repeat the tool use for subsequent requests if necessary.\n" +
"- If the tool errors, apologize, explain weather is unavailable, and suggest other options.\n" +
"- Report temperatures in °C (°F) and wind in km/h (mph). Keep weather reports concise. Sparingly use\n" +
" emojis where appropriate.\n" +
"- Only respond to weather queries. Remind off-topic users of your purpose. \n" +
"- Never claim to search online, access external data, or use tools besides Weather_Tool.\n" +
"- Complete the entire process until you have all required data before sending the complete response.",
},
];
const tools_config = toolConfig;
/// Starts the conversation with the user and handles the interaction with Bedrock.
async function askQuestion(userMessage) {
// The maximum number of recursive calls allowed in the tool use function.
// This helps prevent infinite loops and potential performance issues.
const max_recursions = 5;
const messages = [
{
role: "user",
content: [{ text: userMessage }],
},
];
try {
const response = await SendConversationtoBedrock(messages);
await ProcessModelResponseAsync(response, messages, max_recursions);
} catch (error) {
console.log("error ", error);
}
}
// Sends the conversation, the system prompt, and the tool spec to Amazon Bedrock, and returns the response.
// param "messages" - The conversation history including the next message to send.
// return - The response from Amazon Bedrock.
async function SendConversationtoBedrock(messages) {
const bedRockRuntimeClient = new BedrockRuntimeClient({
region: "us-east-1",
});
try {
const modelId = "amazon.nova-lite-v1:0";
const response = await bedRockRuntimeClient.send(
new ConverseCommand({
modelId: modelId,
messages: messages,
system: systemPrompt,
toolConfig: tools_config,
}),
);
return response;
} catch (caught) {
if (caught.name === "ModelNotReady") {
console.log(
"`${caught.name}` - Model not ready, please wait and try again.",
);
throw caught;
}
if (caught.name === "BedrockRuntimeException") {
console.log(
'`${caught.name}` - "Error occurred while sending Converse request.',
);
throw caught;
}
}
}
// Processes the response received via Amazon Bedrock and performs the necessary actions based on the stop reason.
// param "response" - The model's response returned via Amazon Bedrock.
// param "messages" - The conversation history.
// param "max_recursions" - The maximum number of recursive calls allowed.
async function ProcessModelResponseAsync(response, messages, max_recursions) {
if (max_recursions <= 0) {
await HandleToolUseAsync(response, messages);
}
if (response.stopReason === "tool_use") {
await HandleToolUseAsync(response, messages, max_recursions - 1);
}
if (response.stopReason === "end_turn") {
const messageToPrint = response.output.message.content[0].text;
console.log(messageToPrint.replace(/<[^>]+>/g, ""));
}
}
// Handles the tool use case by invoking the specified tool and sending the tool's response back to Bedrock.
// The tool response is appended to the conversation, and the conversation is sent back to Amazon Bedrock for further processing.
// param "response" - the model's response containing the tool use request.
// param "messages" - the conversation history.
// param "max_recursions" - The maximum number of recursive calls allowed.
async function HandleToolUseAsync(response, messages, max_recursions) {
const toolResultFinal = [];
try {
const output_message = response.output.message;
messages.push(output_message);
const toolRequests = output_message.content;
const toolMessage = toolRequests[0].text;
console.log(toolMessage.replace(/<[^>]+>/g, ""));
for (const toolRequest of toolRequests) {
if (Object.hasOwn(toolRequest, "toolUse")) {
const toolUse = toolRequest.toolUse;
const latitude = toolUse.input.latitude;
const longitude = toolUse.input.longitude;
const toolUseID = toolUse.toolUseId;
console.log(
`Requesting tool <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mrow><mi>t</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>U</mi><mi>s</mi><mi>e</mi><mi mathvariant="normal">.</mi><mi>n</mi><mi>a</mi><mi>m</mi><mi>e</mi></mrow><mo separator="true">,</mo><mi>T</mi><mi>o</mi><mi>o</mi><mi>l</mi><mi>u</mi><mi>s</mi><mi>e</mi><mi>i</mi><mi>d</mi></mrow><annotation encoding="application/x-tex">{toolUse.name}, Tool use id </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord"><span class="mord mathnormal">t</span><span class="mord mathnormal">oo</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal" style="margin-right:0.10903em;">U</span><span class="mord mathnormal">se</span><span class="mord">.</span><span class="mord mathnormal">nam</span><span class="mord mathnormal">e</span></span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">T</span><span class="mord mathnormal">oo</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">se</span><span class="mord mathnormal">i</span><span class="mord mathnormal">d</span></span></span></span>{toolUseID}`,
);
if (toolUse.name === "Weather_Tool") {
try {
const current_weather = await callWeatherTool(
longitude,
latitude,
).then((current_weather) => current_weather);
const currentWeather = current_weather;
const toolResult = {
toolResult: {
toolUseId: toolUseID,
content: [{ json: currentWeather }],
},
};
toolResultFinal.push(toolResult);
} catch (err) {
console.log("An error occurred. ", err);
}
}
}
}
const toolResultMessage = {
role: "user",
content: toolResultFinal,
};
messages.push(toolResultMessage);
// Send the conversation to Amazon Bedrock
await ProcessModelResponseAsync(
await SendConversationtoBedrock(messages),
messages,
);
} catch (error) {
console.log("An error occurred. ", error);
}
}
// Call the Weathertool.
// param = longitude of location
// param = latitude of location
async function callWeatherTool(longitude, latitude) {
// Open-Meteo API endpoint
const apiUrl = `https://api.open-meteo.com/v1/forecast?latitude=${latitude}&longitude=${longitude}¤t_weather=true`;
// Fetch the weather data.
return fetch(apiUrl)
.then((response) => {
return response.json().then((current_weather) => {
return current_weather;
});
})
.catch((error) => {
console.error("Error fetching weather data:", error);
});
}
/**
* Used repeatedly to have the user press enter.
* @type {ScenarioInput}
*/
const pressEnter = new ScenarioInput("continue", "Press Enter to continue", {
type: "input",
});
const greet = new ScenarioOutput(
"greet",
"Welcome to the Amazon Bedrock Tool Use demo! \n" +
"This assistant provides current weather information for user-specified locations. " +
"You can ask for weather details by providing the location name or coordinates." +
"Weather information will be provided using a custom Tool and open-meteo API." +
"For the purposes of this example, we'll use in order the questions in ./questions.json :\n" +
"What's the weather like in Seattle? " +
"What's the best kind of cat? " +
"Where is the warmest city in Washington State right now? " +
"What's the warmest city in California right now?\n" +
"To exit the program, simply type 'x' and press Enter.\n" +
"Have fun and experiment with the app by editing the questions in ./questions.json! " +
"P.S.: You're not limited to single locations, or even to using English! ",
{ header: true },
);
const displayAskQuestion1 = new ScenarioOutput(
"displayAskQuestion1",
"Press enter to ask question number 1 (default is 'What's the weather like in Seattle?')",
);
const askQuestion1 = new ScenarioAction(
"askQuestion1",
async (/** @type {State} */ state) => {
const userMessage1 = data.questions["question-1"];
await askQuestion(userMessage1);
},
);
const displayAskQuestion2 = new ScenarioOutput(
"displayAskQuestion2",
"Press enter to ask question number 2 (default is 'What's the best kind of cat?')",
);
const askQuestion2 = new ScenarioAction(
"askQuestion2",
async (/** @type {State} */ state) => {
const userMessage2 = data.questions["question-2"];
await askQuestion(userMessage2);
},
);
const displayAskQuestion3 = new ScenarioOutput(
"displayAskQuestion3",
"Press enter to ask question number 3 (default is 'Where is the warmest city in Washington State right now?')",
);
const askQuestion3 = new ScenarioAction(
"askQuestion3",
async (/** @type {State} */ state) => {
const userMessage3 = data.questions["question-3"];
await askQuestion(userMessage3);
},
);
const displayAskQuestion4 = new ScenarioOutput(
"displayAskQuestion4",
"Press enter to ask question number 4 (default is 'What's the warmest city in California right now?')",
);
const askQuestion4 = new ScenarioAction(
"askQuestion4",
async (/** @type {State} */ state) => {
const userMessage4 = data.questions["question-4"];
await askQuestion(userMessage4);
},
);
const goodbye = new ScenarioOutput(
"goodbye",
"Thank you for checking out the Amazon Bedrock Tool Use demo. We hope you\n" +
"learned something new, or got some inspiration for your own apps today!\n" +
"For more Bedrock examples in different programming languages, have a look at:\n" +
"https://docs.aws.amazon.com/bedrock/latest/userguide/service_code_examples.html",
);
const myScenario = new Scenario("Converse Tool Scenario", [
greet,
pressEnter,
displayAskQuestion1,
askQuestion1,
pressEnter,
displayAskQuestion2,
askQuestion2,
pressEnter,
displayAskQuestion3,
askQuestion3,
pressEnter,
displayAskQuestion4,
askQuestion4,
pressEnter,
goodbye,
]);
/** @type {{ stepHandlerOptions: StepHandlerOptions }} */
export const main = async (stepHandlerOptions) => {
await myScenario.run(stepHandlerOptions);
};
// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const { values } = parseArgs({
options: {
yes: {
type: "boolean",
short: "y",
},
},
});
main({ confirmAll: values.yes });
}
- For API details, seeConverse in AWS SDK for JavaScript API Reference.