使用 SDK for JavaScript (v3) 的 Amazon Bedrock Agents 執行期範例 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API參考指南詳細描述 AWS SDK for JavaScript 第 3 版 (V3) 的所有API操作。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 SDK for JavaScript (v3) 的 Amazon Bedrock Agents 執行期範例

下列程式碼範例示範如何使用 AWS SDK for JavaScript (v3) 搭配 Amazon Bedrock Agents Runtime 來執行動作和實作常見案例。

Actions 是大型程式的程式碼摘錄,必須在內容中執行。雖然動作會示範如何呼叫個別服務函數,但您可以在相關案例中查看內容中的動作。

每個範例都包含完整原始程式碼的連結,您可以在其中找到如何在內容中設定和執行程式碼的指示。

主題

動作

下列程式碼範例示範如何使用 InvokeAgent

SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

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); }
  • 如需API詳細資訊,請參閱 參考 InvokeAgent中的 。 AWS SDK for JavaScript API

下列程式碼範例示範如何使用 InvokeFlow

SDK 適用於 JavaScript (v3)
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 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")); } }
  • 如需API詳細資訊,請參閱 參考 InvokeFlow中的 。 AWS SDK for JavaScript API