-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
1423 lines (1223 loc) · 46.3 KB
/
server.py
File metadata and controls
1423 lines (1223 loc) · 46.3 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
MLX GPT-OSS Server —
Uses mlx-lm for inference, openai_harmony for prompt formatting, FastAPI for the HTTP layer.
Request queueing keeps /health responsive.
"""
from __future__ import annotations
import argparse
import asyncio
import json
import random
import re
import sys
import time
from concurrent.futures import ThreadPoolExecutor
from contextlib import asynccontextmanager
from dataclasses import dataclass, field
from threading import Event
from typing import Any, AsyncGenerator
import uvicorn
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from loguru import logger
from harmony import HarmonyParser, get_stop_tokens, render_harmony_prompt
from schemas import (
ChatCompletionChunk,
ChatCompletionMessageToolCall,
ChatCompletionRequest,
ChatCompletionResponse,
Choice,
ChoiceDeltaFunctionCall,
ChoiceDeltaToolCall,
Delta,
FunctionCall,
HealthCheckStatus,
Message,
Model,
ModelsResponse,
StreamingChoice,
UsageInfo,
)
# ---------------------------------------------------------------------------
# Logging setup
# ---------------------------------------------------------------------------
_CURRENT_LOG_LEVEL = "INFO"
_CONSOLE_LOG_FORMAT = (
"<green>{time:HH:mm:ss.SS}</green> | "
"<level>{level: <8}</level> | "
"<level>{message}</level>"
)
_FILE_LOG_FORMAT = "{time:YYYY-MM-DD HH:mm:ss.SS} | {level: <8} | {message}"
_DEBUG_PREVIEW_LOG_FORMAT = "{}| rid=<cyan>{}</cyan> chars={} text=<dim>{}</dim>"
_METRICS_LOG_FORMAT = (
"<cyan>{}</cyan>| rid=<cyan>{}</cyan> "
"tok=<green>{}/{}</green> dur=<green>{:.2f}s</green> tps=<green>{:.2f}</green>"
)
_DEBUG_RAW_PREVIEW_CHARS = 0
_DEBUG_EVENT_WIDTH = 11
_HTTP_ACCESS_LOG_ENABLED = False
_STARTUP_DEBUG_ARGS = "args(unset)"
def configure_logging(log_level: str = "INFO", log_file: str | None = None) -> None:
"""Configure console logging and optional rotating file logging."""
global _CURRENT_LOG_LEVEL
_CURRENT_LOG_LEVEL = log_level.upper()
logger.remove()
logger.level("DEBUG", color="<fg #7f8c8d>")
logger.level("INFO", color="<bold>")
logger.level("WARNING", color="<bold><yellow>")
logger.level("ERROR", color="<bold><red>")
logger.add(
sys.stderr,
format=_CONSOLE_LOG_FORMAT,
level=_CURRENT_LOG_LEVEL,
colorize=True,
backtrace=True,
diagnose=False,
)
if log_file:
logger.add(
log_file,
format=_FILE_LOG_FORMAT,
level=_CURRENT_LOG_LEVEL,
backtrace=True,
diagnose=False,
rotation="25 MB",
retention=5,
enqueue=True,
)
def _debug_raw_preview_enabled() -> bool:
return _CURRENT_LOG_LEVEL == "DEBUG" and _DEBUG_RAW_PREVIEW_CHARS > 0
def _new_request_log_id() -> str:
return f"{random.randint(0, 0xFFFF):04x}"
def _request_log_id(http_request: Request) -> str:
request_log_id = getattr(http_request.state, "request_log_id", None)
if isinstance(request_log_id, str) and request_log_id:
return request_log_id
request_log_id = _new_request_log_id()
http_request.state.request_log_id = request_log_id
return request_log_id
def _quote_preview(text: str) -> str:
return json.dumps(text, ensure_ascii=True)
def _event_label(name: str) -> str:
return f"{name:<{_DEBUG_EVENT_WIDTH}}"
# ---------------------------------------------------------------------------
# Model wrapper
# ---------------------------------------------------------------------------
# Just one is enough.
_executor = ThreadPoolExecutor(max_workers=1)
class HarmonyModel:
"""Wrapper around mlx-lm for GPT-OSS inference."""
def __init__(self, model_path: str, context_length: int | None = None) -> None:
from mlx_lm.generate import stream_generate
from mlx_lm.sample_utils import make_logits_processors, make_sampler
from mlx_lm.utils import load
logger.opt(colors=True).info("Loading model: <magenta>{}</magenta>", model_path)
self.model_path = model_path
self.context_length = context_length
self.model, self.tokenizer = load(model_path)
self._make_sampler = make_sampler
self._make_logits_processors = make_logits_processors
self._stream_generate = stream_generate
# Extend EOS tokens with Harmony stop tokens. Important for Harmony tool parsing.
stop_tokens = get_stop_tokens()
existing_eos = getattr(self.tokenizer, "eos_token_ids", [])
if existing_eos is None:
existing_eos = []
elif isinstance(existing_eos, int):
existing_eos = [existing_eos]
else:
existing_eos = list(existing_eos)
for tid in stop_tokens:
if tid not in existing_eos:
existing_eos.append(tid)
self.tokenizer.eos_token_ids = existing_eos
def tokenize(self, prompt: str) -> list[int]:
return self.tokenizer.encode(prompt)
def generate_stream(
self,
input_ids: list[int],
*,
max_tokens: int = 4096,
temperature: float = 1.0,
top_p: float = 1.0,
repetition_penalty: float = 1.08,
repetition_context_size: int = 128,
seed: int | None = None,
):
if seed is not None:
self._set_seed(seed)
sampler = self._make_sampler(temp=temperature, top_p=top_p, top_k=0)
logits_processors = self._make_logits_processors(
repetition_penalty=repetition_penalty,
repetition_context_size=repetition_context_size,
)
generate_kwargs = {
"prompt": input_ids,
"max_tokens": max_tokens,
"sampler": sampler,
"logits_processors": logits_processors,
}
if self.context_length is not None:
generate_kwargs["max_kv_size"] = self.context_length
for response in self._stream_generate(
self.model,
self.tokenizer,
**generate_kwargs,
):
yield response
def _set_seed(self, seed: int) -> None:
import mlx.core as mx
mx.random.seed(seed)
# ---------------------------------------------------------------------------
# Request queue (keeps /health responsive during inference)
# ---------------------------------------------------------------------------
class RequestQueue:
"""
Simple async request queue backed by a thread pool.
Inference runs in a thread via ``run_in_executor``
This way, we can call /health anytime.
"""
def __init__(self) -> None:
self._queue: asyncio.Queue[
tuple[asyncio.Future[Any], Any, tuple[Any, ...], dict[str, Any]]
] = asyncio.Queue(maxsize=32)
self._active = 0
self._worker: asyncio.Task | None = None
async def start(self) -> None:
self._worker = asyncio.create_task(self._loop())
logger.opt(colors=True).info( "Server is <green>ready</green>" )
async def stop(self) -> None:
if self._worker:
self._worker.cancel()
try:
await self._worker
except asyncio.CancelledError:
pass
def stats(self) -> dict[str, int]:
return {"active_requests": self._active, "queued_requests": self._queue.qsize()}
async def submit(self, coro_func, *args, **kwargs):
"""Submit work and wait for the result."""
future: asyncio.Future = asyncio.get_running_loop().create_future()
await self._queue.put((future, coro_func, args, kwargs))
return await future
async def _loop(self) -> None:
while True:
future, coro_func, args, kwargs = await self._queue.get()
self._active += 1
try:
result = await coro_func(*args, **kwargs)
except Exception as exc:
logger.exception("Request worker failed")
if not future.done():
try:
future.set_exception(exc)
except asyncio.InvalidStateError:
logger.debug(
"Skipped exception delivery: request future is no longer pending"
)
else:
if not future.done():
try:
future.set_result(result)
except asyncio.InvalidStateError:
logger.debug(
"Skipped result delivery: request future is no longer pending"
)
finally:
self._active -= 1
# ---------------------------------------------------------------------------
# Generation helpers
# ---------------------------------------------------------------------------
# GPT-OSS defaults (OpenAI recommendations).
_DEFAULT_TEMP = 1.0
_DEFAULT_TOP_P = 1.0
_DEFAULT_MAX_TOKENS = 4096
_DEFAULT_REPETITION_PENALTY = 1.08
_DEFAULT_REPETITION_CONTEXT_SIZE = 128
_ALLOWED_REASONING_EFFORTS = {"low", "medium", "high"}
_FINISH_REASON_MAP = {
"max_tokens": "length",
"length": "length",
"stop": "stop",
"eos": "stop",
}
def _new_prefixed_id(prefix: str) -> str:
return f"{prefix}_{int(time.time())}{random.randint(0, 999999):06d}"
def _sanitize_tool_name(name: str | None) -> str | None:
if not name:
return None
candidate = name.strip()
marker_idx = candidate.find("<|")
if marker_idx != -1:
candidate = candidate[:marker_idx]
candidate = candidate.strip().strip(" \t\r\n.,:;\"'")
match = re.match(r"[A-Za-z_][A-Za-z0-9_.-]*", candidate)
if not match:
return None
return match.group(0)
def _normalize_tool_call(
tool_call: dict[str, Any],
*,
warning_label: str = "tool call",
) -> tuple[str, str] | None:
"""Return sanitized ``(name, arguments_json_or_string)`` for tool calls."""
sanitized_name = _sanitize_tool_name(tool_call.get("name"))
if not sanitized_name:
logger.warning(
"Dropping malformed {} name={!r}",
warning_label,
tool_call.get("name"),
)
return None
args = tool_call.get("arguments", "")
if not isinstance(args, str):
args = json.dumps(args)
return sanitized_name, args
def _common_prefix_len(left: str, right: str) -> int:
max_len = min(len(left), len(right))
idx = 0
while idx < max_len and left[idx] == right[idx]:
idx += 1
return idx
def _only_unseen_suffix(full_text: str, already_emitted: str) -> str:
if not full_text:
return ""
if not already_emitted:
return full_text
return full_text[_common_prefix_len(full_text, already_emitted) :]
def _drop_emitted_tool_call_prefix(
tool_calls: list[dict[str, Any]],
emitted_tool_calls: list[tuple[str, str]],
) -> list[dict[str, Any]]:
"""
Drop a matching emitted prefix while preserving valid repeated tool calls.
During buffered fallback, parser output can include tool calls that were
already streamed. We only trim an exact prefix match to avoid replaying
those earlier calls.
"""
if not tool_calls or not emitted_tool_calls:
return tool_calls
max_compare = min(len(tool_calls), len(emitted_tool_calls))
drop_count = 0
for idx in range(max_compare):
normalized = _normalize_tool_call(
tool_calls[idx],
warning_label="stream fallback tool call",
)
if normalized is None or normalized != emitted_tool_calls[idx]:
break
drop_count += 1
return tool_calls[drop_count:]
def _normalize_reasoning_effort(value: str | None) -> str:
if value is None:
return "medium"
normalized = value.strip().lower()
if normalized in _ALLOWED_REASONING_EFFORTS:
return normalized
logger.warning(
"Invalid reasoning_effort={!r}; falling back to 'medium'.",
value,
)
return "medium"
def _normalize_stop_sequences(stop: str | list[str] | None) -> list[str]:
if stop is None:
return []
if isinstance(stop, str):
return [stop] if stop else []
return [item for item in stop if item]
def _find_first_stop_index(text: str, stop_sequences: list[str]) -> int | None:
first_index: int | None = None
for stop_sequence in stop_sequences:
idx = text.find(stop_sequence)
if idx == -1:
continue
if first_index is None or idx < first_index:
first_index = idx
return first_index
def _truncate_text_at_stop(
text: str | None, stop_sequences: list[str]
) -> tuple[str | None, bool]:
if text is None or not stop_sequences:
return text, False
stop_idx = _find_first_stop_index(text, stop_sequences)
if stop_idx is None:
return text, False
return text[:stop_idx], True
def _process_stop_sequences_chunk(
chunk_text: str,
stop_sequences: list[str],
tail: str,
max_stop_len: int,
) -> tuple[str, str, bool]:
"""
Process a streaming text chunk against stop sequences.
Returns ``(emit_text, new_tail, stop_found)`` where:
- ``emit_text`` is safe to emit now,
- ``new_tail`` must be retained for cross-chunk stop matching,
- ``stop_found`` indicates stop sequence matched and stream should stop.
"""
combined = tail + chunk_text
stop_idx = _find_first_stop_index(combined, stop_sequences)
if stop_idx is not None:
return combined[:stop_idx], "", True
if max_stop_len <= 1:
return combined, "", False
tail_len = max_stop_len - 1
if len(combined) <= tail_len:
return "", combined, False
return combined[:-tail_len], combined[-tail_len:], False
def _normalize_finish_reason(finish_reason: Any) -> str:
normalized = "stop" if finish_reason is None else str(finish_reason).strip().lower()
return _FINISH_REASON_MAP.get(normalized, "stop")
def _resolve_sampling_params(
request: ChatCompletionRequest,
) -> tuple[float, float, int, float, int]:
temperature = (
request.temperature if request.temperature is not None else _DEFAULT_TEMP
)
top_p = request.top_p if request.top_p is not None else _DEFAULT_TOP_P
repetition_penalty = (
request.repetition_penalty
if request.repetition_penalty is not None
else _DEFAULT_REPETITION_PENALTY
)
repetition_context_size = (
request.repetition_context_size
if request.repetition_context_size is not None
else _DEFAULT_REPETITION_CONTEXT_SIZE
)
max_tokens = request.max_tokens or _DEFAULT_MAX_TOKENS
if (
_model is not None
and _model.context_length is not None
and max_tokens > _model.context_length
):
logger.warning(
"Capping client max_tokens={} to model context_length={} to avoid invalid requests.",
max_tokens,
_model.context_length,
)
max_tokens = _model.context_length
return (
temperature,
top_p,
max_tokens,
repetition_penalty,
repetition_context_size,
)
def _response_model_id(request_model: str) -> str:
if _model is not None:
model_path = getattr(_model, "model_path", None)
if isinstance(model_path, str) and model_path:
return model_path
return request_model
def _prepare_prompt(
request: ChatCompletionRequest,
*,
request_log_id: str | None = None,
) -> list[int]:
"""Build input_ids from the request messages."""
reasoning_effort = request.reasoning_effort
if (
request.reasoning_effort is None
and request.chat_template_kwargs
and isinstance(request.chat_template_kwargs.get("reasoning_effort"), str)
):
reasoning_effort = request.chat_template_kwargs["reasoning_effort"]
reasoning_effort = _normalize_reasoning_effort(reasoning_effort)
prompt_str = render_harmony_prompt(
messages=request.messages,
tools=request.tools,
reasoning_effort=reasoning_effort,
)
if _debug_raw_preview_enabled():
prompt_preview = prompt_str[:_DEBUG_RAW_PREVIEW_CHARS]
logger.opt(colors=True).debug(
_DEBUG_PREVIEW_LOG_FORMAT,
_event_label("PROMPT"),
request_log_id or "-",
len(prompt_preview),
_quote_preview(prompt_preview),
)
return _model.tokenize(prompt_str)
async def _generate_response(
request: ChatCompletionRequest,
*,
request_log_id: str | None = None,
) -> ChatCompletionResponse:
"""Non-streaming generation — runs inference in a thread."""
log_id = request_log_id or _new_request_log_id()
request_start_ts = time.time()
input_ids = _prepare_prompt(request, request_log_id=log_id)
prompt_len = len(input_ids)
(
temp,
top_p,
max_tokens,
repetition_penalty,
repetition_context_size,
) = _resolve_sampling_params(request)
stop_sequences = _normalize_stop_sequences(request.stop)
seed = request.seed
loop = asyncio.get_running_loop()
# Collect detokenized text from the sync generator in a thread.
def _run():
generated_parts: list[str] = []
completion_tokens = 0
model_finish_reason = "stop"
for resp in _model.generate_stream(
input_ids,
max_tokens=max_tokens,
temperature=temp,
top_p=top_p,
repetition_penalty=repetition_penalty,
repetition_context_size=repetition_context_size,
seed=seed,
):
if resp.finish_reason is None:
completion_tokens += 1
else:
model_finish_reason = _normalize_finish_reason(resp.finish_reason)
if resp.text:
generated_parts.append(resp.text)
return "".join(generated_parts), completion_tokens, model_finish_reason
full_text, completion_tokens, model_finish_reason = await loop.run_in_executor(
_executor, _run
)
if _debug_raw_preview_enabled():
preview = full_text[:_DEBUG_RAW_PREVIEW_CHARS]
logger.opt(colors=True).debug(
_DEBUG_PREVIEW_LOG_FORMAT,
_event_label("RAW_OUTPUT"),
log_id,
len(preview),
_quote_preview(preview),
)
# Parse through Harmony.
parser = HarmonyParser()
parsed = parser.parse(full_text)
parsed["content"], _ = _truncate_text_at_stop(parsed.get("content"), stop_sequences)
# Build response.
response = _format_response(
parsed,
_response_model_id(request.model),
prompt_len,
completion_tokens,
finish_reason=model_finish_reason,
)
request_end_ts = time.time()
elapsed_s = max(request_end_ts - request_start_ts, 1e-9)
logger.opt(colors=True).debug(
_METRICS_LOG_FORMAT,
_event_label("METRICS"),
log_id,
prompt_len,
completion_tokens,
elapsed_s,
completion_tokens / elapsed_s if completion_tokens else 0.0,
)
return response
def _format_response(
parsed: dict[str, Any],
model: str,
prompt_tokens: int,
completion_tokens: int,
*,
finish_reason: str = "stop",
) -> ChatCompletionResponse:
"""Format parsed result into an OpenAI chat completion response."""
tool_calls = parsed.get("tool_calls") or []
tc_objects = []
for tc in tool_calls:
normalized_tool_call = _normalize_tool_call(tc)
if normalized_tool_call is None:
continue
sanitized_name, args = normalized_tool_call
tc_objects.append(
ChatCompletionMessageToolCall(
id=_new_prefixed_id("call"),
function=FunctionCall(
name=sanitized_name,
arguments=args,
),
index=len(tc_objects),
)
)
resolved_finish_reason = "tool_calls" if tc_objects else finish_reason
return ChatCompletionResponse(
id=_new_prefixed_id("chatcmpl"),
created=int(time.time()),
model=model,
choices=[
Choice(
message=Message(
role="assistant",
content=parsed.get("content"),
reasoning_content=parsed.get("reasoning_content"),
tool_calls=tc_objects or None,
),
finish_reason=resolved_finish_reason,
)
],
usage=UsageInfo(
prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
total_tokens=prompt_tokens + completion_tokens,
),
)
# ---------------------------------------------------------------------------
# Streaming
# ---------------------------------------------------------------------------
def _sse(data: dict | ChatCompletionChunk) -> str:
payload = data.model_dump() if isinstance(data, ChatCompletionChunk) else data
return f"data: {json.dumps(payload)}\n\n"
@dataclass(frozen=True)
class _StreamRequestContext:
"""Immutable per-request inputs for the streaming code path."""
log_id: str
request_start_ts: float
chat_id: str
created: int
model: str
input_ids: list[int]
prompt_len: int
temp: float
top_p: float
max_tokens: int
repetition_penalty: float
repetition_context_size: int
stop_sequences: list[str]
seed: int | None
max_stop_len: int
parse_tools: bool
def _stream_chunk(
context: _StreamRequestContext,
*,
delta: Delta,
finish_reason: str | None = None,
usage: UsageInfo | None = None,
) -> ChatCompletionChunk:
return ChatCompletionChunk(
id=context.chat_id,
created=context.created,
model=context.model,
choices=[StreamingChoice(delta=delta, finish_reason=finish_reason)],
usage=usage,
)
def _build_stream_tool_call_chunks(
tool_calls: list[dict[str, Any]],
*,
context: _StreamRequestContext,
tool_call_index: int,
) -> tuple[list[ChatCompletionChunk], int, list[tuple[str, str]]]:
chunks: list[ChatCompletionChunk] = []
emitted_tool_calls: list[tuple[str, str]] = []
for tool_call in tool_calls:
normalized_tool_call = _normalize_tool_call(
tool_call,
warning_label="stream tool call",
)
if normalized_tool_call is None:
continue
tool_name, arguments = normalized_tool_call
emitted_tool_calls.append((tool_name, arguments))
tool_call_index += 1
chunks.append(
_stream_chunk(
context,
delta=Delta(
tool_calls=[
ChoiceDeltaToolCall(
index=tool_call_index,
id=_new_prefixed_id("call"),
function=ChoiceDeltaFunctionCall(
name=tool_name,
arguments=arguments,
),
)
],
),
)
)
return chunks, tool_call_index, emitted_tool_calls
@dataclass
class _StreamState:
"""Mutable fields that evolve while a stream is running."""
finish_reason: str = "stop"
completion_tokens: int = 0
tool_call_index: int = -1
stop_tail: str = ""
raw_output_parts: list[str] = field(default_factory=list)
emitted_content_parts: list[str] = field(default_factory=list)
emitted_reasoning_parts: list[str] = field(default_factory=list)
emitted_tool_calls: list[tuple[str, str]] = field(default_factory=list)
raw_preview_parts: list[str] = field(default_factory=list)
raw_preview_len: int = 0
raw_text_fallback: bool = False
disconnected: bool = False
stream_cancel_reason: str | None = None
def _build_stream_request_context(
request: ChatCompletionRequest,
*,
request_log_id: str | None = None,
) -> _StreamRequestContext:
log_id = request_log_id or _new_request_log_id()
input_ids = _prepare_prompt(request, request_log_id=log_id)
(
temp,
top_p,
max_tokens,
repetition_penalty,
repetition_context_size,
) = _resolve_sampling_params(request)
stop_sequences = _normalize_stop_sequences(request.stop)
return _StreamRequestContext(
log_id=log_id,
request_start_ts=time.time(),
chat_id=_new_prefixed_id("chatcmpl"),
created=int(time.time()),
model=_response_model_id(request.model),
input_ids=input_ids,
prompt_len=len(input_ids),
temp=temp,
top_p=top_p,
max_tokens=max_tokens,
repetition_penalty=repetition_penalty,
repetition_context_size=repetition_context_size,
stop_sequences=stop_sequences,
seed=request.seed,
max_stop_len=max((len(s) for s in stop_sequences), default=0),
parse_tools=bool(request.tools),
)
def _start_stream_worker(
*,
context: _StreamRequestContext,
loop: asyncio.AbstractEventLoop,
chunk_queue: asyncio.Queue[tuple[str, str | None, int] | None],
stop_event: Event,
worker_failed: Event,
) -> None:
"""Run model streaming in executor and push chunks into ``chunk_queue``."""
def _run():
try:
for resp in _model.generate_stream(
context.input_ids,
max_tokens=context.max_tokens,
temperature=context.temp,
top_p=context.top_p,
repetition_penalty=context.repetition_penalty,
repetition_context_size=context.repetition_context_size,
seed=context.seed,
):
if stop_event.is_set():
break
loop.call_soon_threadsafe(
chunk_queue.put_nowait,
(
resp.text or "",
(
_normalize_finish_reason(resp.finish_reason)
if resp.finish_reason is not None
else None
),
int(getattr(resp, "generation_tokens", 0) or 0),
),
)
if resp.finish_reason is not None:
break
except Exception:
worker_failed.set()
logger.exception("Streaming worker failed")
finally:
try:
loop.call_soon_threadsafe(chunk_queue.put_nowait, None)
except RuntimeError:
# Event loop can already be closed during server shutdown.
pass
_executor.submit(_run)
def _parse_streaming_fallback_output(
raw_text: str,
*,
parse_tools: bool,
) -> dict[str, Any]:
"""Recover a structured response from raw streamed text after parser failure."""
parser = HarmonyParser()
parsed = parser.parse(raw_text)
if not parse_tools:
parsed["tool_calls"] = []
return parsed
async def _stream_response(
request: ChatCompletionRequest,
client_request: Request | None = None,
*,
request_log_id: str | None = None,
) -> AsyncGenerator[str, None]:
"""SSE streaming generator matching the OpenAI protocol."""
global _stream_active_requests
context = _build_stream_request_context(
request,
request_log_id=request_log_id,
)
parse_tools = context.parse_tools
stop_sequences = context.stop_sequences
state = _StreamState()
stop_event = Event()
worker_failed = Event()
_stream_active_requests += 1
# First chunk: role-only delta (OpenAI convention).
yield _sse(_stream_chunk(context, delta=Delta(role="assistant")))
# Run inference in background thread, feed chunks through an async queue.
chunk_queue: asyncio.Queue[tuple[str, str | None, int] | None] = asyncio.Queue()
loop = asyncio.get_running_loop()
_start_stream_worker(
context=context,
loop=loop,
chunk_queue=chunk_queue,
stop_event=stop_event,
worker_failed=worker_failed,
)
parser = HarmonyParser()
async def _client_disconnected() -> bool:
if client_request is None:
return False
try:
return await client_request.is_disconnected()
except RuntimeError:
# Request context may be broken during shutdown.
return False
try:
while True:
if await _client_disconnected():
state.disconnected = True
state.stream_cancel_reason = "disconnect_poll"
break
try:
chunk = await asyncio.wait_for(chunk_queue.get(), timeout=0.2)
except asyncio.TimeoutError:
continue
if chunk is None:
if worker_failed.is_set():
state.stream_cancel_reason = "worker_error"
if stop_sequences and state.stop_tail:
state.emitted_content_parts.append(state.stop_tail)
yield _sse(
_stream_chunk(context, delta=Delta(content=state.stop_tail))
)
state.stop_tail = ""
# End of stream so flush parser.
if not state.raw_text_fallback and parse_tools:
try:
flushed, _ = parser.handle_stream_end(parse_tools=True)
except Exception as exc:
logger.warning(
"Harmony parse failed during stream flush. Switching to buffered fallback. error_type={}",
type(exc).__name__,
)
state.raw_text_fallback = True
flushed = None
if flushed and flushed.get("tool_calls"):
pending_tool_calls = _drop_emitted_tool_call_prefix(
flushed["tool_calls"],
state.emitted_tool_calls,
)
(
tool_call_chunks,
state.tool_call_index,
emitted_tool_calls,
) = (
_build_stream_tool_call_chunks(
pending_tool_calls,
context=context,
tool_call_index=state.tool_call_index,
)
)
state.emitted_tool_calls.extend(emitted_tool_calls)
if tool_call_chunks:
state.finish_reason = "tool_calls"
for chunk_item in tool_call_chunks:
yield _sse(chunk_item)
if state.raw_text_fallback and state.raw_output_parts:
parsed = _parse_streaming_fallback_output(
"".join(state.raw_output_parts),
parse_tools=parse_tools,
)
reasoning_content = parsed.get("reasoning_content")
if reasoning_content:
reasoning_content = _only_unseen_suffix(
reasoning_content,
"".join(state.emitted_reasoning_parts),
)
if reasoning_content:
state.emitted_reasoning_parts.append(reasoning_content)
yield _sse(