Repository navigation
Expand file tree
/
Copy pathgenerate.py
More file actions
152 lines (129 loc) · 5.6 KB
/
Copy pathgenerate.py
File metadata and controls
152 lines (129 loc) · 5.6 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
"""GPT-6.2 API example for PoYo (Python 3.9+, standard library only).
Payloads with "messages" go to Chat Completions; payloads with "input" go to Responses.
Add "stream": true to a payload to receive server-sent events.
Usage:
python python/generate.py # payloads/chat-completions.json
python python/generate.py payloads/responses.json # Responses with reasoning effort
python python/generate.py payloads/responses-stream.json # Responses streaming
python python/generate.py payloads/chat-completions.json gpt-6.2-astra # override the model
"""
from __future__ import annotations
import json
import os
import sys
import urllib.error
import urllib.request
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
DEFAULT_PAYLOAD = ROOT / "payloads" / "chat-completions.json"
MODELS = ("gpt-6.2-astra", "gpt-6.2-sol")
def load_env() -> None:
for env_path in (Path.cwd() / ".env", ROOT / ".env"):
if not env_path.exists():
continue
for line in env_path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip())
load_env()
API_KEY = os.environ.get("POYO_API_KEY", "")
BASE_URL = os.environ.get("POYO_BASE_URL", "https://api.poyo.ai").rstrip("/")
if not API_KEY or API_KEY == "YOUR_POYO_API_KEY_HERE":
sys.exit("Set POYO_API_KEY in your environment or repo-root .env file.")
HEADERS = {
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json",
"User-Agent": "poyo-gpt-6-2-api-example/1.0",
}
def fail(status: int, text: str) -> None:
try:
body = json.loads(text) if text else {}
except ValueError:
body = {"raw": text}
raise RuntimeError(f"PoYo request failed: {json.dumps({'http_status': status, 'body': body}, indent=2)}")
def open_request(path: str, payload: dict):
request = urllib.request.Request(
f"{BASE_URL}{path}", data=json.dumps(payload).encode("utf-8"), headers=HEADERS, method="POST"
)
try:
return urllib.request.urlopen(request, timeout=600)
except urllib.error.HTTPError as error:
fail(error.code, error.read().decode("utf-8", "replace"))
def post_json(path: str, payload: dict) -> dict:
with open_request(path, payload) as response:
status, text = response.status, response.read().decode("utf-8")
try:
body = json.loads(text) if text else {}
except ValueError:
body = {"raw": text}
api_code = body.get("code") if isinstance(body, dict) else None
if status >= 400 or not isinstance(body, dict) or body.get("error") or (isinstance(api_code, int) and api_code not in (0, 200)):
fail(status, text)
return body
def sse_events(path: str, payload: dict):
"""Yield each JSON event from a text/event-stream response until data: [DONE]."""
with open_request(path, payload) as response:
for raw in response:
line = raw.decode("utf-8", "replace").strip()
if not line.startswith("data:"):
continue
data = line[5:].strip()
if data == "[DONE]":
return
event = json.loads(data)
if isinstance(event, dict) and event.get("error"):
fail(response.status, data)
yield event
def response_text(result: dict) -> str:
parts = []
for item in result.get("output") or []:
if item.get("type") != "message":
continue
for block in item.get("content") or []:
if block.get("type") == "output_text":
parts.append(block.get("text") or "")
return "".join(parts).strip()
payload_path = Path(sys.argv[1]) if len(sys.argv) > 1 else DEFAULT_PAYLOAD
payload = json.loads(payload_path.read_text(encoding="utf-8"))
if len(sys.argv) > 2:
payload["model"] = sys.argv[2]
if payload.get("model") not in MODELS:
sys.exit(f"model must be one of {', '.join(MODELS)}")
if "messages" in payload:
if payload.get("stream"):
usage = None
for chunk in sse_events("/v1/chat/completions", payload):
for choice in chunk.get("choices") or []:
print((choice.get("delta") or {}).get("content") or "", end="", flush=True)
usage = chunk.get("usage") or usage
print()
else:
result = post_json("/v1/chat/completions", payload)
choices = result.get("choices") or [{}]
print((choices[0].get("message") or {}).get("content") or "")
usage = result.get("usage")
elif "input" in payload:
if payload.get("stream"):
usage = None
completed = False
for event in sse_events("/v1/responses", payload):
kind = event.get("type")
if kind == "response.output_text.delta":
print(event.get("delta") or "", end="", flush=True)
elif kind == "response.completed":
usage = (event.get("response") or {}).get("usage")
completed = True
elif kind == "response.failed":
fail(200, json.dumps(event))
print()
if not completed:
sys.exit("Stream ended before response.completed; retry the request.")
else:
result = post_json("/v1/responses", payload)
print(response_text(result))
usage = result.get("usage")
else:
sys.exit("Payload needs 'messages' (Chat Completions) or 'input' (Responses).")
print(json.dumps({"model": payload["model"], "usage": usage}, indent=2))