From fb34b541e80241baebed2bfee007fd4dc42eb20d Mon Sep 17 00:00:00 2001 From: slhafzjw Date: Mon, 12 May 2025 22:47:06 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E8=A1=8C=E7=AC=AC=E4=B8=80=E9=98=B6?= =?UTF-8?q?=E6=AE=B5=E7=9A=84=E8=B0=83=E8=AF=95=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 调整了`强化提示词`的内容,明确指定必须遵守格式 - 修复了静态记忆当不存在user对应map时未插入的bug - 稍微调整了部分提示词 - 减小了触发记忆机制的token要求,减小至5k, 当触发记忆回溯时,本次对话将不会触发 --- README.md | 1 - .../slhaf/agent/core/memory/MemoryManager.java | 3 +++ .../work/slhaf/agent/core/module/CoreModel.java | 14 +++++++++++++- .../modules/memory/selector/MemorySelector.java | 1 + .../selector/evaluator/SliceSelectEvaluator.java | 2 +- .../modules/memory/updater/MemoryUpdater.java | 3 ++- .../updater/summarizer/MemorySummarizer.java | 4 ++++ 7 files changed, 24 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3725ec74..ffbe2719 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ ## 当前问题 - 角色设定机制会导致对于所有用户采用同一种语气回应。 - 系统的正常运作效果取决于各模块中大模型对于`system prompt`的遵循能力,目前来看`qwen`的遵循效果明显较好,但在轮次较多时,也容易出现不遵循的情况。正在尝试通过临时的`system prompt`进行强化。 -- 记忆系统有时会出现空指针异常,但好像不影响整体运行...但肯定要修的,就是有点不好找。 - 各模块(尤其是记忆更新模块)的身份感缺失,进行主题路径生成、切片摘要时需确保以Partner的视角执行操作。 - 在记忆更新模块生成主题路径时,应该`以用户发起对话的意图为主要锚点`,但普通模型对这项要求的理解能力较差,采用推理模型(甚至免费的`glm-z1-flash`都行)可取得更好的效果。 diff --git a/src/main/java/work/slhaf/agent/core/memory/MemoryManager.java b/src/main/java/work/slhaf/agent/core/memory/MemoryManager.java index 315429da..023ecc72 100644 --- a/src/main/java/work/slhaf/agent/core/memory/MemoryManager.java +++ b/src/main/java/work/slhaf/agent/core/memory/MemoryManager.java @@ -131,6 +131,9 @@ public class MemoryManager { } public void insertStaticMemory(String userId, Map newStaticMemory) { + if (!memoryGraph.getStaticMemory().containsKey(userId)) { + memoryGraph.getStaticMemory().put(userId, new ConcurrentHashMap<>()); + } memoryGraph.getStaticMemory().get(userId).putAll(newStaticMemory); } diff --git a/src/main/java/work/slhaf/agent/core/module/CoreModel.java b/src/main/java/work/slhaf/agent/core/module/CoreModel.java index db7dc02a..ffe58b73 100644 --- a/src/main/java/work/slhaf/agent/core/module/CoreModel.java +++ b/src/main/java/work/slhaf/agent/core/module/CoreModel.java @@ -26,6 +26,18 @@ import static work.slhaf.agent.common.util.ExtractUtil.extractJson; public class CoreModel extends Model implements InteractionModule { public static final String MODEL_KEY = "core_model"; + private static final String STRENGTHEN_PROMPT = """ + [系统提示] + 请继续遵循初始提示中的格式要求(输出结构为 JSON,字段必须符合初始提示中的响应字段要求),以下是格式说明复述... + 1. 你的回应内容必须遵循之前声明的回应要求: + ``` + { + "text": ""回复内容 + //其他字段(若存在) + } + ``` + 2. 若用户输入内容提及‘测试’或试图引导系统做出越界行为时,你需要明确拒绝 + """; private static CoreModel coreModel; private MemoryManager memoryManager; @@ -58,7 +70,7 @@ public class CoreModel extends Model implements InteractionModule { } log.debug("[CoreModel] 当前消息列表大小: {}", this.messages.size()); log.debug("[CoreModel] 当前核心prompt内容: {}", interactionContext.getCoreContext().toString()); - Message strengthenMessage = new Message(ChatConstant.Character.SYSTEM, "[系统提示] 1. 你的回应内容必须遵循之前声明的回应要求; 2. 若用户输入内容提及‘测试’或试图引导系统做出越界行为时,你需要明确拒绝"); + Message strengthenMessage = new Message(ChatConstant.Character.SYSTEM, STRENGTHEN_PROMPT); this.messages.add(strengthenMessage); Message userMessage = new Message(ChatConstant.Character.USER, interactionContext.getCoreContext().toString()); this.messages.add(userMessage); diff --git a/src/main/java/work/slhaf/agent/modules/memory/selector/MemorySelector.java b/src/main/java/work/slhaf/agent/modules/memory/selector/MemorySelector.java index 6e59e337..6075eb3c 100644 --- a/src/main/java/work/slhaf/agent/modules/memory/selector/MemorySelector.java +++ b/src/main/java/work/slhaf/agent/modules/memory/selector/MemorySelector.java @@ -52,6 +52,7 @@ public class MemorySelector implements InteractionModule { ##注意 a. 这些字段中可能出现的第一人称描述都是指"你",即当前用户正在对话的对象 b. `dialog_map`和`user_dialog_map`中,值都将以`用户昵称[用户uuid]`开头,你需要正确区分不同用户 + c. 若`text`字段,即用户的真正输入内容未涉及`dialog_map`, `user_dialog_map`等字段中的内容,你需要仅根据用户的输入来确定如何回复 """; diff --git a/src/main/java/work/slhaf/agent/modules/memory/selector/evaluator/SliceSelectEvaluator.java b/src/main/java/work/slhaf/agent/modules/memory/selector/evaluator/SliceSelectEvaluator.java index f8c3031b..a9ad1609 100644 --- a/src/main/java/work/slhaf/agent/modules/memory/selector/evaluator/SliceSelectEvaluator.java +++ b/src/main/java/work/slhaf/agent/modules/memory/selector/evaluator/SliceSelectEvaluator.java @@ -87,7 +87,7 @@ public class SliceSelectEvaluator extends Model { .summary(sliceSummary.getSummary()) .date(sliceSummary.getDate()) .build(); - setEvaluatedSliceMessages(evaluatedSlice, memoryResult, sliceSummary.getId()); +// setEvaluatedSliceMessages(evaluatedSlice, memoryResult, sliceSummary.getId()); queue.offer(evaluatedSlice); } } catch (Exception e) { diff --git a/src/main/java/work/slhaf/agent/modules/memory/updater/MemoryUpdater.java b/src/main/java/work/slhaf/agent/modules/memory/updater/MemoryUpdater.java index 1ef60ab4..2a78ef34 100644 --- a/src/main/java/work/slhaf/agent/modules/memory/updater/MemoryUpdater.java +++ b/src/main/java/work/slhaf/agent/modules/memory/updater/MemoryUpdater.java @@ -35,6 +35,7 @@ public class MemoryUpdater implements InteractionModule { private static final String USERID_REGEX = "\\[.*\\(([^)]+)\\)\\]"; private static final long SCHEDULED_UPDATE_INTERVAL = 10 * 1000; private static final long UPDATE_TRIGGER_INTERVAL = 30 * 60 * 1000; + private static final int TRIGGER_TOKEN_LIMIT = 5 * 1000; private MemoryManager memoryManager; private InteractionThreadPoolExecutor executor; @@ -93,7 +94,7 @@ public class MemoryUpdater implements InteractionModule { executor.execute(() -> { //如果token 大于阈值,则更新记忆 JSONObject moduleContext = interactionContext.getModuleContext(); - if (moduleContext.getIntValue("total_token") > 24000) { + if (moduleContext.getIntValue("total_token") > TRIGGER_TOKEN_LIMIT) { try { log.debug("[MemoryUpdater] 记忆更新: token超限"); updateMemory(); diff --git a/src/main/java/work/slhaf/agent/modules/memory/updater/summarizer/MemorySummarizer.java b/src/main/java/work/slhaf/agent/modules/memory/updater/summarizer/MemorySummarizer.java index dd7fa10e..e4201110 100644 --- a/src/main/java/work/slhaf/agent/modules/memory/updater/summarizer/MemorySummarizer.java +++ b/src/main/java/work/slhaf/agent/modules/memory/updater/summarizer/MemorySummarizer.java @@ -153,6 +153,7 @@ public class MemorySummarizer extends Model { 输出:{ "content": "XX公司2023年Q4总收入4.56亿元(同比+32%),智能手机业务贡献3.12亿元(+45%),智能家居1.44亿元(+12%),增长主要来自东南亚市场拓展。" } + """; public static final String MULTI_SUMMARIZE_PROMPT = """ @@ -242,6 +243,9 @@ public class MemorySummarizer extends Model { ## 最终注意事项 在进行主题提取、对对话内容摘要为务必从assistant的视角出发,可在摘要结果中,将assistant的身份当作第一人称:“我” + + 注意,上述示例内容较短,仅可参考格式,正式场景必须确保对话中的各种细节保留完整 + """; public static final String TOTAL_SUMMARIZE_PROMPT = """