mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
检查bot是否运行正常 / bot check (push) Waiting to run
CodeQL Code Security Analysis / Analyze (python) (push) Waiting to run
Sequential Lint and Type Check / pyright-call (push) Blocked by required conditions
Release Drafter / Update Release Draft (push) Waiting to run
Ruff Lint / Ruff Lint (push) Waiting to run
Force Sync to Aliyun / sync (push) Waiting to run
Sequential Lint and Type Check / ruff-call (push) Waiting to run
Update Version / update-version (push) Waiting to run
* ✨ feat(llm): 新增 Grok 适配器及 API 密钥重置指令 - 新增 Grok 适配器并支持原生推特检索工具 `x_search` - 优化 DeepSeek 适配器以迁移至 Responses 协议,增强多模态支持 - 在 LLM 管理插件中新增 `llm reset` 子命令以重置 Key 状态 - 增强 MCP 连接异常处理及流关闭容错能力 * 🚨 auto fix by pre-commit hooks --------- Co-authored-by: webjoin111 <455457521@qq.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
393 lines
11 KiB
Python
393 lines
11 KiB
Python
import fnmatch
|
|
|
|
from zhenxun.services.ai.core.models import (
|
|
ModelCapabilities,
|
|
ModelModality,
|
|
ReasoningMode,
|
|
)
|
|
from zhenxun.utils.pydantic_compat import model_copy
|
|
|
|
CTX_1_05M = 1_050_000
|
|
CTX_1M = 1_000_000
|
|
CTX_500K = 500_000
|
|
CTX_400K = 400_000
|
|
CTX_256K = 256_000
|
|
CTX_200K = 204_800
|
|
CTX_128K = 128_000
|
|
CTX_8K = 8_192
|
|
|
|
CAP_MULTIMODAL_EMBEDDING = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
ModelModality.FILE,
|
|
},
|
|
is_embedding_model=True,
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
STANDARD_TEXT_TOOL_CAPABILITIES = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
)
|
|
CAP_GEMINI_2_5 = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.BUDGET,
|
|
reasoning_visibility="visible",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"google_map",
|
|
"url_context",
|
|
},
|
|
)
|
|
CAP_GEMINI_3_BASE = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.LEVEL,
|
|
reasoning_visibility="visible",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"google_map",
|
|
"url_context",
|
|
},
|
|
features={
|
|
"mixed_tools",
|
|
"server_side_tool_invocations",
|
|
},
|
|
)
|
|
|
|
CAP_GEMINI_3_PRO = model_copy(
|
|
CAP_GEMINI_3_BASE,
|
|
update={
|
|
"reasoning_effort_map": {
|
|
"max": "high",
|
|
"xhigh": "high",
|
|
"minimal": "low",
|
|
"none": "low",
|
|
}
|
|
},
|
|
)
|
|
|
|
CAP_GEMINI_3_FLASH = model_copy(
|
|
CAP_GEMINI_3_BASE,
|
|
update={
|
|
"reasoning_effort_map": {"max": "high", "xhigh": "high", "none": "minimal"}
|
|
},
|
|
)
|
|
CAP_OPENAI_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="hidden",
|
|
supported_native_tools={
|
|
"web_search",
|
|
"code_execution",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
reasoning_effort_map={"max": "xhigh", "minimal": "none"},
|
|
)
|
|
CAP_OPENAI_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
"computer_use",
|
|
"file_search",
|
|
},
|
|
reasoning_effort_map={"max": "xhigh", "minimal": "none"},
|
|
)
|
|
CAP_GROK = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
supported_native_tools={"web_search", "x_search", "code_execution", "file_search"},
|
|
)
|
|
|
|
CAP_DEEPSEEK_PRO = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
reasoning_effort_map={"minimal": "low"},
|
|
supported_native_tools={"web_search"},
|
|
)
|
|
|
|
CAP_DEEPSEEK_FLASH = model_copy(
|
|
CAP_DEEPSEEK_PRO,
|
|
update={
|
|
"input_modalities": {ModelModality.TEXT, ModelModality.IMAGE},
|
|
},
|
|
)
|
|
CAP_MINIMAX_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
)
|
|
|
|
CAP_GLM_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.VIDEO,
|
|
ModelModality.FILE,
|
|
},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
)
|
|
|
|
CAP_GLM_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
)
|
|
|
|
CAP_MINIMAX_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE, ModelModality.VIDEO},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
)
|
|
|
|
CAP_MIMO_TEXT = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
supported_native_tools={"web_search"},
|
|
reasoning_effort_map={"max": "high", "xhigh": "high", "minimal": "low"},
|
|
)
|
|
|
|
CAP_MIMO_MULTIMODAL = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT, ModelModality.AUDIO},
|
|
supports_tool_calling=True,
|
|
supports_thinking_toggle=True,
|
|
supported_native_tools={"web_search"},
|
|
reasoning_effort_map={"max": "high", "xhigh": "high", "minimal": "low"},
|
|
)
|
|
|
|
|
|
CAP_OPENAI_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="alloy",
|
|
)
|
|
|
|
CAP_GEMINI_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="Aoede",
|
|
)
|
|
|
|
CAP_MINIMAX_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="female-shaonv",
|
|
)
|
|
|
|
CAP_MIMO_TTS = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.AUDIO},
|
|
supports_tool_calling=False,
|
|
default_voice_id="mimo_default",
|
|
)
|
|
|
|
CAP_TEXT_EMBEDDING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
is_embedding_model=True,
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
CAP_RERANK_ONLY = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
is_rerank_model=True,
|
|
)
|
|
|
|
CAP_OPENAI_IMAGE = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
output_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
supports_tool_calling=False,
|
|
)
|
|
|
|
CAP_GEMINI_IMAGE = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={ModelModality.TEXT, ModelModality.IMAGE},
|
|
supports_tool_calling=True,
|
|
supported_native_tools={
|
|
"web_search",
|
|
},
|
|
)
|
|
|
|
DEFAULT_PERMISSIVE_CAPABILITIES = ModelCapabilities(
|
|
input_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
ModelModality.VIDEO,
|
|
},
|
|
output_modalities={
|
|
ModelModality.TEXT,
|
|
ModelModality.IMAGE,
|
|
ModelModality.AUDIO,
|
|
},
|
|
supports_tool_calling=True,
|
|
)
|
|
|
|
MODEL_ALIAS_MAPPING: dict[str, str] = {
|
|
"*DeepSeek-V4-Pro*": "deepseek-v4-pro",
|
|
"*DeepSeek-V4-Flash*": "deepseek-v4-flash",
|
|
}
|
|
|
|
|
|
_ROUTING_TABLE: list[tuple[list[str], ModelCapabilities, int]] = [
|
|
(["mimo-*tts*"], CAP_MIMO_TTS, CTX_8K),
|
|
(["gemini-*tts*"], CAP_GEMINI_TTS, CTX_8K),
|
|
(["*minimax-*tts*", "*MiniMax-*tts*"], CAP_MINIMAX_TTS, CTX_8K),
|
|
(["*tts*"], CAP_OPENAI_TTS, CTX_8K),
|
|
(["*gpt*image*"], CAP_OPENAI_IMAGE, CTX_128K),
|
|
(["*gemini*image*", "*nano-banana*"], CAP_GEMINI_IMAGE, CTX_128K),
|
|
(["glm-4.6v*"], CAP_GLM_MULTIMODAL, CTX_128K),
|
|
(["glm-4.7-flash*"], STANDARD_TEXT_TOOL_CAPABILITIES, CTX_128K),
|
|
(["*grok-4.3*", "*grok-4.20*"], CAP_GROK, CTX_1M),
|
|
(["*grok*"], CAP_GROK, CTX_500K),
|
|
(["*deepseek*pro*"], CAP_DEEPSEEK_PRO, CTX_1M),
|
|
(["*deepseek*flash*"], CAP_DEEPSEEK_FLASH, CTX_1M),
|
|
(["glm-4-long*"], STANDARD_TEXT_TOOL_CAPABILITIES, CTX_1M),
|
|
(["*MiniMax-M3*"], CAP_MINIMAX_MULTIMODAL, CTX_1M),
|
|
(["mimo-v2.5-pro*", "mimo-v2-pro*", "mimo-v2-flash*"], CAP_MIMO_TEXT, CTX_1M),
|
|
(["mimo-v2.5", "mimo-v2-omni*"], CAP_MIMO_MULTIMODAL, CTX_1M),
|
|
(
|
|
["gpt-5.3-chat*", "gpt-5.3-instant*", "*codex-spark*"],
|
|
CAP_OPENAI_MULTIMODAL,
|
|
CTX_128K,
|
|
),
|
|
(
|
|
["gpt-[5-9]*mini*", "gpt-[5-9]*nano*", "gpt-[5-9]*-instant*", "*codex*"],
|
|
CAP_OPENAI_MULTIMODAL,
|
|
CTX_400K,
|
|
),
|
|
(["gpt-[5-9]*"], CAP_OPENAI_MULTIMODAL, CTX_1_05M),
|
|
(["gemini-3*pro*"], CAP_GEMINI_3_PRO, CTX_1M),
|
|
(["gemini-3*"], CAP_GEMINI_3_FLASH, CTX_1M),
|
|
(
|
|
["gemini-2.5-pro*", "gemini-2.5-flash*"],
|
|
CAP_GEMINI_2_5,
|
|
CTX_1M,
|
|
),
|
|
(
|
|
["kimi-k2.7*", "kimi-k2.6*", "kimi-k2.5*"],
|
|
DEFAULT_PERMISSIVE_CAPABILITIES,
|
|
CTX_256K,
|
|
),
|
|
(["glm-5v*"], CAP_GLM_MULTIMODAL, CTX_200K),
|
|
(["glm-5*", "glm-4.7*", "glm-4.6*"], CAP_GLM_REASONING, CTX_200K),
|
|
(["*MiniMax-M2*", "*minimax-m2*"], CAP_MINIMAX_REASONING, CTX_200K),
|
|
(["gpt-4*", "gpt-3.5*", "gpt-*"], CAP_OPENAI_MULTIMODAL, CTX_128K),
|
|
(["o1-*", "o3-*"], CAP_OPENAI_REASONING, CTX_128K),
|
|
(["glm-4v*"], CAP_GLM_MULTIMODAL, CTX_128K),
|
|
(
|
|
["glm-4.5*", "glm-4-flashx-*", "glm-4*"],
|
|
STANDARD_TEXT_TOOL_CAPABILITIES,
|
|
CTX_128K,
|
|
),
|
|
(
|
|
["gemini-embedding-2*", "jina-embeddings-v5-omni*"],
|
|
CAP_MULTIMODAL_EMBEDDING,
|
|
CTX_8K,
|
|
),
|
|
(
|
|
["*embedding*", "*Embedding*", "jina-embeddings-*", "bge-m3*", "*bge-large*"],
|
|
CAP_TEXT_EMBEDDING,
|
|
CTX_8K,
|
|
),
|
|
(["*reranker*", "*rerank*", "jina-colbert-*"], CAP_RERANK_ONLY, CTX_8K),
|
|
]
|
|
|
|
|
|
def _build_registry() -> dict[str, ModelCapabilities]:
|
|
"""构建模型能力注册表 (基于声明式路由表)"""
|
|
registry: dict[str, ModelCapabilities] = {}
|
|
|
|
for patterns, cap_template, ctx_limit in _ROUTING_TABLE:
|
|
cap_instance = model_copy(cap_template, update={"max_input_tokens": ctx_limit})
|
|
for pattern in patterns:
|
|
registry[pattern] = cap_instance
|
|
|
|
return registry
|
|
|
|
|
|
MODEL_CAPABILITIES_REGISTRY = _build_registry()
|
|
|
|
|
|
def get_model_capabilities(model_name: str) -> ModelCapabilities:
|
|
"""
|
|
从注册表获取模型能力,支持别名映射和通配符匹配。
|
|
"""
|
|
canonical_name = model_name
|
|
for alias_pattern, c_name in MODEL_ALIAS_MAPPING.items():
|
|
if fnmatch.fnmatch(model_name, alias_pattern):
|
|
canonical_name = c_name
|
|
break
|
|
|
|
parts = canonical_name.split("/")
|
|
names_to_check = ["/".join(parts[i:]) for i in range(len(parts))]
|
|
|
|
for name in names_to_check:
|
|
if name in MODEL_CAPABILITIES_REGISTRY:
|
|
return MODEL_CAPABILITIES_REGISTRY[name]
|
|
|
|
for pattern, capabilities in MODEL_CAPABILITIES_REGISTRY.items():
|
|
if "*" in pattern and fnmatch.fnmatch(name, pattern):
|
|
return capabilities
|
|
|
|
return DEFAULT_PERMISSIVE_CAPABILITIES
|