-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream.py
More file actions
30 lines (21 loc) · 689 Bytes
/
Copy pathstream.py
File metadata and controls
30 lines (21 loc) · 689 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 chat completion through API Keychain."""
import os
import sys
from openai import OpenAI
def main() -> None:
base = os.environ.get("GATEWAY_URL", "http://localhost:8000")
api_key = os.environ["KEYCHAIN_KEY"]
client = OpenAI(base_url=f"{base}/v1", api_key=api_key)
stream = client.chat.completions.create(
model="keychain-medium",
messages=[{"role": "user", "content": "Write a haiku about routing."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
sys.stdout.write(delta)
sys.stdout.flush()
print()
if __name__ == "__main__":
main()