fix(chat): simplify header cards and llm fallback
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
from dataclasses import dataclass
|
||||
import json
|
||||
import os
|
||||
from urllib.error import URLError
|
||||
from urllib.error import HTTPError, URLError
|
||||
from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
@@ -75,12 +75,28 @@ class OpenAICompatibleProvider:
|
||||
if response_format:
|
||||
payload["response_format"] = response_format
|
||||
try:
|
||||
data = _post_json(
|
||||
base_url=self.base_url,
|
||||
endpoint="chat/completions",
|
||||
api_key=self.api_key,
|
||||
payload=payload,
|
||||
)
|
||||
try:
|
||||
data = _post_json(
|
||||
base_url=self.base_url,
|
||||
endpoint="chat/completions",
|
||||
api_key=self.api_key,
|
||||
payload=payload,
|
||||
)
|
||||
except RuntimeError as exc:
|
||||
# 部分 OpenAI 兼容供应商或模型不支持 response_format。
|
||||
# 保留结构化优先,遇到 400 时退回普通对话,避免演示链路被接口能力差异阻断。
|
||||
if not response_format or "HTTP Error 400" not in str(exc):
|
||||
raise
|
||||
fallback_payload = {
|
||||
"model": self.model_name,
|
||||
"messages": messages,
|
||||
}
|
||||
data = _post_json(
|
||||
base_url=self.base_url,
|
||||
endpoint="chat/completions",
|
||||
api_key=self.api_key,
|
||||
payload=fallback_payload,
|
||||
)
|
||||
choice = data.get("choices", [{}])[0]
|
||||
content = choice.get("message", {}).get("content", "")
|
||||
return LLMResponse(
|
||||
@@ -197,5 +213,11 @@ def _post_json(base_url: str, endpoint: str, api_key: str, payload: dict) -> dic
|
||||
try:
|
||||
with urlopen(request, timeout=60) as response:
|
||||
return json.loads(response.read().decode("utf-8"))
|
||||
except HTTPError as exc:
|
||||
error_body = exc.read().decode("utf-8", errors="ignore")
|
||||
error_detail = f"{exc}"
|
||||
if error_body:
|
||||
error_detail = f"{error_detail} {error_body}"
|
||||
raise RuntimeError(f"OpenAI 兼容接口调用失败:{error_detail}") from exc
|
||||
except URLError as exc:
|
||||
raise RuntimeError(f"OpenAI 兼容接口调用失败:{exc}") from exc
|
||||
|
||||
Reference in New Issue
Block a user