helios_engine/
config.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::path::Path;
4use crate::error::{HeliosError, Result};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Config {
8    pub llm: LLMConfig,
9    #[serde(default)]
10    pub local: Option<LocalConfig>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct LLMConfig {
15    pub model_name: String,
16    pub base_url: String,
17    pub api_key: String,
18    #[serde(default = "default_temperature")]
19    pub temperature: f32,
20    #[serde(default = "default_max_tokens")]
21    pub max_tokens: u32,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct LocalConfig {
26    pub huggingface_repo: String,
27    pub model_file: String,
28    #[serde(default = "default_context_size")]
29    pub context_size: usize,
30    #[serde(default = "default_temperature")]
31    pub temperature: f32,
32    #[serde(default = "default_max_tokens")]
33    pub max_tokens: u32,
34}
35
36fn default_temperature() -> f32 {
37    0.7
38}
39
40fn default_max_tokens() -> u32 {
41    2048
42}
43
44fn default_context_size() -> usize {
45    2048
46}
47
48impl Config {
49    /// Load configuration from a TOML file
50    pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
51        let content = fs::read_to_string(path)
52            .map_err(|e| HeliosError::ConfigError(format!("Failed to read config file: {}", e)))?;
53        
54        let config: Config = toml::from_str(&content)?;
55        Ok(config)
56    }
57
58    /// Create a default configuration
59    pub fn new_default() -> Self {
60        Self {
61            llm: LLMConfig {
62                model_name: "gpt-3.5-turbo".to_string(),
63                base_url: "https://api.openai.com/v1".to_string(),
64                api_key: "your-api-key-here".to_string(),
65                temperature: 0.7,
66                max_tokens: 2048,
67            },
68            local: None,
69        }
70    }
71
72    /// Save configuration to a TOML file
73    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<()> {
74        let content = toml::to_string_pretty(self)
75            .map_err(|e| HeliosError::ConfigError(format!("Failed to serialize config: {}", e)))?;
76        
77        fs::write(path, content)
78            .map_err(|e| HeliosError::ConfigError(format!("Failed to write config file: {}", e)))?;
79        
80        Ok(())
81    }
82}