fix(llm): keep saved relay provider configurations working

This commit is contained in:
LIN student
2026-11-25 20:15:17 +08:00
committed by jxxghp
parent 8995e9bfc7
commit 1b373e2eaa
3 changed files with 95 additions and 1 deletions

View File

@@ -6,12 +6,35 @@ from __future__ import annotations
import json
from dataclasses import dataclass
from functools import cache
from pathlib import Path
from typing import Any, Optional, Tuple
from app.agent.llm.runtime import LLMProviderError
from app.runtime.log import logger
# v3.2.12 移除的内置 OpenAI 兼容预设。只用于恢复已保存的标识、原地址和模型列表来源。
_LEGACY_OPENAI_PROVIDERS = {
"aigrow": ("https://aigrow.gg/v1", "openai_compatible"),
"api-route": ("https://global.api-route.com/v1", "openai_compatible"),
"atlascloud": ("https://api.atlascloud.ai/v1", "openai_compatible"),
"cheapaimodel": ("https://api.cheapaimodel.ai/v1", "openai_compatible"),
"cheaperinference": ("https://api.cheaperinference.com/v1", "openai_compatible"),
"fireworks-ai": ("https://api.fireworks.ai/inference/v1", "models_dev_only"),
"huggingface": ("https://router.huggingface.co/v1", "models_dev_only"),
"jiekou": ("https://api.jiekou.ai/openai", "models_dev_only"),
"kilo": ("https://api.kilo.ai/api/gateway", "models_dev_only"),
"llama": ("https://api.llama.com/compat/v1", "models_dev_only"),
"llmgateway": ("https://api.llmgateway.io/v1", "models_dev_only"),
"nova": ("https://api.nova.amazon.com/v1", "models_dev_only"),
"obscurify-ai": ("https://obscurify.ai/v1", "openai_compatible"),
"poe": ("https://api.poe.com/v1", "models_dev_only"),
"qihang-ai": ("https://api.qhaigc.net/v1", "models_dev_only"),
"runcrate": ("https://api.runcrate.ai/v1", "openai_compatible"),
"trustedrouter": ("https://api.trustedrouter.com/v1", "openai_compatible"),
"uberllm": ("https://api.martllm.com/v1", "openai_compatible"),
}
@dataclass(frozen=True)
class ProviderAuthMethod:
@@ -94,6 +117,30 @@ class ProviderSpec:
description: str = ""
@cache
def _legacy_openai_provider(provider_id: str, base_url: str, model_list_strategy: str) -> ProviderSpec:
"""
从旧内置预设恢复连接定义,同一进程中复用定义以免重复记录迁移提示。
:param provider_id: 已移除的内置预设标识
:param base_url: 原内置预设的默认服务地址
:param model_list_strategy: 原模型列表查询策略
:return: 仅用于按标识解析的 OpenAI 兼容定义
"""
logger.warning(
f"LLM 提供商预设 {provider_id} 已移出目录。"
f"重新配置时请选择 OpenAI 兼容并保留原有 Base URL,未填写时使用 {base_url}。"
)
return ProviderSpec(
id=provider_id,
name=provider_id,
runtime="openai_compatible",
default_base_url=base_url,
models_dev_provider_id=provider_id,
model_list_strategy=model_list_strategy,
)
class _ProviderCatalog:
"""
LLM Provider 目录、预设和模型元数据的唯一 owner。
@@ -921,12 +968,18 @@ class _ProviderCatalog:
def get_provider(self, provider_id: str) -> ProviderSpec:
"""
按 provider id 获取定义。
按标识获取内置定义或已移除的旧预设定义。
:param provider_id: 内置或已保存的提供商标识
:return: 用于连接模型服务的定义
:raises LLMProviderError: 无法识别提供商标识
"""
normalized = self._normalize_provider_id(provider_id)
for spec in self._provider_specs():
if spec.id == normalized:
return spec
if legacy := _LEGACY_OPENAI_PROVIDERS.get(normalized):
return _legacy_openai_provider(normalized, *legacy)
raise LLMProviderError(f"不支持的 LLM 提供商:{provider_id}")
@staticmethod

View File

@@ -295,6 +295,11 @@ moviepilot setup --config-dir /path/to/moviepilot-config
- 媒体服务器
- 消息通知渠道
升级后,已移出目录的旧内置中转预设仍使用原有配置。重新配置时选择「OpenAI 兼容」
(`LLM_PROVIDER=openai`),保留原有的模型 ID、API Key 和 Base URL。此前未填写 Base URL
时,填写日志提示中的原预设地址。原来通过 models.dev 动态选择的其他供应商,可按其服务协议
选择通用兼容入口并填写原有 Base URL。
`LLM_TEMPERATURE` 为可选配置,默认留空。设置页留空或清空配置后,LLM 请求不发送
`temperature`,使用模型服务默认值;Google 原生运行时使用 SDK 的模型默认温度。
显式填写 `0`、`1` 等数值仍按填写值发送。

View File

@@ -90,6 +90,42 @@ def test_builtin_provider_catalog_exposes_generic_entries_and_curated_vendors()
assert removed not in provider_ids
@pytest.mark.usefixtures("_isolate_provider_manager")
@pytest.mark.parametrize(
("provider_id", "saved_base_url", "expected_base_url"),
[
("api-route", None, "https://global.api-route.com/v1"),
("aigrow", None, "https://aigrow.gg/v1"),
("uberllm", None, "https://api.martllm.com/v1"),
("atlascloud", " https://proxy.example.test/v1/ ", "https://proxy.example.test/v1"),
("huggingface", None, "https://router.huggingface.co/v1"),
],
)
def test_saved_relay_config_keeps_runtime_endpoint(
provider_id: str, saved_base_url: str | None, expected_base_url: str,
) -> None:
"""
旧中转预设退出目录后,已保存的配置仍使用原接口地址和 API Key。
"""
manager = LLMProviderManager()
manager._models_dev_data = {}
runtime = asyncio.run(
manager.resolve_runtime(
provider_id=provider_id,
model="custom-model",
api_key="test-key",
base_url=saved_base_url,
)
)
assert runtime["runtime"] == "openai_compatible"
assert runtime["base_url"] == expected_base_url
assert runtime["api_key"] == "test-key"
assert runtime["model_id"] == "custom-model"
assert manager.resolve_model_list_base_url(provider_id, saved_base_url) == expected_base_url
@pytest.mark.usefixtures("_isolate_provider_manager")
def test_generic_anthropic_compatible_provider_runtime_and_metadata_boundary() -> None:
"""