Files
BiliNote/backend/app/gpt/provider/OpenAI_compatible_provider.py
huangjianwu 41f17592c2 fix(backend): 部署韧性——模型自愈/就绪门禁/全局代理/启动诊断
- whisper: model.bin 截断/损坏时删目录重下重试一次,修「Unable to
  open file model.bin」死循环;mlx 同样按 config.json 判完整性
- /generate_note 加就绪门禁:本地转写引擎模型没下好直接拦截,返回
  reason=transcriber_model_not_ready,不让任务静默卡在首次下载
- 全局代理:新增 ProxyConfigManager(JSON 配置 + HTTP_PROXY env 兜底)
  + build_openai_client,统一注入代理到 LLM/Groq 客户端;yt-dlp 与
  youtube-transcript-api 也走代理
- build_openai_client 校验 api_key 非空,空 key 给「xxx 的 API Key
  未配置」而不是天书般的 Illegal header value b'Bearer '
- universal_gpt: 模型拒绝自定义 temperature(o1/o3/gpt-5 系列)时
  就地去掉参数重试,不消耗重试预算
- connect_test 改用真实 chat completion 而非 /v1/models 探测
- main.py: lifespan 拆 [startup 1/5..5/5] 分段日志 + 异常清晰定位
- /sys_health 重构为结构化返回 {backend,ffmpeg,db,whisper_model}

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-14 19:01:14 +08:00

41 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from typing import Optional, Union
from app.utils.logger import get_logger
from app.utils.openai_client import build_openai_client
logging= get_logger(__name__)
class OpenAICompatibleProvider:
def __init__(self, api_key: str, base_url: str, model: Union[str, None]=None):
# build_openai_client注入全局代理 + 校验 api_key 非空
self.client = build_openai_client(api_key, base_url, key_label="模型供应商的 API Key")
self.model = model
@property
def get_client(self):
return self.client
@staticmethod
def test_connection(api_key: str, base_url: str, model: str) -> bool:
"""发一条最小化 chat completion 验证 key / base_url / model 三方都通。
为什么不用 client.models.list()
- 部分代理 / 自建供应商不实现 /v1/models如某些 OpenAI 兼容网关)
- 部分供应商 key 在没有 inference 权限时 /v1/models 仍返回 200
最终用户跑的就是 chat.completions.create所以直接测它最忠实。
max_tokens=1 + temperature=0 让请求开销 < 0.0001 美元、延迟 < 2s。
"""
try:
client = build_openai_client(
api_key, base_url, key_label="模型供应商的 API Key", timeout=15.0,
)
client.chat.completions.create(
model=model,
messages=[{"role": "user", "content": "ping"}],
max_tokens=1,
temperature=0,
)
logging.info(f"连通性测试成功model={model}")
return True
except Exception as e:
logging.warning(f"连通性测试失败model={model}{e}")
return False