valentinegb / openai

An unofficial Rust library for the OpenAI API.
https://crates.io/crates/openai/
MIT License
61 stars 18 forks source link

Add way to define API key without environmental variables #13

Closed valentinegb closed 1 year ago

valentinegb commented 1 year ago

Problem

Sometimes, for whatever the reason may be, a developer may opt to not use an environment variable for authentication. Currently, this is impossible.

Solution

I'm thinking of a function something like set_key(key: &str) that stores the key attribute in a variable in the library that can only be read within the library. Then, using an environment variable for the API key would look something like this:

use dotenvy::dotenv;
use std::env;

#[tokio::main]
async fn main() {
    dotenv().ok();

    openai::set_key(env::var("OPENAI_KEY").unwrap());
}

Alternatives

There could just be a key variable in the library that is able to be read and changed from wherever, but that doesn't sound very safe to me.

valentinegb commented 1 year ago

This doesn't quite work actually, because the key needs to be known at compile-time, and set_key would always execute at run-time.

valentinegb commented 1 year ago

Well, unless someone can figure out a way to define a key pre-compile-time without relying on an environment variable, I'm closing this as not planned. It really would be nice, but not nice enough to sacrifice automation of the ModelID enumerator.