mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 16:29:19 +08:00
fix: 探测 Responses/store 能力并在无状态时用 Completions 带全量历史
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""解析是否走 Responses API(显式开关,避免仅靠模型名推断)。"""
|
||||
"""协议偏好与错误分类:先验缓存 + 运行时探测回退(不写死主机)。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from urllib.parse import urlparse
|
||||
from dataclasses import dataclass
|
||||
|
||||
from domain.models import LlmProtocolCapability
|
||||
|
||||
|
||||
def normalize_api_style(raw: str | None) -> str:
|
||||
@@ -12,27 +14,114 @@ def normalize_api_style(raw: str | None) -> str:
|
||||
return "auto"
|
||||
|
||||
|
||||
def resolve_use_responses_api(api_style: str | None, base_url: str | None) -> bool:
|
||||
@dataclass(frozen=True)
|
||||
class ProtocolAttempt:
|
||||
"""单次调用策略。"""
|
||||
|
||||
use_responses: bool
|
||||
use_store: bool
|
||||
use_chain: bool # 是否只发增量 + previous_response_id(须 store 已证实)
|
||||
|
||||
|
||||
def plan_protocol_attempts(
|
||||
api_style: str | None,
|
||||
cap: LlmProtocolCapability | None,
|
||||
*,
|
||||
has_chain_id: bool,
|
||||
) -> list[ProtocolAttempt]:
|
||||
"""
|
||||
auto:仅官方 OpenAI 主机默认 Responses;其余 Completions。
|
||||
completions / responses:强制对应协议。
|
||||
同轮探测顺序(未产出 token 前可依次尝试):
|
||||
|
||||
- store 已证实:Responses+store,有链则增量续聊
|
||||
- store 未知:Responses+store + **Mongo 全量**,成功后再语义探测
|
||||
- store 证伪:走 **Completions + Mongo 全量**
|
||||
(DeepSeek 等无状态 Responses 会返回 id,但多轮 input 不可靠)
|
||||
- Responses 不可用:Completions
|
||||
"""
|
||||
style = normalize_api_style(api_style)
|
||||
if style == "responses":
|
||||
return True
|
||||
cap = cap or LlmProtocolCapability()
|
||||
|
||||
if style == "completions":
|
||||
return False
|
||||
host = _host(base_url)
|
||||
return host == "api.openai.com"
|
||||
return [ProtocolAttempt(False, False, False)]
|
||||
|
||||
attempts: list[ProtocolAttempt] = []
|
||||
|
||||
if cap.store is False:
|
||||
# 无真实 store:一律 Completions + Mongo 全量(DeepSeek Responses 多轮不可靠)
|
||||
attempts.append(ProtocolAttempt(False, False, False))
|
||||
return _dedupe(attempts)
|
||||
|
||||
if cap.responses is not False:
|
||||
if cap.store is True:
|
||||
attempts.append(
|
||||
ProtocolAttempt(True, True, use_chain=bool(has_chain_id))
|
||||
)
|
||||
# 有链失败时可降到同协议全量
|
||||
attempts.append(ProtocolAttempt(True, True, use_chain=False))
|
||||
else:
|
||||
# store 未知:Responses+store,必须全量历史,禁止 use_chain
|
||||
attempts.append(ProtocolAttempt(True, True, use_chain=False))
|
||||
attempts.append(ProtocolAttempt(True, False, False))
|
||||
|
||||
# Completions 兜底
|
||||
if style == "auto" or cap.responses is False:
|
||||
attempts.append(ProtocolAttempt(False, False, False))
|
||||
elif style == "responses" and cap.responses is False:
|
||||
attempts.append(ProtocolAttempt(False, False, False))
|
||||
|
||||
return _dedupe(attempts) or [ProtocolAttempt(False, False, False)]
|
||||
|
||||
|
||||
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 ""
|
||||
def _dedupe(attempts: list[ProtocolAttempt]) -> list[ProtocolAttempt]:
|
||||
seen: set[tuple[bool, bool, bool]] = set()
|
||||
out: list[ProtocolAttempt] = []
|
||||
for a in attempts:
|
||||
key = (a.use_responses, a.use_store, a.use_chain)
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
out.append(a)
|
||||
return out
|
||||
|
||||
|
||||
def classify_protocol_error(exc: BaseException) -> str:
|
||||
"""返回: store | responses | chain | other。"""
|
||||
text = str(exc).lower()
|
||||
status = getattr(exc, "status_code", None) or getattr(
|
||||
getattr(exc, "response", None), "status_code", None
|
||||
)
|
||||
|
||||
if "previous_response_id" in text or "previous response" in text:
|
||||
return "chain"
|
||||
if status == 400 and "response" in text and "not found" in text:
|
||||
return "chain"
|
||||
|
||||
if "store" in text and any(
|
||||
n in text
|
||||
for n in (
|
||||
"unsupported",
|
||||
"unknown",
|
||||
"unrecognized",
|
||||
"invalid",
|
||||
"not support",
|
||||
"not supported",
|
||||
)
|
||||
):
|
||||
return "store"
|
||||
|
||||
if status in (404, 405):
|
||||
return "responses"
|
||||
responses_needles = (
|
||||
"/responses",
|
||||
"responses api",
|
||||
"unknown endpoint",
|
||||
"not implemented",
|
||||
"invalid pathname",
|
||||
"no route",
|
||||
)
|
||||
if any(n in text for n in responses_needles):
|
||||
return "responses"
|
||||
if status == 400 and ("responses" in text or "response.create" in text):
|
||||
return "responses"
|
||||
|
||||
return "other"
|
||||
|
||||
Reference in New Issue
Block a user