Skip to main content

AgentBuilder

Struct AgentBuilder 

Source
pub struct AgentBuilder<M, P = (), ToolState = NoToolConfig>
where M: CompletionModel, P: PromptHook<M>,
{ /* private fields */ }
Expand description

A builder for creating an agent

The builder uses a typestate pattern to enforce that tool configuration is done in a mutually exclusive way: either provide a pre-existing ToolServerHandle, or add tools via the builder API, but not both.

§Example

use rig::{providers::openai, agent::AgentBuilder};

let openai = openai::Client::from_env();

let gpt4o = openai.completion_model("gpt-4o");

// Configure the agent
let agent = AgentBuilder::new(gpt4o)
    .preamble("System prompt")
    .context("Context document 1")
    .context("Context document 2")
    .tool(tool1)
    .tool(tool2)
    .temperature(0.8)
    .additional_params(json!({"foo": "bar"}))
    .build();

Implementations§

Source§

impl<M, P, ToolState> AgentBuilder<M, P, ToolState>
where M: CompletionModel, P: PromptHook<M>,

Source

pub fn name(self, name: &str) -> Self

Set the name of the agent

Source

pub fn description(self, description: &str) -> Self

Set the description of the agent

Source

pub fn preamble(self, preamble: &str) -> Self

Set the system prompt

Source

pub fn without_preamble(self) -> Self

Remove the system prompt

Source

pub fn append_preamble(self, doc: &str) -> Self

Append to the preamble of the agent

Source

pub fn context(self, doc: &str) -> Self

Add a static context document to the agent

Source

pub fn dynamic_context( self, sample: usize, dynamic_context: impl VectorStoreIndexDyn + Send + Sync + 'static, ) -> Self

Add some dynamic context to the agent. On each prompt, sample documents from the dynamic context will be inserted in the request.

Source

pub fn tool_choice(self, tool_choice: ToolChoice) -> Self

Set the tool choice for the agent

Source

pub fn default_max_turns(self, default_max_turns: usize) -> Self

Set the default maximum depth that an agent will use for multi-turn.

Source

pub fn temperature(self, temperature: f64) -> Self

Set the temperature of the model

Source

pub fn max_tokens(self, max_tokens: u64) -> Self

Set the maximum number of tokens for the completion

Source

pub fn additional_params(self, params: Value) -> Self

Set additional parameters to be passed to the model

Source

pub fn output_schema<T>(self) -> Self
where T: JsonSchema,

Set the output schema for structured output. When set, providers that support native structured outputs will constrain the model’s response to match this schema.

Source

pub fn output_schema_raw(self, schema: Schema) -> Self

Set the output schema for structured output. In comparison to AgentBuilder::schema() which requires type annotation, you can put in any schema you’d like here.

Source§

impl<M> AgentBuilder<M, (), NoToolConfig>
where M: CompletionModel,

Source

pub fn new(model: M) -> Self

Create a new agent builder with the given model

Source§

impl<M, P> AgentBuilder<M, P, NoToolConfig>
where M: CompletionModel, P: PromptHook<M>,

Source

pub fn tool_server_handle( self, handle: ToolServerHandle, ) -> AgentBuilder<M, P, WithToolServerHandle>

Set a pre-existing ToolServerHandle for the agent.

After calling this method, tool-adding methods (.tool(), .tools(), etc.) will not be available. Use this when you want to share a ToolServer between multiple agents or have pre-configured tools.

Source

pub fn tool( self, tool: impl Tool + 'static, ) -> AgentBuilder<M, P, WithBuilderTools>

Add a static tool to the agent.

This transitions the builder to the WithBuilderTools state, where additional tools can be added but tool_server_handle() is no longer available.

Source

pub fn tools( self, tools: Vec<Box<dyn ToolDyn>>, ) -> AgentBuilder<M, P, WithBuilderTools>

Add a vector of boxed static tools to the agent.

This is useful when you need to dynamically add static tools to the agent. Transitions the builder to the WithBuilderTools state.

Source

pub fn rmcp_tool( self, tool: Tool, client: ServerSink, ) -> AgentBuilder<M, P, WithBuilderTools>

Available on crate feature rmcp only.

Add an MCP tool (from rmcp) to the agent.

Transitions the builder to the WithBuilderTools state.

Source

pub fn rmcp_tools( self, tools: Vec<Tool>, client: ServerSink, ) -> AgentBuilder<M, P, WithBuilderTools>

Available on crate feature rmcp only.

Add an array of MCP tools (from rmcp) to the agent.

Transitions the builder to the WithBuilderTools state.

Source

pub fn dynamic_tools( self, sample: usize, dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static, toolset: ToolSet, ) -> AgentBuilder<M, P, WithBuilderTools>

Add some dynamic tools to the agent. On each prompt, sample tools from the dynamic toolset will be inserted in the request.

Transitions the builder to the WithBuilderTools state.

Source

pub fn hook<P2>(self, hook: P2) -> AgentBuilder<M, P2, NoToolConfig>
where P2: PromptHook<M>,

Set the default hook for the agent.

This hook will be used for all prompt requests unless overridden via .with_hook() on the request.

Source

pub fn build(self) -> Agent<M, P>

Build the agent with no tools configured.

An empty ToolServer will be created for the agent.

Source§

impl<M, P> AgentBuilder<M, P, WithToolServerHandle>
where M: CompletionModel, P: PromptHook<M>,

Source

pub fn build(self) -> Agent<M, P>

Build the agent using the pre-configured ToolServerHandle.

Source§

impl<M, P> AgentBuilder<M, P, WithBuilderTools>
where M: CompletionModel, P: PromptHook<M>,

Source

pub fn tool(self, tool: impl Tool + 'static) -> Self

Add another static tool to the agent.

Source

pub fn tools(self, tools: Vec<Box<dyn ToolDyn>>) -> Self

Add a vector of boxed static tools to the agent.

Source

pub fn rmcp_tools(self, tools: Vec<Tool>, client: ServerSink) -> Self

Available on crate feature rmcp only.

Add an array of MCP tools (from rmcp) to the agent.

Source

pub fn dynamic_tools( self, sample: usize, dynamic_tools: impl VectorStoreIndexDyn + Send + Sync + 'static, toolset: ToolSet, ) -> Self

Add some dynamic tools to the agent. On each prompt, sample tools from the dynamic toolset will be inserted in the request.

Source

pub fn build(self) -> Agent<M, P>

Build the agent with the configured tools.

A new ToolServer will be created containing all tools added via .tool(), .tools(), .dynamic_tools(), etc.

Auto Trait Implementations§

§

impl<M, P, ToolState> Freeze for AgentBuilder<M, P, ToolState>
where M: Freeze, ToolState: Freeze, P: Freeze,

§

impl<M, P = (), ToolState = NoToolConfig> !RefUnwindSafe for AgentBuilder<M, P, ToolState>

§

impl<M, P, ToolState> Send for AgentBuilder<M, P, ToolState>
where ToolState: Send,

§

impl<M, P, ToolState> Sync for AgentBuilder<M, P, ToolState>
where ToolState: Sync,

§

impl<M, P, ToolState> Unpin for AgentBuilder<M, P, ToolState>
where M: Unpin, ToolState: Unpin, P: Unpin,

§

impl<M, P, ToolState> UnsafeUnpin for AgentBuilder<M, P, ToolState>
where M: UnsafeUnpin, ToolState: UnsafeUnpin, P: UnsafeUnpin,

§

impl<M, P = (), ToolState = NoToolConfig> !UnwindSafe for AgentBuilder<M, P, ToolState>

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> 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: 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: 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 = 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> 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> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,