Struct AgentBuilder

Source
pub struct AgentBuilder { /* private fields */ }
Expand description

Builder for Agent.

Implementations§

Source§

impl AgentBuilder

Source

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.

Source

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.

Source

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();
Source

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.

Source

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

pub fn streaming(&mut self, value: bool) -> &mut Self

Enables streaming the chat completion responses for the agent.

Source

pub fn build(&self) -> Result<Agent, AgentBuilderError>

Builds a new Agent.

§Errors

If a required field has not been initialized.

Source§

impl AgentBuilder

Source

pub fn context( &mut self, context: impl AgentContext + 'static, ) -> &mut AgentBuilder
where Self: Clone,

The context in which the agent operates, by default this is the DefaultContext.

Source

pub fn no_system_prompt(&mut self) -> &mut Self

Disable the system prompt.

Source

pub fn add_hook(&mut self, hook: Hook) -> &mut Self

Add a hook to the agent.

Source

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.

Source

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.

Source

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.

Source

pub fn before_completion( &mut self, hook: impl BeforeCompletionFn + 'static, ) -> &mut Self

Add a hook that runs before each completion.

Source

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.

Source

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.

Source

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.

Source

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

Source

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.

Source

pub fn on_stop(&mut self, hook: impl OnStopFn + 'static) -> &mut Self

Source

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.

Source

pub fn tools<TOOL, I: IntoIterator<Item = TOOL>>( &mut self, tools: I, ) -> &mut Self
where TOOL: Into<Box<dyn Tool>>,

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.

Source

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

Source§

fn clone(&self) -> AgentBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for AgentBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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
Source§

impl<T> ErasedDestructor for T
where T: 'static,