diff options
| author | sigoden <sigoden@gmail.com> | 2024-07-30 06:20:30 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-30 06:20:30 +0800 |
| commit | 4c18e91165269970747bd2734b9b5db013ade2c2 (patch) | |
| tree | 33aeb63a679a8964e61f770f1bbdad532b7efe79 /src | |
| parent | 44aac171b79835048ae09150d392a683930077e1 (diff) | |
| download | aichat-4c18e91165269970747bd2734b9b5db013ade2c2.tar.gz | |
refactor: agent instructions (#764)
- replace `__TOOLS__` with `{{__tools__}}`
- after `.clear messages`, update system message of agent session
- show interpolated instructions in agent info
Diffstat (limited to 'src')
| -rw-r--r-- | src/config/agent.rs | 17 | ||||
| -rw-r--r-- | src/config/mod.rs | 3 | ||||
| -rw-r--r-- | src/config/session.rs | 4 |
3 files changed, 18 insertions, 6 deletions
diff --git a/src/config/agent.rs b/src/config/agent.rs index 5f36097..5f78e4b 100644 --- a/src/config/agent.rs +++ b/src/config/agent.rs @@ -11,8 +11,6 @@ use std::{ use serde::{Deserialize, Serialize}; -const TOOLS_PLACEHOLDER: &str = "__TOOLS__"; - #[derive(Debug, Clone, Serialize)] pub struct Agent { name: String, @@ -94,7 +92,9 @@ impl Agent { } pub fn export(&self) -> Result<String> { - let mut value = serde_json::json!(self); + let mut agent = self.clone(); + agent.definition.instructions = self.interpolated_instructions(); + let mut value = serde_json::json!(agent); value["functions_dir"] = Config::agent_functions_dir(&self.name)? .display() .to_string() @@ -135,6 +135,10 @@ impl Agent { &self.definition.conversation_starters } + pub fn interpolated_instructions(&self) -> String { + self.definition.interpolated_instructions() + } + pub fn variables(&self) -> &[AgentVariable] { &self.definition.variables } @@ -154,7 +158,7 @@ impl Agent { impl RoleLike for Agent { fn to_role(&self) -> Role { - let prompt = self.definition.interpolated_instructions(); + let prompt = self.interpolated_instructions(); let mut role = Role::new("", &prompt); role.sync(self); role @@ -286,7 +290,8 @@ impl AgentDefinition { } fn replace_tools_placeholder(&mut self, functions: &Functions) { - if self.instructions.contains(TOOLS_PLACEHOLDER) { + let tools_placeholder: &str = "{{__tools__}}"; + if self.instructions.contains(tools_placeholder) { let tools = functions .declarations() .iter() @@ -300,7 +305,7 @@ impl AgentDefinition { }) .collect::<Vec<String>>() .join("\n"); - self.instructions = self.instructions.replace(TOOLS_PLACEHOLDER, &tools); + self.instructions = self.instructions.replace(tools_placeholder, &tools); } } } diff --git a/src/config/mod.rs b/src/config/mod.rs index c9a4a73..8f719f4 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -861,6 +861,9 @@ impl Config { pub fn clear_session_messages(&mut self) -> Result<()> { if let Some(session) = self.session.as_mut() { session.clear_messages(); + if let Some(prompt) = self.agent.as_ref().map(|v| v.interpolated_instructions()) { + session.update_role_prompt(&prompt); + } } else { bail!("No session") } diff --git a/src/config/session.rs b/src/config/session.rs index fe94aee..eccffa9 100644 --- a/src/config/session.rs +++ b/src/config/session.rs @@ -249,6 +249,10 @@ impl Session { self.dirty = true; } + pub fn update_role_prompt(&mut self, prompt: &str) { + self.role_prompt = prompt.to_string(); + } + pub fn clear_role(&mut self) { self.role_name.clear(); self.role_prompt.clear(); |
