File size: 61,436 Bytes
d8325d7 761ae15 54e1b21 761ae15 54e1b21 | 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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 | from __future__ import annotations
import os
import json
import logging
import time
import uuid
import threading
import requests
from flask import Flask, request, Response, jsonify, stream_with_context
from flask_cors import CORS
# ββ App setup βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = Flask(__name__)
CORS(app)
app.config["MAX_CONTENT_LENGTH"] = 50 * 1024 * 1024 # 50MB max body
# ββ Logging βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger("bridge")
# ββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
import os
PROVIDER_BASE_URL = os.environ["PROVIDER_BASE_URL"] # will raise KeyError if missing
PROVIDER_API_KEY = os.environ["PROVIDER_API_KEY"]
PROVIDER_HEADERS = {
"Authorization": f"Bearer {PROVIDER_API_KEY}",
"Content-Type": "application/json",
}
TIMEOUTS = {
"chat": 120,
"image": 180,
"audio": 180,
"embeddings": 60,
"models": 10,
"moderations": 30,
"files": 60,
"batches": 30,
}
RETRY_MAX = 3
RETRY_STATUSES = {429, 500, 502, 503, 504}
RETRY_BASE_DELAY = 1.0
KNOWN_CHAT_PARAMS = {
"model", "messages", "stream", "temperature", "top_p", "n",
"max_tokens", "stop", "presence_penalty", "frequency_penalty",
"logit_bias", "logprobs", "top_logprobs", "user", "tools",
"tool_choice", "response_format", "seed", "functions", "function_call",
}
KNOWN_COMPLETIONS_PARAMS = {
"model", "prompt", "stream", "temperature", "top_p", "n", "max_tokens",
"stop", "presence_penalty", "frequency_penalty", "logit_bias", "logprobs",
"echo", "best_of", "suffix", "user", "seed",
}
VALID_ROLES = {"system", "user", "assistant", "tool", "function"}
# ββ In-memory usage stats βββββββββββββββββββββββββββββββββββββββββββββββββββββ
_stats_lock = threading.Lock()
_stats = {
"started_at": int(time.time()),
"total_requests": 0,
"total_errors": 0,
"total_tokens": 0,
"total_latency_s": 0.0,
"by_endpoint": {},
}
def _stats_record(endpoint: str, tokens: int = 0, latency: float = 0.0, error: bool = False):
with _stats_lock:
_stats["total_requests"] += 1
_stats["total_latency_s"] += latency
_stats["total_tokens"] += tokens
if error:
_stats["total_errors"] += 1
ep = _stats["by_endpoint"].setdefault(endpoint, {
"requests": 0, "errors": 0, "tokens": 0, "latency_s": 0.0
})
ep["requests"] += 1
ep["latency_s"] += latency
ep["tokens"] += tokens
if error:
ep["errors"] += 1
# ββ In-memory file + batch stores (stub backing store) βββββββββββββββββββββββ
# Real file bytes are stored here keyed by file_id.
# In production you'd swap this for S3/GCS/disk.
_files_lock = threading.Lock()
_files_store: dict = {} # file_id -> {meta, bytes}
_batches_lock = threading.Lock()
_batches_store: dict = {} # batch_id -> batch object
# ββ Logging helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def log_req(endpoint: str, model: str = "-", stream: bool = False):
logger.info(f"β {endpoint} | model={model} | stream={stream}")
def log_resp(endpoint: str, model: str, usage: dict, latency: float):
tokens = (usage.get("total_tokens")
or usage.get("input_tokens", 0) + usage.get("output_tokens", 0))
logger.info(f"β {endpoint} | model={model} | tokens={tokens} | latency={latency:.2f}s")
_stats_record(endpoint, tokens=tokens, latency=latency)
def log_err(endpoint: str, status: int, message: str):
logger.warning(f"β {endpoint} | status={status} | {message}")
_stats_record(endpoint, error=True)
# ββ Error helpers βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def openai_error(message, error_type="invalid_request_error", code=None, status=400, param=None):
log_err(request.path, status, message)
return jsonify({"error": {
"message": message, "type": error_type, "param": param, "code": code,
}}), status
def provider_error_to_openai(http_error):
status = http_error.response.status_code
try:
d = http_error.response.json()
msg = d.get("error", {}).get("message") or d.get("message") or str(http_error)
etype = d.get("error", {}).get("type", "provider_error")
code = d.get("error", {}).get("code")
except Exception:
msg, etype, code = str(http_error), "provider_error", None
return openai_error(msg, etype, code, status)
# ββ Retry logic βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _do_request(method: str, url: str, timeout: int, **kwargs) -> requests.Response:
is_stream = kwargs.get("stream", False)
delay = RETRY_BASE_DELAY
for attempt in range(1, RETRY_MAX + 1):
try:
resp = requests.request(method, url, timeout=timeout, **kwargs)
if resp.status_code in RETRY_STATUSES and not is_stream and attempt < RETRY_MAX:
retry_after = float(resp.headers.get("Retry-After", delay))
logger.warning(f"Provider {resp.status_code} β retrying in {retry_after:.1f}s ({attempt}/{RETRY_MAX})")
time.sleep(retry_after)
delay *= 2
continue
resp.raise_for_status()
return resp
except (requests.exceptions.Timeout, requests.exceptions.ConnectionError) as e:
if is_stream or attempt == RETRY_MAX:
raise
logger.warning(f"{e.__class__.__name__} β retrying in {delay:.1f}s ({attempt}/{RETRY_MAX})")
time.sleep(delay)
delay *= 2
raise requests.exceptions.RetryError("Max retries exceeded")
# ββ Validation ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def validate_messages(messages):
if not isinstance(messages, list) or len(messages) == 0:
return "Field 'messages' must be a non-empty array."
for i, msg in enumerate(messages):
if not isinstance(msg, dict):
return f"messages[{i}] must be an object."
if "role" not in msg:
return f"messages[{i}] is missing required field 'role'."
if msg["role"] not in VALID_ROLES:
return (f"messages[{i}].role '{msg['role']}' is not valid. "
f"Must be one of: {', '.join(sorted(VALID_ROLES))}.")
if "content" not in msg and "tool_calls" not in msg:
return f"messages[{i}] must have 'content' or 'tool_calls'."
return None
def check_unknown_params(body: dict, known: set, endpoint: str):
unknown = set(body.keys()) - known
if unknown:
logger.warning(f"{endpoint} β unknown params forwarded: {sorted(unknown)}")
def validate_numeric(body: dict, param: str, lo: float, hi: float):
if param not in body:
return None
v = body[param]
if not isinstance(v, (int, float)):
return f"'{param}' must be a number."
if not (lo <= v <= hi):
return f"'{param}' must be between {lo} and {hi}."
return None
# ββ JSON mode / response_format handling βββββββββββββββββββββββββββββββββββββ
def _validate_response_format(response_format) -> str | None:
"""
Validate response_format and return an error string if invalid, else None.
Accepted forms:
{"type": "text"}
{"type": "json_object"}
{"type": "json_schema", "json_schema": {...}}
"""
if response_format is None:
return None
if not isinstance(response_format, dict):
return "'response_format' must be an object."
rf_type = response_format.get("type")
if rf_type not in ("text", "json_object", "json_schema"):
return (f"'response_format.type' must be one of: 'text', 'json_object', 'json_schema'. "
f"Got: {rf_type!r}.")
if rf_type == "json_schema":
if "json_schema" not in response_format:
return "'response_format.json_schema' is required when type is 'json_schema'."
schema = response_format["json_schema"]
if not isinstance(schema, dict):
return "'response_format.json_schema' must be an object."
if "name" not in schema:
return "'response_format.json_schema.name' is required."
return None
def _handle_json_mode(body: dict, provider_resp_data: dict) -> dict:
"""
If response_format is json_object or json_schema, try to ensure
the assistant reply is valid JSON. If it isn't, wrap it gracefully.
"""
rf = body.get("response_format", {})
if not rf or rf.get("type") not in ("json_object", "json_schema"):
return provider_resp_data
for choice in provider_resp_data.get("choices", []):
content = choice.get("message", {}).get("content", "")
if not content:
continue
# Strip markdown code fences if present
stripped = content.strip()
if stripped.startswith("```"):
lines = stripped.split("\n")
# Remove opening fence (```json or ```) and closing fence
inner = lines[1:] if lines[0].startswith("```") else lines
if inner and inner[-1].strip() == "```":
inner = inner[:-1]
stripped = "\n".join(inner).strip()
try:
json.loads(stripped)
# Valid JSON β update content with cleaned version
choice["message"]["content"] = stripped
except json.JSONDecodeError:
logger.warning(
"Provider response is not valid JSON despite json_object mode. "
"Wrapping in error envelope."
)
# Return a valid JSON object that signals the parse failure
choice["message"]["content"] = json.dumps({
"error": "Provider did not return valid JSON.",
"raw": content,
})
return provider_resp_data
def _inject_json_mode_system_message(messages: list, response_format: dict) -> list:
"""
If provider doesn't natively support response_format, inject a system
message instructing the model to reply with pure JSON.
We do this transparently so clients get reliable JSON back.
"""
rf_type = (response_format or {}).get("type")
if rf_type not in ("json_object", "json_schema"):
return messages
# Check if there's already a system message with JSON instruction
for msg in messages:
if msg.get("role") == "system" and "json" in msg.get("content", "").lower():
return messages # Already has JSON instruction
instruction = "You must respond with valid JSON only. Do not include any explanation, markdown, or code fences."
if rf_type == "json_schema":
schema_obj = response_format.get("json_schema", {})
schema_def = schema_obj.get("schema")
if schema_def:
instruction += f" Your response must conform to this JSON schema: {json.dumps(schema_def)}"
# Prepend or append to existing system message
new_messages = []
has_system = any(m.get("role") == "system" for m in messages)
if has_system:
for msg in messages:
if msg.get("role") == "system":
new_messages.append({**msg, "content": msg["content"] + "\n\n" + instruction})
else:
new_messages.append(msg)
else:
new_messages = [{"role": "system", "content": instruction}] + messages
return new_messages
# ββ Provider calls ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def call_provider_chat(messages, model="gpt-4", stream=False, **kwargs):
payload = {"model": model, "messages": messages, "stream": stream, **kwargs}
return _do_request(
"POST", f"{PROVIDER_BASE_URL}/chat/completions",
timeout=TIMEOUTS["chat"],
headers=PROVIDER_HEADERS,
json=payload,
stream=stream,
)
# ββ Streaming heartbeat βββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def heartbeat_generator(inner_gen, interval=15):
import queue
q = queue.Queue()
sentinel = object()
def producer():
try:
for chunk in inner_gen:
q.put(chunk)
finally:
q.put(sentinel)
t = threading.Thread(target=producer, daemon=True)
t.start()
while True:
try:
item = q.get(timeout=interval)
if item is sentinel:
break
yield item
except Exception:
yield ": keep-alive\n\n"
# ββ /v1/models ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/models", methods=["GET"])
def list_models():
logger.info("β GET /v1/models")
t0 = time.time()
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/models",
timeout=TIMEOUTS["models"], headers=PROVIDER_HEADERS)
logger.info(f"β GET /v1/models | latency={time.time()-t0:.2f}s")
return jsonify(resp.json())
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Provider timed out fetching models.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
@app.route("/v1/models/<path:model_id>", methods=["GET"])
def retrieve_model(model_id):
logger.info(f"β GET /v1/models/{model_id}")
t0 = time.time()
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/models/{model_id}",
timeout=TIMEOUTS["models"], headers=PROVIDER_HEADERS)
logger.info(f"β GET /v1/models/{model_id} | latency={time.time()-t0:.2f}s")
return jsonify(resp.json())
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Provider timed out fetching model.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
# ββ /v1/moderations ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/moderations", methods=["POST"])
def moderations():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "input" not in body:
return openai_error("Missing required field: input.")
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/moderations",
timeout=TIMEOUTS["moderations"],
headers=PROVIDER_HEADERS, json=body)
return jsonify(resp.json())
except Exception:
pass
inputs = body["input"] if isinstance(body["input"], list) else [body["input"]]
cats = ["sexual", "hate", "harassment", "self-harm", "sexual/minors",
"hate/threatening", "violence/graphic", "self-harm/intent",
"self-harm/instructions", "harassment/threatening", "violence"]
results = [{"flagged": False,
"categories": {k: False for k in cats},
"category_scores": {k: 0.0 for k in cats}} for _ in inputs]
logger.info("β /v1/moderations | stub (provider unsupported)")
return jsonify({"id": "modr-" + str(uuid.uuid4()).replace("-", ""),
"model": body.get("model", "text-moderation-latest"),
"results": results})
# ββ /v1/chat/completions ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/chat/completions", methods=["POST"])
def chat_completions():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
err = validate_messages(body.get("messages", []))
if err:
return openai_error(err, param="messages")
for param, lo, hi in [("temperature", 0.0, 2.0), ("top_p", 0.0, 1.0),
("presence_penalty", -2.0, 2.0), ("frequency_penalty", -2.0, 2.0),
("n", 1, 128)]:
err = validate_numeric(body, param, lo, hi)
if err:
return openai_error(err, param=param)
# ββ response_format / JSON mode ββββββββββββββββββββββββββββββββββββββββββ
rf = body.get("response_format")
err = _validate_response_format(rf)
if err:
return openai_error(err, param="response_format")
check_unknown_params(body, KNOWN_CHAT_PARAMS, "/v1/chat/completions")
model = body.get("model", "gpt-4")
stream = body.get("stream", False)
messages = body["messages"]
# Inject JSON instruction into messages so providers that don't support
# response_format natively still return valid JSON
messages = _inject_json_mode_system_message(messages, rf)
log_req("/v1/chat/completions", model, stream)
t0 = time.time()
extra = {k: v for k, v in body.items() if k not in ("messages", "model", "stream")}
try:
provider_resp = call_provider_chat(messages=messages, model=model, stream=stream, **extra)
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Request to provider timed out.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
if stream:
logger.info(f"β /v1/chat/completions | stream=True | latency={time.time()-t0:.2f}s")
_stats_record("/v1/chat/completions", latency=time.time() - t0)
def generate():
for line in provider_resp.iter_lines():
if line:
yield line.decode("utf-8") + "\n\n"
return Response(stream_with_context(heartbeat_generator(generate())),
content_type="text/event-stream")
data = provider_resp.json()
# Post-process JSON mode β ensure content is valid JSON
data = _handle_json_mode(body, data)
log_resp("/v1/chat/completions", model, data.get("usage", {}), time.time() - t0)
return jsonify(data)
# ββ /v1/completions (legacy) ββββββββββββββββββββββββββββββββββββββββββββββββββ
def _completions_to_chat_messages(prompt) -> list:
if isinstance(prompt, str):
return [{"role": "user", "content": prompt}]
if isinstance(prompt, list):
return [{"role": "user", "content": "\n".join(str(p) for p in prompt)}]
return [{"role": "user", "content": str(prompt)}]
def _chat_to_completions_response(chat_resp: dict, model: str) -> dict:
choices = []
for i, choice in enumerate(chat_resp.get("choices", [])):
text = choice.get("message", {}).get("content", "") or ""
choices.append({"text": text, "index": i, "logprobs": None,
"finish_reason": choice.get("finish_reason", "stop")})
return {
"id": chat_resp.get("id", "cmpl-" + str(uuid.uuid4()).replace("-", "")),
"object": "text_completion",
"created": chat_resp.get("created", int(time.time())),
"model": chat_resp.get("model", model),
"choices": choices,
"usage": chat_resp.get("usage", {}),
}
def _stream_completions_format(provider_resp, model: str):
cmpl_id = "cmpl-" + str(uuid.uuid4()).replace("-", "")
created = int(time.time())
for line in provider_resp.iter_lines():
if not line:
continue
decoded = line.decode("utf-8")
if decoded.startswith("data: "):
decoded = decoded[6:]
if decoded.strip() == "[DONE]":
yield "data: [DONE]\n\n"
break
try:
chunk = json.loads(decoded)
except json.JSONDecodeError:
continue
delta = chunk.get("choices", [{}])[0].get("delta", {})
finish_reason = chunk.get("choices", [{}])[0].get("finish_reason")
text = delta.get("content", "") or ""
out = {"id": cmpl_id, "object": "text_completion", "created": created, "model": model,
"choices": [{"text": text, "index": 0, "logprobs": None, "finish_reason": finish_reason}]}
yield f"data: {json.dumps(out)}\n\n"
@app.route("/v1/completions", methods=["POST"])
def legacy_completions():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "prompt" not in body:
return openai_error("Missing required field: prompt.", param="prompt")
for param, lo, hi in [("temperature", 0.0, 2.0), ("top_p", 0.0, 1.0),
("presence_penalty", -2.0, 2.0), ("frequency_penalty", -2.0, 2.0)]:
err = validate_numeric(body, param, lo, hi)
if err:
return openai_error(err, param=param)
check_unknown_params(body, KNOWN_COMPLETIONS_PARAMS, "/v1/completions")
model = body.get("model", "gpt-3.5-turbo")
stream = body.get("stream", False)
messages = _completions_to_chat_messages(body["prompt"])
extra = {k: v for k, v in body.items()
if k in ("temperature", "top_p", "n", "max_tokens", "stop",
"presence_penalty", "frequency_penalty", "logit_bias", "user", "seed")}
log_req("/v1/completions", model, stream)
t0 = time.time()
try:
provider_resp = call_provider_chat(messages=messages, model=model, stream=stream, **extra)
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Request to provider timed out.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
if stream:
logger.info(f"β /v1/completions | stream=True | latency={time.time()-t0:.2f}s")
_stats_record("/v1/completions", latency=time.time() - t0)
return Response(stream_with_context(heartbeat_generator(_stream_completions_format(provider_resp, model))),
content_type="text/event-stream")
data = provider_resp.json()
log_resp("/v1/completions", model, data.get("usage", {}), time.time() - t0)
return jsonify(_chat_to_completions_response(data, model))
# ββ /v1/responses βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def messages_from_responses_input(input_data):
if isinstance(input_data, str):
return [{"role": "user", "content": input_data}]
if isinstance(input_data, list):
messages = []
for item in input_data:
if isinstance(item, str):
messages.append({"role": "user", "content": item})
elif isinstance(item, dict):
role = item.get("role", "user")
content = item.get("content", "")
if isinstance(content, list):
parts = []
for p in content:
if not isinstance(p, dict):
continue
ptype = p.get("type", "")
if ptype in ("input_text", "text"):
parts.append({"type": "text", "text": p.get("text", "")})
elif ptype == "input_image":
url = p.get("image_url") or p.get("url", "")
if url:
parts.append({"type": "image_url", "image_url": {"url": url}})
content = parts if parts else ""
messages.append({"role": role, "content": content})
return messages
return []
def tools_from_responses(tools):
if not tools:
return []
converted = []
for t in tools:
if t.get("type") == "function":
converted.append(t)
elif t.get("type") == "web_search":
converted.append({"type": "function", "function": {
"name": "web_search",
"description": "Search the web for current information.",
"parameters": {
"type": "object",
"properties": {"query": {"type": "string", "description": "Search query"}},
"required": ["query"],
},
}})
return converted
def chat_choice_to_responses_output(choice):
message = choice.get("message", {})
content_text = message.get("content", "") or ""
tool_calls = message.get("tool_calls") or []
item_id = "msg_" + str(uuid.uuid4()).replace("-", "")
output_content = []
if content_text:
output_content.append({"type": "output_text", "text": content_text})
for tc in tool_calls:
fn = tc.get("function", {})
try:
args = json.loads(fn.get("arguments", "{}"))
except Exception:
args = {}
output_content.append({
"type": "tool_use",
"id": tc.get("id", "call_" + str(uuid.uuid4()).replace("-", "")),
"name": fn.get("name", ""),
"input": args,
})
return {"id": item_id, "type": "message", "role": "assistant",
"content": output_content, "status": "completed"}
def chat_response_to_responses_format(chat_resp: dict, model: str) -> dict:
choice = chat_resp.get("choices", [{}])[0]
usage = chat_resp.get("usage", {})
output_item = chat_choice_to_responses_output(choice)
return {
"id": "resp_" + chat_resp.get("id", str(uuid.uuid4())).replace("chatcmpl-", ""),
"object": "response",
"created_at": chat_resp.get("created", int(time.time())),
"model": chat_resp.get("model", model),
"status": "completed",
"output": [output_item],
"usage": {
"input_tokens": usage.get("prompt_tokens", 0),
"output_tokens": usage.get("completion_tokens", 0),
"total_tokens": usage.get("total_tokens", 0),
},
"finish_reason": choice.get("finish_reason", "stop"),
}
def stream_responses_format(provider_resp, model):
response_id = "resp_" + str(uuid.uuid4()).replace("-", "")
created_at = int(time.time())
output_index = 0
item_id = "msg_" + str(uuid.uuid4()).replace("-", "")
def event(data):
return f"data: {json.dumps(data)}\n\n"
yield event({"type": "response.created", "response": {
"id": response_id, "object": "response", "created_at": created_at,
"model": model, "status": "in_progress", "output": [],
}})
yield event({"type": "response.output_item.added", "output_index": output_index, "item": {
"id": item_id, "type": "message", "role": "assistant", "content": [], "status": "in_progress",
}})
yield event({"type": "response.content_part.added", "item_id": item_id,
"output_index": output_index, "content_index": 0,
"part": {"type": "output_text", "text": ""}})
full_text = ""
tool_calls_map = {}
for line in provider_resp.iter_lines():
if not line:
continue
decoded = line.decode("utf-8")
if decoded.startswith("data: "):
decoded = decoded[6:]
if decoded.strip() == "[DONE]":
break
try:
chunk = json.loads(decoded)
except json.JSONDecodeError:
continue
delta = chunk.get("choices", [{}])[0].get("delta", {})
text = delta.get("content", "")
if text:
full_text += text
yield event({"type": "response.output_text.delta", "item_id": item_id,
"output_index": output_index, "content_index": 0, "delta": text})
for tc_delta in (delta.get("tool_calls") or []):
idx = tc_delta.get("index", 0)
if idx not in tool_calls_map:
tool_calls_map[idx] = {
"id": tc_delta.get("id", "call_" + str(uuid.uuid4()).replace("-", "")),
"name": "", "arguments": "",
}
fn = tc_delta.get("function", {})
if fn.get("name"): tool_calls_map[idx]["name"] += fn["name"]
if fn.get("arguments"): tool_calls_map[idx]["arguments"] += fn["arguments"]
yield event({"type": "response.content_part.done", "item_id": item_id,
"output_index": output_index, "content_index": 0,
"part": {"type": "output_text", "text": full_text}})
final_content = [{"type": "output_text", "text": full_text}]
content_index = 1
for tc in tool_calls_map.values():
try:
args = json.loads(tc["arguments"] or "{}")
except Exception:
args = {}
tool_part = {"type": "tool_use", "id": tc["id"], "name": tc["name"], "input": args}
final_content.append(tool_part)
yield event({"type": "response.content_part.added", "item_id": item_id,
"output_index": output_index, "content_index": content_index, "part": tool_part})
yield event({"type": "response.content_part.done", "item_id": item_id,
"output_index": output_index, "content_index": content_index, "part": tool_part})
content_index += 1
yield event({"type": "response.output_item.done", "output_index": output_index, "item": {
"id": item_id, "type": "message", "role": "assistant",
"content": final_content, "status": "completed",
}})
yield event({"type": "response.done", "response": {
"id": response_id, "object": "response", "created_at": created_at,
"model": model, "status": "completed",
"output": [{"id": item_id, "type": "message", "role": "assistant",
"content": final_content, "status": "completed"}],
}})
yield "data: [DONE]\n\n"
@app.route("/v1/responses", methods=["POST"])
def responses():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "input" not in body:
return openai_error("Missing required field: input.")
model = body.get("model", "gpt-4")
stream = body.get("stream", False)
log_req("/v1/responses", model, stream)
t0 = time.time()
messages = []
if body.get("instructions"):
messages.append({"role": "system", "content": body["instructions"]})
messages.extend(messages_from_responses_input(body["input"]))
for prev in body.get("conversation", []):
messages.append({"role": prev.get("role", "user"), "content": prev.get("content", "")})
extra = {}
for src, dst in [("max_output_tokens", "max_tokens"), ("temperature", "temperature"),
("top_p", "top_p"), ("stop", "stop")]:
if src in body:
extra[dst] = body[src]
tools = tools_from_responses(body.get("tools"))
if tools:
extra["tools"] = tools
if body.get("tool_choice"):
extra["tool_choice"] = body["tool_choice"]
try:
provider_resp = call_provider_chat(messages=messages, model=model, stream=stream, **extra)
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Request to provider timed out.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
if stream:
logger.info(f"β /v1/responses | stream=True | latency={time.time()-t0:.2f}s")
_stats_record("/v1/responses", latency=time.time() - t0)
return Response(stream_with_context(heartbeat_generator(stream_responses_format(provider_resp, model))),
content_type="text/event-stream")
data = provider_resp.json()
log_resp("/v1/responses", model, data.get("usage", {}), time.time() - t0)
return jsonify(chat_response_to_responses_format(data, model))
# ββ /v1/images/generations ββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/images/generations", methods=["POST"])
@app.route("/v1/image/generations", methods=["POST"])
def image_generations():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "prompt" not in body:
return openai_error("Missing required field: prompt.", param="prompt")
logger.info(f"β /v1/images/generations | prompt={body['prompt'][:60]!r}")
t0 = time.time()
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/images/generations",
timeout=TIMEOUTS["image"], headers=PROVIDER_HEADERS, json=body)
except requests.HTTPError as e:
return provider_error_to_openai(e)
except requests.exceptions.Timeout:
return openai_error("Request to provider timed out.", "server_error", "timeout", 504)
except requests.exceptions.ConnectionError:
return openai_error("Could not connect to provider.", "server_error", "connection_error", 502)
log_resp("/v1/images/generations", body.get("model", "dall-e-3"), {}, time.time() - t0)
return jsonify(resp.json())
# ββ /v1/embeddings ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/embeddings", methods=["POST"])
def embeddings():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "input" not in body:
return openai_error("Missing required field: input.", param="input")
t0 = time.time()
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/embeddings",
timeout=TIMEOUTS["embeddings"], headers=PROVIDER_HEADERS, json=body)
log_resp("/v1/embeddings", body.get("model", "text-embedding-ada-002"),
resp.json().get("usage", {}), time.time() - t0)
return jsonify(resp.json())
except requests.HTTPError as e:
return provider_error_to_openai(e)
except Exception:
return openai_error("Embeddings are not supported by this provider.",
"invalid_request_error", "unsupported_endpoint", 400)
# ββ /v1/audio/* βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/audio/transcriptions", methods=["POST"])
def audio_transcriptions():
if "file" not in request.files:
return openai_error("Missing required file field: file.", param="file")
audio_file = request.files["file"]
form = request.form.to_dict()
logger.info(f"β /v1/audio/transcriptions | model={form.get('model', 'whisper-1')}")
t0 = time.time()
try:
resp = _do_request(
"POST", f"{PROVIDER_BASE_URL}/audio/transcriptions",
timeout=TIMEOUTS["audio"],
headers={"Authorization": f"Bearer {PROVIDER_API_KEY}"},
files={"file": (audio_file.filename, audio_file.stream, audio_file.content_type)},
data=form,
)
logger.info(f"β /v1/audio/transcriptions | latency={time.time()-t0:.2f}s")
return jsonify(resp.json())
except requests.HTTPError as e:
return provider_error_to_openai(e)
except Exception:
return openai_error("Audio transcription is not supported by this provider.",
"invalid_request_error", "unsupported_endpoint", 501)
@app.route("/v1/audio/speech", methods=["POST"])
def audio_speech():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
if "input" not in body:
return openai_error("Missing required field: input.", param="input")
logger.info(f"β /v1/audio/speech | voice={body.get('voice', 'alloy')}")
t0 = time.time()
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/audio/speech",
timeout=TIMEOUTS["audio"], headers=PROVIDER_HEADERS, json=body, stream=True)
content_type = resp.headers.get("Content-Type", "audio/mpeg")
logger.info(f"β /v1/audio/speech | latency={time.time()-t0:.2f}s")
return Response(resp.iter_content(chunk_size=4096), content_type=content_type)
except requests.HTTPError as e:
return provider_error_to_openai(e)
except Exception:
return openai_error("Text-to-speech is not supported by this provider.",
"invalid_request_error", "unsupported_endpoint", 501)
# ββ /v1/files βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Full CRUD implementation backed by in-memory store.
# Tries provider first; falls back to local store if provider doesn't support files.
def _file_meta(file_id: str, filename: str, purpose: str, size: int, created: int) -> dict:
return {
"id": file_id,
"object": "file",
"bytes": size,
"created_at": created,
"filename": filename,
"purpose": purpose,
"status": "processed",
"status_details": None,
}
@app.route("/v1/files", methods=["POST"])
def upload_file():
"""Upload a file. Tries provider first, falls back to local store."""
if "file" not in request.files:
return openai_error("Missing required field: file.", param="file")
purpose = request.form.get("purpose", "")
if not purpose:
return openai_error("Missing required field: purpose.", param="purpose")
valid_purposes = {"fine-tune", "assistants", "batch", "vision", "user_data"}
if purpose not in valid_purposes:
return openai_error(f"'purpose' must be one of: {', '.join(sorted(valid_purposes))}.", param="purpose")
uploaded = request.files["file"]
file_bytes = uploaded.read()
log_req("/v1/files", stream=False)
# Try provider
try:
resp = _do_request(
"POST", f"{PROVIDER_BASE_URL}/files",
timeout=TIMEOUTS["files"],
headers={"Authorization": f"Bearer {PROVIDER_API_KEY}"},
files={"file": (uploaded.filename, file_bytes, uploaded.content_type)},
data={"purpose": purpose},
)
data = resp.json()
logger.info(f"β /v1/files (upload) | id={data.get('id')} | provider=true")
return jsonify(data), 200
except Exception:
pass
# Local fallback
file_id = "file-" + str(uuid.uuid4()).replace("-", "")
created = int(time.time())
meta = _file_meta(file_id, uploaded.filename or "upload", purpose, len(file_bytes), created)
with _files_lock:
_files_store[file_id] = {"meta": meta, "data": file_bytes}
logger.info(f"β /v1/files (upload) | id={file_id} | provider=false (local)")
return jsonify(meta), 200
@app.route("/v1/files", methods=["GET"])
def list_files():
"""List files. Tries provider first, merges with local store."""
purpose = request.args.get("purpose")
log_req("/v1/files", stream=False)
provider_files = []
try:
params = {}
if purpose:
params["purpose"] = purpose
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/files",
timeout=TIMEOUTS["files"], headers=PROVIDER_HEADERS, params=params)
provider_files = resp.json().get("data", [])
except Exception:
pass
with _files_lock:
local_files = [v["meta"] for v in _files_store.values()
if not purpose or v["meta"]["purpose"] == purpose]
# Merge β provider files take priority if same id
seen = {f["id"] for f in provider_files}
merged = provider_files + [f for f in local_files if f["id"] not in seen]
merged.sort(key=lambda f: f.get("created_at", 0), reverse=True)
return jsonify({"object": "list", "data": merged})
@app.route("/v1/files/<file_id>", methods=["GET"])
def retrieve_file(file_id):
"""Retrieve file metadata."""
log_req(f"/v1/files/{file_id}", stream=False)
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/files/{file_id}",
timeout=TIMEOUTS["files"], headers=PROVIDER_HEADERS)
return jsonify(resp.json())
except Exception:
pass
with _files_lock:
entry = _files_store.get(file_id)
if not entry:
return openai_error(f"No such file: {file_id!r}", "invalid_request_error", "file_not_found", 404)
return jsonify(entry["meta"])
@app.route("/v1/files/<file_id>", methods=["DELETE"])
def delete_file(file_id):
"""Delete a file."""
log_req(f"/v1/files/{file_id} DELETE", stream=False)
try:
resp = _do_request("DELETE", f"{PROVIDER_BASE_URL}/files/{file_id}",
timeout=TIMEOUTS["files"], headers=PROVIDER_HEADERS)
return jsonify(resp.json())
except Exception:
pass
with _files_lock:
deleted = _files_store.pop(file_id, None)
if not deleted:
return openai_error(f"No such file: {file_id!r}", "invalid_request_error", "file_not_found", 404)
return jsonify({"id": file_id, "object": "file", "deleted": True})
@app.route("/v1/files/<file_id>/content", methods=["GET"])
def retrieve_file_content(file_id):
"""Download raw file content."""
log_req(f"/v1/files/{file_id}/content", stream=False)
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/files/{file_id}/content",
timeout=TIMEOUTS["files"], headers=PROVIDER_HEADERS, stream=True)
content_type = resp.headers.get("Content-Type", "application/octet-stream")
return Response(resp.iter_content(chunk_size=8192), content_type=content_type)
except Exception:
pass
with _files_lock:
entry = _files_store.get(file_id)
if not entry:
return openai_error(f"No such file: {file_id!r}", "invalid_request_error", "file_not_found", 404)
return Response(entry["data"], content_type="application/octet-stream",
headers={"Content-Disposition": f"attachment; filename={entry['meta']['filename']}"})
# ββ /v1/batches βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Full lifecycle: create, retrieve, list, cancel.
# Tries provider first; falls back to local async execution backed by threads.
BATCH_VALID_ENDPOINTS = {"/v1/chat/completions", "/v1/embeddings", "/v1/completions"}
def _make_batch(batch_id: str, input_file_id: str, endpoint: str,
completion_window: str, metadata: dict, created: int) -> dict:
return {
"id": batch_id,
"object": "batch",
"endpoint": endpoint,
"errors": None,
"input_file_id": input_file_id,
"completion_window": completion_window,
"status": "validating",
"output_file_id": None,
"error_file_id": None,
"created_at": created,
"in_progress_at": None,
"expires_at": created + 86400,
"finalizing_at": None,
"completed_at": None,
"failed_at": None,
"expired_at": None,
"cancelling_at": None,
"cancelled_at": None,
"request_counts": {"total": 0, "completed": 0, "failed": 0},
"metadata": metadata or {},
}
def _run_batch(batch_id: str):
"""Background worker: read JSONL from file store, run requests, write output file."""
with _batches_lock:
batch = _batches_store.get(batch_id)
if not batch:
return
now = int(time.time())
batch["status"] = "in_progress"
batch["in_progress_at"] = now
# Read input file
with _files_lock:
entry = _files_store.get(batch["input_file_id"])
if not entry:
batch["status"] = "failed"
batch["failed_at"] = int(time.time())
batch["errors"] = {"object": "list", "data": [
{"code": "file_not_found", "message": "Input file not found.", "line": None, "param": None}
]}
return
try:
lines = entry["data"].decode("utf-8").strip().split("\n")
except Exception:
batch["status"] = "failed"
batch["failed_at"] = int(time.time())
return
output_lines = []
total = len(lines)
success = 0
failed = 0
endpoint = batch["endpoint"]
for i, line in enumerate(lines):
line = line.strip()
if not line:
continue
try:
req = json.loads(line)
except json.JSONDecodeError:
failed += 1
output_lines.append(json.dumps({
"id": f"batch_req_{i}", "custom_id": None,
"response": None,
"error": {"code": "json_parse_error", "message": "Could not parse JSONL line."},
}))
continue
custom_id = req.get("custom_id", f"req_{i}")
body = req.get("body", {})
try:
if endpoint == "/v1/chat/completions":
messages = body.get("messages", [])
model = body.get("model", "gpt-4")
extra = {k: v for k, v in body.items() if k not in ("messages", "model", "stream")}
pr = call_provider_chat(messages=messages, model=model, stream=False, **extra)
resp_body = pr.json()
elif endpoint == "/v1/embeddings":
pr = _do_request("POST", f"{PROVIDER_BASE_URL}/embeddings",
timeout=TIMEOUTS["embeddings"],
headers=PROVIDER_HEADERS, json=body)
resp_body = pr.json()
elif endpoint == "/v1/completions":
msgs = _completions_to_chat_messages(body.get("prompt", ""))
extra = {k: v for k, v in body.items()
if k in ("temperature", "top_p", "max_tokens", "stop", "user")}
pr = call_provider_chat(messages=msgs, model=body.get("model", "gpt-3.5-turbo"),
stream=False, **extra)
resp_body = _chat_to_completions_response(pr.json(), body.get("model", "gpt-3.5-turbo"))
else:
raise ValueError(f"Unsupported batch endpoint: {endpoint}")
success += 1
output_lines.append(json.dumps({
"id": f"batch_req_{uuid.uuid4().hex[:8]}",
"custom_id": custom_id,
"response": {"status_code": 200, "request_id": str(uuid.uuid4()), "body": resp_body},
"error": None,
}))
except Exception as e:
failed += 1
output_lines.append(json.dumps({
"id": f"batch_req_{uuid.uuid4().hex[:8]}",
"custom_id": custom_id,
"response": None,
"error": {"code": "request_failed", "message": str(e)},
}))
# Write output file
output_bytes = "\n".join(output_lines).encode("utf-8")
output_file_id = "file-" + str(uuid.uuid4()).replace("-", "")
out_meta = _file_meta(output_file_id, f"batch_{batch_id}_output.jsonl",
"batch_output", len(output_bytes), int(time.time()))
with _files_lock:
_files_store[output_file_id] = {"meta": out_meta, "data": output_bytes}
now = int(time.time())
with _batches_lock:
b = _batches_store[batch_id]
b["status"] = "completed"
b["output_file_id"] = output_file_id
b["finalizing_at"] = now
b["completed_at"] = now
b["request_counts"] = {"total": total, "completed": success, "failed": failed}
logger.info(f"Batch {batch_id} completed | total={total} ok={success} failed={failed}")
@app.route("/v1/batches", methods=["POST"])
def create_batch():
body = request.get_json(force=True, silent=True)
if not body:
return openai_error("Could not parse request body as JSON.")
for field in ("input_file_id", "endpoint", "completion_window"):
if field not in body:
return openai_error(f"Missing required field: {field}.", param=field)
if body["endpoint"] not in BATCH_VALID_ENDPOINTS:
return openai_error(
f"'endpoint' must be one of: {', '.join(sorted(BATCH_VALID_ENDPOINTS))}.",
param="endpoint"
)
log_req("/v1/batches", stream=False)
# Try provider first
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/batches",
timeout=TIMEOUTS["batches"], headers=PROVIDER_HEADERS, json=body)
return jsonify(resp.json()), 200
except Exception:
pass
# Local fallback: create batch and run in background thread
batch_id = "batch_" + str(uuid.uuid4()).replace("-", "")
created = int(time.time())
batch = _make_batch(batch_id, body["input_file_id"], body["endpoint"],
body["completion_window"], body.get("metadata"), created)
with _batches_lock:
_batches_store[batch_id] = batch
t = threading.Thread(target=_run_batch, args=(batch_id,), daemon=True)
t.start()
logger.info(f"β /v1/batches | id={batch_id} | local=true")
return jsonify(batch), 200
@app.route("/v1/batches/<batch_id>", methods=["GET"])
def retrieve_batch(batch_id):
log_req(f"/v1/batches/{batch_id}", stream=False)
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/batches/{batch_id}",
timeout=TIMEOUTS["batches"], headers=PROVIDER_HEADERS)
return jsonify(resp.json())
except Exception:
pass
with _batches_lock:
batch = _batches_store.get(batch_id)
if not batch:
return openai_error(f"No such batch: {batch_id!r}", "invalid_request_error", "batch_not_found", 404)
return jsonify(batch)
@app.route("/v1/batches", methods=["GET"])
def list_batches():
log_req("/v1/batches list", stream=False)
provider_batches = []
try:
resp = _do_request("GET", f"{PROVIDER_BASE_URL}/batches",
timeout=TIMEOUTS["batches"], headers=PROVIDER_HEADERS,
params=request.args)
provider_batches = resp.json().get("data", [])
except Exception:
pass
with _batches_lock:
local_batches = list(_batches_store.values())
seen = {b["id"] for b in provider_batches}
merged = provider_batches + [b for b in local_batches if b["id"] not in seen]
merged.sort(key=lambda b: b.get("created_at", 0), reverse=True)
return jsonify({"object": "list", "data": merged, "has_more": False, "first_id": None, "last_id": None})
@app.route("/v1/batches/<batch_id>/cancel", methods=["POST"])
def cancel_batch(batch_id):
log_req(f"/v1/batches/{batch_id}/cancel", stream=False)
try:
resp = _do_request("POST", f"{PROVIDER_BASE_URL}/batches/{batch_id}/cancel",
timeout=TIMEOUTS["batches"], headers=PROVIDER_HEADERS)
return jsonify(resp.json())
except Exception:
pass
with _batches_lock:
batch = _batches_store.get(batch_id)
if not batch:
return openai_error(f"No such batch: {batch_id!r}", "invalid_request_error", "batch_not_found", 404)
if batch["status"] in ("completed", "failed", "expired", "cancelled"):
return openai_error(f"Batch {batch_id} cannot be cancelled (status: {batch['status']}).",
"invalid_request_error", "batch_not_cancellable")
now = int(time.time())
batch["status"] = "cancelled"
batch["cancelled_at"] = now
batch["cancelling_at"] = now
return jsonify(batch)
# ββ /stats ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/stats", methods=["GET"])
def stats():
with _stats_lock:
s = dict(_stats)
by_ep = {k: dict(v) for k, v in _stats["by_endpoint"].items()}
total_req = s["total_requests"]
avg_latency = round(s["total_latency_s"] / total_req, 3) if total_req else 0.0
error_rate = round(s["total_errors"] / total_req, 4) if total_req else 0.0
for ep, d in by_ep.items():
d["avg_latency_s"] = round(d["latency_s"] / d["requests"], 3) if d["requests"] else 0.0
d["error_rate"] = round(d["errors"] / d["requests"], 4) if d["requests"] else 0.0
with _files_lock:
file_count = len(_files_store)
with _batches_lock:
batch_count = len(_batches_store)
return jsonify({
"uptime_s": int(time.time()) - s["started_at"],
"total_requests": total_req,
"total_errors": s["total_errors"],
"error_rate": error_rate,
"total_tokens": s["total_tokens"],
"avg_latency_s": avg_latency,
"local_files": file_count,
"local_batches": batch_count,
"by_endpoint": by_ep,
})
# ββ /v1/ discovery ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/v1/", methods=["GET"])
def openapi_discovery():
return jsonify({
"name": "OpenAI-compatible bridge",
"version": "1.0.0",
"provider": PROVIDER_BASE_URL,
"notes": {
"json_mode": (
"response_format={type:json_object} and json_schema are fully supported. "
"If the provider does not natively support them, a system message is injected "
"to enforce JSON output and the response is post-processed to ensure validity."
),
"files": (
"Full CRUD file support. Tries provider first; falls back to in-memory store. "
"In-memory files are lost on restart."
),
"batches": (
"Full batch lifecycle (create/retrieve/list/cancel). Tries provider first; "
"falls back to local background thread execution. "
"Supports endpoints: /v1/chat/completions, /v1/embeddings, /v1/completions."
),
},
"endpoints": [
{"method": "GET", "path": "/v1/models", "description": "List models (proxied from provider)."},
{"method": "GET", "path": "/v1/models/{model}", "description": "Retrieve a model."},
{"method": "POST", "path": "/v1/chat/completions", "description": "Chat completion. Supports response_format (json_object, json_schema, text), tools, streaming."},
{"method": "POST", "path": "/v1/completions", "description": "Legacy text completions (converted to chat internally)."},
{"method": "POST", "path": "/v1/responses", "description": "OpenAI Responses API (converted to chat internally)."},
{"method": "POST", "path": "/v1/images/generations", "description": "Image generation."},
{"method": "POST", "path": "/v1/embeddings", "description": "Text embeddings."},
{"method": "POST", "path": "/v1/moderations", "description": "Content moderation (stub fallback)."},
{"method": "POST", "path": "/v1/audio/transcriptions", "description": "Audio transcription."},
{"method": "POST", "path": "/v1/audio/speech", "description": "Text-to-speech."},
{"method": "POST", "path": "/v1/files", "description": "Upload a file."},
{"method": "GET", "path": "/v1/files", "description": "List files."},
{"method": "GET", "path": "/v1/files/{file_id}", "description": "Retrieve file metadata."},
{"method": "DELETE", "path": "/v1/files/{file_id}", "description": "Delete a file."},
{"method": "GET", "path": "/v1/files/{file_id}/content", "description": "Download file content."},
{"method": "POST", "path": "/v1/batches", "description": "Create a batch job."},
{"method": "GET", "path": "/v1/batches", "description": "List batch jobs."},
{"method": "GET", "path": "/v1/batches/{batch_id}", "description": "Retrieve a batch job."},
{"method": "POST", "path": "/v1/batches/{batch_id}/cancel", "description": "Cancel a batch job."},
{"method": "GET", "path": "/stats", "description": "Bridge usage stats."},
{"method": "GET", "path": "/health", "description": "Provider health check."},
{"method": "GET", "path": "/ping", "description": "Liveness probe."},
],
})
# ββ Health & ping βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.route("/health", methods=["GET"])
def health():
provider_ok = False
provider_latency = None
try:
t0 = time.time()
r = requests.get(f"{PROVIDER_BASE_URL}/models", headers=PROVIDER_HEADERS, timeout=5)
provider_latency = round(time.time() - t0, 3)
provider_ok = r.ok
except Exception:
pass
with _stats_lock:
total_req = _stats["total_requests"]
uptime = int(time.time()) - _stats["started_at"]
return jsonify({
"status": "healthy" if provider_ok else "degraded",
"uptime_s": uptime,
"provider": {"reachable": provider_ok, "latency_s": provider_latency, "url": PROVIDER_BASE_URL},
"requests": total_req,
"timestamp": int(time.time()),
}), 200 if provider_ok else 207
@app.route("/ping", methods=["GET"])
def ping():
return jsonify({"pong": True, "timestamp": int(time.time())})
if __name__ == "__main__":
app.run(host="0.0.0.0", port=7860, debug=False)
|