Skip to main content

ClientBuilder

Struct ClientBuilder 

Source
pub struct ClientBuilder<ModelState = ModelMissing> { /* private fields */ }
Expand description

Builder for creating a Client.

Set credentials (usually with from_env), then optionally a default model, generation config, retry policy, and shared tools, and finish with build.

Explicit setters win over the environment regardless of chain order relative to from_env(), because from_env() replaces the accumulated config — so call it first.

§Examples

use std::time::Duration;

use rai_sdk::{ClientBuilder, GenerationConfig, Model, RetryConfig};

let client = ClientBuilder::new()
    .from_env()
    .model(Model::gpt4o_mini())
    .config(GenerationConfig::new().with_max_tokens(1024))
    .retry_config(RetryConfig::new().with_initial_delay(Duration::from_millis(250)))
    .timeout(60)
    .build()?;

Implementations§

Source§

impl ClientBuilder<ModelMissing>

Source

pub fn new() -> Self

Start a client builder.

Source§

impl<ModelState> ClientBuilder<ModelState>

Source

pub fn from_env(self) -> Self

Load configuration from environment variables.

Reads the API keys, base URLs, timeout, and retry variables documented in config. This replaces any configuration already accumulated on the builder, so call it first and then override individual values.

Source

pub fn openai_key(self, key: impl Into<String>) -> Self

Set the OpenAI API key.

Source

pub fn openai_base_url(self, url: impl Into<String>) -> Self

Set the OpenAI base URL.

Source

pub fn openai_compatible_base_url(self, url: impl Into<String>) -> Self

Point this client at an OpenAI-compatible endpoint.

The URL is the API root serving POST /chat/completions, so it usually ends in /v1http://localhost:8000/v1 for vLLM, http://localhost:1234/v1 for LM Studio. Setting it is what makes ProviderKind::OpenAICompatible available; there is no default endpoint and no environment variable, because the endpoint is a property of this client rather than of the process. See Config::openai_compatible_base_url.

§Examples

Two endpoints in one process, each with its own client:

use rai_sdk::{ClientBuilder, Model};

let local = ClientBuilder::new()
    .ollama()
    .model(Model::openai_compatible("llama3.1:8b"))
    .build()?;

let cluster = ClientBuilder::new()
    .openai_compatible_base_url("https://vllm.internal.example/v1")
    .openai_compatible_key("shared-secret")
    .model(Model::openai_compatible("Qwen/Qwen2.5-7B-Instruct"))
    .build()?;
Source

pub fn openai_compatible_key(self, key: impl Into<String>) -> Self

Set the bearer token for the OpenAI-compatible endpoint.

Optional. With no key set, requests carry no Authorization header at all, which is what a local runtime expects.

Source

pub fn openai_compatible_capabilities( self, capabilities: EndpointCapabilities, ) -> Self

Declare what the OpenAI-compatible endpoint supports.

Requests needing something it was declared not to support fail with Error::CapabilityUnsupported before any HTTP call, instead of reaching the endpoint and coming back as an opaque bad request.

§Examples
use rai_sdk::{ClientBuilder, EndpointCapabilities, Model};

let client = ClientBuilder::new()
    .ollama()
    .openai_compatible_capabilities(
        EndpointCapabilities::default().with_tool_calling(false),
    )
    .model(Model::openai_compatible("gemma3:4b"))
    .build()?;
Source

pub fn ollama(self) -> Self

Point this client at a local Ollama server.

Shorthand for openai_compatible_base_url with OLLAMA_BASE_URL (http://localhost:11434/v1). Pass the URL explicitly for any other host or port.

Source

pub fn anthropic_key(self, key: impl Into<String>) -> Self

Set the Anthropic API key.

Source

pub fn anthropic_base_url(self, url: impl Into<String>) -> Self

Set the Anthropic base URL.

Source

pub fn openrouter_key(self, key: impl Into<String>) -> Self

Set the OpenRouter API key.

Source

pub fn openrouter_base_url(self, url: impl Into<String>) -> Self

Set the OpenRouter base URL.

Source

pub fn openrouter_http_referer(self, referer: impl Into<String>) -> Self

Set the OpenRouter HTTP referer attribution header.

Source

pub fn openrouter_title(self, title: impl Into<String>) -> Self

Set the OpenRouter title attribution header.

Source

pub fn openrouter_categories(self, categories: Vec<String>) -> Self

Set OpenRouter app categories attribution header.

Source

pub fn openrouter_app_url(self, url: impl Into<String>) -> Self

Set the OpenRouter App URL.

Source

pub fn openrouter_app_title(self, title: impl Into<String>) -> Self

Set the OpenRouter App Title.

Source

pub fn timeout(self, seconds: u64) -> Self

Set the request timeout.

Source

pub fn model(self, model: Model) -> ClientBuilder<ModelReady>

Set the default model used by request builders.

This also moves the builder into the model-ready state, so the resulting client can start requests that need only a prompt. Individual requests can still override it with RequestBuilder::model.

Source

pub fn config(self, config: GenerationConfig) -> Self

Set the default generation config used by request builders.

Source

pub fn retry_config(self, config: RetryConfig) -> Self

Set the default retry configuration for all requests.

Source

pub fn no_retry(self) -> Self

Disable retries by default for all requests.

Source

pub fn tool(self, tool: Tool) -> Self

Register a tool that RequestBuilder::generate may auto-execute.

Client-level tools are available to every request. Requests that stream must opt out with RequestBuilder::no_tools, since streaming cannot run a tool loop; see RequestBuilder::stream.

Source

pub fn tools<T>(self, tools: T) -> Self
where T: IntoIterator<Item = Tool>,

Register multiple tools to be auto-executed by generate().

Source

pub fn build(self) -> Result<Client<ModelState>>

Build the client.

Providers with credentials are initialized; providers without them are left unavailable rather than causing a failure, so this succeeds even if only one key is present.

§Errors
  • Error::InvalidRequest if two registered tools share a name, or a tool’s input schema is invalid.
  • An error if a provider’s HTTP client cannot be constructed.

Trait Implementations§

Source§

impl<ModelState> Debug for ClientBuilder<ModelState>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<ModelState = ModelMissing> !RefUnwindSafe for ClientBuilder<ModelState>

§

impl<ModelState = ModelMissing> !UnwindSafe for ClientBuilder<ModelState>

§

impl<ModelState> Freeze for ClientBuilder<ModelState>

§

impl<ModelState> Send for ClientBuilder<ModelState>
where ModelState: Send,

§

impl<ModelState> Sync for ClientBuilder<ModelState>
where ModelState: Sync,

§

impl<ModelState> Unpin for ClientBuilder<ModelState>
where ModelState: Unpin,

§

impl<ModelState> UnsafeUnpin for ClientBuilder<ModelState>

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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: Sized + 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: Sized + 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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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