Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bedrock Runtime Converse SDK Document Block does not recognize Uint8Array() when reading PDF files #6774

Open
3 of 4 tasks
Ceebig opened this issue Jan 3, 2025 · 1 comment
Assignees
Labels
bug This issue is a bug. p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.

Comments

@Ceebig
Copy link

Ceebig commented Jan 3, 2025

Checkboxes for prior research

Describe the bug

Based on the AWS Bedrock Runtime SDK documentation, if a file is present in a message, it should be passed in as an Uint8Array, and the SDK will convert the Uint8Array into based 64 encoded string.

File upload is handled by middleware multer, I used the readFileSync method to read the PDF file as raw bytes and then convert it to an Uint8Array as shown in the code. However, it keeps emitting the error message Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array. I did a console.log() of the Uint8Array that I created, and I can confirm it is a Uint8Array; the SDK does not seem to recognize it for some reason. Thank you.

Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.
    at toBase64 (/node_modules/@smithy/util-base64/dist-cjs/toBase64.js:15:15)
    at Object.bytes (/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1810:41)

Regression Issue

  • Select this option if this issue appears to be a regression.

SDK version number

@aws-sdk/[email protected]

Which JavaScript Runtime is this issue in?

Node.js

Details of the browser/Node.js/ReactNative version

v20.8.1

Reproduction Steps

const fs = require("fs");
const { BedrockRuntimeClient, ConverseCommand } = require("@aws-sdk/client-bedrock-runtime")
const config = {
  region: "us-east-1",
  credentials: {
    accessKeyId: awsKeyID,
    secretAccessKey: awsKeySecret
  }
}
const bedrockRuntimeClient = new BedrockRuntimeClient(config)

let files = req.files
files.forEach(file => {
      // Read the file synchronously
      const filePath = path.resolve(file.path);
      const rawBytes = fs.readFileSync(filePath);

      userPrompt.content.push({
        document: {
          format: file.originalname.split('.').pop(),
          name: new StringService().sanitizeFileName(file.originalname),
          source: {
            bytes: new Uint8Array(rawBytes),
          }
        }
      })
    })

messages.push(userPrompt)

const input = { // ConverseRequest
     modelId: modelID, // required
     messages: messages,
     inferenceConfig: inferenceConfig,
};

const command = new ConverseCommand(input);
return await bedrockRuntimeClient.send(command)

Observed Behavior

Error: @smithy/util-base64: toBase64 encoder function only accepts string | Uint8Array.
at toBase64 (/node_modules/@smithy/util-base64/dist-cjs/toBase64.js:15:15)
at Object.bytes (/node_modules/@aws-sdk/client-bedrock-runtime/dist-cjs/index.js:1810:41)

Expected Behavior

The AWS SDK should accept the document block with bytes in Uint8Array without any errors

Possible Solution

No response

Additional Information/Context

No response

@Ceebig Ceebig added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 3, 2025
@zshzbh zshzbh self-assigned this Jan 3, 2025
@zshzbh
Copy link
Contributor

zshzbh commented Jan 3, 2025

Hey @Ceebig,

Can you check what is the type of userPrompt ? I used a single pdf and it works, I can share my code below -

// Use the Conversation API to send a text message to Anthropic Claude.

import {
  BedrockRuntimeClient,
  ConverseCommand,
} from "@aws-sdk/client-bedrock-runtime";
import fs from "fs";
const rawBytes = fs.readFileSync('./DogPolicy.pdf')
// Create a Bedrock Runtime client in the AWS Region you want to use.
const client = new BedrockRuntimeClient({ region: "us-west-2",
  credentials: {
    accessKeyId: "XXX",
    secretAccessKey: "XXXX",
  },
});

  const uint8 = new Uint8Array(rawBytes);

// Set the model ID, e.g., Claude 3 Haiku.
const modelId = "anthropic.claude-3-haiku-20240307-v1:0";

// Start a conversation

const conversation = [
  {
    role: "user",
    content: [
      {
        document: {
          format: "pdf",
          name: "DogPolicy",
          source: {
            bytes: uint8,
          },
        },
      },
      {
      text: "Describe this pdf",
      }
    ],
  },
];

// Create a command with the model ID, the message, and a basic configuration.
const command = new ConverseCommand({
  modelId,
  messages: conversation,
  inferenceConfig: { maxTokens: 512, temperature: 0.5, topP: 0.9 },
});

try {
  // Send the command to the model and wait for the response
  const response = await client.send(command);

  // Extract and print the response text.
  const responseText = response.output.message.content[0].text;
  console.log(responseText);
} catch (err) {
  console.log(`ERROR: Can't invoke '${modelId}'. Reason: ${err}`);
  process.exit(1);
}

And the result I got
This PDF document outlines Amazon's policy for allowing employees to bring their dogs to work at select corporate offices. The key points are: ....

I think the root cause is the type of rawBytes or the field in content is not string or Uint8Array Can you console log the rawBytes and userPrompt and their type for further investigation?

Thanks

@zshzbh zshzbh added p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jan 3, 2025
@aBurmeseDev aBurmeseDev added the response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days. label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. p2 This is a standard priority issue response-requested Waiting on additional info and feedback. Will move to \"closing-soon\" in 7 days.
Projects
None yet
Development

No branches or pull requests

3 participants