-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathproxy_transport.py
More file actions
656 lines (589 loc) · 23.8 KB
/
proxy_transport.py
File metadata and controls
656 lines (589 loc) · 23.8 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
from __future__ import annotations
import contextlib
import copy
import json
import os
import time
import uuid
from collections.abc import Callable
from typing import Any, cast
from modules.proxy.upstream_adapter import LiteLLMUpstreamAdapter
from modules.runtime.resource_manager import ResourceManager
type LogFunc = Callable[[str], None]
class ProxyTransport:
"""代理传输层:LiteLLM 上游调用与响应归一化。"""
def __init__(
self,
*,
resource_manager: ResourceManager,
disable_ssl_strict_mode: bool,
log_func: LogFunc = print,
) -> None:
self._resource_manager = resource_manager
self._log = log_func
self._adapter = LiteLLMUpstreamAdapter(
disable_ssl_strict_mode=disable_ssl_strict_mode,
log_func=log_func,
)
@property
def adapter(self) -> LiteLLMUpstreamAdapter:
return self._adapter
def close(self) -> None:
with contextlib.suppress(Exception):
self._adapter.close()
def prepare_sse_log_path(self) -> str:
log_dir = os.path.join(self._resource_manager.user_data_dir, "logs", "SSE")
os.makedirs(log_dir, exist_ok=True)
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"sse_{timestamp}_{int(time.time() * 1000)}.log"
return os.path.join(log_dir, filename)
def coerce_payload_dict(self, payload: Any) -> dict[str, Any] | None:
if isinstance(payload, dict):
return cast(dict[str, Any], payload)
model_dump = getattr(payload, "model_dump", None)
if callable(model_dump):
dumped = model_dump(exclude_none=False)
if isinstance(dumped, dict):
return cast(dict[str, Any], dumped)
dict_method = getattr(payload, "dict", None)
if callable(dict_method):
dumped = dict_method(exclude_none=False)
if isinstance(dumped, dict):
return cast(dict[str, Any], dumped)
json_method = getattr(payload, "json", None)
if callable(json_method):
try:
raw_json = json_method()
dumped = json.loads(raw_json) if isinstance(raw_json, str) else None
except Exception: # noqa: BLE001
dumped = None
if isinstance(dumped, dict):
return cast(dict[str, Any], dumped)
return None
def dump_payload_json(self, payload: Any) -> str:
payload_dict = self.coerce_payload_dict(payload)
if payload_dict is not None:
return json.dumps(payload_dict, ensure_ascii=False)
if isinstance(payload, str):
return payload
return repr(payload)
@staticmethod
def normalize_provider_model_name(model_name: str, *, provider: str) -> str:
prefix = f"{provider}/"
if model_name.startswith(prefix):
return model_name[len(prefix) :]
return model_name
def normalize_chat_completion_payload(
self,
payload: Any,
*,
provider: str,
fallback_model: str,
) -> dict[str, Any] | None:
payload_dict = self.coerce_payload_dict(payload)
if payload_dict is None:
return None
normalized = copy.deepcopy(payload_dict)
model_obj = normalized.get("model")
if isinstance(model_obj, str):
normalized["model"] = self.normalize_provider_model_name(
model_obj,
provider=provider,
)
elif fallback_model:
normalized["model"] = self.normalize_provider_model_name(
fallback_model,
provider=provider,
)
return normalized
def normalize_openai_event(
self, payload_input: Any, event_index: int, *, model_name: str, log: LogFunc
) -> tuple[bytes, str | None]:
if isinstance(payload_input, str) and payload_input.strip() == "[DONE]":
return b"data: [DONE]\n\n", None
payload = self.coerce_payload_dict(payload_input)
if payload is None:
payload_str = self.dump_payload_json(payload_input)
try:
payload_obj = json.loads(payload_str)
except Exception as exc: # noqa: BLE001
log(f"chunk#{event_index} JSON 解析失败,原样透传: {exc}")
return f"data: {payload_str}\n\n".encode(), None
if not isinstance(payload_obj, dict):
return f"data: {payload_str}\n\n".encode(), None
payload = cast(dict[str, Any], payload_obj)
chunk_obj = copy.deepcopy(payload)
chunk_obj["id"] = payload.get("id") or self._new_request_id("chatcmpl")
chunk_obj["object"] = "chat.completion.chunk"
chunk_obj["created"] = int(payload.get("created") or time.time())
chunk_obj["model"] = model_name or payload.get("model") or ""
choices_obj = payload.get("choices")
normalized_choices: list[dict[str, Any]] = []
finish_reason: str | None = None
if isinstance(choices_obj, list):
choices_list = cast(list[object], choices_obj)
for choice_index, item in enumerate(choices_list):
if not isinstance(item, dict):
continue
normalized_choice = self._normalize_openai_choice_chunk(
cast(dict[str, Any], item),
event_index=event_index,
choice_index=choice_index,
)
normalized_choices.append(normalized_choice)
if finish_reason is None:
finish_reason_obj = normalized_choice.get("finish_reason")
if isinstance(finish_reason_obj, str) and finish_reason_obj:
finish_reason = finish_reason_obj
chunk_obj["choices"] = normalized_choices
chunk_json = json.dumps(chunk_obj, ensure_ascii=False)
return f"data: {chunk_json}\n\n".encode(), finish_reason
@staticmethod
def _normalize_openai_choice_chunk(
choice: dict[str, Any],
*,
event_index: int,
choice_index: int,
) -> dict[str, Any]:
normalized_choice = copy.deepcopy(choice)
raw_delta_obj = normalized_choice.get("delta")
has_raw_delta = isinstance(raw_delta_obj, dict)
raw_delta: dict[str, Any] = (
cast(dict[str, Any], raw_delta_obj) if has_raw_delta else {}
)
message_obj = normalized_choice.pop("message", None)
message: dict[str, Any] = (
cast(dict[str, Any], message_obj) if isinstance(message_obj, dict) else {}
)
if has_raw_delta or message:
delta = copy.deepcopy(raw_delta)
role = delta.get("role") or message.get("role")
if role or event_index == 1:
delta.setdefault("role", role or "assistant")
if "content" not in delta:
content = message.get("content")
if content:
delta["content"] = content
for key in ("tool_calls", "function_calls", "reasoning_content"):
if key not in delta:
value = message.get(key)
if value not in (None, []):
delta[key] = value
normalized_choice["delta"] = delta
normalized_choice.setdefault("index", choice_index)
if "finish_reason" not in normalized_choice:
normalized_choice["finish_reason"] = None
return normalized_choice
@staticmethod
def _new_request_id(prefix: str = "resp") -> str:
return f"{prefix}_{uuid.uuid4().hex}"
@staticmethod
def _split_text_chunks(text: str, *, chunk_size: int = 10) -> list[str]:
if not text:
return []
return [text[i : i + chunk_size] for i in range(0, len(text), chunk_size)]
def normalize_response_payload(self, payload: Any) -> dict[str, Any] | None:
payload_dict = self.coerce_payload_dict(payload)
if payload_dict is None:
return None
normalized = copy.deepcopy(payload_dict)
response_id = normalized.get("id")
if not isinstance(response_id, str) or not response_id.strip():
normalized["id"] = self._new_request_id()
created_at = normalized.get("created_at")
if not isinstance(created_at, int):
created = normalized.pop("created", None)
if isinstance(created, (int, float)):
normalized["created_at"] = int(created)
else:
normalized["created_at"] = int(time.time())
else:
normalized.pop("created", None)
normalized["object"] = "response"
if not isinstance(normalized.get("status"), str):
normalized["status"] = "completed"
output = normalized.get("output")
if not isinstance(output, list):
normalized["output"] = []
return normalized
def serialize_response_event(
self,
payload_input: Any,
*,
log: LogFunc,
) -> tuple[bytes, str | None]:
if isinstance(payload_input, str) and payload_input.strip() == "[DONE]":
return b"data: [DONE]\n\n", None
payload = self.coerce_payload_dict(payload_input)
if payload is None:
payload_str = self.dump_payload_json(payload_input)
try:
payload_obj = json.loads(payload_str)
except Exception as exc: # noqa: BLE001
log(f"响应事件 JSON 解析失败,原样透传: {exc}")
return f"data: {payload_str}\n\n".encode(), None
if not isinstance(payload_obj, dict):
return f"data: {payload_str}\n\n".encode(), None
payload = cast(dict[str, Any], payload_obj)
event_type_obj = payload.get("type")
event_type = event_type_obj if isinstance(event_type_obj, str) else None
payload_json = json.dumps(payload, ensure_ascii=False)
if event_type:
return f"event: {event_type}\ndata: {payload_json}\n\n".encode(), event_type
return f"data: {payload_json}\n\n".encode(), None
@staticmethod
def _new_output_item_id(item_type: str | None) -> str:
prefix = "msg"
if item_type == "reasoning":
prefix = "rs"
elif item_type == "function_call":
prefix = "fc"
return f"{prefix}_{uuid.uuid4().hex}"
def _build_content_part_stream_events(
self,
*,
item_id: str,
output_index: int,
content_index: int,
part: dict[str, Any],
) -> list[dict[str, Any]]:
part_type = part.get("type")
if part_type == "output_text":
text_obj = part.get("text")
text = text_obj if isinstance(text_obj, str) else ""
annotations_obj = part.get("annotations")
annotations: list[Any] = (
cast(list[Any], annotations_obj) if isinstance(annotations_obj, list) else []
)
events: list[dict[str, Any]] = [
{
"type": "response.content_part.added",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": {
"type": "output_text",
"text": "",
"annotations": annotations,
},
}
]
for chunk in self._split_text_chunks(text):
events.append(
{
"type": "response.output_text.delta",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"delta": chunk,
}
)
events.append(
{
"type": "response.output_text.done",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"text": text,
}
)
done_part = copy.deepcopy(part)
done_part["annotations"] = annotations
events.append(
{
"type": "response.content_part.done",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": done_part,
}
)
return events
if part_type == "refusal":
refusal_obj = part.get("refusal")
refusal = refusal_obj if isinstance(refusal_obj, str) else ""
events = [
{
"type": "response.content_part.added",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": {"type": "refusal", "refusal": ""},
}
]
for chunk in self._split_text_chunks(refusal):
events.append(
{
"type": "response.refusal.delta",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"delta": chunk,
}
)
events.append(
{
"type": "response.refusal.done",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"refusal": refusal,
}
)
events.append(
{
"type": "response.content_part.done",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": {"type": "refusal", "refusal": refusal},
}
)
return events
return [
{
"type": "response.content_part.added",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": copy.deepcopy(part),
},
{
"type": "response.content_part.done",
"item_id": item_id,
"output_index": output_index,
"content_index": content_index,
"part": copy.deepcopy(part),
},
]
def _build_output_item_stream_events(
self,
*,
item: dict[str, Any],
output_index: int,
sequence_number: int,
) -> tuple[list[dict[str, Any]], int]:
item_copy = copy.deepcopy(item)
item_type_obj = item_copy.get("type")
item_type = item_type_obj if isinstance(item_type_obj, str) else None
item_id_obj = item_copy.get("id")
item_id = (
item_id_obj
if isinstance(item_id_obj, str) and item_id_obj.strip()
else self._new_output_item_id(item_type)
)
item_copy["id"] = item_id
added_item = copy.deepcopy(item_copy)
if item_type == "message":
added_item["status"] = "in_progress"
added_item["content"] = []
elif item_type == "reasoning" and not isinstance(added_item.get("status"), str):
added_item["status"] = "in_progress"
events: list[dict[str, Any]] = [
{
"type": "response.output_item.added",
"output_index": output_index,
"item": added_item,
}
]
content_obj = item_copy.get("content")
if item_type == "message" and isinstance(content_obj, list):
content_list = cast(list[object], content_obj)
for content_index, part_obj in enumerate(content_list):
if not isinstance(part_obj, dict):
continue
events.extend(
self._build_content_part_stream_events(
item_id=item_id,
output_index=output_index,
content_index=content_index,
part=cast(dict[str, Any], part_obj),
)
)
next_sequence_number = sequence_number + 1
completed_item = copy.deepcopy(item_copy)
if item_type == "message" and not isinstance(completed_item.get("status"), str):
completed_item["status"] = "completed"
events.append(
{
"type": "response.output_item.done",
"output_index": output_index,
"sequence_number": next_sequence_number,
"item": completed_item,
}
)
return events, next_sequence_number
def build_response_stream_events(
self,
response_payload: dict[str, Any],
) -> list[dict[str, Any]]:
normalized = self.normalize_response_payload(response_payload)
if normalized is None:
return []
in_progress_response = copy.deepcopy(normalized)
in_progress_response["status"] = "in_progress"
in_progress_response["output"] = []
events: list[dict[str, Any]] = [
{
"type": "response.created",
"response": copy.deepcopy(in_progress_response),
},
{
"type": "response.in_progress",
"response": copy.deepcopy(in_progress_response),
},
]
output_obj = normalized.get("output")
output_items = cast(list[object], output_obj) if isinstance(output_obj, list) else []
sequence_number = 0
for output_index, item_obj in enumerate(output_items):
if not isinstance(item_obj, dict):
continue
item_events, sequence_number = self._build_output_item_stream_events(
item=cast(dict[str, Any], item_obj),
output_index=output_index,
sequence_number=sequence_number,
)
events.extend(item_events)
events.append({"type": "response.completed", "response": normalized})
return events
def build_chat_completion_stream_chunks(
self,
response_payload: dict[str, Any],
) -> list[dict[str, Any]]:
response = copy.deepcopy(response_payload)
choices_obj = response.get("choices")
if not isinstance(choices_obj, list) or not choices_obj:
return []
choices = cast(list[object], choices_obj)
simulated_choices: list[dict[str, Any]] = []
for fallback_index, choice_obj in enumerate(choices):
if not isinstance(choice_obj, dict):
continue
choice = cast(dict[str, Any], choice_obj)
choice_index_obj = choice.get("index")
choice_index = (
choice_index_obj if isinstance(choice_index_obj, int) else fallback_index
)
message_obj = choice.get("message")
message = (
cast(dict[str, Any], message_obj) if isinstance(message_obj, dict) else {}
)
finish_reason_obj = choice.get("finish_reason")
finish_reason = (
finish_reason_obj if isinstance(finish_reason_obj, str) else "stop"
)
simulated_choices.append(
{
"index": choice_index,
"message": message,
"finish_reason": finish_reason,
}
)
if not simulated_choices:
return []
model = response.get("model")
model_name = model if isinstance(model, str) else ""
created = response.get("created")
created_at = created if isinstance(created, int) else int(time.time())
response_id = response.get("id")
chunk_id = (
response_id
if isinstance(response_id, str) and response_id
else self._new_request_id("chatcmpl")
)
base_chunk = {
"id": chunk_id,
"object": "chat.completion.chunk",
"created": created_at,
"model": model_name,
}
chunks: list[dict[str, Any]] = [
{
**base_chunk,
"choices": [
{
"index": cast(int, choice["index"]),
"delta": {
"role": (
choice["message"].get("role")
if isinstance(choice["message"].get("role"), str)
else "assistant"
)
},
"finish_reason": None,
}
for choice in simulated_choices
],
}
]
for choice in simulated_choices:
choice_index = cast(int, choice["index"])
message = cast(dict[str, Any], choice["message"])
reasoning_obj = message.get("reasoning_content")
reasoning_content = reasoning_obj if isinstance(reasoning_obj, str) else ""
for chunk_text in self._split_text_chunks(reasoning_content):
chunks.append(
{
**base_chunk,
"choices": [
{
"index": choice_index,
"delta": {"reasoning_content": chunk_text},
"finish_reason": None,
}
],
}
)
content_obj = message.get("content")
content = content_obj if isinstance(content_obj, str) else ""
for chunk_text in self._split_text_chunks(content):
chunks.append(
{
**base_chunk,
"choices": [
{
"index": choice_index,
"delta": {"content": chunk_text},
"finish_reason": None,
}
],
}
)
tool_calls_obj = message.get("tool_calls")
if isinstance(tool_calls_obj, list) and tool_calls_obj:
tool_calls: list[Any] = []
for tool_call_index, tool_call_obj in enumerate(cast(list[Any], tool_calls_obj)):
if isinstance(tool_call_obj, dict):
tool_call = copy.deepcopy(cast(dict[str, Any], tool_call_obj))
tool_call.setdefault("index", tool_call_index)
tool_calls.append(tool_call)
else:
tool_calls.append(tool_call_obj)
chunks.append(
{
**base_chunk,
"choices": [
{
"index": choice_index,
"delta": {"tool_calls": tool_calls},
"finish_reason": None,
}
],
}
)
chunks.append(
{
**base_chunk,
"choices": [
{
"index": cast(int, choice["index"]),
"delta": {},
"finish_reason": cast(str, choice["finish_reason"]),
}
for choice in simulated_choices
],
}
)
return chunks
__all__ = ["ProxyTransport"]