Files
ecom-dmb/apps/agent/infrastructure/llm/api_style.py
T

39 lines
1.0 KiB
Python

"""解析是否走 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 ""