feat: 支持 Completions/Responses 双协议,并在同选型下续链加速

This commit is contained in:
2026-09-24 17:10:34 +08:00
parent 9c2c1ed061
commit d20c6a1302
26 changed files with 533 additions and 43 deletions
@@ -0,0 +1,38 @@
"""解析是否走 Responses API(显式开关,避免仅靠模型名推断)。"""
from __future__ import annotations
from urllib.parse import urlparse
def normalize_api_style(raw: str | None) -> str:
s = (raw or "auto").strip().lower()
if s in ("auto", "completions", "responses"):
return s
return "auto"
def resolve_use_responses_api(api_style: str | None, base_url: str | None) -> bool:
"""
auto:仅官方 OpenAI 主机默认 Responses;其余 Completions。
completions / responses:强制对应协议。
"""
style = normalize_api_style(api_style)
if style == "responses":
return True
if style == "completions":
return False
host = _host(base_url)
return host == "api.openai.com"
def _host(base_url: str | None) -> str:
raw = (base_url or "").strip()
if not raw:
return ""
if "://" not in raw:
raw = "https://" + raw
try:
return (urlparse(raw).hostname or "").lower()
except Exception: # noqa: BLE001
return ""