pub struct AgentExecutor<A>where
A: Agent,{
pub memory: Option<Arc<Mutex<dyn BaseMemory>>>,
/* private fields */
}Fields§
§memory: Option<Arc<Mutex<dyn BaseMemory>>>Implementations§
Source§impl<A> AgentExecutor<A>where
A: Agent,
impl<A> AgentExecutor<A>where
A: Agent,
pub fn from_agent(agent: A) -> Self
pub fn with_max_iterations(self, max_iterations: i32) -> Self
pub fn with_memory(self, memory: Arc<Mutex<dyn BaseMemory>>) -> Self
pub fn with_break_if_error(self, break_if_error: bool) -> Self
Sourcepub fn with_cancellation(self, token: CancellationToken) -> Self
pub fn with_cancellation(self, token: CancellationToken) -> Self
Stops runs when token is cancelled: the in-flight LLM or tool call is dropped and
the run returns a ChainError::AgentError (“agent run cancelled”); check
token.is_cancelled() to tell it apart from other failures. Nothing is written to
memory for a cancelled run.
Applies to every run of this executor. Dropping the future (or the stream from
stream_events) also cancels a run.
Source§impl<A> AgentExecutor<A>
impl<A> AgentExecutor<A>
Sourcepub fn stream_events(&self, input_variables: PromptArgs) -> AgentEventStream<'_>
pub fn stream_events(&self, input_variables: PromptArgs) -> AgentEventStream<'_>
Runs the agent and streams what it does: model text as it is generated, each tool
call and its result, the tokens of each model call (Usage, when the LLM reports
them), and finally the answer.
Memory is updated the same way as Chain::call, once the run finishes. Stop early
by dropping the stream or with with_cancellation.
use futures::StreamExt;
use langchain_rust::agent::AgentStreamEvent;
let mut events = executor.stream_events(prompt_args! { "input" => "weather in Lima?" });
while let Some(event) = events.next().await {
match event? {
AgentStreamEvent::Text(chunk) => print!("{chunk}"),
AgentStreamEvent::ToolStart { tool, input } => println!("\n-> {tool}({input})"),
AgentStreamEvent::ToolEnd { tool, output } => println!("<- {tool}: {output}"),
AgentStreamEvent::Finish { .. } => println!(),
_ => {}
}
}Trait Implementations§
Source§impl<A> Chain for AgentExecutor<A>
impl<A> Chain for AgentExecutor<A>
Source§fn call<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<GenerateResult, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn call<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<GenerateResult, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Runs the agent. tokens is the sum over every model call of the run (None when
the LLM doesn’t report usage).
Source§fn invoke<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<String, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn invoke<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<String, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Chain and receive just the generation result as a String.
The input is a set of variables passed as a PromptArgs hashmap. Read moreSource§fn execute<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<HashMap<String, Value>, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Chain and return the result of the generation process
along with additional information like token consumption formatted as a HashMap.
The input is a set of variables passed as a PromptArgs hashmap.
The key for the generated output is specified by the get_output_keys
method (default key is output). Read moreSource§fn stream<'life0, 'async_trait>(
&'life0 self,
_input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamData, ChainError>> + Send>>, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream<'life0, 'async_trait>(
&'life0 self,
_input_variables: PromptArgs,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamData, ChainError>> + Send>>, ChainError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Chain and get an asynchronous stream of chain generations.
The input is a set of variables passed as a PromptArgs hashmap.
If the chain have memroy, the tream method will not be able to automaticaly
set the memroy, bocause it will not know if the how to extract the output message
out of the stram Read moreSource§fn call_stream<'a>(
&'a self,
input_variables: PromptArgs,
) -> ChainEventStream<'a>
fn call_stream<'a>( &'a self, input_variables: PromptArgs, ) -> ChainEventStream<'a>
fn get_input_keys(&self) -> Vec<String>
fn get_output_keys(&self) -> Vec<String>
Auto Trait Implementations§
impl<A> !RefUnwindSafe for AgentExecutor<A>
impl<A> !UnwindSafe for AgentExecutor<A>
impl<A> Freeze for AgentExecutor<A>where
A: Freeze,
impl<A> Send for AgentExecutor<A>
impl<A> Sync for AgentExecutor<A>
impl<A> Unpin for AgentExecutor<A>where
A: Unpin,
impl<A> UnsafeUnpin for AgentExecutor<A>where
A: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more