pub struct OpenAIClient { /* private fields */ }Expand description
A client for interacting with the OpenAI API.
This struct holds the configuration (e.g., API key, organization ID, base URL) and
an underlying reqwest::Client for making HTTP requests. Typically, you’ll create an
OpenAIClient using:
- The
OpenAIClient::newmethod, which optionally reads the API key from an environment variable, or - The builder pattern via
OpenAIClient::builder.
Implementations§
Source§impl OpenAIClient
impl OpenAIClient
Sourcepub fn new(api_key: Option<String>) -> Result<Self, OpenAIError>
pub fn new(api_key: Option<String>) -> Result<Self, OpenAIError>
Creates a new OpenAIClient using the provided API key, or reads it from the
OPENAI_API_KEY environment variable if api_key is None.
§Errors
Returns an OpenAIError if no API key can be found in the given argument or
the environment variable.
§Examples
use chat_gpt_lib_rs::OpenAIClient;
// load environment variables from a .env file, if present (optional).
dotenvy::dotenv().ok();
// Reads `OPENAI_API_KEY` from the environment.
let client = OpenAIClient::new(None).unwrap();
// Provide an explicit API key.
let client = OpenAIClient::new(Some("sk-...".to_string())).unwrap();Sourcepub fn builder() -> ClientBuilder
pub fn builder() -> ClientBuilder
Returns a new ClientBuilder to configure and build an OpenAIClient.
§Examples
let client = OpenAIClient::builder()
.with_api_key("sk-EXAMPLE")
.with_organization("org-EXAMPLE")
.build()
.unwrap();Sourcepub fn base_url(&self) -> &str
pub fn base_url(&self) -> &str
Returns the current base URL as a string slice.
Useful if you need to verify or debug the client’s configuration.
Sourcepub fn api_key(&self) -> &str
pub fn api_key(&self) -> &str
Returns the API key as a string slice.
For security reasons, you might not want to expose this in production logs.
Sourcepub fn organization(&self) -> Option<&str>
pub fn organization(&self) -> Option<&str>
Returns the optional organization ID, if it was set.
Trait Implementations§
Source§impl Clone for OpenAIClient
impl Clone for OpenAIClient
Source§fn clone(&self) -> OpenAIClient
fn clone(&self) -> OpenAIClient
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more