Repository navigation
Expand file tree
/
Copy pathopenai_sdk.py
More file actions
79 lines (71 loc) · 2.81 KB
/
Copy pathopenai_sdk.py
File metadata and controls
79 lines (71 loc) · 2.81 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
"""GPT-6.2 through PoYo with the official OpenAI Python SDK.
pip install openai
python python/openai_sdk.py chat # Chat Completions, gpt-6.2-sol
python python/openai_sdk.py responses gpt-6.2-astra # Responses with reasoning effort
python python/openai_sdk.py stream # Responses streaming
The only PoYo-specific settings are the API key and base_url.
"""
from __future__ import annotations
import os
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
for env_path in (Path.cwd() / ".env", ROOT / ".env"):
if env_path.exists():
for line in env_path.read_text(encoding="utf-8").splitlines():
line = line.strip()
if line and not line.startswith("#") and "=" in line:
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip())
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.")
try:
from openai import OpenAI
except ImportError:
sys.exit("Install the OpenAI SDK first: pip install openai")
mode = sys.argv[1] if len(sys.argv) > 1 else "chat"
model = sys.argv[2] if len(sys.argv) > 2 else "gpt-6.2-sol"
client = OpenAI(api_key=API_KEY, base_url=f"{BASE_URL}/v1", timeout=600)
if mode == "chat":
completion = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You write concise git commit messages."},
{"role": "user", "content": "Write a one-line commit message for: fix off-by-one error in pagination."},
],
max_tokens=200,
)
print(completion.choices[0].message.content)
print(completion.usage)
elif mode == "responses":
response = client.responses.create(
model=model,
instructions="Answer in two sentences.",
input="When should a web service return HTTP 409 instead of 400?",
reasoning={"effort": "low"},
max_output_tokens=300,
)
print(response.output_text)
print(response.usage)
elif mode == "stream":
stream = client.responses.create(
model=model,
instructions="Answer in plain text.",
input="Name three uses of a bloom filter, one line each.",
reasoning={"effort": "low"},
max_output_tokens=300,
stream=True,
store=False,
)
usage = None
for event in stream:
if event.type == "response.output_text.delta":
print(event.delta, end="", flush=True)
elif event.type == "response.completed":
usage = event.response.usage
print()
print(usage)
else:
sys.exit("Mode must be chat, responses, or stream.")