pub struct ReActAgent { /* private fields */ }Expand description
ReAct Agent
An agent that uses the ReAct (Reasoning + Acting) pattern:
it first thinks, then decides which tool to execute, and finally observes the
result. Supports any LLM provider that implements BaseChatModel.
Implementations§
Source§impl ReActAgent
impl ReActAgent
Sourcepub fn new<L>(
llm: L,
tools: Vec<Arc<dyn BaseTool>>,
system_prompt: Option<String>,
) -> Self
pub fn new<L>( llm: L, tools: Vec<Arc<dyn BaseTool>>, system_prompt: Option<String>, ) -> Self
Creates a new ReAct Agent
§Parameters
llm- LLM client (any type implementingBaseChatModel)tools- available toolssystem_prompt- custom system prompt (optional)
§Backward compatibility
Legacy code ReActAgent::new(openai_chat, tools, None) still works,
because OpenAIChat: BaseChatModel and OpenAIError: Into<Error>.
Trait Implementations§
Source§impl BaseAgent for ReActAgent
impl BaseAgent for ReActAgent
Source§fn plan<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
config: Option<&'life3 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
fn plan<'life0, 'life1, 'life2, 'life3, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
config: Option<&'life3 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Source§fn plan_stream<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
on_token: &'life3 mut (dyn FnMut(String) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send),
config: Option<&'life4 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
fn plan_stream<'life0, 'life1, 'life2, 'life3, 'life4, 'async_trait>(
&'life0 self,
intermediate_steps: &'life1 [AgentStep],
inputs: &'life2 HashMap<String, String>,
on_token: &'life3 mut (dyn FnMut(String) -> Pin<Box<dyn Future<Output = ()> + Send>> + Send),
config: Option<&'life4 RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<AgentOutput, AgentError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
'life4: 'async_trait,
Streaming plan (F3): forwards model output token by token, accumulating the full text before parsing.
plan() goes through non-streaming chat (with retry, records token
usage); this goes through stream_chat, forwarding each chunk via
on_token as a live Text event while accumulating the full text for
Action / Final Answer parsing.
Trade-off: stream_chat chunks carry optional token_usage; after the
stream ends it is written to last_token_usage for the budget gate to
read. When the provider does not report usage (chunk.token_usage is
None), the streaming path’s metrics usage is filled in by the
non-streaming invoke path. If stream_chat fails immediately (e.g. the
provider does not implement streaming), it falls back to non-streaming
plan() so the agent loop is not interrupted.
Source§fn last_token_usage(&self) -> Option<TokenUsage>
fn last_token_usage(&self) -> Option<TokenUsage>
Reports the token usage from the most recent plan() call (P1-5).