类 ReActAgent
java.lang.Object
io.agentscope.core.agent.AgentBase
io.agentscope.core.agent.StructuredOutputCapableAgent
io.agentscope.core.ReActAgent
- 所有已实现的接口:
Agent,CallableAgent,ObservableAgent,StreamableAgent,StateModule
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();
- 另请参阅:
-
嵌套类概要
嵌套类 -
字段概要
从类继承的字段 io.agentscope.core.agent.StructuredOutputCapableAgent
STRUCTURED_OUTPUT_TOOL_NAME, structuredOutputReminder, toolkit -
方法概要
修饰符和类型方法说明static ReActAgent.Builderbuilder()protected GenerateOptionsBuild generate options for model calls.protected reactor.core.publisher.Mono<Msg> Internal implementation for processing multiple input messages.protected reactor.core.publisher.Mono<Void> Observe a message without generating a reply.Gets the configured generation options for this agent.intGet the memory for structured output hook.getModel()protected reactor.core.publisher.Mono<Msg> handleInterrupt(InterruptContext context, Msg... originalArgs) Handle an interruption that occurred during execution.voidloadFrom(Session session, SessionKey sessionKey) Load state from the session.booleanloadIfExists(Session session, SessionKey sessionKey) Load agent state from the session using the new API.voidsaveTo(Session session, SessionKey sessionKey) Save agent state to the session using the new API.voidprotected reactor.core.publisher.Mono<Msg> Generate summary when max iterations reached.从类继承的方法 io.agentscope.core.agent.StructuredOutputCapableAgent
doCall, doCall, getToolkit从类继承的方法 io.agentscope.core.agent.AgentBase
addHook, addSystemHook, call, call, call, checkInterruptedAsync, getAgentId, getDescription, getHooks, getInterruptFlag, getInterruptSource, getName, getSortedHooks, getSubscriberCount, hasSubscribers, interrupt, interrupt, interrupt, observe, observe, removeHook, removeSubscribers, removeSystemHook, resetInterruptFlag, resetSubscribers, stream, stream, stream, toString从类继承的方法 java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait从接口继承的方法 io.agentscope.core.state.StateModule
loadFrom, loadIfExists, saveTo
-
方法详细资料
-
saveTo
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 tosessionKey- the session identifier
-
loadIfExists
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 fromsessionKey- the session identifier- 返回:
- true if the session existed and state was loaded, false otherwise
-
loadFrom
从接口复制的说明:StateModuleLoad 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 fromsessionKey- the session identifier
-
doCall
从类复制的说明:AgentBaseInternal implementation for processing multiple input messages. Subclasses must implement their specific logic here. -
summarizing
Generate summary when max iterations reached. -
buildGenerateOptions
从类复制的说明:StructuredOutputCapableAgentBuild generate options for model calls. Subclasses must implement this. -
handleInterrupt
protected reactor.core.publisher.Mono<Msg> handleInterrupt(InterruptContext context, Msg... originalArgs) 从类复制的说明:AgentBaseHandle 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 interruptionoriginalArgs- The original arguments passed to the call() method (empty, single Msg, or List)- 返回:
- Recovery message to return to the user
-
doObserve
从类复制的说明:AgentBaseObserve 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
-
getMemory
从类复制的说明:StructuredOutputCapableAgentGet the memory for structured output hook. Subclasses must implement this.- 指定者:
getMemory在类中StructuredOutputCapableAgent
-
setMemory
-
getSysPrompt
-
getModel
-
getMaxIters
public int getMaxIters() -
getPlanNotebook
-
getGenerateOptions
Gets the configured generation options for this agent.- 返回:
- The generation options, or null if not configured
-
builder
-