Skip to content

Commit

Permalink
JavaScript (v3): BedrockAgentRuntime invokeFlow example added.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgolub authored and DennisTraub committed Oct 20, 2024
1 parent 51d9d0f commit 83a5789
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
1 change: 1 addition & 0 deletions .doc_gen/metadata/bedrock-agent-runtime_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ bedrock-agent-runtime_InvokeAgent:
- description:
snippet_files:
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-agent.js
- javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js
services:
bedrock-agent-runtime: {InvokeAgent}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import {
BedrockAgentRuntimeClient,
InvokeFlowCommand,
} from "@aws-sdk/client-bedrock-agent-runtime";

/**
* @typedef {Object} ResponseBody
* @returns {Object} flowResponse
*/

/**
* Invokes a Bedrock flow to run an inference using the input
* provided in the request body.
*
* @param {string} prompt - The prompt that you want the Agent to complete.
*/
export const invokeBedrockFlow = async (prompt) => {
const client = new BedrockAgentRuntimeClient({ region: "us-east-1" });
// const client = new BedrockAgentRuntimeClient({
// region: "us-east-1",
// credentials: {
// accessKeyId: "accessKeyId", // permission to invoke flow
// secretAccessKey: "accessKeySecret",
// },
// });

const flowIdentifier = "AJBHXXILZN";
const flowAliasIdentifier = "AVKP1ITZAA";

const command = new InvokeFlowCommand({
flowIdentifier,
flowAliasIdentifier,
inputs: [
{
content: {
document: prompt,
},
nodeName: "FlowInputNode",
nodeOutputName: "document",
}
]
});

try {
let flowResponse = {};
const response = await client.send(command);

if (response.responseStream === undefined) {
throw new Error("responseStream is undefined");
}

for await (let chunkEvent of response.responseStream) {
const { flowOutputEvent } = chunkEvent;
flowResponse = { ...flowResponse, ...flowOutputEvent };
console.log(flowOutputEvent);
}

return flowResponse;
} catch (err) {
console.error(err);
}
};

// Call function if run directly
import { fileURLToPath } from "url";
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const result = await invokeBedrockFlow("I need help.");
console.log(result);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import { describe, it, expect, vi } from "vitest";
import { invokeBedrockFlow } from "../actions/invoke-flow.js";

const mocks = vi.hoisted(() => ({
clientSendResolve: () =>
Promise.resolve({
responseStream: [
{
flowOutputEvent: {
content: {
document: 'Test prompt',
},
},
},
],
}),
clientSendReject: () => Promise.reject(new Error("Mocked error")),
send: vi.fn(),
}));

vi.mock("@aws-sdk/client-bedrock-agent-runtime", () => {
return {
BedrockAgentRuntimeClient: vi.fn(() => ({ send: mocks.send })),
InvokeFlowCommand: vi.fn(),
};
});

describe("invokeBedrockFlow", () => {
it("should return an object with responseStream", async () => {
const prompt = "Test prompt";
mocks.send.mockImplementationOnce(mocks.clientSendResolve);

const result = await invokeBedrockFlow(prompt);

expect(result).toEqual({
content: {
document: prompt,
},
});
});

it("should log errors", async () => {
mocks.send.mockImplementationOnce(mocks.clientSendReject);
const spy = vi.spyOn(console, "error");
const prompt = "Test prompt";

await invokeBedrockFlow(prompt);

expect(spy).toHaveBeenCalledWith(
expect.objectContaining({ message: "Mocked error" }),
);
});
});

0 comments on commit 83a5789

Please sign in to comment.