aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

BedrockRuntimeClient ConverseStreamCommand can't receive any response in react native

Open zhu-xiaowei opened this issue 6 months ago • 8 comments

Checkboxes for prior research

Describe the bug

When using BedrockRuntimeClient in React Native environment, InvokeModelCommand works well, but ConverseStreamCommand and InvokeModelWithResponseStreamCommand will not return any response.

And I've read the getting-started for react native and added following imports

import "react-native-get-random-values";
import "react-native-url-polyfill/auto";
import "web-streams-polyfill/dist/polyfill";

then added blow code in metro.config.js

const config = {
  transformer: {
    getTransformOptions: async () => ({
      transform: {
        experimentalImportSupport: true, // false is still not work. 
        inlineRequires: false,
      },
    }),
  },
};

module.exports = mergeConfig(getDefaultConfig(__dirname), config);

the bedrock code as:

const command = new ConverseStreamCommand({
  messages: messages,
  modelId,
});
const apiResponse = await client.send(command);
console.log(JSON.stringify(apiResponse.stream));

for await (const item of apiResponse.stream) {
  console.log(item);
  if (item.contentBlockDelta) {
    const text = item.contentBlockDelta.delta?.text;
    console.log(text);
  }
}

client.send(command) works well, and the log for apiResponse.stream is:

{"options":{"messageStream":{"options":{"inputStream":{},"decoder":{"headerMarshaller":{},"messageBuffer":[],"isEndOfStream":false}}}}}

The for await block never goes inside, then I try to remove the transformer in metro.config.js and the apiResponse.stream will return the following error:

 WARN  Invalid responseType: blob
 ERROR [TypeError: Cannot read property 'blobId' of undefined]
 WARN  Invalid responseType: blob
 WARN  Invalid responseType: blob
 WARN  Invalid responseType: blob

I don't know what configuration I'm missing or is there any sample code for calling methods of ConverseStreamCommandor InvokeModelWithResponseStreamCommand in a React Native environment?

SDK version number

"@aws-sdk/client-bedrock-runtime": "^3.614.0"

Which JavaScript Runtime is this issue in?

React Native

Details of the browser/Node.js/ReactNative version

"react-native": "0.74.5"

Reproduction Steps

see the above describe.

Observed Behavior

ConverseStreamCommand command send successfully but apiResponse.stream receive without any streaming response.

Expected Behavior

can receive the streaming response for ConverseStreamCommand

Possible Solution

No response

Additional Information/Context

The credentials are configured correctly and the same code runs perfectly in the node environment.

zhu-xiaowei avatar Aug 19 '24 02:08 zhu-xiaowei