From 80d3edbd43e874bb30decf1c5d13834a9a896007 Mon Sep 17 00:00:00 2001 From: sigoden Date: Sat, 13 Jul 2024 13:02:23 +0800 Subject: [PATCH] feat: agent instructions support `__TOOLS__` placeholder (#711) --- src/config/agent.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/config/agent.rs b/src/config/agent.rs index 7d1a43b..139b02d 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -11,6 +11,8 @@ use std::{ use serde::{Deserialize, Serialize}; +const TOOLS_PLACEHOLDER: &str = "__TOOLS__"; + #[derive(Debug, Clone, Serialize)] pub struct Agent { name: String, @@ -45,6 +47,7 @@ impl Agent { } else { Functions::default() }; + definition.replace_tools_placeholder(&functions); let agent_config = config .read() .agents @@ -279,6 +282,25 @@ impl AgentDefinition { } output } + + fn replace_tools_placeholder(&mut self, functions: &Functions) { + if self.instructions.contains(TOOLS_PLACEHOLDER) { + let tools = functions + .declarations() + .iter() + .enumerate() + .map(|(i, v)| { + let description = match v.description.split_once('\n') { + Some((v, _)) => v, + None => &v.description, + }; + format!("{}. {}: {description}", i + 1, v.name) + }) + .collect::>() + .join("\n"); + self.instructions = self.instructions.replace(TOOLS_PLACEHOLDER, &tools); + } + } } #[derive(Debug, Clone, Default, Deserialize, Serialize)]