Skip to content

Agent 策略

策略契约

策略实现 AgentStrategy ABC(或有状态实例用 StrategyLikedObject),并通过 get_category() 声明类别

类别执行方式框架的角色
agent / agent-mixed每轮 single_execute()框架运行循环
rag / workflow一次 run()策略完全掌控

_run_strategy 按类别分派并跳入对应的工作流块。

通过 DI 获取资源

策略从不穿过 ChatObject 拿资源——_StrategyBase 暴露便捷属性, 从 StrategyContext DI 字段解析,回退到 chat_object

属性解析自回退
self.presetctx.presetchat_object.preset
self.configctx.configchat_object.config
self.io_streamctx.io_streamchat_object.io_stream
self.train_contentctx.train_contentchat_object.train.content
self.stream_idctx.stream_idchat_object.stream_id
self.resp_extra_usagectx.resp_extra_usagechat_object._di_resp.extra_usage

chat_object生命周期管理器句柄——核心引用,不是弃用路径。 优先 DI 字段;回退 chat_object

内置 Step 驱动 ReAct 策略

ReActAgentStrategy(类别 agent-mixed)是默认策略。其执行是节点驱动 的:LLM 决定是否把任务分解为语义 DAG;框架按拓扑序走 DAG,每个Step 对应一个节点。

intro_step → [NATIVE_WHILE: single_execute → after_iteration] → leave_step
  • decompose —— LLM 返回 {needs_decomposition, dag, reason}(或 simple 模式)
  • Step —— 一个 DAG 节点;可跨多轮工具调用
  • 停滞检测 —— 重复相同签名 → give-up prompt + 取消
  • 摘要 —— 每个 Step 以主谓短语摘要结束(可被事件覆盖)
  • 生命周期事件 —— step_intro/leave/iterationtool_call/return
  • update_step 工具 —— agent 可中途修订计划

完整细节:进阶 → Step 循环

其他内置策略

策略类别用途
HybridReActAgentStrategyagent-mixedMoE 模型;XML 风格结果(已弃用,v0.14.0 移除
NoActionAgentStrategyworkflow完全跳过工具调用

编写自定义策略

python
from amrita_core.agent.strategy import AgentStrategy
from typing import Literal


class MyStrategy(AgentStrategy):
    async def single_execute(self) -> bool:
        # 一轮工具调用。返回 True 继续,False 停止。
        return True

    async def on_post_process(self) -> None:
        pass  # 循环结束后

    @classmethod
    def get_category(cls) -> Literal["agent"]:
        return "agent"

ReAct 风格策略请扩展 BaseReActAgentStrategy,覆写模板方法 (_append_tool_result_to_context_handle_error_append_append_reasoning……)。

下一步

数据层——消息、记忆与后端。

Apache 2.0 许可证(一些内容可能没有完全翻译成中文,请以英文文档为准。)