diff options
| author | sigoden <sigoden@gmail.com> | 2024-11-06 17:35:05 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-11-06 17:35:05 +0800 |
| commit | 3ffa87689169b50cf3b551a7dcd31358ddafe32e (patch) | |
| tree | 0d40315333212530d775a6c6b65ed847f6f78dff /src/config/agent.rs | |
| parent | 0fac7fea5090642eb5155b2b24f041476b89c813 (diff) | |
| download | aichat-3ffa87689169b50cf3b551a7dcd31358ddafe32e.tar.gz | |
feat: support overriding agent config with env vars (#974)
Diffstat (limited to 'src/config/agent.rs')
| -rw-r--r-- | src/config/agent.rs | 29 |
1 files changed, 28 insertions, 1 deletions
diff --git a/src/config/agent.rs b/src/config/agent.rs index 5589f0e..9a7e67c 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -41,7 +41,7 @@ impl Agent { let functions_file_path = functions_dir.join("functions.json"); let rag_path = Config::agent_rag_file(name, DEFAULT_AGENT_NAME)?; let config_path = Config::agent_config_file(name)?; - let agent_config = if config_path.exists() { + let mut agent_config = if config_path.exists() { AgentConfig::load(&config_path)? } else { AgentConfig::new(&config.read()) @@ -54,6 +54,8 @@ impl Agent { }; definition.replace_tools_placeholder(&functions); + agent_config.load_envs(&definition.name); + let model = { let config = config.read(); match agent_config.model_id.as_ref() { @@ -330,6 +332,31 @@ impl AgentConfig { .with_context(|| format!("Failed to load agent config at '{}'", path.display()))?; Ok(config) } + + fn load_envs(&mut self, name: &str) { + let with_prefix = |v: &str| normalize_env_name(&format!("{name}_{v}")); + + if let Some(v) = read_env_value::<String>(&with_prefix("model")) { + self.model_id = v; + } + if let Some(v) = read_env_value::<f64>(&with_prefix("temperature")) { + self.temperature = v; + } + if let Some(v) = read_env_value::<f64>(&with_prefix("top_p")) { + self.top_p = v; + } + if let Some(v) = read_env_value::<String>(&with_prefix("use_tools")) { + self.use_tools = v; + } + if let Some(v) = read_env_value::<String>(&with_prefix("agent_prelude")) { + self.agent_prelude = v; + } + if let Ok(v) = env::var(with_prefix("variables")) { + if let Ok(v) = serde_json::from_str(&v) { + self.variables = v; + } + } + } } #[derive(Debug, Clone, Default, Deserialize, Serialize)] |
