Process Amazon Bedrock's Response Stream with JavaScript
Learn how to use Claude 3 through Amazon Bedrock's InvokeModelWithResponseStream API and process the response with the AWS SDK for JavaScript.
Dennis Traub
Amazon Employee
Published Mar 18, 2024
Last Modified Mar 20, 2024
Here's how you can process Amazon Bedrock's response stream with the AWS SDK for JavaScript v3, using Anthropic Claude 3 Haiku as an example.
No fluff, just code. Let's go!
The stream contains different chunk types, allowing us to extract the message along with some additional information:
- The
"message_start"
chunk contains the role the model has attached to the message. - The
"content_block_delta"
chunk contains the actual message parts. - The
"message_stop"
chunk contains some metrics, like token count.
The individual chunks can be processed inside a
for await ... of
loop:And that's it!
Learned something new? Like this post or let me know in the comments.
If you want to learn more, have a look at the following resources:
- Amazon Bedrock code examples - Our constantly growing list of examples across models and programming languages.
- The inference parameter reference for Claude and all other models on Amazon Bedrock.
- And, of course, the Generative AI Space here on community.aws with a curated list of articles all around Amazon Bedrock and Generative AI.
Any opinions in this post are those of the individual author and may not reflect the opinions of AWS.