mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-09-28 16:20:56 +08:00
♻️ refactor(agent): 重构 Agent 状态管理与执行器流程,优化 Token 预估与自愈反思机制 (#2150)
- 统一使用 `run_context.run.messages` 作为消息历史的单一数据源,清理 `AgentState` 冗余字段 - 将工具消息装配逻辑 `assemble_tool_message` 提取并重构至 `ToolExecutor` - 引入 `token_drift` 动态校准偏移量,并精确计算工具与系统提示词的 Token 开销 - 重构 `ReflexionCapability` 自愈反思引擎,基于异常多态与模板字典动态生成反馈提示词 - 支持通过 `resolve_model_capabilities` 解析并合并用户自定义的模型能力覆盖 - 在执行器循环中支持 `should_reset_cycle`,以优雅处理外部干预(如用户追加指示) - 扩展 `capabilities` 中对 `gpt-[5-9]*` 等新型号模型的能力定义与上下文限制 Co-authored-by: webjoin111 <455457521@qq.com>
This commit is contained in:
@@ -11,11 +11,11 @@ from zhenxun.services.ai.config import (
|
||||
get_llm_config,
|
||||
)
|
||||
from zhenxun.services.ai.core.exceptions import ConfigurationException
|
||||
from zhenxun.services.ai.core.models import ModelDetail
|
||||
from zhenxun.services.ai.core.models import ModelCapabilities, ModelDetail
|
||||
from zhenxun.services.ai.core.options import GenerationConfig
|
||||
from zhenxun.services.ai.utils.logger import log_llm as logger
|
||||
from zhenxun.utils.manager.priority_manager import PriorityLifecycle
|
||||
from zhenxun.utils.pydantic_compat import model_dump
|
||||
from zhenxun.utils.pydantic_compat import model_copy, model_dump
|
||||
|
||||
from .system.cache import clear_model_cache, get_or_create_model
|
||||
from .system.capabilities import get_model_capabilities
|
||||
@@ -191,6 +191,40 @@ def get_default_model(task: str = "chat") -> str | None:
|
||||
return getattr(config.default_models, task, None)
|
||||
|
||||
|
||||
async def resolve_model_capabilities(
|
||||
provider_model_name: str | None = None, task: str = "chat"
|
||||
) -> ModelCapabilities:
|
||||
"""解析并合并带有用户自定义覆盖(如 max_input_tokens) 的模型能力。"""
|
||||
resolved_name = provider_model_name
|
||||
if resolved_name is None:
|
||||
resolved_name = get_default_model(task)
|
||||
if resolved_name is None:
|
||||
avail = list_available_models()
|
||||
if not avail:
|
||||
return get_model_capabilities("unknown")
|
||||
resolved_name = avail[0]["full_name"]
|
||||
|
||||
group_name = _get_group_name(resolved_name)
|
||||
if group_name is not None:
|
||||
model_names = _resolve_model_group(group_name)
|
||||
if model_names:
|
||||
resolved_name = model_names[0]
|
||||
|
||||
prov_name, mod_name = parse_provider_model_string(resolved_name)
|
||||
caps = get_model_capabilities(mod_name or resolved_name)
|
||||
|
||||
if prov_name and mod_name:
|
||||
config_tuple = find_model_config(prov_name, mod_name)
|
||||
if config_tuple:
|
||||
_, model_detail = config_tuple
|
||||
if model_detail.max_input_tokens is not None:
|
||||
caps = model_copy(
|
||||
caps, update={"max_input_tokens": model_detail.max_input_tokens}
|
||||
)
|
||||
|
||||
return caps
|
||||
|
||||
|
||||
async def get_key_usage_stats() -> dict[str, Any]:
|
||||
"""获取所有 Provider 的 Key 使用统计。"""
|
||||
providers = get_configured_providers()
|
||||
|
||||
Reference in New Issue
Block a user