Skip to content

Commit

Permalink
Merge pull request #9 from theoforger/fix/env-handling
Browse files Browse the repository at this point in the history
Move all environment variable handling inside Instance
  • Loading branch information
theoforger authored Sep 18, 2024
2 parents a7940e1 + 97e6a05 commit 0ba0d57
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 19 deletions.
15 changes: 9 additions & 6 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,28 @@ impl Instance {
pub fn new() -> Result<Self, Box<dyn std::error::Error>> {
dotenv().ok();

let base_url = env::var("OPENAI_API_BASE_URL")
.map_err(|_| "Cannot read environment variable: OPENAI_API_BASE_URL")?;

let base_url = Self::get_env_var("OPENAI_API_BASE_URL")?;
let base_url = if !base_url.ends_with('/') {
format!("{}/", base_url)
} else {
base_url
};

let key = env::var("API_KEY").map_err(|_| "Cannot read environment variable: API_KEY")?;
let key = Self::get_env_var("API_KEY")?;
let model_id = Self::get_env_var("DEFAULT_MODEL_ID")?;

Ok(Self {
client: reqwest::Client::new(),
base_url,
key,
model_id: "".to_string(),
model_id,
})
}

fn get_env_var(var_name: &str) -> Result<String, Box<dyn std::error::Error>> {
env::var(var_name)
.map_err(|_| format!("Cannot read environment variable: {}", var_name).into())
}

pub async fn set_model_id(
&mut self,
model_id: String,
Expand Down
17 changes: 4 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::env;

use clap::Parser;
use dotenv::dotenv;

use mastermind::api::Instance;
use mastermind::*;
Expand All @@ -10,7 +7,6 @@ use mastermind::*;
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Read arguments and environment variables
let args = Args::parse();
dotenv().ok();

// Create an API instance
let mut api_instance = Instance::new()?;
Expand All @@ -22,14 +18,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

// If -m is set, use a preferred language model
// Otherwise, set the default
let model_id = match args.model {
Some(id) => id,
None => env::var("DEFAULT_MODEL_ID").map_err(|_| {
"Could not read environment variable: DEFAULT_MODEL_ID. Use -m to specify a language model"
})?,
};
api_instance.set_model_id(model_id).await?;
if let Some(model_id) = args.model {
api_instance.set_model_id(model_id).await?;
}

// Attempt to read words from the two files
let link_words = read_words_from_file(args.to_link.unwrap()).map_err(|e| e.to_string())?;
Expand All @@ -44,7 +35,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
if clue_collection.is_empty() {
println!("The language model didn't return any useful clues. Maybe try again?");
} else if let Some(output_path) = args.output {
println!("-o option is present, writing to file...");
println!("Writing to file '{}'...", output_path.display());
write_content_to_file(output_path, clue_collection.output())?;
} else {
clue_collection.display();
Expand Down

0 comments on commit 0ba0d57

Please sign in to comment.