mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
* ✨ feat!(llm): 重构并升级大语言模型服务为全新 AI 智能体框架 - 【重构】将原 services/llm 重构并迁移至全新的 services/ai 架构,提供向下兼容垫片 - 【新增】引入 Agent、Team、Workflow 三大智能体与工作流编排范式 - 【新增】引入基于 RAG 的长期向量记忆与中期槽位记忆系统 - 【新增】引入基于 Docker 的安全代码执行沙箱环境 - 【新增】支持 MCP 协议,允许动态管理和调用 MCP 服务 - 【新增】引入输入输出安全合规护栏与自愈反思机制 - 【优化】重构并优化多厂商 API 适配器 (Gemini, OpenAI, DeepSeek, GLM 等) - 【优化】优化日志脱敏与 Token 预估机制 - 【移除】移除旧版 llm default 和 llm reset-key 命令,新增 llm mcp 管理命令 * 🔧 chore(deps): 更新项目依赖与配置 - 添加 mcp、jieba 和 aiodocker 依赖到配置文件及 requirements.txt - 在 pyright 配置中设置 reportMissingImports 为 none - 调整 .gitignore 中 resources 目录的忽略规则 * ♻️ refactor(tools): 重构工具终止机制并清理知识库日志输出 - 统一使用 `context.state["__end_run__"]` 替代 `EndRunResult` 控制任务结束 - 移除文件系统和向量知识库检索工具中 `ToolResult` 的 `.with_log` 调用 - 调整指令处理器(Directive)的返回值为 `tool_res.output` - 修复部分类型检查警告并优化联合类型判断语法 * ♻️ refactor(tools): 重构工具副作用指令与控制流熔断机制 - 引入 `DirectivePayload` 及 `ToolResult` 的子类以结构化表达工具副作用 - 移除通过 `context.state` 传递魔术变量的隐式控制流设计 - 重构 `DirectiveManager` 处理器接口,直接在处理器中修改 `AgentState` 并构建 `AgentRunResult` - 在 `StandardAgentExecutor` 中统一通过 `directive_manager` 调度工具返回的副作用指令 - 补全 `MessageBuilder` 中部分核心方法的文档注释 * 🐛 fix(sandbox): 修复 Docker 沙箱容器状态检测与会话清理逻辑 -【修复】修正 `is_alive` 中直接读取私有属性的问题,改用 `show()` 返回值 -【修复】解决 `execute_code` 中缓存的执行器与当前会话不一致的问题 -【优化】在清理工作区前增加容器存活检测,避免向已死容器发送请求 -【优化】创建容器时增加运行状态校验,若已停止则自动从缓存中移除并重建 -【优化】优化容器销毁和清理逻辑,静默处理容器不存在 (404) 的异常 * 📝 docs(core): 补充核心模块初始化方法的文档注释 * 🚨 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>
353 lines
10 KiB
Python
353 lines
10 KiB
Python
import fnmatch
|
|
|
|
from zhenxun.services.ai.core.models import (
|
|
ModelCapabilities,
|
|
ModelModality,
|
|
ReasoningMode,
|
|
)
|
|
from zhenxun.utils.pydantic_compat import model_copy
|
|
|
|
CTX_1M = 1_000_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_DEEPSEEK_V4 = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=True,
|
|
reasoning_mode=ReasoningMode.EFFORT,
|
|
reasoning_visibility="visible",
|
|
reasoning_effort_map={"minimal": "low"},
|
|
)
|
|
CAP_MINIMAX_REASONING = ModelCapabilities(
|
|
input_modalities={ModelModality.TEXT},
|
|
output_modalities={ModelModality.TEXT},
|
|
supports_tool_calling=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,
|
|
)
|
|
|
|
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,
|
|
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,
|
|
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),
|
|
(["deepseek-v4-pro*", "deepseek-v4-flash*"], CAP_DEEPSEEK_V4, 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.5*", "gpt-5.4*"], CAP_OPENAI_MULTIMODAL, CTX_1M),
|
|
(["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,
|
|
),
|
|
(
|
|
["gpt-5*", "gpt-5-mini*", "gpt-5-nano*", "*codex*"],
|
|
CAP_OPENAI_MULTIMODAL,
|
|
CTX_400K,
|
|
),
|
|
(
|
|
["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*"], STANDARD_TEXT_TOOL_CAPABILITIES, 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
|