类 ReActAgent

所有已实现的接口:
Agent, CallableAgent, ObservableAgent, StreamableAgent, StateModule

public class ReActAgent extends StructuredOutputCapableAgent
ReAct (Reasoning and Acting) Agent implementation.

ReAct is an agent design pattern that combines reasoning (thinking and planning) with acting (tool execution) in an iterative loop. The agent alternates between these two phases until it either completes the task or reaches the maximum iteration limit.

Key Features:

  • Reactive Streaming: Uses Project Reactor for non-blocking execution
  • Hook System: Extensible hooks for monitoring and intercepting agent execution
  • HITL Support: Human-in-the-loop via stopAgent() in PostReasoningEvent/PostActingEvent
  • Structured Output: StructuredOutputCapableAgent provides type-safe output generation

Usage Example:


 // Create a model
 DashScopeChatModel model = DashScopeChatModel.builder()
     .apiKey(System.getenv("DASHSCOPE_API_KEY"))
     .modelName("qwen-plus")
     .build();

 // Create a toolkit with tools
 Toolkit toolkit = new Toolkit();
 toolkit.registerObject(new MyToolClass());

 // Build the agent
 ReActAgent agent = ReActAgent.builder()
     .name("Assistant")
     .sysPrompt("You are a helpful assistant.")
     .model(model)
     .toolkit(toolkit)
     .memory(new InMemoryMemory())
     .maxIters(10)
     .build();

 // Use the agent
 Msg response = agent.call(Msg.builder()
     .name("user")
     .role(MsgRole.USER)
     .content(TextBlock.builder().text("What's the weather?").build())
     .build()).block();
 
另请参阅:
  • 方法详细资料

    • saveTo

      public void saveTo(Session session, SessionKey sessionKey)
      Save agent state to the session using the new API.

      This method saves the state of all managed components according to the StatePersistence configuration:

      • Agent metadata (always saved)
      • Memory messages (if memoryManaged is true)
      • Toolkit activeGroups (if toolkitManaged is true)
      • PlanNotebook state (if planNotebookManaged is true)
      参数:
      session - the session to save state to
      sessionKey - the session identifier
    • loadIfExists

      public boolean loadIfExists(Session session, SessionKey sessionKey)
      Load agent state from the session using the new API.

      This method loads the state of all managed components according to the StatePersistence configuration.

      参数:
      session - the session to load state from
      sessionKey - the session identifier
      返回:
      true if the session existed and state was loaded, false otherwise
    • loadFrom

      public void loadFrom(Session session, SessionKey sessionKey)
      从接口复制的说明: StateModule
      Load state from the session.

      Components should implement this method to restore their state using the Session's get methods.

      参数:
      session - the session to load state from
      sessionKey - the session identifier
    • doCall

      protected reactor.core.publisher.Mono<Msg> doCall(List<Msg> msgs)
      从类复制的说明: AgentBase
      Internal implementation for processing multiple input messages. Subclasses must implement their specific logic here.
      指定者:
      doCall 在类中 AgentBase
      参数:
      msgs - Input messages
      返回:
      Response message
    • summarizing

      protected reactor.core.publisher.Mono<Msg> summarizing()
      Generate summary when max iterations reached.
    • buildGenerateOptions

      protected GenerateOptions buildGenerateOptions()
      从类复制的说明: StructuredOutputCapableAgent
      Build generate options for model calls. Subclasses must implement this.
      指定者:
      buildGenerateOptions 在类中 StructuredOutputCapableAgent
    • handleInterrupt

      protected reactor.core.publisher.Mono<Msg> handleInterrupt(InterruptContext context, Msg... originalArgs)
      从类复制的说明: AgentBase
      Handle an interruption that occurred during execution. Subclasses must implement this to provide recovery logic based on the interrupt context.

      Implementation guidance:

      • Simple agents: Return a basic interrupt acknowledgment message
      • Complex agents: Generate a summary including any pending operations or partial results
      • Stateful agents: Ensure state is saved appropriately before returning
      指定者:
      handleInterrupt 在类中 AgentBase
      参数:
      context - The interrupt context containing metadata about the interruption
      originalArgs - The original arguments passed to the call() method (empty, single Msg, or List)
      返回:
      Recovery message to return to the user
    • doObserve

      protected reactor.core.publisher.Mono<Void> doObserve(Msg msg)
      从类复制的说明: AgentBase
      Observe a message without generating a reply. This allows agents to receive messages from other agents or the environment without responding. It's commonly used in multi-agent collaboration scenarios.

      Common implementation patterns:

      • Stateless agents: Empty implementation if observation is not needed
      • Stateful agents: Store message in memory/context for use in future calls
      • Collaborative agents: Update shared knowledge or trigger side effects
      覆盖:
      doObserve 在类中 AgentBase
      参数:
      msg - The message to observe
      返回:
      Mono that completes when observation is done
    • getMemory

      public Memory getMemory()
      从类复制的说明: StructuredOutputCapableAgent
      Get the memory for structured output hook. Subclasses must implement this.
      指定者:
      getMemory 在类中 StructuredOutputCapableAgent
    • setMemory

      public void setMemory(Memory memory)
    • getSysPrompt

      public String getSysPrompt()
    • getModel

      public Model getModel()
    • getMaxIters

      public int getMaxIters()
    • getPlanNotebook

      public PlanNotebook getPlanNotebook()
    • getGenerateOptions

      public GenerateOptions getGenerateOptions()
      Gets the configured generation options for this agent.
      返回:
      The generation options, or null if not configured
    • builder

      public static ReActAgent.Builder builder()