Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move agent config to separate file #741

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ function_calling: true # Enables or disables function calling (Globall
mapping_tools: # Alias for a tool or toolset
# fs: 'fs_cat,fs_ls,fs_mkdir,fs_rm,fs_write'
use_tools: null # Which tools to use by default
# Per-Agent configuration
agents:
- name: todo-sh
model: null
temperature: null
top_p: null

# ---- RAG ----
rag_embedding_model: null # Specifies the embedding model to use
Expand Down
29 changes: 14 additions & 15 deletions src/config/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ impl Agent {
let functions_file_path = functions_dir.join("functions.json");
let variables_path = Config::agent_variables_file(name)?;
let rag_path = Config::agent_rag_file(name)?;

let config_path = Config::agent_config_file(name)?;
let agent_config = if config_path.exists() {
AgentConfig::load(&config_path)?
} else {
AgentConfig::default()
};
let mut definition = AgentDefinition::load(&definition_file_path)?;
init_variables(&variables_path, &mut definition.variables)
.context("Failed to init variables")?;
Expand All @@ -48,13 +53,7 @@ impl Agent {
Functions::default()
};
definition.replace_tools_placeholder(&functions);
let agent_config = config
.read()
.agents
.iter()
.find(|v| v.name == name)
.cloned()
.unwrap_or_else(|| AgentConfig::new(name));

let model = {
let config = config.read();
match agent_config.model_id.as_ref() {
Expand Down Expand Up @@ -201,7 +200,6 @@ impl RoleLike for Agent {

#[derive(Debug, Clone, Default, Deserialize, Serialize)]
pub struct AgentConfig {
pub name: String,
#[serde(rename(serialize = "model", deserialize = "model"))]
pub model_id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -213,11 +211,12 @@ pub struct AgentConfig {
}

impl AgentConfig {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
..Default::default()
}
pub fn load(path: &Path) -> Result<Self> {
let contents = read_to_string(path)
.with_context(|| format!("Failed to read agent config file at '{}'", path.display()))?;
let config: Self = serde_yaml::from_str(&contents)
.with_context(|| format!("Failed to load agent config at '{}'", path.display()))?;
Ok(config)
}
}

Expand All @@ -242,7 +241,7 @@ impl AgentDefinition {
let contents = read_to_string(path)
.with_context(|| format!("Failed to read agent index file at '{}'", path.display()))?;
let definition: Self = serde_yaml::from_str(&contents)
.with_context(|| format!("Failed to load agent at '{}'", path.display()))?;
.with_context(|| format!("Failed to load agent index at '{}'", path.display()))?;
Ok(definition)
}

Expand Down
38 changes: 18 additions & 20 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod input;
mod role;
mod session;

pub use self::agent::{list_agents, Agent, AgentConfig};
pub use self::agent::{list_agents, Agent};
pub use self::input::Input;
pub use self::role::{Role, RoleLike, CODE_ROLE, EXPLAIN_SHELL_ROLE, SHELL_ROLE};
use self::session::Session;
Expand Down Expand Up @@ -107,7 +107,6 @@ pub struct Config {
pub function_calling: bool,
pub mapping_tools: IndexMap<String, String>,
pub use_tools: Option<String>,
pub agents: Vec<AgentConfig>,

pub rag_embedding_model: Option<String>,
pub rag_reranker_model: Option<String>,
Expand Down Expand Up @@ -169,7 +168,6 @@ impl Default for Config {
function_calling: true,
mapping_tools: Default::default(),
use_tools: None,
agents: vec![],

rag_embedding_model: None,
rag_reranker_model: None,
Expand Down Expand Up @@ -337,14 +335,18 @@ impl Config {
}

pub fn agents_config_dir() -> Result<PathBuf> {
match env::var(get_env_name("agents_config_dir")) {
Self::local_path(AGENTS_DIR_NAME)
}

pub fn agent_config_dir(name: &str) -> Result<PathBuf> {
match env::var(format!("{}_CONFIG_DIR", convert_env_prefix(name))) {
Ok(value) => Ok(PathBuf::from(value)),
Err(_) => Self::local_path(AGENTS_DIR_NAME),
Err(_) => Ok(Self::agents_config_dir()?.join(name)),
}
}

pub fn agent_config_dir(name: &str) -> Result<PathBuf> {
Ok(Self::agents_config_dir()?.join(name))
pub fn agent_config_file(name: &str) -> Result<PathBuf> {
Ok(Self::agent_config_dir(name)?.join(CONFIG_FILE_NAME))
}

pub fn agent_rag_file(name: &str) -> Result<PathBuf> {
Expand All @@ -356,14 +358,14 @@ impl Config {
}

pub fn agents_functions_dir() -> Result<PathBuf> {
match env::var(get_env_name("agents_functions_dir")) {
Ok(value) => Ok(PathBuf::from(value)),
Err(_) => Ok(Self::functions_dir()?.join(AGENTS_DIR_NAME)),
}
Ok(Self::functions_dir()?.join(AGENTS_DIR_NAME))
}

pub fn agent_functions_dir(name: &str) -> Result<PathBuf> {
Ok(Self::agents_functions_dir()?.join(name))
match env::var(format!("{}_FUNCTIONS_DIR", convert_env_prefix(name))) {
Ok(value) => Ok(PathBuf::from(value)),
Err(_) => Ok(Self::agents_functions_dir()?.join(name)),
}
}

pub fn state(&self) -> StateFlags {
Expand Down Expand Up @@ -528,14 +530,6 @@ impl Config {
("roles_file", display_path(&Self::roles_file()?)),
("env_file", display_path(&Self::env_file()?)),
("functions_dir", display_path(&Self::functions_dir()?)),
(
"agents_functions_dir",
display_path(&Self::agents_functions_dir()?),
),
(
"agents_config_dir",
display_path(&Self::agents_config_dir()?),
),
("rags_dir", display_path(&Self::rags_dir()?)),
("sessions_dir", display_path(&self.sessions_dir()?)),
("messages_file", display_path(&self.messages_file()?)),
Expand Down Expand Up @@ -1798,3 +1792,7 @@ fn complete_option_bool(value: Option<bool>) -> Vec<String> {
None => vec!["true".to_string(), "false".to_string()],
}
}

fn convert_env_prefix(value: &str) -> String {
value.replace('-', "_").to_ascii_uppercase()
}