-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_direct_stream.py
More file actions
30 lines (21 loc) · 983 Bytes
/
Copy path03_direct_stream.py
File metadata and controls
30 lines (21 loc) · 983 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
"""Example 3 — stream the reply as it is generated, in-process.
client.stream() yields pieces of text as they arrive, instead of waiting for the
whole reply. Good for showing output live (like a chat UI typing).
Run it from the project root:
python examples/03_direct_stream.py
"""
# Make the project importable when this file is run directly.
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
from copilot import CopilotClient
client = CopilotClient(anonymous=False)
# Omit the id to start a fresh conversation. Its id is filled in on
# .conversation_id as the stream runs, so you can read it afterwards.
stream = client.stream("Tell me a short, clean joke.")
for chunk in stream:
# Text arrives as strings; generated images would arrive as objects.
if isinstance(chunk, str):
print(chunk, end="", flush=True)
print() # newline after the streamed text
print("conversation_id:", stream.conversation_id)