Start learning free

LESSON 6 OF 129 · AI ENGINEERING: CALLING A MODEL

Streaming Answers in Python

Practice

With client.messages.create your program waits until the model has written its whole answer, and only then gets it. A model writes a few words at a time, so a long answer means seconds in front of an empty screen. Streaming hands you the words while they are being written.

You open a stream with a with line that calls client.messages.stream with the same arguments as create. Inside its block, stream.text_stream gives the answer piece by piece: a for loop over it runs once for every piece, at the moment that piece arrives. When the pieces are over, stream.get_final_message() gives the whole message, with its stop_reason and usage.

Printing pieces on one line: print normally starts a new line after what it prints. end="" turns that off, and flush=True sends the text to the screen right away. An empty print() ends the line when you are done.

print("tea", end="", flush=True)
print("pot", end="", flush=True)
print()
print("Done")

This prints teapot on one line, then Done on the next.

The course model answers with a welcome message for the Corner Shop. Your program must print it from its pieces, then three facts about the stream:

Welcome to the Corner Shop! Browse our mugs, teapots and lamps, all picked with care. Every order ships within two days.
Pieces: 10
Stop reason: end_turn
Output tokens: 30

Your task: the client, MODEL, messages and a counter pieces = 0 are already there. Open a stream, print every piece as it arrives, count the pieces, then print how many came, the stop reason and the output tokens of the final message.


Assignment
Follow the Coding Tutorial to stream the model's answer piece by piece and print the final message's stop reason and output tokens.


Hint
The loop, the empty print() and stream.get_final_message() all go inside the with block. The last three print lines go below it, with no indent.


STEP 1 OF 9
    Ctrl + Enter to check

    Ai Streaming in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free