Show the answer word by word as it is written, like ChatGPT does.
1# part1/streaming.py
2from openai import OpenAI
3client = OpenAI()
4
5print("Asking the AI to write (streaming)...")
6
7# stream=True: get the answer piece by piece
8stream = client.chat.completions.create(
9 model="gpt-4o-mini",
10 messages=[
11 {"role": "user",
12 "content": "Tell a 3-sentence robot story."}
13 ],
14 stream=True,
15)
16
17# Starts empty; every piece gets added to it
18full_story = ""
19
20# Each chunk carries a tiny new piece of the answer
21for chunk in stream:
22 piece = chunk.choices[0].delta.content
23 # Skip chunks with no text ("" or None)
24 if piece:
25 # end="": same line. flush=True: show now
26 print(piece, end="", flush=True)
27 full_story += piece
28
29print(f"\n\nDone! {len(full_story)} characters in total.")No output yet...