Skip to main content

Module react

Module react 

Source
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§

pub use patterns::*;
pub use tools::*;

Modules§

patterns
Agent 执行模式
prelude
便捷 prelude 模块
tools
ReAct 内置工具实现

Structs§

AutoAgent
AutoAgent - 自动选择最佳策略的智能 Agent
AutoAgentResult
AutoAgent 执行结果
ReActActor
ReAct Actor
ReActActorRef
ReAct Actor 引用包装
ReActActorState
ReAct Actor 内部状态
ReActActorStatus
ReAct Actor 状态
ReActAgent
ReAct Agent 核心实现
ReActAgentBuilder
ReAct Agent 构建器
ReActConfig
ReAct 配置
ReActResult
ReAct 执行结果
ReActStep
ReAct 执行步骤

Enums§

ExecutionMode
执行模式
ReActMessage
ReAct Actor 消息类型
ReActStepType
ReAct 步骤类型
TaskComplexity
任务复杂度

Traits§

ReActTool
ReAct 工具 trait

Functions§

spawn_react_actor
启动 ReAct Actor