pub struct ClientBuilder { /* private fields */ }
Expand description
A builder for OpenAIClient
that follows the builder pattern.
§Examples
use chat_gpt_lib_rs::OpenAIClient;
use std::time::Duration;
let client = OpenAIClient::builder()
.with_api_key("sk-...YourKey...")
.with_organization("org-MyOrganization")
.with_timeout(Duration::from_secs(30))
.build()
.unwrap();
Implementations§
Source§impl ClientBuilder
impl ClientBuilder
Sourcepub fn with_base_url(self, url: &str) -> Self
pub fn with_base_url(self, url: &str) -> Self
Sets a custom base URL for all OpenAI requests.
§Example
let client = OpenAIClient::builder()
.with_base_url("https://custom-openai-proxy.example.com/v1/")
.with_api_key("sk-EXAMPLE")
.build()
.unwrap();
Sourcepub fn with_api_key(self, key: &str) -> Self
pub fn with_api_key(self, key: &str) -> Self
Sets the API key explicitly. If not provided, the client will attempt to
read from the OPENAI_API_KEY
environment variable.
§Example
let client = OpenAIClient::builder()
.with_api_key("sk-EXAMPLE")
.build()
.unwrap();
Sourcepub fn with_organization(self, org: &str) -> Self
pub fn with_organization(self, org: &str) -> Self
Sets the organization ID for the client. Some accounts or requests require specifying an organization ID.
§Example
let client = OpenAIClient::builder()
.with_api_key("sk-EXAMPLE")
.with_organization("org-EXAMPLE")
.build()
.unwrap();
Sourcepub fn with_timeout(self, duration: Duration) -> Self
pub fn with_timeout(self, duration: Duration) -> Self
Sets a timeout for all HTTP requests made by this client.
If not specified, the timeout behavior of the underlying
reqwest::Client
defaults are used.
§Example
let client = OpenAIClient::builder()
.with_api_key("sk-EXAMPLE")
.with_timeout(Duration::from_secs(30))
.build()
.unwrap();
Sourcepub fn build(self) -> Result<OpenAIClient, OpenAIError>
pub fn build(self) -> Result<OpenAIClient, OpenAIError>
Builds the OpenAIClient
using the specified configuration.
If the API key is not set through with_api_key
, it attempts to read from
the OPENAI_API_KEY
environment variable. If no key is found, an error is returned.
§Errors
Returns an OpenAIError
if no API key is provided or discovered in the environment,
or if building the underlying HTTP client fails.