mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-07 05:47:56 +08:00
- Chat document attachments (PDF/DOCX/XLSX/PPTX) with text extraction - Reasoning model <think> block display as collapsible cards - Tri-state send_dimensions toggle for embeddings (#368) - LLM provider_core module refactor + context_window detection - Soul template editor (PR #373), Co-Writer save-to-notebook - Knowledge Base drag-and-drop upload, delete resilience (#370), progress persistence, supported-file-types endpoint - Question generation language fidelity via shared prompt directives - Settings tour redesign, CLI notebook add-md/replace-md - Bug fixes: CJK bot name slug, I18nProvider init, Skills default off, Moonshot base URL, version badge - Provider registry: custom_anthropic, thinking_style, alias expansion - +1900 test lines across 15 new/expanded test files Made-with: Cursor
396 lines
12 KiB
Python
396 lines
12 KiB
Python
"""
|
|
Provider Registry — single source of truth for LLM provider metadata.
|
|
|
|
Adding a new provider:
|
|
1. Add a ProviderSpec to PROVIDERS below.
|
|
Done. Env vars, config matching, status display all derive from here.
|
|
|
|
Order matters — it controls match priority and fallback. Gateways first.
|
|
Every entry writes out all fields so you can copy-paste as a template.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
from typing import Any
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProviderSpec:
|
|
"""One LLM provider's metadata. See PROVIDERS below for real examples.
|
|
|
|
Placeholders in env_extras values:
|
|
{api_key} — the user's API key
|
|
{api_base} — api_base from config, or this spec's default_api_base
|
|
"""
|
|
|
|
# identity
|
|
name: str
|
|
keywords: tuple[str, ...]
|
|
env_key: str
|
|
display_name: str = ""
|
|
|
|
# Which provider implementation to use:
|
|
# "openai_compat" | "anthropic" | "azure_openai" | "openai_codex" | "github_copilot"
|
|
backend: str = "openai_compat"
|
|
|
|
# extra env vars, e.g. (("ZHIPUAI_API_KEY", "{api_key}"),)
|
|
env_extras: tuple[tuple[str, str], ...] = ()
|
|
|
|
# gateway / local detection
|
|
is_gateway: bool = False
|
|
is_local: bool = False
|
|
detect_by_key_prefix: str = ""
|
|
detect_by_base_keyword: str = ""
|
|
default_api_base: str = ""
|
|
|
|
# gateway behavior
|
|
strip_model_prefix: bool = False
|
|
supports_max_completion_tokens: bool = False
|
|
|
|
# per-model param overrides, e.g. (("kimi-k2.5", {"temperature": 1.0}),)
|
|
model_overrides: tuple[tuple[str, dict[str, Any]], ...] = ()
|
|
|
|
# OAuth-based providers (e.g., OpenAI Codex) don't use API keys
|
|
is_oauth: bool = False
|
|
|
|
# Direct providers skip API-key validation
|
|
is_direct: bool = False
|
|
|
|
# Provider supports cache_control on content blocks (e.g. Anthropic prompt caching)
|
|
supports_prompt_caching: bool = False
|
|
|
|
@property
|
|
def mode(self) -> str:
|
|
if self.is_oauth:
|
|
return "oauth"
|
|
if self.is_direct:
|
|
return "direct"
|
|
if self.is_gateway:
|
|
return "gateway"
|
|
if self.is_local:
|
|
return "local"
|
|
return "standard"
|
|
|
|
@property
|
|
def label(self) -> str:
|
|
return self.display_name or self.name.title()
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# PROVIDERS — the registry. Order = priority. Copy any entry as template.
|
|
# ---------------------------------------------------------------------------
|
|
|
|
PROVIDERS: tuple[ProviderSpec, ...] = (
|
|
# === Direct (user supplies everything) ==================================
|
|
ProviderSpec(
|
|
name="custom",
|
|
keywords=(),
|
|
env_key="",
|
|
display_name="Custom",
|
|
backend="openai_compat",
|
|
is_direct=True,
|
|
),
|
|
ProviderSpec(
|
|
name="custom_anthropic",
|
|
keywords=(),
|
|
env_key="",
|
|
display_name="Custom (Anthropic API)",
|
|
backend="anthropic",
|
|
is_direct=True,
|
|
),
|
|
ProviderSpec(
|
|
name="azure_openai",
|
|
keywords=("azure", "azure-openai"),
|
|
env_key="",
|
|
display_name="Azure OpenAI",
|
|
backend="azure_openai",
|
|
is_direct=True,
|
|
),
|
|
# === Gateways (detected by api_key / api_base, route any model) ========
|
|
ProviderSpec(
|
|
name="openrouter",
|
|
keywords=("openrouter",),
|
|
env_key="OPENROUTER_API_KEY",
|
|
display_name="OpenRouter",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
detect_by_key_prefix="sk-or-",
|
|
detect_by_base_keyword="openrouter",
|
|
default_api_base="https://openrouter.ai/api/v1",
|
|
supports_prompt_caching=True,
|
|
),
|
|
ProviderSpec(
|
|
name="aihubmix",
|
|
keywords=("aihubmix",),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="AiHubMix",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
detect_by_base_keyword="aihubmix",
|
|
default_api_base="https://aihubmix.com/v1",
|
|
strip_model_prefix=True,
|
|
),
|
|
ProviderSpec(
|
|
name="siliconflow",
|
|
keywords=("siliconflow",),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="SiliconFlow",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
detect_by_base_keyword="siliconflow",
|
|
default_api_base="https://api.siliconflow.cn/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="volcengine",
|
|
keywords=("volcengine", "volces", "ark"),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="VolcEngine",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
detect_by_base_keyword="volces",
|
|
default_api_base="https://ark.cn-beijing.volces.com/api/v3",
|
|
),
|
|
ProviderSpec(
|
|
name="volcengine_coding_plan",
|
|
keywords=("volcengine-plan",),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="VolcEngine Coding Plan",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
default_api_base="https://ark.cn-beijing.volces.com/api/coding/v3",
|
|
strip_model_prefix=True,
|
|
),
|
|
ProviderSpec(
|
|
name="byteplus",
|
|
keywords=("byteplus",),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="BytePlus",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
detect_by_base_keyword="bytepluses",
|
|
default_api_base="https://ark.ap-southeast.bytepluses.com/api/v3",
|
|
strip_model_prefix=True,
|
|
),
|
|
ProviderSpec(
|
|
name="byteplus_coding_plan",
|
|
keywords=("byteplus-plan",),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="BytePlus Coding Plan",
|
|
backend="openai_compat",
|
|
is_gateway=True,
|
|
default_api_base="https://ark.ap-southeast.bytepluses.com/api/coding/v3",
|
|
strip_model_prefix=True,
|
|
),
|
|
# === Standard providers (matched by model-name keywords) ===============
|
|
ProviderSpec(
|
|
name="anthropic",
|
|
keywords=("anthropic", "claude"),
|
|
env_key="ANTHROPIC_API_KEY",
|
|
display_name="Anthropic",
|
|
backend="anthropic",
|
|
default_api_base="https://api.anthropic.com/v1",
|
|
supports_prompt_caching=True,
|
|
),
|
|
ProviderSpec(
|
|
name="openai",
|
|
keywords=("openai", "gpt"),
|
|
env_key="OPENAI_API_KEY",
|
|
display_name="OpenAI",
|
|
backend="openai_compat",
|
|
supports_max_completion_tokens=True,
|
|
),
|
|
ProviderSpec(
|
|
name="openai_codex",
|
|
keywords=("openai-codex",),
|
|
env_key="",
|
|
display_name="OpenAI Codex",
|
|
backend="openai_codex",
|
|
detect_by_base_keyword="codex",
|
|
default_api_base="https://chatgpt.com/backend-api",
|
|
is_oauth=True,
|
|
),
|
|
ProviderSpec(
|
|
name="github_copilot",
|
|
keywords=("github_copilot", "copilot"),
|
|
env_key="",
|
|
display_name="Github Copilot",
|
|
backend="github_copilot",
|
|
default_api_base="https://api.githubcopilot.com",
|
|
strip_model_prefix=True,
|
|
is_oauth=True,
|
|
),
|
|
ProviderSpec(
|
|
name="deepseek",
|
|
keywords=("deepseek",),
|
|
env_key="DEEPSEEK_API_KEY",
|
|
display_name="DeepSeek",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.deepseek.com",
|
|
),
|
|
ProviderSpec(
|
|
name="gemini",
|
|
keywords=("gemini",),
|
|
env_key="GEMINI_API_KEY",
|
|
display_name="Gemini",
|
|
backend="openai_compat",
|
|
default_api_base="https://generativelanguage.googleapis.com/v1beta/openai/",
|
|
),
|
|
ProviderSpec(
|
|
name="zhipu",
|
|
keywords=("zhipu", "glm", "zai"),
|
|
env_key="ZAI_API_KEY",
|
|
display_name="Zhipu AI",
|
|
backend="openai_compat",
|
|
env_extras=(("ZHIPUAI_API_KEY", "{api_key}"),),
|
|
default_api_base="https://open.bigmodel.cn/api/paas/v4",
|
|
),
|
|
ProviderSpec(
|
|
name="dashscope",
|
|
keywords=("qwen", "dashscope"),
|
|
env_key="DASHSCOPE_API_KEY",
|
|
display_name="DashScope",
|
|
backend="openai_compat",
|
|
default_api_base="https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="moonshot",
|
|
keywords=("moonshot", "kimi"),
|
|
env_key="MOONSHOT_API_KEY",
|
|
display_name="Moonshot",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.moonshot.cn/v1",
|
|
model_overrides=(("kimi-k2.5", {"temperature": 1.0}),),
|
|
),
|
|
ProviderSpec(
|
|
name="minimax",
|
|
keywords=("minimax",),
|
|
env_key="MINIMAX_API_KEY",
|
|
display_name="MiniMax",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.minimaxi.com/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="mistral",
|
|
keywords=("mistral",),
|
|
env_key="MISTRAL_API_KEY",
|
|
display_name="Mistral",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.mistral.ai/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="stepfun",
|
|
keywords=("stepfun", "step"),
|
|
env_key="STEPFUN_API_KEY",
|
|
display_name="Step Fun",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.stepfun.com/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="xiaomi_mimo",
|
|
keywords=("xiaomi_mimo", "mimo"),
|
|
env_key="XIAOMIMIMO_API_KEY",
|
|
display_name="Xiaomi MIMO",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.xiaomimimo.com/v1",
|
|
),
|
|
# === Local deployment ==================================================
|
|
ProviderSpec(
|
|
name="vllm",
|
|
keywords=("vllm",),
|
|
env_key="HOSTED_VLLM_API_KEY",
|
|
display_name="vLLM/Local",
|
|
backend="openai_compat",
|
|
is_local=True,
|
|
default_api_base="http://localhost:8000/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="ollama",
|
|
keywords=("ollama", "nemotron"),
|
|
env_key="OLLAMA_API_KEY",
|
|
display_name="Ollama",
|
|
backend="openai_compat",
|
|
is_local=True,
|
|
detect_by_base_keyword="11434",
|
|
default_api_base="http://localhost:11434/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="ovms",
|
|
keywords=("openvino", "ovms"),
|
|
env_key="",
|
|
display_name="OpenVINO Model Server",
|
|
backend="openai_compat",
|
|
is_direct=True,
|
|
is_local=True,
|
|
default_api_base="http://localhost:8000/v3",
|
|
),
|
|
# === Auxiliary ==========================================================
|
|
ProviderSpec(
|
|
name="groq",
|
|
keywords=("groq",),
|
|
env_key="GROQ_API_KEY",
|
|
display_name="Groq",
|
|
backend="openai_compat",
|
|
default_api_base="https://api.groq.com/openai/v1",
|
|
),
|
|
ProviderSpec(
|
|
name="qianfan",
|
|
keywords=("qianfan", "ernie"),
|
|
env_key="QIANFAN_API_KEY",
|
|
display_name="Qianfan",
|
|
backend="openai_compat",
|
|
default_api_base="https://qianfan.baidubce.com/v2",
|
|
),
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Lookup helpers
|
|
# ---------------------------------------------------------------------------
|
|
|
|
|
|
def find_by_model(model: str) -> ProviderSpec | None:
|
|
"""Match a standard provider by model-name keyword (case-insensitive)."""
|
|
model_lower = model.lower()
|
|
model_normalized = model_lower.replace("-", "_")
|
|
model_prefix = model_lower.split("/", 1)[0] if "/" in model_lower else ""
|
|
normalized_prefix = model_prefix.replace("-", "_")
|
|
std_specs = [s for s in PROVIDERS if not s.is_gateway and not s.is_local]
|
|
|
|
for spec in std_specs:
|
|
if model_prefix and normalized_prefix == spec.name:
|
|
return spec
|
|
for spec in std_specs:
|
|
if any(
|
|
kw in model_lower or kw.replace("-", "_") in model_normalized for kw in spec.keywords
|
|
):
|
|
return spec
|
|
return None
|
|
|
|
|
|
def find_gateway(
|
|
provider_name: str | None = None,
|
|
api_key: str | None = None,
|
|
api_base: str | None = None,
|
|
) -> ProviderSpec | None:
|
|
"""Detect gateway/local provider."""
|
|
if provider_name:
|
|
spec = find_by_name(provider_name)
|
|
if spec and (spec.is_gateway or spec.is_local):
|
|
return spec
|
|
|
|
for spec in PROVIDERS:
|
|
if spec.detect_by_key_prefix and api_key and api_key.startswith(spec.detect_by_key_prefix):
|
|
return spec
|
|
if spec.detect_by_base_keyword and api_base and spec.detect_by_base_keyword in api_base:
|
|
return spec
|
|
return None
|
|
|
|
|
|
def find_by_name(name: str) -> ProviderSpec | None:
|
|
"""Find a provider spec by config field name, e.g. "dashscope"."""
|
|
for spec in PROVIDERS:
|
|
if spec.name == name:
|
|
return spec
|
|
return None
|