Gunakan InvokeFlow dengan AWS SDK - AWS SDKContoh Kode

Ada lebih banyak AWS SDK contoh yang tersedia di GitHub repo SDKContoh AWS Dokumen.

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Gunakan InvokeFlow dengan AWS SDK

Contoh kode berikut menunjukkan cara menggunakanInvokeFlow.

JavaScript
SDKuntuk JavaScript (v3)
catatan

Ada lebih banyak tentang GitHub. Temukan contoh lengkapnya dan pelajari cara pengaturan dan menjalankannya di Repositori Contoh Kode AWS.

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")); } }
  • Untuk API detailnya, lihat InvokeFlowdi AWS SDK for JavaScript APIReferensi.