pub struct OpenAiClientBuilder { /* private fields */ }Expand description
Builder for OpenAiClient.
Created via OpenAiClientBuilder::default or
OpenAiClient::builder. All fields have sensible defaults except
api_key, which must be set before build.
Implementations§
Source§impl OpenAiClientBuilder
impl OpenAiClientBuilder
Sourcepub fn with_api_key(self, key: impl Into<String>) -> Self
pub fn with_api_key(self, key: impl Into<String>) -> Self
Set the API key for authentication.
Required — build returns an error if this is not set.
The key is sent as the Authorization: Bearer <key> header on every
request.
Sourcepub fn with_base_url(self, url: impl Into<String>) -> Self
pub fn with_base_url(self, url: impl Into<String>) -> Self
Set the base URL for API requests.
Defaults to https://api.openai.com/v1. Override when targeting an
OpenAI-compatible endpoint (e.g. https://api.deepseek.com/v1,
http://localhost:11434/v1 for Ollama, or a vLLM server).
Trailing / separators are trimmed, so joined request paths never
contain // — a …/v1/ base behaves identically to …/v1.
Sourcepub fn with_model(self, model: impl Into<String>) -> Self
pub fn with_model(self, model: impl Into<String>) -> Self
Set the default model identifier.
The model string is sent as the model field on every request,
unless a per-request
RequestOptions::model
override names another. Can also be swapped wholesale at runtime
via OpenAiClient::set_model.
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Set the HTTP read timeout.
This bounds idleness — how long a gap between bytes on the
connection may last — not the total request lifetime: a stream
that keeps producing bytes runs as long as it keeps producing.
Defaults to 120 seconds. Ignored when a client was supplied via
with_http_client; bound the whole
turn with a StreamHandler
total timeout instead.
Sourcepub fn with_connect_timeout(self, timeout: Duration) -> Self
pub fn with_connect_timeout(self, timeout: Duration) -> Self
Set the TCP connection establishment timeout.
Defaults to 10 seconds. Ignored when a client was supplied via
with_http_client.
Sourcepub fn with_http_client(self, client: Client) -> Self
pub fn with_http_client(self, client: Client) -> Self
Inject a pre-built, shared reqwest::Client.
When set, the client’s connection pool is shared with every other provider built from the same handle, and the pool/TCP knobs are ignored. Configure timeouts on the injected client, not here.
Sourcepub fn with_stream_usage(self, enabled: bool) -> Self
pub fn with_stream_usage(self, enabled: bool) -> Self
Control whether streaming requests include stream_options.include_usage.
Defaults to true — real OpenAI, DeepSeek, and Grok support it and
send a final usage chunk. Pass false for OpenAI-compatible servers
that reject the parameter with a validation error (older Ollama, some
self-hosted vLLM/LM Studio deployments). When disabled, streamed turns
report usage: None instead of real token counts.
Ignored on non-streaming requests.
Sourcepub fn with_pool_max_idle_per_host(self, n: usize) -> Self
pub fn with_pool_max_idle_per_host(self, n: usize) -> Self
Set the maximum idle connections kept alive per host.
Defaults to reqwest’s built-in default (unlimited). Ignored when a
client was supplied via with_http_client.
Sourcepub fn with_pool_idle_timeout(self, d: Duration) -> Self
pub fn with_pool_idle_timeout(self, d: Duration) -> Self
Set how long an idle connection stays in the pool before being closed.
Defaults to reqwest’s built-in default (90s). Ignored when a client
was supplied via with_http_client.
Sourcepub fn with_tcp_keepalive(self, d: Duration) -> Self
pub fn with_tcp_keepalive(self, d: Duration) -> Self
Set the OS-level TCP keepalive interval.
Defaults to disabled (reqwest default). Ignored when a client was
supplied via with_http_client.
Sourcepub fn with_tcp_nodelay(self, enabled: bool) -> Self
pub fn with_tcp_nodelay(self, enabled: bool) -> Self
Control whether TCP_NODELAY is set on connections.
Defaults to true — SSE streaming benefits from disabling Nagle’s
algorithm. Pass false to re-enable it. Ignored when a client was
supplied via with_http_client.