Expand description
ReAct (Reasoning + Acting) Agent 框架
基于 ractor Actor 模型实现的 ReAct Agent,支持:
- 思考-行动-观察循环: 标准 ReAct 推理模式
- 工具调用: 支持自定义工具注册和执行
- Actor 模型: 基于 ractor 实现,支持并发和消息传递
- AutoAgent: 自动选择最佳行动策略
- 流式输出: 支持流式思考过程输出
§架构
┌─────────────────────────────────────────────────────────────────┐
│ ReAct Agent 架构 │
├─────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Input │─────▶│ Thought │─────▶│ Action │ │
│ │ (任务) │ │ (推理) │ │ (行动) │ │
│ └─────────────┘ └─────────────┘ └──────┬──────┘ │
│ │ │
│ ▼ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ Output │◀─────│ Final │◀─────│ Observation │ │
│ │ (结果) │ │ Answer │ │ (观察) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────┐ │
│ │ Tool Registry │ │
│ │ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐ │ │
│ │ │Tool1│ │Tool2│ │Tool3│ │Tool4│ │ ... │ │ │
│ │ └─────┘ └─────┘ └─────┘ └─────┘ └─────┘ │ │
│ └─────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────┘§示例
§基本用法
ⓘ
use mofa_foundation::react::{ReActAgent, ReActTool};
use std::sync::Arc;
// 定义工具
struct SearchTool;
#[async_trait::async_trait]
impl ReActTool for SearchTool {
fn name(&self) -> &str { "search" }
fn description(&self) -> &str { "Search the web for information" }
async fn execute(&self, input: &str) -> Result<String, String> {
Ok(format!("Search results for: {}", input))
}
}
// 创建 ReAct Agent
let agent = ReActAgent::builder()
.with_llm(llm_agent)
.with_tool(Arc::new(SearchTool))
.with_max_iterations(5)
.build()?;
// 执行任务
let result = agent.run("What is the capital of France?").await?;
info!("Answer: {}", result.answer);§使用 Actor 模型
ⓘ
use mofa_foundation::react::{ReActActorRef, spawn_react_agent};
// 启动 ReAct Actor
let (actor, handle) = spawn_react_agent(config).await?;
// 发送任务
let result = actor.run_task("Analyze this data").await?;Re-exports§
Modules§
Structs§
- Auto
Agent - AutoAgent - 自动选择最佳策略的智能 Agent
- Auto
Agent Result - AutoAgent 执行结果
- ReAct
Actor - ReAct Actor
- ReAct
Actor Ref - ReAct Actor 引用包装
- ReAct
Actor State - ReAct Actor 内部状态
- ReAct
Actor Status - ReAct Actor 状态
- ReAct
Agent - ReAct Agent 核心实现
- ReAct
Agent Builder - ReAct Agent 构建器
- ReAct
Config - ReAct 配置
- ReAct
Result - ReAct 执行结果
- ReAct
Step - ReAct 执行步骤
Enums§
- Execution
Mode - 执行模式
- ReAct
Message - ReAct Actor 消息类型
- ReAct
Step Type - ReAct 步骤类型
- Task
Complexity - 任务复杂度
Traits§
- ReAct
Tool - ReAct 工具 trait
Functions§
- spawn_
react_ actor - 启动 ReAct Actor