pub struct Client<ModelState = ModelMissing> { /* private fields */ }Expand description
Unified AI client for OpenAI, Anthropic, and OpenRouter.
A client owns provider credentials and HTTP clients, an optional default model, default generation and retry settings, and any tools shared by every request. Build one once and reuse it: individual requests are cheap, but constructing a client initializes a client per configured provider.
Use ClientBuilder for the common path, or Client::new when you
already have a Config.
§Typestate
The ModelState parameter records whether a default model is present.
ClientBuilder::model moves the builder into the model-ready state, and
only a model-ready client hands out request builders that can call
RequestBuilder::generate without naming a model. A client built without a
default model is still fully usable — every request just has to call
RequestBuilder::model first. Either way, a request missing a model is a
compile error rather than a runtime one.
§Examples
use rai_sdk::{ClientBuilder, Model};
let client = ClientBuilder::new()
.from_env()
.model(Model::gpt4o_mini())
.build()?;
// Reuse the same client for many requests.
for prompt in ["Define a trait.", "Define a lifetime."] {
let response = client.request().prompt(prompt).generate().await?;
println!("{}", response.text());
}Implementations§
Source§impl Client<ModelMissing>
impl Client<ModelMissing>
Sourcepub fn new(config: Config) -> Result<Self>
pub fn new(config: Config) -> Result<Self>
Create a client from an explicit Config, with no default model.
Every request from this client must select a model with
RequestBuilder::model. Use ClientBuilder instead if you want a
default model or client-level tools.
A provider whose API key is missing is simply left uninitialized rather
than failing here; using it later returns
Error::ProviderNotConfigured.
§Errors
Returns an error if a configured provider’s HTTP client cannot be constructed, for example because the request timeout is invalid.
§Examples
use rai_sdk::{Client, Config, Model};
let client = Client::new(Config::from_env())?;
let response = client
.request()
.model(Model::gpt4o_mini())
.prompt("Hello")
.generate()
.await?;Sourcepub fn builder() -> ClientBuilder<ModelMissing>
pub fn builder() -> ClientBuilder<ModelMissing>
Create a builder for configuring a client with defaults.
Equivalent to ClientBuilder::new.
Sourcepub fn request(
&self,
) -> RequestBuilder<'_, PromptMissing, ModelMissing, ModelMissing>
pub fn request( &self, ) -> RequestBuilder<'_, PromptMissing, ModelMissing, ModelMissing>
Start a request.
This client has no default model, so the returned builder requires
RequestBuilder::model before it will expose generate and friends.
Source§impl<ModelState> Client<ModelState>
impl<ModelState> Client<ModelState>
Sourcepub async fn generate_stream(
&self,
model: Model,
prompt: &Prompt,
config: &GenerationConfig,
) -> Result<Pin<Box<dyn Stream<Item = Result<ProviderStreamEvent>> + Send>>>
pub async fn generate_stream( &self, model: Model, prompt: &Prompt, config: &GenerationConfig, ) -> Result<Pin<Box<dyn Stream<Item = Result<ProviderStreamEvent>> + Send>>>
Stream a completion for an explicit model and prompt.
Prefer RequestBuilder::stream, which applies the client’s defaults,
retry policy, and per-request tool overrides. This lower-level entry
point is useful when you are driving the model and prompt yourself.
§Errors
Error::InvalidRequestif any tool is registered on this client, since streaming cannot run a tool loop. Because this method takes no request context, it can only consider the client’s tools; useRequestBuilder::streamwithRequestBuilder::no_toolsto stream from a client that has tools registered.Error::ProviderNotConfiguredif the model’s provider has no API key.Error::ProviderNotEnabledif its Cargo feature is disabled.- A transport or provider error if the request itself fails.
Sourcepub fn is_provider_available(&self, provider: ProviderKind) -> bool
pub fn is_provider_available(&self, provider: ProviderKind) -> bool
Whether a provider is usable: its feature is enabled and it has credentials.
Use this to branch at runtime instead of discovering a missing key through a failed request.
§Examples
use rai_sdk::{ClientBuilder, Model, ProviderKind};
let client = ClientBuilder::new().from_env().build()?;
let model = if client.is_provider_available(ProviderKind::Anthropic) {
Model::claude_sonnet_46()
} else {
Model::gpt4o_mini()
};Source§impl Client<ModelReady>
impl Client<ModelReady>
Sourcepub fn request(
&self,
) -> RequestBuilder<'_, PromptMissing, ModelReady, ModelReady>
pub fn request( &self, ) -> RequestBuilder<'_, PromptMissing, ModelReady, ModelReady>
Start a request that inherits this client’s default model.
Because the model is already known, the returned builder only needs a
prompt before you can call RequestBuilder::generate. Override the
model per request with RequestBuilder::model.