Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Create Client with Client::default(); use default parameters #35

Merged
merged 3 commits into from
Feb 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions async-openai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
Completions, Embeddings, FineTunes, Models,
};

#[derive(Debug, Default, Clone)]
#[derive(Debug, Clone)]
/// Client is a container for api key, base url, organization id, and backoff
/// configuration used to make API calls.
pub struct Client {
Expand All @@ -29,15 +29,23 @@ pub const API_BASE: &str = "https://api.openai.com/v1";
/// Name for organization header
pub const ORGANIZATION_HEADER: &str = "OpenAI-Organization";

impl Client {
impl Default for Client {
/// Create client with default [API_BASE] url and default API key from OPENAI_API_KEY env var
pub fn new() -> Self {
fn default() -> Self {
Self {
api_base: API_BASE.to_string(),
api_key: std::env::var("OPENAI_API_KEY").unwrap_or_else(|_| "".to_string()),
..Default::default()
org_id: Default::default(),
backoff: Default::default(),
}
}
}

impl Client {
/// Create client with default [API_BASE] url and default API key from OPENAI_API_KEY env var
pub fn new() -> Self {
Default::default()
}

/// To use a different API key different from default OPENAI_API_KEY env var
pub fn with_api_key<S: Into<String>>(mut self, api_key: S) -> Self {
Expand Down