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: export agent variable as LLM_AGENT_VAR_* #766

Merged
merged 1 commit into from
Jul 30, 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
8 changes: 2 additions & 6 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl Config {
}

pub fn agent_config_dir(name: &str) -> Result<PathBuf> {
match env::var(format!("{}_CONFIG_DIR", convert_env_prefix(name))) {
match env::var(format!("{}_CONFIG_DIR", normalize_env_name(name))) {
Ok(value) => Ok(PathBuf::from(value)),
Err(_) => Ok(Self::agents_config_dir()?.join(name)),
}
Expand All @@ -365,7 +365,7 @@ impl Config {
}

pub fn agent_functions_dir(name: &str) -> Result<PathBuf> {
match env::var(format!("{}_FUNCTIONS_DIR", convert_env_prefix(name))) {
match env::var(format!("{}_FUNCTIONS_DIR", normalize_env_name(name))) {
Ok(value) => Ok(PathBuf::from(value)),
Err(_) => Ok(Self::agents_functions_dir()?.join(name)),
}
Expand Down Expand Up @@ -1904,7 +1904,3 @@ 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()
}
28 changes: 24 additions & 4 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,44 @@ impl ToolCall {

pub fn eval(&self, config: &GlobalConfig) -> Result<Value> {
let function_name = self.name.clone();
let (call_name, cmd_name, mut cmd_args) = match &config.read().agent {
let (call_name, cmd_name, mut cmd_args, mut envs) = match &config.read().agent {
Some(agent) => match agent.functions().find(&function_name) {
Some(function) => {
if function.agent {
let envs: HashMap<String, String> = agent
.variables()
.iter()
.map(|v| {
(
format!("LLM_AGENT_VAR_{}", normalize_env_name(&v.name)),
v.value.clone(),
)
})
.collect();
(
format!("{}:{}", agent.name(), function_name),
agent.name().to_string(),
vec![function_name],
envs,
)
} else {
(function_name.clone(), function_name, vec![])
(
function_name.clone(),
function_name,
vec![],
Default::default(),
)
}
}
None => bail!("Unexpected call {function_name} {}", self.arguments),
},
None => match config.read().functions.contains(&function_name) {
true => (function_name.clone(), function_name, vec![]),
true => (
function_name.clone(),
function_name,
vec![],
Default::default(),
),
false => bail!("Unexpected call: {function_name} {}", self.arguments),
},
};
Expand All @@ -193,7 +214,6 @@ impl ToolCall {
cmd_args.push(json_data.to_string());
let prompt = format!("Call {cmd_name} {}", cmd_args.join(" "));

let mut envs = HashMap::new();
let bin_dir = Config::functions_bin_dir()?;
if bin_dir.exists() {
envs.insert("PATH".into(), prepend_env_path(&bin_dir)?);
Expand Down
4 changes: 4 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ pub fn get_env_name(key: &str) -> String {
format!("{}_{key}", env!("CARGO_CRATE_NAME"),).to_ascii_uppercase()
}

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

pub fn estimate_token_length(text: &str) -> usize {
let words: Vec<&str> = text.unicode_words().collect();
let mut output: f32 = 0.0;
Expand Down