-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsampling_proxy.py
More file actions
2920 lines (2571 loc) · 152 KB
/
Copy pathsampling_proxy.py
File metadata and controls
2920 lines (2571 loc) · 152 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
import os
import json
import logging
import httpx
import fnmatch
from typing import Optional
from fastapi import FastAPI, Request, Response, status
from fastapi.responses import StreamingResponse
from contextlib import asynccontextmanager
import uvicorn
import asyncio # Import asyncio for potential sleep
import argparse # Import argparse for command-line arguments
import threading
# Configure logger
logger = logging.getLogger("sampling_proxy")
logger.setLevel(logging.DEBUG)
logging.basicConfig(
level=logging.INFO,
format="%(levelname)s: %(message)s",
)
# Global request counter for log correlation
_request_counter = 0
_request_counter_lock = threading.Lock()
def get_request_id():
"""Get next request ID for log correlation."""
global _request_counter
with _request_counter_lock:
_request_counter += 1
return _request_counter
def log_info(request_id: int, message: str):
"""Log info with request ID prefix."""
logger.info("[R:%s] %s", request_id, message)
# Import validator module for garbage detection
from validator import (
validate_response,
validate_response_partial,
save_failed_response,
save_mid_stream_failure,
create_error_message,
calculate_retry_delay,
ValidationResult,
StreamingValidator,
StreamingValidationBuffer,
count_words_in_text,
extract_text_from_sse_chunks,
build_anthropic_error_stream,
build_openai_error_stream,
build_anthropic_error_json,
build_openai_error_json
)
# Import throttle manager for request throttling
from throttle_manager import ThrottleManager
def load_config(config_path="config.json"):
"""
Load configuration from JSON file.
Returns a dictionary with configuration values.
If config file doesn't exist or is invalid, returns default values.
"""
default_config = {
"listen": {
"host": "0.0.0.0",
"port": 8001,
"base_path": ""
},
"upstreams": [],
"model_upstream_binding": {},
"logging": {
"enable_debug_logs": False,
"enable_override_logs": False
},
"default_sampling_params": {},
"override": {
"only_anthropic": False,
"model_name": None,
"sampling_params": {}
},
"model_sampling_params": {},
"validation": {
"enabled": False,
"validator_url": "http://127.0.0.1:1234",
"validator_model": "qwen-3.5-0.8b",
"supports_openai": True,
"supports_anthropic": False,
"connect_timeout_seconds": 5.0,
"timeout_seconds": 300.0,
"max_retries": 3,
"retry_base_delay_seconds": 1.0,
"retry_multiplier": 2.0,
"mid_stream_validation_enabled": False,
"mid_stream_validation_interval_words": 300
},
"parallel_limits": {},
"throttle": {
"enabled": False,
"global": {
"start_pause_seconds": None,
"end_pause_seconds": None
},
"per_model": {}
}
}
if not os.path.exists(config_path):
logger.warning(f"Config file '{config_path}' not found. Using default values.")
return default_config
try:
with open(config_path, 'r', encoding='utf-8') as f:
config = json.load(f)
# Merge with defaults to ensure all required keys exist
merged_config = default_config.copy()
for key, value in config.items():
if key in merged_config:
if isinstance(merged_config[key], dict) and isinstance(value, dict):
merged_config[key].update(value)
else:
merged_config[key] = value
else:
merged_config[key] = value
# --- Backward compat: convert legacy 'server' block to 'upstreams' ---
# If 'upstreams' is empty/absent but 'server.target_base_url' exists, wrap as single upstream
if not merged_config.get("upstreams") and "server" in config:
server_cfg = config["server"]
base_url = server_cfg.get("target_base_url", "http://127.0.0.1:8000/v1")
merged_config["upstreams"] = [{
"name": "default",
"base_url": base_url,
"connect_timeout_seconds": server_cfg.get("connect_timeout_seconds", 5.0),
"timeout_seconds": server_cfg.get("timeout_seconds", 1200.0),
"supports_openai": server_cfg.get("supports_openai", True),
"supports_anthropic": server_cfg.get("supports_anthropic", False),
}]
# Migrate proxy listen config from server block
listen_cfg = merged_config.setdefault("listen", {})
if not listen_cfg.get("host"):
listen_cfg["host"] = server_cfg.get("sampling_proxy_host", "0.0.0.0")
if not listen_cfg.get("port"):
listen_cfg["port"] = server_cfg.get("sampling_proxy_port", 8001)
if not listen_cfg.get("base_path"):
listen_cfg["base_path"] = server_cfg.get("sampling_proxy_base_path", "")
logger.info("Legacy 'server' block detected — wrapped as single upstream named 'default'")
# If 'model_upstream_binding' is absent and upstreams exist with a single entry,
# auto-bind everything to that upstream
if not merged_config.get("model_upstream_binding") and merged_config.get("upstreams"):
upstreams = merged_config["upstreams"]
if len(upstreams) == 1:
merged_config["model_upstream_binding"] = {"*": upstreams[0]["name"]}
logger.info("Single upstream detected — auto-bound all models via wildcard '*'")
# Validate: at least one upstream required
if not merged_config.get("upstreams"):
logger.warning("No upstream servers configured. Requests cannot be proxied.")
# Filter out null values from sampling params (convert to empty dicts)
if merged_config.get("default_sampling_params"):
merged_config["default_sampling_params"] = {
k: v for k, v in merged_config["default_sampling_params"].items()
if v is not None
}
# Filter out null values from override.sampling_params
if merged_config.get("override"):
override_config = merged_config["override"]
if "sampling_params" in override_config:
override_config["sampling_params"] = {
k: v for k, v in override_config["sampling_params"].items()
if v is not None
}
if merged_config.get("model_sampling_params"):
filtered_model_params = {}
for model, params in merged_config["model_sampling_params"].items():
filtered_params = {
k: v for k, v in params.items()
if v is not None
}
if filtered_params: # Only include models with non-null params
filtered_model_params[model] = filtered_params
merged_config["model_sampling_params"] = filtered_model_params
logger.info(f"Configuration loaded from '{config_path}'")
return merged_config
except json.JSONDecodeError as e:
logger.error(f"Invalid JSON in config file '{config_path}': {e}. Using default values.")
return default_config
except Exception as e:
logger.error(f"Error loading config file '{config_path}': {e}. Using default values.")
return default_config
def extract_base_path(url):
"""
Extract the base path from a URL.
For example, "http://127.0.0.1:8000/abc/v4" returns "/abc/v4"
"""
from urllib.parse import urlparse
parsed = urlparse(url)
return parsed.path
def transform_path(original_path, from_base_path, to_base_path):
"""
Transform a path from one base path to another.
For example, with from_base_path="/v1" and to_base_path="/abc/v4":
"/v1/completions" -> "/abc/v4/completions"
"/v1/chat/completions" -> "/abc/v4/chat/completions"
If original_path doesn't start with from_base_path, it's returned unchanged.
"""
# Ensure base paths start with /
if not from_base_path.startswith('/'):
from_base_path = '/' + from_base_path
if not to_base_path.startswith('/'):
to_base_path = '/' + to_base_path
# Remove trailing slashes for consistent comparison
from_base_path = from_base_path.rstrip('/')
to_base_path = to_base_path.rstrip('/')
# Check if the path starts with the from_base_path
if original_path.startswith(from_base_path):
# Replace the base path
return original_path.replace(from_base_path, to_base_path, 1)
else:
# Path doesn't start with the expected base path, return as is
return original_path
# --- Configuration ---
# These will be initialized in the main block after loading config
SAMPLING_PROXY_HOST = None
SAMPLING_PROXY_PORT = None
SAMPLING_PROXY_BASE_PATH = None
ENABLE_DEBUG_LOGS = False
ENABLE_OVERRIDE_LOGS = False
ENABLE_VALIDATION_LOGS = False
DEFAULT_SAMPLING_PARAMS = {}
OVERRIDE_CONFIG = {}
OVERRIDE_ONLY_ANTHROPIC = False
OVERRIDE_MODEL_NAME = None
OVERRIDE_SAMPLING_PARAMS = {}
MODEL_SAMPLING_PARAMS = {}
# Upstream server pool: name -> {name, base_url, base_path, supports_openai, supports_anthropic}
UPSTREAM_CONFIGS: dict = {}
# Per-upstream httpx clients: name -> httpx.AsyncClient
UPSTREAM_CLIENTS: dict = {}
# Model-to-upstream binding: pattern -> upstream_name (supports glob patterns)
MODEL_UPSTREAM_BINDING: dict = {}
# Server capability configuration — DEPRECATED, kept for backward compat within handler
# Replaced by per-upstream lookups
SERVER_SUPPORTS_OPENAI = True
SERVER_SUPPORTS_ANTHROPIC = False
VALIDATION_CONFIG = {"enabled": False}
THROTTLE_CONFIG = {"enabled": False}
# Per-model parallel request limits (model_name -> asyncio.Semaphore)
PARALLEL_LIMITS = {}
MODEL_SEMAPHORES = {}
# Global parallel request limit (across all models)
GLOBAL_LIMIT = None
GLOBAL_SEMAPHORE = None
# Throttle manager for request pacing
throttle_manager = None
# List of API path suffixes that are considered "generation" endpoints.
# Note: We check if the path ENDS WITH these suffixes to handle various prefixes
GENERATION_ENDPOINT_SUFFIXES = [
"generate", # Common SGLang generation endpoint
"completions", # OpenAI-compatible completions endpoint
"chat/completions", # OpenAI-compatible chat completions endpoint
"v1/messages", # Anthropic-compatible messages endpoint
]
# List of Anthropic-specific endpoints that should be handled locally
ANTHROPIC_ENDPOINTS = [
"api/event_logging/batch", # Anthropic event logging endpoint
"v1/messages/count_tokens", # Anthropic token counting endpoint
]
# Global variable to store the first available model name from /models to be used for anthropic requests
FIRST_AVAILABLE_MODEL = "any" # sglang allows any model name, vllm require exact match
def resolve_upstream_for_model(model_name: str) -> Optional[str]:
"""Resolve which upstream a model should route to.
Lookup order:
1. Exact match in model_upstream_binding
2. First matching glob pattern (fnmatch) in config order
3. None if no match
"""
if not model_name:
return None
# Exact match first
if model_name in MODEL_UPSTREAM_BINDING:
return MODEL_UPSTREAM_BINDING[model_name]
# Then glob patterns (in config order, first match wins)
for pattern, upstream_name in MODEL_UPSTREAM_BINDING.items():
if '*' in pattern or '?' in pattern or '[' in pattern:
if fnmatch.fnmatch(model_name, pattern):
return upstream_name
return None
def get_upstream_config(upstream_name: str) -> Optional[dict]:
"""Get upstream config by name. Returns None if not found."""
return UPSTREAM_CONFIGS.get(upstream_name)
def get_upstream_client(upstream_name: str) -> Optional[httpx.AsyncClient]:
"""Get httpx client for an upstream by name. Returns None if not found."""
return UPSTREAM_CLIENTS.get(upstream_name)
def get_model_semaphore(model_name: str):
"""Get the semaphore for a model if a parallel limit is configured. Returns None if no limit."""
return MODEL_SEMAPHORES.get(model_name.lower())
def extract_model_for_throttle(request_data: dict) -> str:
"""Extract model name from request data for throttle lookup."""
model = request_data.get("model")
if model:
return model
return "global"
def get_global_semaphore():
"""Get the global semaphore if a global limit is configured. Returns None if no limit."""
return GLOBAL_SEMAPHORE
# --- FastAPI Application Lifespan Setup ---
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Handles startup and shutdown events for the FastAPI application.
Ensures the httpx clients are properly closed when the application shuts down.
"""
global FIRST_AVAILABLE_MODEL
logger.info("FastAPI application startup.")
# Initialize per-upstream clients
for name, upstream_cfg in UPSTREAM_CONFIGS.items():
connect_timeout = upstream_cfg.get("connect_timeout_seconds", 5.0)
read_timeout = upstream_cfg.get("timeout_seconds", 1200.0)
timeout = httpx.Timeout(connect=connect_timeout, read=read_timeout, write=read_timeout, pool=connect_timeout)
client = httpx.AsyncClient(base_url=upstream_cfg["base_url"], timeout=timeout)
UPSTREAM_CLIENTS[name] = client
logger.info(f"Initialized upstream client '{name}': {upstream_cfg['base_url']}")
# Validate: at least one upstream must support some format
has_any_capability = any(
cfg.get("supports_openai") or cfg.get("supports_anthropic")
for cfg in UPSTREAM_CONFIGS.values()
)
if not has_any_capability:
raise ValueError(
"Invalid configuration: at least one upstream must support OpenAI or Anthropic format."
)
# Poll /models to get the first available model from the first OpenAI-capable upstream
# Skip if: 1) no upstream supports OpenAI, or 2) override model already configured
if not OVERRIDE_MODEL_NAME:
openai_upstream = None
for name, cfg in UPSTREAM_CONFIGS.items():
if cfg.get("supports_openai"):
openai_upstream = (name, cfg)
break
if openai_upstream:
upstream_name, upstream_cfg = openai_upstream
upstream_client = UPSTREAM_CLIENTS[upstream_name]
base_path = upstream_cfg["base_path"]
models_path = "/models" if base_path else "/v1/models"
try:
logger.info(f"Polling {upstream_cfg['base_url']}{models_path} to get available models...")
response = await upstream_client.get(models_path)
if response.status_code == 200:
models_data = response.json()
if "data" in models_data and len(models_data["data"]) > 0:
FIRST_AVAILABLE_MODEL = models_data["data"][0]["id"]
logger.info(f"Successfully retrieved first available model: {FIRST_AVAILABLE_MODEL}")
else:
logger.warning("No models found in /models response")
else:
logger.warning(f"Failed to get models from {models_path}. Status: {response.status_code}")
except Exception as e:
logger.warning(f"Error polling {models_path}: {e}")
elif OVERRIDE_MODEL_NAME:
FIRST_AVAILABLE_MODEL = OVERRIDE_MODEL_NAME
logger.info(f"Using override model name: {FIRST_AVAILABLE_MODEL}")
yield # Application starts here
logger.info("FastAPI application shutdown.")
for name, client in UPSTREAM_CLIENTS.items():
await client.aclose()
logger.info(f"Upstream client '{name}' closed.")
# --- FastAPI Application Setup ---
app = FastAPI(
title="Sampling Proxy",
description="A middleware server to override sampling parameters for generation requests, supports OpenAI-compatible and Anthropic request formats.",
version="1.0.0",
lifespan=lifespan # Register the lifespan context manager
)
@app.get("/")
async def read_root():
"""
Root endpoint for a basic health check and to display middleware configuration.
"""
upstreams_info = []
for name, cfg in UPSTREAM_CONFIGS.items():
upstreams_info.append({
"name": name,
"base_url": cfg["base_url"],
"supports_openai": cfg.get("supports_openai"),
"supports_anthropic": cfg.get("supports_anthropic"),
})
return {
"message": "Sampling Proxy is running.",
"upstreams": upstreams_info,
"model_upstream_binding": MODEL_UPSTREAM_BINDING,
"sampling_proxy_port": SAMPLING_PROXY_PORT,
"default_sampling_params": DEFAULT_SAMPLING_PARAMS,
"override": OVERRIDE_CONFIG,
"model_sampling_params_configured": list(MODEL_SAMPLING_PARAMS.keys()),
"generation_endpoints_monitored": GENERATION_ENDPOINT_SUFFIXES,
"anthropic_endpoints_handled_locally": ANTHROPIC_ENDPOINTS,
"debug_logs_enabled": ENABLE_DEBUG_LOGS,
"parallel_limits": {**({"global": GLOBAL_LIMIT} if GLOBAL_LIMIT is not None else {}), **PARALLEL_LIMITS},
}
def parse_sse_to_response(sse_text: str) -> Optional[dict]:
"""Parse SSE stream text to extract final response dict."""
content_blocks = {}
current_index = None
message_data = None
for line in sse_text.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('data: '):
data_str = line[6:]
if data_str == '[DONE]':
continue
try:
data = json.loads(data_str)
event_type = data.get('type')
if event_type == 'message_start':
message_data = data.get('message', {})
elif event_type == 'content_block_start':
index = data.get('index', 0)
content_blocks[index] = data.get('content_block', {}).copy()
current_index = index
elif event_type == 'content_block_delta':
index = data.get('index', 0)
delta = data.get('delta', {})
if index not in content_blocks:
content_blocks[index] = {}
if delta.get('type') == 'text_delta':
existing_text = content_blocks[index].get('text', '')
content_blocks[index]['text'] = existing_text + delta.get('text', '')
elif delta.get('type') == 'input_json_delta':
existing_json = content_blocks[index].get('_partial_json', '')
content_blocks[index]['_partial_json'] = existing_json + delta.get('partial_json', '')
elif event_type == 'message_stop':
# Build final response
if message_data:
content = []
for idx in sorted(content_blocks.keys()):
block = content_blocks[idx]
block_type = block.get('type', 'text')
if block_type == 'tool_use':
partial_json = block.pop('_partial_json', '')
if partial_json:
try:
block['input'] = json.loads(partial_json)
except json.JSONDecodeError:
block['input'] = {}
content.append(block)
else:
content.append(block)
message_data['content'] = content
return message_data
except json.JSONDecodeError:
continue
return None
def parse_openai_sse_to_response(sse_text: str) -> Optional[dict]:
"""Parse OpenAI SSE stream text to reconstruct the full response dict."""
content_parts = []
tool_calls = {} # index -> {id, name, arguments}
finish_reason = None
response_id = None
model = None
usage = None
for line in sse_text.split('\n'):
line = line.strip()
if not line:
continue
if line.startswith('data: '):
data_str = line[6:]
if data_str == '[DONE]':
continue
try:
data = json.loads(data_str)
# Extract metadata from first chunk
if response_id is None:
response_id = data.get('id')
model = data.get('model')
# Extract usage if present
if 'usage' in data:
usage = data['usage']
choices = data.get('choices', [])
if choices:
choice = choices[0]
delta = choice.get('delta', {})
finish_reason = choice.get('finish_reason') or finish_reason
# Handle text content
if 'content' in delta and delta['content']:
content_parts.append(delta['content'])
# Handle tool calls
if 'tool_calls' in delta:
for tc in delta['tool_calls']:
idx = tc.get('index', 0)
if idx not in tool_calls:
tool_calls[idx] = {'id': '', 'name': '', 'arguments': ''}
if 'id' in tc:
tool_calls[idx]['id'] = tc['id']
if 'function' in tc:
if 'name' in tc['function']:
tool_calls[idx]['name'] = tc['function']['name']
if 'arguments' in tc['function']:
tool_calls[idx]['arguments'] += tc['function']['arguments']
except json.JSONDecodeError:
continue
# Build final OpenAI response
if response_id is None:
return None
# Build message content
message = {'role': 'assistant'}
if content_parts:
message['content'] = ''.join(content_parts)
else:
message['content'] = ''
if tool_calls:
message['tool_calls'] = []
for idx in sorted(tool_calls.keys()):
tc = tool_calls[idx]
message['tool_calls'].append({
'id': tc['id'] or f'call_{idx}',
'type': 'function',
'function': {
'name': tc['name'],
'arguments': tc['arguments']
}
})
response = {
'id': response_id,
'object': 'chat.completion',
'created': 0,
'model': model or '',
'choices': [{
'index': 0,
'message': message,
'finish_reason': finish_reason or 'stop'
}]
}
if usage:
response['usage'] = usage
return response
def convert_openai_sse_to_anthropic_chunks(sse_text: str) -> list:
"""Convert OpenAI SSE chunks to Anthropic SSE chunks for streaming."""
anthropic_chunks = []
content_block_index = 0
has_tool_calls = False
# First, collect all chunks to determine structure
openai_chunks = []
for line in sse_text.split('\n'):
line = line.strip()
if line.startswith('data: ') and line[6:] != '[DONE]':
try:
openai_chunks.append(json.loads(line[6:]))
except json.JSONDecodeError:
pass
# Generate message_start
if openai_chunks:
first_chunk = openai_chunks[0]
anthropic_chunks.append({
'type': 'message_start',
'message': {
'id': first_chunk.get('id', 'msg_unknown'),
'type': 'message',
'role': 'assistant',
'content': [],
'model': first_chunk.get('model', ''),
'stop_reason': None,
'usage': {'input_tokens': 0, 'output_tokens': 0}
}
})
# Process each chunk
for data in openai_chunks:
choices = data.get('choices', [])
if not choices:
continue
choice = choices[0]
delta = choice.get('delta', {})
# Handle text content
if 'content' in delta and delta['content']:
if not has_tool_calls:
# Only emit text deltas if we haven't started tool calls
anthropic_chunks.append({
'type': 'content_block_delta',
'index': content_block_index,
'delta': {
'type': 'text_delta',
'text': delta['content']
}
})
# Handle tool calls
if 'tool_calls' in delta:
has_tool_calls = True
for tc in delta['tool_calls']:
idx = tc.get('index', 0)
if 'function' in tc:
func = tc['function']
if 'name' in func:
# Start new tool call block
content_block_index = idx
anthropic_chunks.append({
'type': 'content_block_start',
'index': idx,
'content_block': {
'type': 'tool_use',
'id': tc.get('id', f'toolu_{idx}'),
'name': func['name'],
'input': {}
}
})
elif 'arguments' in func:
# Arguments delta
anthropic_chunks.append({
'type': 'content_block_delta',
'index': idx,
'delta': {
'type': 'input_json_delta',
'partial_json': func['arguments']
}
})
# Handle finish_reason
if 'finish_reason' in choice and choice['finish_reason']:
finish_reason = choice['finish_reason']
stop_reason_map = {
'stop': 'end_turn',
'length': 'max_tokens',
'tool_calls': 'tool_use',
'content_filter': 'stop_sequence',
'function_call': 'tool_use'
}
stop_reason = stop_reason_map.get(finish_reason, 'end_turn')
# Add usage if present
usage_data = None
if 'usage' in data:
usage_data = {
'input_tokens': data['usage'].get('prompt_tokens', 0),
'output_tokens': data['usage'].get('completion_tokens', 0)
}
anthropic_chunks.append({
'type': 'message_delta',
'delta': {'stop_reason': stop_reason},
'usage': usage_data or {'output_tokens': 0}
})
anthropic_chunks.append({'type': 'message_stop'})
# Ensure we have message_stop if not added
if anthropic_chunks and anthropic_chunks[-1].get('type') != 'message_stop':
anthropic_chunks.append({'type': 'message_stop'})
return anthropic_chunks
class _StatusStreamingResponse(StreamingResponse):
"""StreamingResponse that allows the async generator to override the status code before any bytes are sent."""
def __init__(self, content, status_holder: dict, **kwargs):
super().__init__(content, **kwargs)
self._status_holder = status_holder
async def __call__(self, scope, receive, send):
# Override status code if the generator set it
if "status_code" in self._status_holder:
self.status_code = self._status_holder["status_code"]
await super().__call__(scope, receive, send)
@app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
async def proxy_target_requests(path: str, request: Request):
"""
Catch-all route to proxy all incoming requests to the upstream server.
For POST requests to configured generation endpoints, it applies
the sampling parameter override logic.
Supports streaming responses from the upstream server back to the client.
"""
# Access ENABLE_DEBUG_LOGS from the global scope
global ENABLE_DEBUG_LOGS
# Get request ID at the START for proper log correlation
request_id = get_request_id()
if ENABLE_DEBUG_LOGS:
logger.info(f"\n--- Incoming Request: {request.method} {path} ---")
# Normalize path by removing leading/trailing slashes for consistent matching
original_path = path
path = path.strip('/')
if ENABLE_DEBUG_LOGS:
logger.debug(f"Normalized path for matching: '{path}' (Original: '{original_path}')")
# Handle Anthropic-specific endpoints
if path in ANTHROPIC_ENDPOINTS:
# Determine which upstream to use:
# - count_tokens: extract model from body, resolve by model binding
# - event_logging: no model field, use first Anthropic-capable upstream
au_name = None
au_model = None
if path == "v1/messages/count_tokens":
try:
raw = await request.body()
body_json = json.loads(raw)
au_model = body_json.get("model")
if au_model:
au_name = resolve_upstream_for_model(au_model)
except (json.JSONDecodeError, Exception):
pass
if au_name is None:
# Fall back to first Anthropic-capable upstream
for uname, ucfg in UPSTREAM_CONFIGS.items():
if ucfg.get("supports_anthropic"):
au_name = uname
break
# Re-read body (await request.body() caches, so it's fine)
else:
# event_logging — no model to route by
for uname, ucfg in UPSTREAM_CONFIGS.items():
if ucfg.get("supports_anthropic"):
au_name = uname
break
if au_name:
au_cfg = UPSTREAM_CONFIGS[au_name]
au_client = UPSTREAM_CLIENTS[au_name]
au_base_path = au_cfg["base_path"]
# In Anthropic passthrough mode, proxy these endpoints upstream as-is
if ENABLE_DEBUG_LOGS:
logger.debug(f"Passthrough Anthropic endpoint '{path}' to upstream '{au_name}'" +
(f" (model: {au_model})" if au_model else ""))
target_path = transform_path("/" + original_path, SAMPLING_PROXY_BASE_PATH, au_base_path)
passthrough_headers = dict(request.headers)
passthrough_headers.pop("host", None)
passthrough_headers.pop("content-length", None)
passthrough_body = await request.body()
# Strip upstream base path to avoid doubling it (httpx client has base_url set)
if au_base_path and target_path.startswith(au_base_path):
relative_path = target_path[len(au_base_path):]
if relative_path and not relative_path.startswith('/'):
relative_path = '/' + relative_path
else:
relative_path = target_path
# Preserve query string from original request
passthrough_url = httpx.URL(path=relative_path, query=request.url.query.encode("utf-8"))
try:
upstream_response = await au_client.request(
method=request.method,
url=passthrough_url,
headers=passthrough_headers,
content=passthrough_body,
)
return Response(
content=upstream_response.content,
status_code=upstream_response.status_code,
media_type=upstream_response.headers.get("content-type", "application/json"),
)
except Exception as e:
if ENABLE_DEBUG_LOGS:
logger.error(f"Failed to proxy '{path}' upstream: {e}")
return Response(
content=json.dumps({"error": {"type": "api_error", "message": f"Failed to proxy request upstream: {str(e)}"}}),
status_code=502,
media_type="application/json"
)
else:
# In conversion mode, handle locally
if ENABLE_DEBUG_LOGS:
logger.debug(f"Handling Anthropic endpoint '{path}' locally")
if path == "api/event_logging/batch":
# Handle event logging endpoint - return success response
if ENABLE_DEBUG_LOGS:
logger.debug("Processing event logging request")
try:
# Read the request body to acknowledge receipt
body = await request.body()
if ENABLE_DEBUG_LOGS:
logger.debug(f"Event logging body received: {len(body)} bytes")
# Return a success response that mimics what Anthropic expects
response_data = {
"status": "success",
"message": "Events logged successfully"
}
return Response(
content=json.dumps(response_data),
status_code=200,
media_type="application/json"
)
except Exception as e:
if ENABLE_DEBUG_LOGS:
logger.error(f"Error processing event logging: {e}")
return Response(
content=json.dumps({"error": "Failed to process events"}),
status_code=500,
media_type="application/json"
)
elif path == "v1/messages/count_tokens":
# Handle token counting endpoint
if ENABLE_DEBUG_LOGS:
logger.debug("Processing token counting request")
try:
# Read and parse the request body
body = await request.body()
if ENABLE_DEBUG_LOGS:
logger.debug(f"Token counting body received: {len(body)} bytes")
request_data = json.loads(body.decode('utf-8'))
messages = request_data.get("messages", [])
model = request_data.get("model", "claude-3-sonnet-20241022")
if ENABLE_DEBUG_LOGS:
logger.debug(f"Token counting request - model: {model}, messages: {messages}")
# Simple token estimation (rough approximation)
# In a real implementation, you might want to use a proper tokenizer
total_tokens = 0
for message in messages:
content = message.get("content", "")
if isinstance(content, list):
# Handle complex content format
for content_item in content:
if isinstance(content_item, dict) and content_item.get("type") == "text":
text = content_item.get("text", "")
# Rough estimation: ~4 characters per token for English text
total_tokens += len(text) // 4 + 1
elif isinstance(content_item, str):
total_tokens += len(content_item) // 4 + 1
elif isinstance(content, str):
total_tokens += len(content) // 4 + 1
else:
total_tokens += len(str(content)) // 4 + 1
# Return response in Anthropic format
response_data = {
"input_tokens": total_tokens
}
if ENABLE_DEBUG_LOGS:
logger.debug(f"Token counting result: {total_tokens} tokens")
return Response(
content=json.dumps(response_data),
status_code=200,
media_type="application/json"
)
except json.JSONDecodeError as e:
if ENABLE_DEBUG_LOGS:
logger.error(f"Invalid JSON in token counting request: {e}")
return Response(
content=json.dumps({"error": {"type": "invalid_request_error", "message": "Invalid JSON"}}),
status_code=400,
media_type="application/json"
)
except Exception as e:
if ENABLE_DEBUG_LOGS:
logger.error(f"Error processing token counting: {e}")
return Response(
content=json.dumps({"error": {"type": "api_error", "message": "Failed to count tokens"}}),
status_code=500,
media_type="application/json"
)
# For any other Anthropic endpoints, return a generic success
return Response(
content=json.dumps({"status": "ok"}),
status_code=200,
media_type="application/json"
)
# Prepare headers for the outgoing request to upstream server.
# We copy the incoming headers and remove 'host' and 'content-length'
# as httpx will manage these for the new request.
headers = dict(request.headers)
headers.pop("host", None)
headers.pop("content-length", None) # httpx will recalculate if body changes
if ENABLE_DEBUG_LOGS:
logger.debug(f"Outgoing Request Headers (initial): {headers}")
request_content = None # This will hold the request body to be sent to target
is_generation_request = False
is_anthropic_request = False # Initialize Anthropic request flag
incoming_json_body = {} # Initialize in case it's not a POST/JSON request
model_name = None # Model name extracted from request
# Determine if the current request path is a recognized generation endpoint
# Use suffix matching to handle paths with or without v1 prefix
is_generation_request = any(path.endswith(suffix) for suffix in GENERATION_ENDPOINT_SUFFIXES)
is_anthropic_request = path.endswith("v1/messages") # Check if this is an Anthropic request
if ENABLE_DEBUG_LOGS:
logger.debug(f"is_generation_request after check: {is_generation_request}")
logger.debug(f"is_anthropic_request: {is_anthropic_request}")
# --- Upstream resolution ---
# For generation POST requests, we need the model name first (parsed below).
# For non-generation requests, resolve upstream now based on path format.
upstream_name = None
upstream_cfg = None
upstream_client = None
def _resolve_upstream_now():
"""Resolve upstream for non-generation or non-POST requests based on path format."""
nonlocal upstream_name, upstream_cfg, upstream_client
if is_anthropic_request:
for uname, ucfg in UPSTREAM_CONFIGS.items():
if ucfg.get("supports_anthropic"):
upstream_name = uname
upstream_cfg = ucfg
upstream_client = UPSTREAM_CLIENTS[uname]
return
# Fall back to OpenAI-compatible, then any upstream
for uname, ucfg in UPSTREAM_CONFIGS.items():
if ucfg.get("supports_openai"):
upstream_name = uname