Streaming responses with ConverseStream

ConverseStream sends the answer as it is written, so users see words straight away. What changes in your code, the extra IAM permission it needs, and the inference settings that shape every answer.

GazeekAMAZON BEDROCK DEEP DIVE·LESSON 6
LESSON 6

Streaming responses with ConverseStream

ConverseStream sends the answer as it is written, so users see words straight away. What changes in your code, the extra IAM permission it needs, and the inference settings that shape every answer.

0:00 / 2:14

Diagrams in this video · 1Free in archhy

Copy and edit in archhy: Waiting, or streaming (new tab)

The diagrams, full size

Copy any of them into archhy, free, and change it for your own system.

After this lesson you can

  • Explain why streaming matters for users
  • Name the extra permission ConverseStream needs
  • Set maxTokens, temperature and stop sequences
Transcript

Streaming responses with ConverseStream

Space Llama here. Last time, the Converse API. Today, streaming. Because nobody likes staring at a spinner.

Waiting, or streaming

Here's the difference, as a picture. With Converse, your app waits, then gets the whole answer at once. With Converse stream, the answer arrives in pieces, as the model writes it. And at the end of the stream, the stop reason and the token usage.

Converse vs ConverseStream

Converse waits until the model has finished, then gives you the whole answer. Converse stream sends the answer as it's written, as a stream of events. The same request, the same kinds of results, a piece at a time. The total time is about the same. But the user sees words straight away, and that feels much faster.

Three things to know

Three things to know. Streaming needs both permissions. Bedrock invoke model, and invoke model with response stream. Without the second, you get access denied. The AWS command line doesn't stream. Use a software development kit, an SDK, like boto3. And the token usage comes in the last event, after the text.

Streaming in Python

In Python, it's the same request, sent to converse stream. Then you loop over the stream. Each content block delta carries the next piece of text, so print it straight away. Message stop brings the stop reason. And metadata, the last event, brings the token usage.

Inference settings

Whichever you use, inference settings shape the answer. Max tokens, the most the model may write. Temperature, lower for steady, factual answers, higher for variety. Top P, another way to narrow the model's choices. And stop sequences, text that ends the answer when it appears. Anything provider-specific goes in additional model request fields.

Remember

Let's recap. Converse stream sends text as it's written, and needs its own permission. And set max tokens on every call.

Practice and what's next

Next, letting the model use your tools. See you there.

Gazeek Flash · free

Exam questions on your phone

Free questions on Amazon Bedrock: quick-fire batches and timed exams, on the web or Android.