UltrafastClientBuilder

Struct UltrafastClientBuilder 

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

Builder for creating UltrafastClient instances with custom configuration.

The UltrafastClientBuilder provides a fluent API for configuring and creating UltrafastClient instances. It supports both standalone and gateway modes.

§Examples

§Standalone Mode

use ultrafast_models_sdk::{UltrafastClient, RetryPolicy};
use std::time::Duration;

let retry_policy = RetryPolicy {
    max_retries: 5,
    initial_delay: Duration::from_millis(100),
    max_delay: Duration::from_secs(10),
    backoff_multiplier: 2.0,
    jitter_factor: 0.1,
};

let client = UltrafastClientBuilder::default()
    .with_retry_policy(retry_policy)
    .standalone()
    .with_openai("your-openai-key")
    .with_anthropic("your-anthropic-key")
    .build()?;

§Gateway Mode

let client = UltrafastClientBuilder::default()
    .gateway("http://localhost:3000".to_string())
    .with_api_key("your-gateway-key")
    .with_timeout(Duration::from_secs(60))
    .build()?;

§Builder Pattern

The builder follows the fluent builder pattern, allowing method chaining:

let client = UltrafastClientBuilder::default()
    .with_retry_policy(custom_retry_policy)
    .standalone()
    .with_openai("key1")
    .with_anthropic("key2")
    .with_routing_strategy(RoutingStrategy::LoadBalance {
        weights: vec![0.6, 0.4],
    })
    .with_cache_config(cache_config)
    .build()?;

§Configuration Options

§Retry Policy

Configure retry behavior for failed requests:

  • Max Retries: Maximum number of retry attempts
  • Initial Delay: Starting delay before first retry
  • Max Delay: Maximum delay between retries
  • Backoff Multiplier: Exponential backoff factor
  • Jitter Factor: Randomization to prevent thundering herd

§Provider Configuration

Add and configure AI providers:

  • OpenAI: GPT models with API key
  • Anthropic: Claude models with API key
  • Azure OpenAI: Azure-hosted OpenAI models
  • Google Vertex AI: Google AI models
  • Cohere: Command models
  • Groq: Fast inference models
  • Ollama: Local models
  • Custom Providers: Extensible provider system

§Routing Strategy

Choose how requests are routed to providers:

  • Single: Route all requests to one provider
  • Load Balance: Distribute requests across providers
  • Failover: Primary provider with automatic fallback
  • Conditional: Route based on request characteristics
  • A/B Testing: Route for testing different providers

§Caching

Configure response caching:

  • Backend: In-memory or Redis cache
  • TTL: Time-to-live for cached responses
  • Max Size: Maximum number of cached items
  • Key Strategy: Custom cache key generation

§Thread Safety

The builder is not thread-safe and should not be shared across threads. Build the client first, then share the client instance.

§Performance Considerations

  • Connection Pooling: Configure appropriate pool sizes
  • Timeout Settings: Set realistic timeouts for your use case
  • Retry Policies: Balance retry attempts with user experience
  • Cache Configuration: Enable caching for repeated requests

§Error Handling

The builder validates configuration and returns errors for:

  • Invalid provider configurations
  • Missing required fields
  • Configuration conflicts
  • Network connectivity issues

§See Also

Implementations§

Trait Implementations§

Source§

impl Default for UltrafastClientBuilder

Source§

fn default() -> UltrafastClientBuilder

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