Skip to main content

ReActAgent

Struct ReActAgent 

Source
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

Source

pub fn new<L>( llm: L, tools: Vec<Arc<dyn BaseTool>>, system_prompt: Option<String>, ) -> Self
where L: BaseChatModel + Send + Sync + 'static, L::Error: Into<ProviderError>,

Creates a new ReAct Agent

§Parameters
  • llm - LLM client (any type implementing BaseChatModel)
  • tools - available tools
  • system_prompt - custom system prompt (optional)
§Backward compatibility

Legacy code ReActAgent::new(openai_chat, tools, None) still works, because OpenAIChat: BaseChatModel and OpenAIError: Into<Error>.

Source

pub fn from_arc( llm: Arc<dyn BaseChatModel<Error = ProviderError> + Send + Sync>, tools: Vec<Arc<dyn BaseTool>>, system_prompt: Option<String>, ) -> Self

Creates an agent from an already-wrapped Arc<dyn BaseChatModel>

Trait Implementations§

Source§

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,

Plans the next action

§Parameters
  • intermediate_steps - history of executed steps
  • inputs - user input
§Returns
  • AgentOutput::Action - the action to execute
  • AgentOutput::Finish - the final answer
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,

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 get_allowed_tools(&self) -> Option<Vec<&str>>

Returns the allowed tools list

Source§

fn last_token_usage(&self) -> Option<TokenUsage>

Reports the token usage from the most recent plan() call (P1-5).

Source§

fn input_keys(&self) -> Vec<&str>

Returns input keys.
Source§

fn return_stopped_response( &self, _intermediate_steps: &[AgentStep], ) -> AgentFinish

Returns stopped response when max iterations reached.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more