AiClientBuilder

Struct AiClientBuilder 

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

AI client builder with progressive custom configuration

Usage examples:

use ai_lib::{AiClientBuilder, Provider};

// Simplest usage - automatic environment variable detection
let client = AiClientBuilder::new(Provider::Groq).build()?;

// Custom base_url and proxy
let client = AiClientBuilder::new(Provider::Groq)
    .with_base_url("https://custom.groq.com")
    .with_proxy(Some("http://proxy.example.com:8080"))
    .build()?;

// Full custom configuration
let client = AiClientBuilder::new(Provider::Groq)
    .with_base_url("https://custom.groq.com")
    .with_proxy(Some("http://proxy.example.com:8080"))
    .with_timeout(std::time::Duration::from_secs(60))
    .with_pool_config(32, std::time::Duration::from_secs(90))
    .build()?;

Implementations§

Source§

impl AiClientBuilder

Source

pub fn new(provider: Provider) -> Self

Create a new builder instance

§Arguments
  • provider - The AI model provider to use
§Returns
  • Self - Builder instance
Source

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

Set custom base URL

§Arguments
  • base_url - Custom base URL
§Returns
  • Self - Builder instance for method chaining
Source

pub fn with_proxy(self, proxy_url: Option<&str>) -> Self

Set custom proxy URL

§Arguments
  • proxy_url - Custom proxy URL, or None to use AI_PROXY_URL environment variable
§Returns
  • Self - Builder instance for method chaining
§Examples
use ai_lib::{AiClientBuilder, Provider};

// Use specific proxy URL
let client = AiClientBuilder::new(Provider::Groq)
    .with_proxy(Some("http://proxy.example.com:8080"))
    .build()?;

// Use AI_PROXY_URL environment variable
let client = AiClientBuilder::new(Provider::Groq)
    .with_proxy(None)
    .build()?;
Source

pub fn without_proxy(self) -> Self

Explicitly disable proxy usage

This method ensures that no proxy will be used, regardless of environment variables.

§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .build()?;
Source

pub fn with_timeout(self, timeout: Duration) -> Self

Set custom timeout duration

§Arguments
  • timeout - Custom timeout duration
§Returns
  • Self - Builder instance for method chaining
Source

pub fn with_pool_config(self, max_idle: usize, idle_timeout: Duration) -> Self

Set connection pool configuration

§Arguments
  • max_idle - Maximum idle connections per host
  • idle_timeout - Idle connection timeout duration
§Returns
  • Self - Builder instance for method chaining
Source

pub fn with_metrics(self, metrics: Arc<dyn Metrics>) -> Self

Set custom metrics implementation

§Arguments
  • metrics - Custom metrics implementation
§Returns
  • Self - Builder instance for method chaining
Source

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

Set default chat model for the client

§Arguments
  • model - Default chat model name
§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .with_default_chat_model("llama-3.1-8b-instant")
    .build()?;
Source

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

Set default multimodal model for the client

§Arguments
  • model - Default multimodal model name
§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .with_default_multimodal_model("llama-3.2-11b-vision")
    .build()?;
Source

pub fn with_smart_defaults(self) -> Self

Enable smart defaults for resilience features

This method enables reasonable default configurations for circuit breaker, rate limiting, and error handling without requiring detailed configuration.

§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .with_smart_defaults()
    .build()?;
Source

pub fn for_production(self) -> Self

Configure for production environment

This method applies production-ready configurations for all resilience features with conservative settings for maximum reliability.

§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .for_production()
    .build()?;
Source

pub fn for_development(self) -> Self

Configure for development environment

This method applies development-friendly configurations with more lenient settings for easier debugging and testing.

§Returns
  • Self - Builder instance for method chaining
§Example
use ai_lib::{AiClientBuilder, Provider};

let client = AiClientBuilder::new(Provider::Groq)
    .for_development()
    .build()?;
Source

pub fn with_resilience_config(self, config: ResilienceConfig) -> Self

Set custom resilience configuration

§Arguments
  • config - Custom resilience configuration
§Returns
  • Self - Builder instance for method chaining
Source

pub fn build(self) -> Result<AiClient, AiLibError>

Build AiClient instance

The build process applies configuration in the following priority order:

  1. Explicitly set configuration (via with_* methods)
  2. Environment variable configuration
  3. Default configuration
§Returns
  • Result<AiClient, AiLibError> - Returns client instance on success, error on failure

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