-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstreaming.py
More file actions
30 lines (26 loc) · 804 Bytes
/
streaming.py
File metadata and controls
30 lines (26 loc) · 804 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
"""Streaming example — tokens arrive in real-time with PII restored inline."""
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
base_url="https://veil-api.com/v1",
default_headers={
"Authorization": f"Bearer {os.environ['VEIL_API_KEY']}",
"x-upstream-key": os.environ["OPENAI_API_KEY"],
},
)
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{
"role": "user",
"content": "Write a formal reply to: Dear support, I am Tom Ryan (tom@example.com, 555-867-5309) and I need help with my account.",
}
],
stream=True,
)
for chunk in stream:
content = chunk.choices[0].delta.content
if content:
print(content, end="", flush=True)
print()