-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcodec_llm.py
More file actions
396 lines (362 loc) · 15.5 KB
/
codec_llm.py
File metadata and controls
396 lines (362 loc) · 15.5 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
"""CODEC LLM call helper — the single canonical OpenAI-style chat/completions caller.
A-12 (PR-3E): before this, ~45 sites hand-rolled the same `chat/completions`
POST — build headers (`Authorization: Bearer …`, `Content-Type`), assemble the
payload (`model`/`messages`/`max_tokens`/`temperature`/
`chat_template_kwargs.enable_thinking=False`), parse `choices[0].message`
(content, with a `reasoning` fallback), and strip `<think>…</think>`. A model
upgrade or API-shape fix then meant editing 20+ places.
This module centralizes the **non-streaming** call. It is intentionally
config-agnostic — each caller passes its own `base_url` / `model` / `api_key`
/ tuning — so it's a pure "build payload → POST → parse" helper with no import
cycle into codec_config. (Streaming SSE + the remaining call sites are migrated
in later A-12 tranches; this PR covers the call() API + codec.py + codec_session.)
NOTE: `codec_llm_proxy` is a *priority queue* (semaphore), not an HTTP proxy —
orthogonal to this module. Callers that want prioritization still wrap the call
in `llm_queue_sync(...)`; behavior parity for the migrated sites means we do NOT
add queue acquisition here (none of them used it).
"""
from __future__ import annotations
import logging
import re
import time
from typing import Any, AsyncIterator, Dict, Iterator, List, Optional
log = logging.getLogger("codec.llm")
class LLMError(Exception):
"""Raised by ``call(raise_on_error=True)`` on any non-success outcome —
non-200 (after retries), a request exception (after retries), or a 200 with
empty/unparseable content. The default ``raise_on_error=False`` keeps the
never-raise → "" contract that the streaming/best-effort callers rely on.
Fail-loud callers (agent_plan/runner, textassist, the regen script) opt in
and map this onto their own error handling."""
_THINK_RE = re.compile(r"<think>.*?</think>", re.DOTALL)
# Sentinel yielded by stream(keepalive=True) on empty "thinking" chunks so an SSE
# caller (e.g. the dashboard) can emit a transport keepalive to hold its tunnel
# open. Never yielded when keepalive=False (the default) — content-only callers
# (codec_session.qwen_stream) are unaffected.
KEEPALIVE = object()
def strip_think(text: str) -> str:
"""Remove <think>…</think> reasoning blocks and surrounding whitespace."""
if not text:
return ""
return _THINK_RE.sub("", text).strip()
def extract_content(response_json: Dict[str, Any]) -> str:
"""Pull the assistant text from an OpenAI-style response: prefer
`choices[0].message.content`, fall back to `.reasoning` (some local
servers put the answer there when content is empty). `<think>` stripped.
Returns "" on any shape mismatch."""
try:
msg = response_json["choices"][0]["message"]
except (KeyError, IndexError, TypeError):
return ""
content = (msg.get("content") or "").strip()
if content:
return strip_think(content)
reasoning = (msg.get("reasoning") or "").strip()
if reasoning:
return strip_think(reasoning)
return ""
def _build_request(
messages: List[Dict[str, Any]],
*,
model: str,
api_key: str,
max_tokens: int,
temperature: float,
enable_thinking: bool,
extra_kwargs: Optional[Dict[str, Any]],
stream: bool = False,
) -> tuple[Dict[str, str], Dict[str, Any]]:
"""Build the (headers, payload) for an OpenAI-style chat/completions request.
Shared by call() and stream() so headers/auth/payload shape never drift.
The `stream` flag is applied LAST so extra_kwargs can't clobber it."""
headers = {"Content-Type": "application/json"}
if api_key:
headers["Authorization"] = "Bearer " + api_key
payload: Dict[str, Any] = {
"model": model,
"messages": messages,
"max_tokens": max_tokens,
"temperature": temperature,
"chat_template_kwargs": {"enable_thinking": enable_thinking},
}
if extra_kwargs:
payload.update(extra_kwargs)
if stream:
payload["stream"] = True
return headers, payload
def _cloud_blocked_msg(base_url: str) -> Optional[str]:
"""License gate for the 'cloud_proxy' feature (paid edition only).
Returns a user-facing message if this is a CLOUD call (non-localhost
base_url) that the current license doesn't permit; otherwise None.
Local calls (localhost / 127.0.0.1 / 0.0.0.0) are NEVER gated. OSS/dev
builds always return None (feature_allowed → True). Fail-open: any
licensing fault returns None so transport is never broken by licensing.
"""
try:
bl = (base_url or "").lower()
if "localhost" in bl or "127.0.0.1" in bl or "0.0.0.0" in bl:
return None # local model — always allowed
import codec_license
if codec_license.feature_allowed("cloud_proxy"):
return None
st = codec_license.license_state()
return (f"\U0001F512 Cloud models require an active CODEC license — "
f"{st.reason}. Activate in Settings, or switch to the local model.")
except Exception:
return None # fail-open — licensing must never break the LLM transport
def call(
messages: List[Dict[str, Any]],
*,
base_url: str,
model: str,
api_key: str = "",
max_tokens: int = 500,
temperature: float = 0.7,
timeout: float = 120.0,
retries: int = 1,
enable_thinking: bool = False,
extra_kwargs: Optional[Dict[str, Any]] = None,
raise_on_error: bool = False,
) -> str:
"""POST `messages` to `<base_url>/chat/completions` and return the parsed,
`<think>`-stripped assistant text.
`retries` includes the first attempt (retries=3 → up to 3 tries with
exponential 2**n backoff between them, matching codec_session.qwen_call).
Error contract:
- `raise_on_error=False` (default): never raises — network/parse errors and
empty/unparseable 200s are logged and yield "".
- `raise_on_error=True`: raises `LLMError` on EVERY non-success outcome
(non-200 after retries, request exception after retries, or a 200 with
empty/unparseable content). For fail-loud callers that must not silently
proceed on an empty answer.
"""
import requests
_blocked = _cloud_blocked_msg(base_url)
if _blocked is not None:
if raise_on_error:
raise LLMError(_blocked)
return _blocked
headers, payload = _build_request(
messages, model=model, api_key=api_key, max_tokens=max_tokens,
temperature=temperature, enable_thinking=enable_thinking,
extra_kwargs=extra_kwargs,
)
attempts = max(1, retries)
url = base_url.rstrip("/") + "/chat/completions"
last_error: Optional[Exception] = None
for attempt in range(attempts):
try:
r = requests.post(url, json=payload, headers=headers, timeout=timeout)
if r.status_code == 200:
resp = extract_content(r.json())
if resp:
return resp
# 200 but empty/odd shape — nothing more to get; don't retry.
if raise_on_error:
raise LLMError("LLM returned empty or unparseable content")
return ""
last_error = LLMError(f"LLM call returned {r.status_code}: {r.text[:200]}")
log.warning("LLM call %s returned %s: %s", url, r.status_code, r.text[:200])
except LLMError:
raise # empty-200 in raise mode — propagate, don't swallow as a retry
except Exception as e:
last_error = e
log.warning("LLM call attempt %d/%d failed: %s", attempt + 1, attempts, e)
if attempt < attempts - 1:
time.sleep(2 ** attempt)
if raise_on_error:
raise LLMError(f"LLM call failed after {attempts} attempt(s): {last_error}")
return ""
def stream(
messages: List[Dict[str, Any]],
*,
base_url: str,
model: str,
api_key: str = "",
max_tokens: int = 500,
temperature: float = 0.7,
timeout: float = 120.0,
enable_thinking: bool = False,
extra_kwargs: Optional[Dict[str, Any]] = None,
keepalive: bool = False,
) -> Iterator[Any]:
"""POST with `stream=True` and yield the RAW assistant content deltas in
order. Centralizes the SSE plumbing: header/payload build (shared with
call()), `data: ` framing, the `[DONE]` sentinel, `choices[0].delta.content`
extraction, and per-chunk parse tolerance.
Think-stripping is intentionally NOT done here — callers that show tokens
live (e.g. codec_session.qwen_stream) strip `<think>` on the accumulated
result, and the dashboard owns its own cross-chunk tag machine. Never
raises: on connect/HTTP/parse error it logs and stops yielding, so the
caller sees a short/empty stream and applies its own fallback.
`keepalive=True` (default off): on an empty "thinking" chunk, yield the
`KEEPALIVE` sentinel every 10th empty (1st, 11th, …) so an SSE caller can
emit a transport keepalive. Content-only callers leave it off and only ever
see `str` deltas.
"""
import json as _json
import requests
_blocked = _cloud_blocked_msg(base_url)
if _blocked is not None:
yield _blocked
return
headers, payload = _build_request(
messages, model=model, api_key=api_key, max_tokens=max_tokens,
temperature=temperature, enable_thinking=enable_thinking,
extra_kwargs=extra_kwargs, stream=True,
)
url = base_url.rstrip("/") + "/chat/completions"
_empty = 0 # empty "thinking" chunks seen (drives keepalive)
try:
with requests.post(url, json=payload, headers=headers,
timeout=timeout, stream=True) as r:
if r.status_code != 200:
log.warning("LLM stream %s returned %s: %s",
url, r.status_code, getattr(r, "text", "")[:200])
return
for line in r.iter_lines():
if not line:
continue
if isinstance(line, (bytes, bytearray)):
line = line.decode("utf-8", "replace")
if not line.startswith("data: "):
continue
data = line[6:]
if data.strip() == "[DONE]":
return
try:
delta = (_json.loads(data).get("choices", [{}])[0]
.get("delta", {}).get("content", ""))
except Exception as e:
log.warning("LLM stream chunk parse failed: %s", e)
continue
if delta:
yield delta
elif keepalive:
_empty += 1
if _empty % 10 == 1: # 1st, 11th, 21st … (matches dashboard)
yield KEEPALIVE
except Exception as e:
log.warning("LLM stream call failed: %s", e)
return
async def acall(
messages: List[Dict[str, Any]],
*,
base_url: str,
model: str,
api_key: str = "",
max_tokens: int = 500,
temperature: float = 0.7,
timeout: float = 120.0,
enable_thinking: bool = False,
extra_kwargs: Optional[Dict[str, Any]] = None,
http: Optional[Any] = None,
raise_on_error: bool = False,
) -> str:
"""Async sibling of call() — a single non-streaming POST via an httpx
AsyncClient. Reuses the caller's client when `http` is given (e.g. agents'
module `_async_http`), else makes + closes its own. When a client is
injected we do NOT pass a per-request timeout — the client's configured
timeout applies (exact parity with the inline sites). `raise_on_error`
mirrors call(): raise `LLMError` on non-200 / exception / empty, else "".
The queue (codec_llm_proxy) stays at the call site — never owned here.
"""
import httpx
_blocked = _cloud_blocked_msg(base_url)
if _blocked is not None:
if raise_on_error:
raise LLMError(_blocked)
return _blocked
headers, payload = _build_request(
messages, model=model, api_key=api_key, max_tokens=max_tokens,
temperature=temperature, enable_thinking=enable_thinking,
extra_kwargs=extra_kwargs,
)
url = base_url.rstrip("/") + "/chat/completions"
own_client = http is None
client = http or httpx.AsyncClient(timeout=timeout)
try:
try:
r = await client.post(url, json=payload, headers=headers)
if r.status_code == 200:
resp = extract_content(r.json())
if resp:
return resp
if raise_on_error:
raise LLMError("LLM returned empty or unparseable content")
return ""
if raise_on_error:
raise LLMError(f"async LLM call returned {r.status_code}")
log.warning("async LLM call %s returned %s", url, r.status_code)
return ""
except LLMError:
raise
except Exception as e:
if raise_on_error:
raise LLMError(f"async LLM call failed: {e}") from e
log.warning("async LLM call failed: %s", e)
return ""
finally:
if own_client:
await client.aclose()
async def astream(
messages: List[Dict[str, Any]],
*,
base_url: str,
model: str,
api_key: str = "",
max_tokens: int = 500,
temperature: float = 0.7,
timeout: float = 120.0,
enable_thinking: bool = False,
extra_kwargs: Optional[Dict[str, Any]] = None,
http: Optional[Any] = None,
keepalive: bool = False,
) -> AsyncIterator[Any]:
"""Async sibling of stream() — yields the RAW assistant content deltas (and
the `KEEPALIVE` sentinel on empty chunks when `keepalive=True`) over httpx
streaming. Reuses the caller's client when `http` is given (e.g. voice's
`self._http`), else makes + closes its own.
Contract difference vs sync stream(): astream **propagates** exceptions —
it does NOT swallow connect/stream errors — because its consumer
(codec_voice._stream_qwen) wraps the loop in try/except to speak a failure
and a silent stream would be a UX regression. The queue stays at the call
site. `<think>` stripping is the caller's job (voice strips per-token).
"""
import json as _json
import httpx
_blocked = _cloud_blocked_msg(base_url)
if _blocked is not None:
yield _blocked
return
headers, payload = _build_request(
messages, model=model, api_key=api_key, max_tokens=max_tokens,
temperature=temperature, enable_thinking=enable_thinking,
extra_kwargs=extra_kwargs, stream=True,
)
url = base_url.rstrip("/") + "/chat/completions"
own_client = http is None
client = http or httpx.AsyncClient(timeout=timeout)
_empty = 0
try:
async with client.stream("POST", url, json=payload, headers=headers) as resp:
async for line in resp.aiter_lines():
if not line or not line.startswith("data: "):
continue
data = line[6:]
if data.strip() == "[DONE]":
return
try:
delta = (_json.loads(data).get("choices", [{}])[0]
.get("delta", {}).get("content", ""))
except (ValueError, KeyError, IndexError, TypeError):
continue
if delta:
yield delta
elif keepalive:
_empty += 1
if _empty % 10 == 1:
yield KEEPALIVE
finally:
if own_client:
await client.aclose()