pub struct AgentBuilder { /* private fields */ }
Expand description
Builder for Agent
.
Implementations§
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub fn hooks<VALUE: Into<Vec<Hook>>>(&mut self, value: VALUE) -> &mut Self
pub fn hooks<VALUE: Into<Vec<Hook>>>(&mut self, value: VALUE) -> &mut Self
Hooks are functions that are called at specific points in the agent’s lifecycle.
Sourcepub fn toolboxes(&mut self, value: Vec<Box<dyn ToolBox>>) -> &mut Self
pub fn toolboxes(&mut self, value: Vec<Box<dyn ToolBox>>) -> &mut Self
Toolboxes are collections of tools that can be added to the agent.
Toolboxes make their tools available to the agent at runtime.
Sourcepub fn system_prompt<VALUE: Into<Prompt>>(&mut self, value: VALUE) -> &mut Self
pub fn system_prompt<VALUE: Into<Prompt>>(&mut self, value: VALUE) -> &mut Self
System prompt for the agent when it starts
Some agents profit significantly from a tailored prompt. But it is not always needed.
See SystemPrompt
for an opiniated, customizable system prompt.
Swiftide provides a default system prompt for all agents.
§Example
Agent::builder()
.system_prompt(
SystemPrompt::builder().role("You are an expert engineer")
.build().unwrap())
.build().unwrap();
Sourcepub fn limit(&mut self, value: usize) -> &mut Self
pub fn limit(&mut self, value: usize) -> &mut Self
Optional limit on the amount of loops the agent can run. The counter is reset when the agent is stopped.
Sourcepub fn tool_retry_limit(&mut self, value: usize) -> &mut Self
pub fn tool_retry_limit(&mut self, value: usize) -> &mut Self
The maximum amount of times the failed output of a tool will be send to an LLM before the agent stops. Defaults to 3.
LLMs sometimes send missing arguments, or a tool might actually fail, but retrying could be worth while. If the limit is not reached, the agent will send the formatted error back to the LLM.
The limit is hashed based on the tool call name and arguments, so the limit is per tool call.
This limit is not reset when the agent is stopped.
Source§impl AgentBuilder
impl AgentBuilder
Sourcepub fn context(
&mut self,
context: impl AgentContext + 'static,
) -> &mut AgentBuilderwhere
Self: Clone,
pub fn context(
&mut self,
context: impl AgentContext + 'static,
) -> &mut AgentBuilderwhere
Self: Clone,
The context in which the agent operates, by default this is the DefaultContext
.
Sourcepub fn no_system_prompt(&mut self) -> &mut Self
pub fn no_system_prompt(&mut self) -> &mut Self
Disable the system prompt.
Sourcepub fn before_all(&mut self, hook: impl BeforeAllFn + 'static) -> &mut Self
pub fn before_all(&mut self, hook: impl BeforeAllFn + 'static) -> &mut Self
Add a hook that runs once, before all completions. Even if the agent is paused and resumed, before all will not trigger again.
Sourcepub fn on_start(&mut self, hook: impl OnStartFn + 'static) -> &mut Self
pub fn on_start(&mut self, hook: impl OnStartFn + 'static) -> &mut Self
Add a hook that runs once, when the agent starts. This hook also runs if the agent stopped
and then starts again. The hook runs after any before_all
hooks and before the
before_completion
hooks.
Sourcepub fn on_stream(&mut self, hook: impl OnStreamFn + 'static) -> &mut Self
pub fn on_stream(&mut self, hook: impl OnStreamFn + 'static) -> &mut Self
Add a hook that runs when the agent receives a streaming response
The response will always include both the current accumulated message and the delta
This will set self.streaming
to true, there is no need to set it manually for the default
behaviour.
Sourcepub fn before_completion(
&mut self,
hook: impl BeforeCompletionFn + 'static,
) -> &mut Self
pub fn before_completion( &mut self, hook: impl BeforeCompletionFn + 'static, ) -> &mut Self
Add a hook that runs before each completion.
Sourcepub fn after_tool(&mut self, hook: impl AfterToolFn + 'static) -> &mut Self
pub fn after_tool(&mut self, hook: impl AfterToolFn + 'static) -> &mut Self
Add a hook that runs after each tool. The Result<ToolOutput, ToolError>
is provided
as mut, so the tool output can be fully modified.
The ToolOutput
also references the original ToolCall
, allowing you to match at runtime
what tool to interact with.
Sourcepub fn before_tool(&mut self, hook: impl BeforeToolFn + 'static) -> &mut Self
pub fn before_tool(&mut self, hook: impl BeforeToolFn + 'static) -> &mut Self
Add a hook that runs before each tool. Yields an immutable reference to the ToolCall
.
Sourcepub fn after_completion(
&mut self,
hook: impl AfterCompletionFn + 'static,
) -> &mut Self
pub fn after_completion( &mut self, hook: impl AfterCompletionFn + 'static, ) -> &mut Self
Add a hook that runs after each completion, before tool invocation and/or new messages.
Sourcepub fn after_each(&mut self, hook: impl AfterEachFn + 'static) -> &mut Self
pub fn after_each(&mut self, hook: impl AfterEachFn + 'static) -> &mut Self
Add a hook that runs after each completion, after tool invocations, right before a new loop might start
Sourcepub fn on_new_message(
&mut self,
hook: impl MessageHookFn + 'static,
) -> &mut Self
pub fn on_new_message( &mut self, hook: impl MessageHookFn + 'static, ) -> &mut Self
Add a hook that runs when a new message is added to the context. Note that each tool adds a separate message.
pub fn on_stop(&mut self, hook: impl OnStopFn + 'static) -> &mut Self
Sourcepub fn llm<LLM: ChatCompletion + Clone + 'static>(
&mut self,
llm: &LLM,
) -> &mut Self
pub fn llm<LLM: ChatCompletion + Clone + 'static>( &mut self, llm: &LLM, ) -> &mut Self
Set the LLM for the agent. An LLM must implement the ChatCompletion
trait.
Sourcepub fn tools<TOOL, I: IntoIterator<Item = TOOL>>(
&mut self,
tools: I,
) -> &mut Self
pub fn tools<TOOL, I: IntoIterator<Item = TOOL>>( &mut self, tools: I, ) -> &mut Self
Define the available tools for the agent. Tools must implement the Tool
trait.
See the tool attribute macro and the tool derive macro for easy ways to create (many) tools.
Sourcepub fn add_toolbox(&mut self, toolbox: impl ToolBox + 'static) -> &mut Self
pub fn add_toolbox(&mut self, toolbox: impl ToolBox + 'static) -> &mut Self
Add a toolbox to the agent. Toolboxes are collections of tools that can be added to the to the agent. Available tools are evaluated at runtime, when the agent starts for the first time.
Agents can have many toolboxes.
Trait Implementations§
Source§impl Clone for AgentBuilder
impl Clone for AgentBuilder
Source§fn clone(&self) -> AgentBuilder
fn clone(&self) -> AgentBuilder
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for AgentBuilder
impl !RefUnwindSafe for AgentBuilder
impl Send for AgentBuilder
impl Sync for AgentBuilder
impl Unpin for AgentBuilder
impl !UnwindSafe for AgentBuilder
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