Skip to content

Commit 83a5789

Browse files
danielgolubDennisTraub
authored andcommitted
JavaScript (v3): BedrockAgentRuntime invokeFlow example added.
1 parent 51d9d0f commit 83a5789

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

.doc_gen/metadata/bedrock-agent-runtime_metadata.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ bedrock-agent-runtime_InvokeAgent:
1818
- description:
1919
snippet_files:
2020
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-agent.js
21+
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js
2122
services:
2223
bedrock-agent-runtime: {InvokeAgent}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import {
5+
BedrockAgentRuntimeClient,
6+
InvokeFlowCommand,
7+
} from "@aws-sdk/client-bedrock-agent-runtime";
8+
9+
/**
10+
* @typedef {Object} ResponseBody
11+
* @returns {Object} flowResponse
12+
*/
13+
14+
/**
15+
* Invokes a Bedrock flow to run an inference using the input
16+
* provided in the request body.
17+
*
18+
* @param {string} prompt - The prompt that you want the Agent to complete.
19+
*/
20+
export const invokeBedrockFlow = async (prompt) => {
21+
const client = new BedrockAgentRuntimeClient({ region: "us-east-1" });
22+
// const client = new BedrockAgentRuntimeClient({
23+
// region: "us-east-1",
24+
// credentials: {
25+
// accessKeyId: "accessKeyId", // permission to invoke flow
26+
// secretAccessKey: "accessKeySecret",
27+
// },
28+
// });
29+
30+
const flowIdentifier = "AJBHXXILZN";
31+
const flowAliasIdentifier = "AVKP1ITZAA";
32+
33+
const command = new InvokeFlowCommand({
34+
flowIdentifier,
35+
flowAliasIdentifier,
36+
inputs: [
37+
{
38+
content: {
39+
document: prompt,
40+
},
41+
nodeName: "FlowInputNode",
42+
nodeOutputName: "document",
43+
}
44+
]
45+
});
46+
47+
try {
48+
let flowResponse = {};
49+
const response = await client.send(command);
50+
51+
if (response.responseStream === undefined) {
52+
throw new Error("responseStream is undefined");
53+
}
54+
55+
for await (let chunkEvent of response.responseStream) {
56+
const { flowOutputEvent } = chunkEvent;
57+
flowResponse = { ...flowResponse, ...flowOutputEvent };
58+
console.log(flowOutputEvent);
59+
}
60+
61+
return flowResponse;
62+
} catch (err) {
63+
console.error(err);
64+
}
65+
};
66+
67+
// Call function if run directly
68+
import { fileURLToPath } from "url";
69+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
70+
const result = await invokeBedrockFlow("I need help.");
71+
console.log(result);
72+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { describe, it, expect, vi } from "vitest";
5+
import { invokeBedrockFlow } from "../actions/invoke-flow.js";
6+
7+
const mocks = vi.hoisted(() => ({
8+
clientSendResolve: () =>
9+
Promise.resolve({
10+
responseStream: [
11+
{
12+
flowOutputEvent: {
13+
content: {
14+
document: 'Test prompt',
15+
},
16+
},
17+
},
18+
],
19+
}),
20+
clientSendReject: () => Promise.reject(new Error("Mocked error")),
21+
send: vi.fn(),
22+
}));
23+
24+
vi.mock("@aws-sdk/client-bedrock-agent-runtime", () => {
25+
return {
26+
BedrockAgentRuntimeClient: vi.fn(() => ({ send: mocks.send })),
27+
InvokeFlowCommand: vi.fn(),
28+
};
29+
});
30+
31+
describe("invokeBedrockFlow", () => {
32+
it("should return an object with responseStream", async () => {
33+
const prompt = "Test prompt";
34+
mocks.send.mockImplementationOnce(mocks.clientSendResolve);
35+
36+
const result = await invokeBedrockFlow(prompt);
37+
38+
expect(result).toEqual({
39+
content: {
40+
document: prompt,
41+
},
42+
});
43+
});
44+
45+
it("should log errors", async () => {
46+
mocks.send.mockImplementationOnce(mocks.clientSendReject);
47+
const spy = vi.spyOn(console, "error");
48+
const prompt = "Test prompt";
49+
50+
await invokeBedrockFlow(prompt);
51+
52+
expect(spy).toHaveBeenCalledWith(
53+
expect.objectContaining({ message: "Mocked error" }),
54+
);
55+
});
56+
});

0 commit comments

Comments
 (0)