-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
126 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,56 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
use toml::Value; | ||
|
||
pub fn get_api_key() -> String { | ||
let config_path = format!( | ||
"{}/.config/pipelm/.api_configs.toml", | ||
std::env::var("HOME").unwrap() | ||
); | ||
let content = fs::read_to_string(config_path).expect("Failed to read the TOML file"); | ||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Prompt { | ||
#[serde(skip_serializing)] // internal use only | ||
pub service: String, | ||
pub model: String, | ||
pub messages: Vec<Message>, | ||
} | ||
|
||
#[derive(Debug, Deserialize, Serialize)] | ||
pub struct Message { | ||
pub role: String, | ||
pub content: String, | ||
} | ||
|
||
pub const PLACEHOLDER_TOKEN: &str = "#[<input>]"; | ||
|
||
const DEFAULT_CONFIG_PATH: &str = ".config/pipelm/"; | ||
const CUSTOM_CONFIG_ENV_VAR: &str = "PIPLE_CONFIG_PATH"; | ||
const API_KEYS_FILE: &str = ".api_keys.toml"; | ||
const PROMPT_FILE: &str = "prompts.toml"; | ||
|
||
fn resolve_config_path() -> PathBuf { | ||
match std::env::var(CUSTOM_CONFIG_ENV_VAR) { | ||
Ok(p) => PathBuf::new().join(p), | ||
Err(_) => PathBuf::new().join(env!("HOME")).join(DEFAULT_CONFIG_PATH), | ||
} | ||
} | ||
|
||
pub fn get_api_key(service: &str) -> String { | ||
let api_keys_path = resolve_config_path().join(API_KEYS_FILE); | ||
let content = fs::read_to_string(&api_keys_path) | ||
.unwrap_or_else(|error| panic!("Could not read file {:?}, {:?}", api_keys_path, error)); | ||
let value: Value = content.parse().expect("Failed to parse TOML"); | ||
|
||
// Extract the API key from the TOML table. | ||
let api_key = value | ||
.get("openai") | ||
.and_then(|table| table.get("API_KEY")) | ||
.and_then(|api_key| api_key.as_str()) | ||
.unwrap_or_else(|| { | ||
eprintln!("API_KEY not found in the TOML file."); | ||
std::process::exit(1); | ||
}); | ||
.get("API_KEYS") | ||
.expect("API_KEYS section not found") | ||
.get(service) | ||
.unwrap_or_else(|| panic!("No api key found for service {}.", &service)); | ||
|
||
api_key.to_string() | ||
} | ||
|
||
pub fn get_prompts() -> HashMap<String, Prompt> { | ||
let prompts_path = resolve_config_path().join(PROMPT_FILE); | ||
let content = fs::read_to_string(&prompts_path) | ||
.unwrap_or_else(|error| panic!("Could not read file {:?}, {:?}", prompts_path, error)); | ||
toml::from_str(&content).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters