mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2026-10-08 21:30:01 +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:
@@ -8,21 +8,12 @@ from pydantic import BaseModel, Field, ValidationError, create_model
|
||||
|
||||
from zhenxun.services.ai.core.exceptions import (
|
||||
SchemaParseError,
|
||||
SchemaValidationError,
|
||||
)
|
||||
from zhenxun.services.ai.core.messages.types import OutputDataT
|
||||
from zhenxun.services.ai.utils.logger import log_core as logger
|
||||
from zhenxun.utils.pydantic_compat import model_json_schema, model_validate
|
||||
|
||||
DEFAULT_IVR_TEMPLATE = (
|
||||
"### ❌ [输出内容或格式验证失败]\n"
|
||||
"你的上一次输出未能通过系统的校验与规则检查。请立即启动修正流程:\n\n"
|
||||
"**错误反馈报告:**\n"
|
||||
"> {error_msg}\n\n"
|
||||
"**修正要求:** 请结合反馈报告,"
|
||||
"仔细反思你的输出内容或格式,\n"
|
||||
"并重新生成正确的数据以满足所有的规则与规范。"
|
||||
)
|
||||
|
||||
|
||||
class BaseOutputProcessor(Generic[OutputDataT]):
|
||||
"""
|
||||
@@ -47,7 +38,7 @@ class BaseOutputProcessor(Generic[OutputDataT]):
|
||||
如果不为 None 则跳过根据 response_model 生成,默认 None。
|
||||
""" # noqa: E501
|
||||
self.original_model = response_model
|
||||
self.error_template = error_template or DEFAULT_IVR_TEMPLATE
|
||||
self.error_template = error_template
|
||||
self.raw_schema = raw_schema
|
||||
self.target_model = None
|
||||
self.is_union_wrapped = False
|
||||
@@ -120,7 +111,7 @@ class BaseOutputProcessor(Generic[OutputDataT]):
|
||||
msg = err.get("msg", "")
|
||||
error_msgs.append(f"字段 `{loc}`: {msg}")
|
||||
clean_error_str = "\n".join(error_msgs)
|
||||
raise SchemaParseError(
|
||||
raise SchemaValidationError(
|
||||
f"数据内容未通过规则校验:\n{clean_error_str}"
|
||||
)
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ LLM Token 动态预估与上下文管理模块
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
import json
|
||||
import math
|
||||
import re
|
||||
from typing import Any
|
||||
@@ -56,17 +57,11 @@ class TokenCounter:
|
||||
@classmethod
|
||||
def count_tools_schema(cls, obj: dict | list | str | Any) -> int:
|
||||
"""递归计算 JSON Schema 结构在被大模型作为工具时的 Token 开销。"""
|
||||
if isinstance(obj, dict):
|
||||
cost = len(obj.keys()) * 12
|
||||
for k, v in obj.items():
|
||||
if k == "description" and isinstance(v, str):
|
||||
cost += int(len(v) * 0.3)
|
||||
else:
|
||||
cost += cls.count_tools_schema(v)
|
||||
return cost
|
||||
elif isinstance(obj, list):
|
||||
return sum(cls.count_tools_schema(item) for item in obj)
|
||||
return 0
|
||||
try:
|
||||
json_str = json.dumps(obj, ensure_ascii=False)
|
||||
return int(cls._count_text(json_str) * 1.2) + 15
|
||||
except Exception:
|
||||
return 50
|
||||
|
||||
@classmethod
|
||||
def count_message(cls, msg: LLMMessage, model_name: str) -> int:
|
||||
|
||||
Reference in New Issue
Block a user