2023-03-03 01:15:58 +00:00
|
|
|
use std::{
|
|
|
|
fs::read_to_string,
|
|
|
|
path::{Path, PathBuf},
|
|
|
|
};
|
2023-03-02 11:52:11 +00:00
|
|
|
|
|
|
|
use anyhow::{anyhow, Result};
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct Config {
|
|
|
|
/// Openai api key
|
|
|
|
pub api_key: String,
|
2023-03-02 15:11:34 +00:00
|
|
|
/// What sampling temperature to use, between 0 and 2
|
|
|
|
pub temperature: Option<f64>,
|
2023-03-03 01:15:58 +00:00
|
|
|
/// Specify a file path to save chat messages to
|
|
|
|
pub save_path: Option<PathBuf>,
|
2023-03-02 11:52:11 +00:00
|
|
|
/// Set proxy
|
|
|
|
pub proxy: Option<String>,
|
|
|
|
/// Used only for debugging
|
|
|
|
#[serde(default)]
|
|
|
|
pub dry_run: bool,
|
2023-03-02 14:25:55 +00:00
|
|
|
/// Predefined roles
|
2023-03-02 11:52:11 +00:00
|
|
|
#[serde(default)]
|
|
|
|
pub roles: Vec<Role>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub fn init(path: &Path) -> Result<Config> {
|
|
|
|
let content = read_to_string(path)
|
|
|
|
.map_err(|err| anyhow!("Failed to load config at {}, {err}", path.display()))?;
|
|
|
|
let config: Config =
|
|
|
|
toml::from_str(&content).map_err(|err| anyhow!("Invalid config, {err}"))?;
|
|
|
|
Ok(config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct Role {
|
|
|
|
/// Role name
|
|
|
|
pub name: String,
|
2023-03-02 14:25:55 +00:00
|
|
|
/// Prompt text send to ai for setting up a role
|
2023-03-02 11:52:11 +00:00
|
|
|
pub prompt: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Role {
|
|
|
|
pub fn generate(&self, text: &str) -> String {
|
2023-03-03 00:26:59 +00:00
|
|
|
format!("{} {}", self.prompt, text)
|
2023-03-02 11:52:11 +00:00
|
|
|
}
|
|
|
|
}
|