I have set up an AWS bedrock agent and am successfully sending text requests and receiving responses.
I need to send across a PNG file for the Agent to view and return the contents as JSON.
This is my current code:
import {
BedrockAgentRuntimeClient,
InvokeAgentCommand,
} from "@aws-sdk/client-bedrock-agent-runtime";
import dotenv from 'dotenv';
dotenv.config();
import https from 'https';
async function getImageData(imageUrl) {
return new Promise((resolve, reject) => {
https.get(imageUrl, (res) => {
const chunks = [];
res.on('data', (chunk) => {
chunks.push(chunk);
});
res.on('end', () => {
const imageData = Buffer.concat(chunks);
resolve(imageData);
});
res.on('error', (err) => {
reject(err);
});
});
});
}`
`async function invokeBedrockAgent(prompt, sessionId, imageUrl) {
const client = new BedrockAgentRuntimeClient({
region: "eu-west-2",
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
const agentId = process.env.AGENTID;
const agentAliasId = process.env.AGENTALIASID;
const imageData = await getImageData(imageUrl);
const imageUint8Array = new Uint8Array(imageData);
const input = {
agentId,
agentAliasId,
sessionId,
sessionState: {
files: [
{
name: "image-file",
source: {
sourceType: "BYTE_CONTENT",
byteContent: {
mediaType: "image/png",
//data: imageUint8Array,
data: new TextEncoder().encode(imageData.toString('base64')),
},
},
useCase: "CHAT",
},
],
},
inputText: prompt,
};
const command = new InvokeAgentCommand(input);
try {
let completion = "";
const response = await client.send(command);
if (!response.completion) {
throw new Error("Completion is undefined");
}
for await (let chunkEvent of response.completion) {
if (chunkEvent.chunk) {
const chunk = chunkEvent.chunk;
const decodedResponse = new TextDecoder("utf-8").decode(chunk.bytes);
completion += decodedResponse;
}
}
console.log("Final completion response:", completion);
return { sessionId: sessionId, completion };
} catch (err) {
console.error("Error occurred:", err);
throw err;
}
}
I am getting this response:
Error occurred: ValidationException: The overridden prompt that you provided is incorrectly formatted. Check the format for errors, such as invalid JSON, and retry your request.
at de_ValidationExceptionRes (/Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1683:21)
at de_ValidationException_event (/Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1934:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1782:30
at async Object.deserializer (/Users/JG/Desktop/storm ai/node_modules/@smithy/eventstream-serde-universal/dist-cjs/index.js:127:37)
at async _SmithyMessageDecoderStream.asyncIterator (/Users/JG/Desktop/storm ai/node_modules/@smithy/eventstream-codec/dist-cjs/index.js:430:28)
at async invokeBedrockAgent (file:///Users/JG/Desktop/storm%20ai/functions/bedrockTesting.js:81:24) {
'$fault': 'client',
'$metadata': {
httpStatusCode: undefined,
requestId: undefined,
extendedRequestId: undefined,
cfId: undefined
}
}
node:internal/process/promises:391
triggerUncaughtException(err, true /* fromPromise */);
^
ValidationException: The overridden prompt that you provided is incorrectly formatted. Check the format for errors, such as invalid JSON, and retry your request.
at de_ValidationExceptionRes (/Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1683:21)
at de_ValidationException_event (/Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1934:10)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/JG/Desktop/storm ai/node_modules/@aws-sdk/client-bedrock-agent-runtime/dist-cjs/index.js:1782:30
at async Object.deserializer (/Users/JG/Desktop/storm ai/node_modules/@smithy/eventstream-serde-universal/dist-cjs/index.js:127:37)
at async _SmithyMessageDecoderStream.asyncIterator (/Users/JG/Desktop/storm ai/node_modules/@smithy/eventstream-codec/dist-cjs/index.js:430:28)
at async invokeBedrockAgent (file:///Users/JG/Desktop/storm%20ai/functions/bedrockTesting.js:81:24) {
'$fault': 'client',
'$metadata': {
httpStatusCode: undefined,
requestId: undefined,
extendedRequestId: undefined,
cfId: undefined
}
}
This function works well if i exclude the file transfer.
The file is coming from cloudconvert storage (converted from a PDF)