-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
145 lines (131 loc) · 5.4 KB
/
Copy pathclient.py
File metadata and controls
145 lines (131 loc) · 5.4 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
"""Model client for the computer-use demo.
OpenAI-compatible chat/completions over plain HTTP, so the SAME loop drives any
provider that speaks that shape. Default is Deep Infra (GLM). Swap provider by
setting CU_BASE_URL / CU_MODEL / CU_API_KEY.
Examples:
Deep Infra GLM (default):
CU_BASE_URL=https://api.deepinfra.com/v1/openai
CU_MODEL=zai-org/GLM-4.5V
OpenAI:
CU_BASE_URL=https://api.openai.com/v1
CU_MODEL=gpt-4o # or a computer-use capable model
"""
from __future__ import annotations
import json
import os
import requests
def _load_dotenv() -> None:
"""Populate os.environ from a sibling .env file (no dependency needed)."""
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), ".env")
try:
with open(path) as fh:
for line in fh:
line = line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
k, v = line.split("=", 1)
os.environ.setdefault(k.strip(), v.strip())
except FileNotFoundError:
pass
_load_dotenv()
BASE_URL = os.environ.get("CU_BASE_URL", "https://api.deepinfra.com/v1/openai").rstrip("/")
MODEL = os.environ.get("CU_MODEL", "zai-org/GLM-5.3-Flash")
REASONING_EFFORT = os.environ.get("CU_REASONING_EFFORT", "high") # high|medium|low|off
# NOTE: with tools present (always, for computer-use), GLM-5.3-Flash only emits
# reasoning_content at "high". "medium"/"low" return no visible thinking here.
API_KEY = (
os.environ.get("CU_API_KEY")
or os.environ.get("DEEPINFRA_API_KEY")
or os.environ.get("OPENAI_API_KEY")
)
def chat(messages: list, tools: list, temperature: float = 0.2, max_tokens: int = 1024) -> dict:
if not API_KEY:
raise RuntimeError("No API key: set CU_API_KEY (or DEEPINFRA_API_KEY / OPENAI_API_KEY).")
payload = {
"model": MODEL,
"messages": messages,
"tools": tools,
"tool_choice": "auto",
"temperature": temperature,
"max_tokens": max_tokens,
}
if REASONING_EFFORT and REASONING_EFFORT.lower() != "off":
payload["reasoning_effort"] = REASONING_EFFORT # turns on GLM thinking (reasoning_content)
resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json=payload,
timeout=180,
)
if resp.status_code >= 400:
raise RuntimeError(f"model API {resp.status_code}: {resp.text[:500]}")
data = resp.json()
return data["choices"][0]["message"], data.get("usage", {}) or {}
def chat_stream(messages, tools, on_delta=None, temperature: float = 0.2, max_tokens: int = 1024):
"""Streamed chat. Calls on_delta(kind, text) as reasoning/content arrives
(kind is 'reasoning' or 'content'). Returns (assembled_message, usage) at the
end. Tool-call fragments are reassembled by index."""
if not API_KEY:
raise RuntimeError("No API key: set CU_API_KEY (or DEEPINFRA_API_KEY / OPENAI_API_KEY).")
payload = {
"model": MODEL, "messages": messages, "tools": tools, "tool_choice": "auto",
"temperature": temperature, "max_tokens": max_tokens,
"stream": True, "stream_options": {"include_usage": True},
}
if REASONING_EFFORT and REASONING_EFFORT.lower() != "off":
payload["reasoning_effort"] = REASONING_EFFORT
resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json"},
json=payload, stream=True, timeout=180,
)
if resp.status_code >= 400:
raise RuntimeError(f"model API {resp.status_code}: {resp.text[:500]}")
reasoning, content, usage = [], [], {}
tcs: dict = {}
for raw in resp.iter_lines():
if not raw:
continue
line = raw.decode("utf-8", "replace")
if not line.startswith("data:"):
continue
data = line[5:].strip()
if data == "[DONE]":
break
try:
obj = json.loads(data)
except json.JSONDecodeError:
continue
if obj.get("usage"):
usage = obj["usage"]
for choice in obj.get("choices", []) or []:
delta = choice.get("delta", {}) or {}
rc = delta.get("reasoning_content")
if rc:
reasoning.append(rc)
if on_delta:
on_delta("reasoning", rc)
c = delta.get("content")
if c:
content.append(c)
if on_delta:
on_delta("content", c)
for tc in delta.get("tool_calls") or []:
idx = tc.get("index", 0)
slot = tcs.setdefault(idx, {"id": None, "name": None, "args": ""})
if tc.get("id"):
slot["id"] = tc["id"]
fn = tc.get("function") or {}
if fn.get("name"):
slot["name"] = fn["name"]
if fn.get("arguments"):
slot["args"] += fn["arguments"]
calls = [
{"id": v["id"], "type": "function",
"function": {"name": v["name"], "arguments": v["args"]}}
for _, v in sorted(tcs.items())
if v.get("name")
]
msg = {"role": "assistant", "content": "".join(content) or None,
"reasoning_content": "".join(reasoning), "tool_calls": calls or None}
return msg, usage