Module config

Source
Expand description

This module will contain the central configuration, including the OpenAIClient struct, environment variable helpers, and other utilities. The config module provides functionality for configuring and creating the OpenAIClient, including handling API keys, organization IDs, timeouts, and base URLs.

§Overview

This module exposes the OpenAIClient struct, which is your main entry point for interacting with the OpenAI API. It provides a builder-pattern (ClientBuilder) for customizing various aspects of the client configuration, such as the API key, organization ID, timeouts, and so on.

§Usage

use chat_gpt_lib_rs::OpenAIClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
     // Load environment variables from a .env file, if present (optional).
     dotenvy::dotenv().ok();

    // Example 1: Use environment variable `OPENAI_API_KEY`.
    let client = OpenAIClient::new(None)?;

    // Example 2: Use a builder pattern to set more configuration.
    let client_with_org = OpenAIClient::builder()
        .with_api_key("sk-...YourKey...")
        .with_organization("org-MyOrganization")
        .with_timeout(std::time::Duration::from_secs(30))
        .build()?;

    // Use `client` or `client_with_org` to make API requests...

    Ok(())
}

Structs§

ClientBuilder
A builder for OpenAIClient that follows the builder pattern.
OpenAIClient
A client for interacting with the OpenAI API.

Constants§

DEFAULT_BASE_URL
The default base URL for the OpenAI API.