浏览代码

【修复】优化提示词生成逻辑

- 仅在用户发送第一条消息时生成提示词
- 减少不必要的提示词生成调用,提高性能
PJ568 1 年之前
父节点
当前提交
2b884bae63
共有 3 个文件被更改,包括 17 次插入 和 15 次删除
  1. 1 1
      Cargo.lock
  2. 1 1
      Cargo.toml
  3. 15 13
      src/agent/mod.rs

+ 1 - 1
Cargo.lock

@@ -2433,7 +2433,7 @@ checksum = "1e9df38ee2d2c3c5948ea468a8406ff0db0b29ae1ffde1bcf20ef305bcc95c51"
 
 [[package]]
 name = "yaa"
-version = "2.2.2"
+version = "2.2.3"
 dependencies = [
  "actix-web",
  "anyhow",

+ 1 - 1
Cargo.toml

@@ -1,6 +1,6 @@
 [package]
 name = "yaa"
-version = "2.2.2"
+version = "2.2.3"
 edition = "2024"
 
 [dependencies]

+ 15 - 13
src/agent/mod.rs

@@ -49,21 +49,23 @@ pub async fn process_session(
     loop {
         // 当状态为“已中断”时,很可能是用户授权工具调用,直接跳到工具解析
         if matches!(session_data.status, SessionStatus::InProgress) {
-            // 1. 提示词生成(只有当最后一条消息是 user 的时才调用)
+            // 1. 提示词生成(仅有一条消息且只有当最后一条消息是 user 的时才调用)
             if let Some(last_msg) = session_data.messages.last() {
                 if matches!(last_msg.role, Role::User) {
-                    let prompt = PromptGenerator::generate_with_current_time(
-                        &last_msg.content,
-                        &session_data.character,
-                        &get_tools_info(tool_registry)[..],
-                        &session_data
-                            .config
-                            .yaa
-                            .language
-                            .as_deref()
-                            .unwrap_or("zh-CN"),
-                    );
-                    session_data.add_message(Role::System, prompt);
+                    if session_data.messages.len() == 1 {
+                        let prompt = PromptGenerator::generate_with_current_time(
+                            &last_msg.content,
+                            &session_data.character,
+                            &get_tools_info(tool_registry)[..],
+                            &session_data
+                                .config
+                                .yaa
+                                .language
+                                .as_deref()
+                                .unwrap_or("zh-CN"),
+                        );
+                        session_data.add_message(Role::System, prompt);
+                    }
                 }
             }