-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JavaScript (v3): BedrockAgentRuntime invokeFlow example added.
- Loading branch information
1 parent
51d9d0f
commit 83a5789
Showing
3 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
javascriptv3/example_code/bedrock-agent-runtime/actions/invoke-flow.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
56 changes: 56 additions & 0 deletions
56
javascriptv3/example_code/bedrock-agent-runtime/tests/invoke-flow.unit.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }), | ||
); | ||
}); | ||
}); |