feat: Support extending the system prompt (#1167)

This commit is contained in:
Bradley Axen
2025-02-11 20:03:32 -08:00
committed by GitHub
parent a5e2419380
commit 6220ef054f
8 changed files with 88 additions and 1 deletions
+3
View File
@@ -28,4 +28,7 @@ pub trait Agent: Send + Sync {
/// Get the total usage of the agent
async fn usage(&self) -> Vec<ProviderUsage>;
/// Add custom text to be included in the system prompt
async fn extend_system_prompt(&mut self, extension: String);
}
+18 -1
View File
@@ -30,6 +30,7 @@ pub struct Capabilities {
resource_capable_extensions: HashSet<String>,
provider: Box<dyn Provider>,
provider_usage: Mutex<Vec<ProviderUsage>>,
system_prompt_extensions: Vec<String>,
}
/// A flattened representation of a resource used by the agent to prepare inference
@@ -88,6 +89,7 @@ impl Capabilities {
resource_capable_extensions: HashSet::new(),
provider,
provider_usage: Mutex::new(Vec::new()),
system_prompt_extensions: Vec::new(),
}
}
@@ -164,6 +166,11 @@ impl Capabilities {
Ok(())
}
/// Add a system prompt extension
pub fn add_system_prompt_extension(&mut self, extension: String) {
self.system_prompt_extensions.push(extension);
}
/// Get a reference to the provider
pub fn provider(&self) -> &dyn Provider {
&*self.provider
@@ -303,7 +310,17 @@ impl Capabilities {
context.insert("extensions", serde_json::to_value(extensions_info).unwrap());
context.insert("current_date_time", Value::String(current_date_time));
load_prompt_file("system.md", &context).expect("Prompt should render")
let base_prompt = load_prompt_file("system.md", &context).expect("Prompt should render");
if self.system_prompt_extensions.is_empty() {
base_prompt
} else {
format!(
"{}\n\n# Additional Instructions:\n\n{}",
base_prompt,
self.system_prompt_extensions.join("\n\n")
)
}
}
/// Find and return a reference to the appropriate client for a tool call
+5
View File
@@ -184,6 +184,11 @@ impl Agent for ReferenceAgent {
let capabilities = self.capabilities.lock().await;
capabilities.get_usage().await
}
async fn extend_system_prompt(&mut self, extension: String) {
let mut capabilities = self.capabilities.lock().await;
capabilities.add_system_prompt_extension(extension);
}
}
register_agent!("reference", ReferenceAgent);
+5
View File
@@ -292,6 +292,11 @@ impl Agent for TruncateAgent {
let capabilities = self.capabilities.lock().await;
capabilities.get_usage().await
}
async fn extend_system_prompt(&mut self, extension: String) {
let mut capabilities = self.capabilities.lock().await;
capabilities.add_system_prompt_extension(extension);
}
}
register_agent!("truncate", TruncateAgent);