pub struct Agent { /* private fields */ }
Expand description
Agents are the main interface for building agentic systems.
Construct agents by calling the builder, setting an llm, configure hooks, tools and other customizations.
§Important defaults
- The default context is the
DefaultContext
, executing tools locally with theLocalExecutor
. - A default
stop
tool is provided for agents to explicitly stop if needed - The default
SystemPrompt
instructs the agent with chain of thought and some common safeguards, but is otherwise quite bare. In a lot of cases this can be sufficient.
Implementations§
Source§impl Agent
impl Agent
Sourcepub fn builder() -> AgentBuilder
pub fn builder() -> AgentBuilder
Build a new agent
Sourcepub fn default_tools() -> HashSet<Box<dyn Tool>>
pub fn default_tools() -> HashSet<Box<dyn Tool>>
Default tools for the agent that it always includes
Right now this is the stop
tool, which allows the agent to stop itself.
Sourcepub async fn query(
&mut self,
query: impl Into<Prompt>,
) -> Result<(), AgentError>
pub async fn query( &mut self, query: impl Into<Prompt>, ) -> Result<(), AgentError>
Run the agent with a user message. The agent will loop completions, make tool calls, until no new messages are available.
§Errors
Errors if anything goes wrong, see AgentError
for more details.
Sourcepub async fn query_once(
&mut self,
query: impl Into<Prompt>,
) -> Result<(), AgentError>
pub async fn query_once( &mut self, query: impl Into<Prompt>, ) -> Result<(), AgentError>
Run the agent with a user message once.
§Errors
Errors if anything goes wrong, see AgentError
for more details.
Sourcepub async fn run(&mut self) -> Result<(), AgentError>
pub async fn run(&mut self) -> Result<(), AgentError>
Run the agent with without user message. The agent will loop completions, make tool calls, until no new messages are available.
§Errors
Errors if anything goes wrong, see AgentError
for more details.
Sourcepub async fn run_once(&mut self) -> Result<(), AgentError>
pub async fn run_once(&mut self) -> Result<(), AgentError>
Run the agent with without user message. The agent will loop completions, make tool calls, until
§Errors
Errors if anything goes wrong, see AgentError
for more details.
Sourcepub async fn history(&self) -> Result<Vec<ChatMessage>, AgentError>
pub async fn history(&self) -> Result<Vec<ChatMessage>, AgentError>
Retrieve the message history of the agent
§Errors
Error if the message history cannot be retrieved, e.g. if the context is not set up or a connection fails
Sourcepub async fn add_message(&self, message: ChatMessage) -> Result<(), AgentError>
pub async fn add_message(&self, message: ChatMessage) -> Result<(), AgentError>
Add a message to the agent’s context
This will trigger a OnNewMessage
hook if its present.
If you want to add a message without triggering the hook, use the context directly.
§Errors
Errors if the message cannot be added to the context. With the default in memory context that is not supposed to happen.
Sourcepub async fn stop(&mut self, reason: impl Into<StopReason>)
pub async fn stop(&mut self, reason: impl Into<StopReason>)
Tell the agent to stop. It will finish it’s current loop and then stop.
pub async fn stop_with_error(&mut self, error: &AgentError)
Sourcepub fn context(&self) -> &dyn AgentContext
pub fn context(&self) -> &dyn AgentContext
Access the agent’s context
Sourcepub fn is_running(&self) -> bool
pub fn is_running(&self) -> bool
The agent is still running
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
The agent stopped
Sourcepub fn is_pending(&self) -> bool
pub fn is_pending(&self) -> bool
The agent has not (ever) started
pub fn state(&self) -> &State
pub fn stop_reason(&self) -> Option<&StopReason>
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Agent
impl !RefUnwindSafe for Agent
impl Send for Agent
impl Sync for Agent
impl Unpin for Agent
impl !UnwindSafe for Agent
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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