-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-streaming.py
More file actions
74 lines (58 loc) · 2.19 KB
/
Copy pathpython-streaming.py
File metadata and controls
74 lines (58 loc) · 2.19 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""Streaming chat completion example using APIVAI (OpenAI-compatible)."""
import json
import os
import sys
from typing import Any
import requests
API_KEY = os.getenv("APIVAI_API_KEY", "YOUR_APIVAI_API_KEY")
BASE_URL = os.getenv("APIVAI_BASE_URL", "https://api.apivai.com/v1")
MODEL = os.getenv("APIVAI_MODEL", "YOUR_MODEL_NAME")
def extract_content(chunk: dict[str, Any]) -> str:
"""Return text content from an OpenAI-style streaming chunk."""
choices = chunk.get("choices") or []
if not choices:
return ""
delta = choices[0].get("delta") or {}
content = delta.get("content")
return content if isinstance(content, str) else ""
def main() -> None:
url = f"{BASE_URL}/chat/completions"
headers = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
}
payload = {
"model": MODEL,
"messages": [
{"role": "system", "content": "You are a concise technical assistant."},
{"role": "user", "content": "Write a three-item API testing checklist."},
],
"temperature": 0.2,
"stream": True,
}
with requests.post(url, headers=headers, json=payload, stream=True, timeout=60) as response:
response.raise_for_status()
for raw_line in response.iter_lines(decode_unicode=True):
if not raw_line:
continue
if not raw_line.startswith("data:"):
continue
data = raw_line.removeprefix("data:").strip()
if data == "[DONE]":
break
try:
chunk = json.loads(data)
except json.JSONDecodeError:
print(f"\nSkipping non-JSON stream chunk: {data}", file=sys.stderr)
continue
text = extract_content(chunk)
if text:
print(text, end="", flush=True)
print()
if __name__ == "__main__":
if API_KEY == "YOUR_APIVAI_API_KEY":
print("Warning: set APIVAI_API_KEY before sending real requests.", file=sys.stderr)
if MODEL == "YOUR_MODEL_NAME":
print("Warning: set APIVAI_MODEL to a model returned by GET /models.", file=sys.stderr)
main()