Skip to main content

AgentBuilder

Struct AgentBuilder 

Source
pub struct AgentBuilder<Deps = (), Output = String> { /* private fields */ }
Expand description

Builder for creating agents.

Implementations§

Source§

impl<Deps, Output> AgentBuilder<Deps, Output>
where Deps: Send + Sync + 'static, Output: Send + Sync + 'static,

Source

pub fn new<M>(model: M) -> AgentBuilder<Deps, Output>
where M: Model + 'static,

Create a new agent builder with the given model.

This is the most flexible constructor, accepting any type that implements the Model trait. Use this when you need full control over model configuration.

§Example
use serdes_ai_agent::AgentBuilder;
use serdes_ai_models::openai::OpenAIChatModel;

let model = OpenAIChatModel::new("gpt-4o", "sk-your-api-key");
let agent = AgentBuilder::new(model)
    .system_prompt("You are helpful.")
    .build();
Source

pub fn from_arc(model: Arc<dyn Model>) -> AgentBuilder<Deps, Output>

Create a new agent builder from an Arc<dyn Model>.

This is useful when you already have a model wrapped in an Arc, such as from infer_model().

§Example
use serdes_ai_agent::AgentBuilder;
use serdes_ai_models::infer_model;

let model = infer_model("openai:gpt-4o")?;
let agent = AgentBuilder::from_arc(model)
    .system_prompt("You are helpful.")
    .build();
Source

pub fn from_model( spec: impl Into<String>, ) -> Result<AgentBuilder<Deps, Output>, ModelError>

Create a new agent builder from a model spec string.

This is the simplest way to create an agent when you just need to specify the model. API keys are read from environment variables.

§Model Spec Format

The spec should be in provider:model format:

  • "openai:gpt-4o" - OpenAI GPT-4o
  • "anthropic:claude-3-5-sonnet-20241022" - Anthropic Claude
  • "groq:llama-3.1-70b-versatile" - Groq
  • "ollama:llama3.1" - Local Ollama

If no provider prefix is given, OpenAI is assumed.

§Example
use serdes_ai_agent::AgentBuilder;

let agent = AgentBuilder::from_model("openai:gpt-4o")?
    .system_prompt("You are helpful.")
    .build();
§Errors

Returns an error if the model cannot be created (e.g., missing API key, unsupported provider, or disabled feature).

Source

pub fn from_config( config: ModelConfig, ) -> Result<AgentBuilder<Deps, Output>, ModelError>

Create a new agent builder from a model configuration.

This allows specifying custom API keys, base URLs, and other options while still using the convenient string-based model spec.

§Example
use serdes_ai_agent::{AgentBuilder, ModelConfig};

let config = ModelConfig::new("openai:gpt-4o")
    .with_api_key("sk-your-api-key")
    .with_base_url("https://your-proxy.com/v1");

let agent = AgentBuilder::from_config(config)?
    .system_prompt("You are helpful.")
    .build();
§Errors

Returns an error if the model cannot be created.

Source

pub fn name(self, name: impl Into<String>) -> AgentBuilder<Deps, Output>

Set agent name.

Source

pub fn model_settings( self, settings: ModelSettings, ) -> AgentBuilder<Deps, Output>

Set model settings.

Source

pub fn temperature(self, temp: f64) -> AgentBuilder<Deps, Output>

Set temperature.

Source

pub fn max_tokens(self, tokens: u64) -> AgentBuilder<Deps, Output>

Set max tokens.

Source

pub fn top_p(self, p: f64) -> AgentBuilder<Deps, Output>

Set top-p.

Source

pub fn instructions( self, instructions: impl Into<String>, ) -> AgentBuilder<Deps, Output>

Add static instructions.

Source

pub fn instructions_fn<F, Fut>(self, f: F) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<String>> + Send + 'static,

Add dynamic instructions function (async).

Source

pub fn instructions_fn_sync<F>(self, f: F) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync + 'static,

Add dynamic instructions function (sync).

Source

pub fn system_prompt( self, prompt: impl Into<String>, ) -> AgentBuilder<Deps, Output>

Add system prompt.

Source

pub fn system_prompt_fn<F, Fut>(self, f: F) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<String>> + Send + 'static,

Add dynamic system prompt function (async).

Source

pub fn system_prompt_fn_sync<F>(self, f: F) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync + 'static,

Add dynamic system prompt function (sync).

Source

pub fn tool_with_executor<E>( self, definition: ToolDefinition, executor: E, ) -> AgentBuilder<Deps, Output>
where E: ToolExecutor<Deps> + 'static,

Add a tool with a custom executor.

Source

pub fn tool_fn<F, Args>( self, name: impl Into<String>, description: impl Into<String>, f: F, ) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>, Args) -> Result<ToolReturn, ToolError> + Send + Sync + 'static, Args: DeserializeOwned + Send + 'static,

Add a tool from a sync function.

Source

pub fn tool_fn_async<F, Fut, Args>( self, name: impl Into<String>, description: impl Into<String>, f: F, ) -> AgentBuilder<Deps, Output>
where F: Fn(&RunContext<Deps>, Args) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<ToolReturn, ToolError>> + Send + Sync + 'static, Args: DeserializeOwned + Send + Sync + 'static,

Add a tool from an async function.

Source

pub fn output_schema<S>(self, schema: S) -> AgentBuilder<Deps, Output>
where S: OutputSchema<Output> + 'static,

Set custom output schema.

Source

pub fn output_validator<V>(self, validator: V) -> AgentBuilder<Deps, Output>
where V: OutputValidator<Output, Deps> + 'static,

Add output validator.

Source

pub fn output_validator_fn<F>(self, f: F) -> AgentBuilder<Deps, Output>
where F: Fn(Output, &RunContext<Deps>) -> Result<Output, OutputValidationError> + Send + Sync + 'static,

Add output validator from sync function.

Source

pub fn end_strategy(self, strategy: EndStrategy) -> AgentBuilder<Deps, Output>

Set end strategy.

Source

pub fn max_output_retries(self, retries: u32) -> AgentBuilder<Deps, Output>

Set max output retries.

Source

pub fn max_tool_retries(self, retries: u32) -> AgentBuilder<Deps, Output>

Set max tool retries.

Source

pub fn usage_limits(self, limits: UsageLimits) -> AgentBuilder<Deps, Output>

Set usage limits.

Source

pub fn history_processor<P>(self, processor: P) -> AgentBuilder<Deps, Output>
where P: HistoryProcessor<Deps> + 'static,

Add history processor.

Source

pub fn instrument( self, settings: InstrumentationSettings, ) -> AgentBuilder<Deps, Output>

Enable instrumentation.

Source

pub fn parallel_tool_calls(self, enabled: bool) -> AgentBuilder<Deps, Output>

Enable or disable parallel tool execution.

When enabled (default), multiple tool calls from the model will be executed concurrently using futures::future::join_all.

When disabled, tools are executed sequentially in order.

Source

pub fn max_concurrent_tools(self, max: usize) -> AgentBuilder<Deps, Output>

Set the maximum number of concurrent tool calls.

When set, limits the number of tools that can execute simultaneously using a semaphore. This is useful for rate-limiting or resource control.

Only applies when parallel_tool_calls is enabled.

Source

pub fn build(self) -> Agent<Deps, Output>
where Output: DeserializeOwned,

Build the agent.

Source§

impl<Deps> AgentBuilder<Deps>
where Deps: Send + Sync + 'static,

Source

pub fn output_type<T>(self) -> AgentBuilder<Deps, T>
where T: DeserializeOwned + Send + Sync + 'static,

Change output type to a JSON-parsed type.

Source

pub fn output_type_with_schema<T>(self, schema: Value) -> AgentBuilder<Deps, T>
where T: DeserializeOwned + Send + Sync + 'static,

Change output type with JSON schema.

Source

pub fn output_tool<T>( self, tool_name: impl Into<String>, schema: Value, ) -> AgentBuilder<Deps, T>
where T: DeserializeOwned + Send + Sync + 'static,

Use tool-based output.

Auto Trait Implementations§

§

impl<Deps, Output> Freeze for AgentBuilder<Deps, Output>

§

impl<Deps = (), Output = String> !RefUnwindSafe for AgentBuilder<Deps, Output>

§

impl<Deps, Output> Send for AgentBuilder<Deps, Output>
where Deps: Send, Output: Send,

§

impl<Deps, Output> Sync for AgentBuilder<Deps, Output>
where Deps: Sync, Output: Sync,

§

impl<Deps, Output> Unpin for AgentBuilder<Deps, Output>
where Deps: Unpin, Output: Unpin,

§

impl<Deps = (), Output = String> !UnwindSafe for AgentBuilder<Deps, Output>

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, 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> 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