There are more AWS SDK examples available in the AWS Doc SDK Examples
Amazon Bedrock Agents Runtime examples using SDK for JavaScript (v3)
The following code examples show you how to perform actions and implement common scenarios by using the AWS SDK for JavaScript (v3) with Amazon Bedrock Agents Runtime.
Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.
Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.
Topics
Actions
The following code example shows how to use InvokeAgent
.
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import { BedrockAgentRuntimeClient, InvokeAgentCommand, } from "@aws-sdk/client-bedrock-agent-runtime"; /** * @typedef {Object} ResponseBody * @property {string} completion */ /** * Invokes a Bedrock agent to run an inference using the input * provided in the request body. * * @param {string} prompt - The prompt that you want the Agent to complete. * @param {string} sessionId - An arbitrary identifier for the session. */ export const invokeBedrockAgent = async (prompt, sessionId) => { const client = new BedrockAgentRuntimeClient({ region: "us-east-1" }); // const client = new BedrockAgentRuntimeClient({ // region: "us-east-1", // credentials: { // accessKeyId: "accessKeyId", // permission to invoke agent // secretAccessKey: "accessKeySecret", // }, // }); const agentId = "AJBHXXILZN"; const agentAliasId = "AVKP1ITZAA"; const command = new InvokeAgentCommand({ agentId, agentAliasId, sessionId, inputText: prompt, }); try { let completion = ""; const response = await client.send(command); if (response.completion === undefined) { throw new Error("Completion is undefined"); } for await (const chunkEvent of response.completion) { const chunk = chunkEvent.chunk; console.log(chunk); const decodedResponse = new TextDecoder("utf-8").decode(chunk.bytes); completion += decodedResponse; } return { sessionId: sessionId, completion }; } catch (err) { console.error(err); } }; // Call function if run directly import { fileURLToPath } from "node:url"; if (process.argv[1] === fileURLToPath(import.meta.url)) { const result = await invokeBedrockAgent("I need help.", "123"); console.log(result); }
-
For API details, see InvokeAgent in AWS SDK for JavaScript API Reference.
-
The following code example shows how to use InvokeFlow
.
- SDK for JavaScript (v3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. import { fileURLToPath } from "node:url"; import { BedrockAgentRuntimeClient, InvokeFlowCommand, } from "@aws-sdk/client-bedrock-agent-runtime"; /** * Invokes an alias of a flow to run the inputs that you specify and return * the output of each node as a stream. * * @param {{ * flowIdentifier: string, * flowAliasIdentifier: string, * prompt?: string, * region?: string * }} options * @returns {Promise<import("@aws-sdk/client-bedrock-agent").FlowNodeOutput>} An object containing information about the output from flow invocation. */ export const invokeBedrockFlow = async ({ flowIdentifier, flowAliasIdentifier, prompt = "Hi, how are you?", region = "us-east-1", }) => { const client = new BedrockAgentRuntimeClient({ region }); const command = new InvokeFlowCommand({ flowIdentifier, flowAliasIdentifier, inputs: [ { content: { document: prompt, }, nodeName: "FlowInputNode", nodeOutputName: "document", }, ], }); let flowResponse = {}; const response = await client.send(command); for await (const chunkEvent of response.responseStream) { const { flowOutputEvent, flowCompletionEvent } = chunkEvent; if (flowOutputEvent) { flowResponse = { ...flowResponse, ...flowOutputEvent }; console.log("Flow output event:", flowOutputEvent); } else if (flowCompletionEvent) { flowResponse = { ...flowResponse, ...flowCompletionEvent }; console.log("Flow completion event:", flowCompletionEvent); } } return flowResponse; }; // Call function if run directly import { parseArgs } from "node:util"; import { isMain, validateArgs, } from "@aws-doc-sdk-examples/lib/utils/util-node.js"; const loadArgs = () => { const options = { flowIdentifier: { type: "string", required: true, }, flowAliasIdentifier: { type: "string", required: true, }, prompt: { type: "string", }, region: { type: "string", }, }; const results = parseArgs({ options }); const { errors } = validateArgs({ options }, results); return { errors, results }; }; if (isMain(import.meta.url)) { const { errors, results } = loadArgs(); if (!errors) { invokeBedrockFlow(results.values); } else { console.error(errors.join("\n")); } }
-
For API details, see InvokeFlow in AWS SDK for JavaScript API Reference.
-