-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathconverse-tool-scenario.js
More file actions
314 lines (292 loc) · 11.3 KB
/
converse-tool-scenario.js
File metadata and controls
314 lines (292 loc) · 11.3 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[Bedrock.ConverseTool.javascriptv3.Scenario]
/* 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 ${toolUse.name}, Tool use id ${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 });
}
// snippet-end:[Bedrock.ConverseTool.javascriptv3.Scenario]