-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathopenai_compat.py
More file actions
413 lines (379 loc) · 15 KB
/
openai_compat.py
File metadata and controls
413 lines (379 loc) · 15 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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from __future__ import annotations
import json
from typing import Any, Iterator
from urllib import error, request
from .agent_types import (
AssistantTurn,
ModelConfig,
OutputSchemaConfig,
StreamEvent,
ToolCall,
UsageStats,
)
class OpenAICompatError(RuntimeError):
"""Raised when the local OpenAI-compatible backend returns an invalid response."""
def _join_url(base_url: str, suffix: str) -> str:
base = base_url.rstrip('/')
return f'{base}/{suffix.lstrip("/")}'
def _normalize_content(content: Any) -> str:
if content is None:
return ''
if isinstance(content, str):
return content
if isinstance(content, list):
parts: list[str] = []
for item in content:
if isinstance(item, str):
parts.append(item)
continue
if not isinstance(item, dict):
parts.append(str(item))
continue
if item.get('type') == 'text' and isinstance(item.get('text'), str):
parts.append(item['text'])
continue
if isinstance(item.get('text'), str):
parts.append(item['text'])
continue
parts.append(json.dumps(item, ensure_ascii=True))
return ''.join(parts)
return str(content)
def _parse_tool_arguments(raw_arguments: Any) -> dict[str, Any]:
if raw_arguments is None:
return {}
if isinstance(raw_arguments, dict):
return raw_arguments
if isinstance(raw_arguments, str):
raw_arguments = raw_arguments.strip()
if not raw_arguments:
return {}
try:
parsed = json.loads(raw_arguments)
except json.JSONDecodeError as exc:
raise OpenAICompatError(
f'Invalid tool arguments returned by model: {raw_arguments!r}'
) from exc
if not isinstance(parsed, dict):
raise OpenAICompatError(
f'Tool arguments must decode to an object, got {type(parsed).__name__}'
)
return parsed
raise OpenAICompatError(
f'Unsupported tool arguments payload: {type(raw_arguments).__name__}'
)
def _optional_int(value: Any) -> int:
if isinstance(value, bool):
return 0
if isinstance(value, int):
return value
if isinstance(value, float):
return int(value)
if isinstance(value, str):
try:
return int(value)
except ValueError:
return 0
return 0
def _parse_usage(payload: Any) -> UsageStats:
if not isinstance(payload, dict):
return UsageStats()
completion_details = payload.get('completion_tokens_details')
if not isinstance(completion_details, dict):
completion_details = {}
return UsageStats(
input_tokens=(
_optional_int(payload.get('input_tokens'))
or _optional_int(payload.get('prompt_tokens'))
or _optional_int(payload.get('prompt_eval_count'))
),
output_tokens=(
_optional_int(payload.get('output_tokens'))
or _optional_int(payload.get('completion_tokens'))
or _optional_int(payload.get('eval_count'))
),
cache_creation_input_tokens=_optional_int(
payload.get('cache_creation_input_tokens')
),
cache_read_input_tokens=_optional_int(payload.get('cache_read_input_tokens')),
reasoning_tokens=(
_optional_int(payload.get('reasoning_tokens'))
or _optional_int(completion_details.get('reasoning_tokens'))
),
)
def _build_response_format(
schema: OutputSchemaConfig | None,
) -> dict[str, Any] | None:
if schema is None:
return None
return {
'type': 'json_schema',
'json_schema': {
'name': schema.name,
'schema': schema.schema,
'strict': schema.strict,
},
}
class OpenAICompatClient:
"""Minimal OpenAI-compatible chat client for local model servers."""
def __init__(self, config: ModelConfig) -> None:
self.config = config
def complete(
self,
messages: list[dict[str, Any]],
tools: list[dict[str, Any]],
*,
output_schema: OutputSchemaConfig | None = None,
) -> AssistantTurn:
payload = self._request_json(
self._build_payload(
messages=messages,
tools=tools,
stream=False,
output_schema=output_schema,
)
)
choices = payload.get('choices')
if not isinstance(choices, list) or not choices:
raise OpenAICompatError('Local model backend returned no choices')
first_choice = choices[0]
if not isinstance(first_choice, dict):
raise OpenAICompatError('Local model backend returned malformed choice data')
message = first_choice.get('message')
if not isinstance(message, dict):
raise OpenAICompatError('Local model backend returned no assistant message')
content = _normalize_content(message.get('content'))
tool_calls = self._parse_tool_calls_from_message(message)
finish_reason = first_choice.get('finish_reason')
if finish_reason is not None and not isinstance(finish_reason, str):
finish_reason = str(finish_reason)
return AssistantTurn(
content=content,
tool_calls=tuple(tool_calls),
finish_reason=finish_reason,
raw_message=message,
usage=_parse_usage(payload.get('usage')),
)
def stream(
self,
messages: list[dict[str, Any]],
tools: list[dict[str, Any]],
*,
output_schema: OutputSchemaConfig | None = None,
) -> Iterator[StreamEvent]:
payload = self._build_payload(
messages=messages,
tools=tools,
stream=True,
output_schema=output_schema,
)
req = request.Request(
_join_url(self.config.base_url, '/chat/completions'),
data=json.dumps(payload).encode('utf-8'),
headers={
'Authorization': f'Bearer {self.config.api_key}',
'Content-Type': 'application/json',
},
method='POST',
)
try:
with request.urlopen(req, timeout=self.config.timeout_seconds) as response:
yield StreamEvent(type='message_start')
for event_payload in self._iter_sse_payloads(response):
yield from self._parse_stream_payload(event_payload)
except error.HTTPError as exc:
detail = exc.read().decode('utf-8', errors='replace')
raise OpenAICompatError(
f'HTTP {exc.code} from local model backend: {detail}'
) from exc
except error.URLError as exc:
raise OpenAICompatError(
f'Unable to reach local model backend at {self.config.base_url}: {exc.reason}'
) from exc
def _request_json(self, payload: dict[str, Any]) -> dict[str, Any]:
body = json.dumps(payload).encode('utf-8')
req = request.Request(
_join_url(self.config.base_url, '/chat/completions'),
data=body,
headers={
'Authorization': f'Bearer {self.config.api_key}',
'Content-Type': 'application/json',
},
method='POST',
)
try:
with request.urlopen(req, timeout=self.config.timeout_seconds) as response:
raw = response.read()
except error.HTTPError as exc:
detail = exc.read().decode('utf-8', errors='replace')
raise OpenAICompatError(
f'HTTP {exc.code} from local model backend: {detail}'
) from exc
except error.URLError as exc:
raise OpenAICompatError(
f'Unable to reach local model backend at {self.config.base_url}: {exc.reason}'
) from exc
try:
payload = json.loads(raw.decode('utf-8'))
except json.JSONDecodeError as exc:
raise OpenAICompatError('Local model backend returned invalid JSON') from exc
if not isinstance(payload, dict):
raise OpenAICompatError('Local model backend returned malformed JSON payload')
return payload
def _build_payload(
self,
*,
messages: list[dict[str, Any]],
tools: list[dict[str, Any]],
stream: bool,
output_schema: OutputSchemaConfig | None,
) -> dict[str, Any]:
payload: dict[str, Any] = {
'model': self.config.model,
'messages': messages,
'tools': tools,
'tool_choice': 'auto',
'temperature': self.config.temperature,
'stream': stream,
}
if stream:
payload['stream_options'] = {'include_usage': True}
response_format = _build_response_format(output_schema)
if response_format is not None:
payload['response_format'] = response_format
return payload
def _parse_tool_calls_from_message(self, message: dict[str, Any]) -> list[ToolCall]:
tool_calls: list[ToolCall] = []
raw_tool_calls = message.get('tool_calls')
if isinstance(raw_tool_calls, list):
for idx, raw_call in enumerate(raw_tool_calls):
if not isinstance(raw_call, dict):
raise OpenAICompatError('Malformed tool call payload from model')
function_block = raw_call.get('function') or {}
if not isinstance(function_block, dict):
raise OpenAICompatError('Malformed tool call function payload from model')
name = function_block.get('name')
if not isinstance(name, str) or not name:
raise OpenAICompatError('Tool call missing function name')
call_id = raw_call.get('id')
if not isinstance(call_id, str) or not call_id:
call_id = f'call_{idx}'
arguments = _parse_tool_arguments(function_block.get('arguments'))
tool_calls.append(ToolCall(id=call_id, name=name, arguments=arguments))
elif isinstance(message.get('function_call'), dict):
function_call = message['function_call']
name = function_call.get('name')
if not isinstance(name, str) or not name:
raise OpenAICompatError('Function call missing name')
arguments = _parse_tool_arguments(function_call.get('arguments'))
tool_calls.append(ToolCall(id='call_0', name=name, arguments=arguments))
return tool_calls
def _iter_sse_payloads(self, response: Any) -> Iterator[dict[str, Any]]:
buffer: list[str] = []
while True:
line = response.readline()
if not line:
break
if isinstance(line, bytes):
text = line.decode('utf-8', errors='replace')
else:
text = str(line)
stripped = text.strip()
if not stripped:
if not buffer:
continue
joined = '\n'.join(buffer)
buffer.clear()
if joined == '[DONE]':
break
try:
payload = json.loads(joined)
except json.JSONDecodeError as exc:
raise OpenAICompatError(
f'Invalid JSON in streaming response: {joined!r}'
) from exc
if not isinstance(payload, dict):
raise OpenAICompatError('Malformed SSE payload from model backend')
yield payload
continue
if stripped.startswith('data:'):
buffer.append(stripped[5:].strip())
if buffer:
joined = '\n'.join(buffer)
if joined != '[DONE]':
try:
payload = json.loads(joined)
except json.JSONDecodeError as exc:
raise OpenAICompatError(
f'Invalid trailing JSON in streaming response: {joined!r}'
) from exc
if not isinstance(payload, dict):
raise OpenAICompatError('Malformed trailing SSE payload from model backend')
yield payload
def _parse_stream_payload(
self,
payload: dict[str, Any],
) -> Iterator[StreamEvent]:
usage = _parse_usage(payload.get('usage'))
if usage.total_tokens:
yield StreamEvent(
type='usage',
usage=usage,
raw_event=payload,
)
choices = payload.get('choices')
if not isinstance(choices, list):
return
for choice in choices:
if not isinstance(choice, dict):
continue
delta = choice.get('delta')
if not isinstance(delta, dict):
delta = {}
content = delta.get('content')
if isinstance(content, str) and content:
yield StreamEvent(
type='content_delta',
delta=content,
raw_event=choice,
)
tool_calls = delta.get('tool_calls')
if isinstance(tool_calls, list):
for raw_tool_call in tool_calls:
if not isinstance(raw_tool_call, dict):
continue
function_block = raw_tool_call.get('function')
if not isinstance(function_block, dict):
function_block = {}
yield StreamEvent(
type='tool_call_delta',
tool_call_index=(
raw_tool_call.get('index')
if isinstance(raw_tool_call.get('index'), int)
else 0
),
tool_call_id=(
raw_tool_call.get('id')
if isinstance(raw_tool_call.get('id'), str)
else None
),
tool_name=(
function_block.get('name')
if isinstance(function_block.get('name'), str)
else None
),
arguments_delta=(
function_block.get('arguments')
if isinstance(function_block.get('arguments'), str)
else ''
),
raw_event=raw_tool_call,
)
finish_reason = choice.get('finish_reason')
if finish_reason is not None:
if not isinstance(finish_reason, str):
finish_reason = str(finish_reason)
yield StreamEvent(
type='message_stop',
finish_reason=finish_reason,
raw_event=choice,
)