Invoke Bedrock Agent using the Javacript SDK v3. Link to the SDK: InvokeAgentCommand
Here's my code so far:
const { BedrockAgentRuntimeClient, InvokeAgentCommand } = require("@aws-sdk/client-bedrock-agent-runtime");
const config = {
accessKeyId: process.env.AWS_BEDROCK_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_BEDROCK_SECRET_ACCESS_KEY,
};
const client = new BedrockAgentRuntimeClient(config);
const input = {
agentId: "agentId",
agentAliasId: "agentAliasId",
sessionId: "abc123",
inputText: "Hello",
};
async invokeAgent() {
return new Promise(async (resolve, reject) => {
const command = new InvokeAgentCommand(input);
const response = await client.send(command);
resolve(response);
});
}
The response I get currently from the endpoint is:
{
"$metadata": {
"httpStatusCode": 200,
"requestId": "reqId",
"attempts": 1,
"totalRetryDelay": 0
},
"contentType": "application/json",
"sessionId": "abc123",
"completion": {
"options": {
"messageStream": {
"options": {
"inputStream": {},
"decoder": {
"headerMarshaller": {},
"messageBuffer": [],
"isEndOfStream": false
}
}
}
}
}
}
There is no error that is specified in the SDK either.
In the example(invoke a bedrock agent using the API) that was given there was no high level documentation informing users about the need to properly handle the asynchronous nature of streaming data in this case.
async invokeAgent() {
return new Promise(async (resolve, reject) => {
let completion = "";
const command = new InvokeAgentCommand(input);
const response = await client.send(command);
for await (const chunkEvent of response.completion) {
if (chunkEvent.chunk) {
const chunk = chunkEvent.chunk;
let decoded = new TextDecoder("utf-8").decode(chunk.bytes);
completion += decoded;
}
}
resolve(response);
});
}
for await
is used to iterate over the chunks
in the response asynchronously. The loop will wait for each chunk to be available before moving on to the next iteration.