Use InvokeFlow with an AWS SDK - Amazon Bedrock

Use InvokeFlow with an AWS SDK

The following code example shows how to use InvokeFlow.

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

For a complete list of AWS SDK developer guides and code examples, see Using Amazon Bedrock with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.