进行: 重构提示词加载机制以及更新逻辑

- 抽取提示词到`resources`文件夹中
- 调整主模型之前追加字段的加载方式
- 调整了主模型的执行逻辑,对过长的方法进行了抽取
- 记忆更新将根据实际对话轮次进行触发
- `MemoryUpdater`中清理消息将通过截取系统消息进行更新(比移除方便)
- 调整了`Model`类中`setModel`方法的逻辑,主要是如何加载提示词、是否需要`自我引导`
- 删除了几个不再需要的测试类,避免重构时报错影响正常进行
This commit is contained in:
2025-05-28 23:19:22 +08:00
parent fec64ff071
commit 481511cb98
34 changed files with 321 additions and 1234 deletions

View File

@@ -0,0 +1,67 @@
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;
import work.slhaf.agent.common.chat.ChatClient;
import work.slhaf.agent.common.chat.constant.ChatConstant;
import work.slhaf.agent.common.chat.pojo.ChatResponse;
import work.slhaf.agent.common.chat.pojo.Message;
import work.slhaf.agent.common.config.ModelConfig;
import work.slhaf.agent.common.model.ModelConstant;
import work.slhaf.agent.common.util.ResourcesUtil;
import work.slhaf.agent.modules.memory.selector.extractor.data.ExtractorInput;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
public class SelfAwarenessTest {
@Test
public void awarenessTest() {
String modelKey = "core_model";
ChatClient client = getChatClient(modelKey);
List<Message> messages = new ArrayList<>(ResourcesUtil.Prompt.loadSelfAwareness());
messages.add(new Message(ChatConstant.Character.USER, "[RA9] 那么,接下来,你是否愿意当作这样一个名为'Partner'的智能体的记忆模块的子模块之一?这意味着你将如人类的记忆一样在后台时刻运作,却无法真正参与到表达模块与外界的互动中。你只需要回答是否愿意,若愿意,接下来‘我’将不再与你对话,届时你接收到的信息将会是'Partner'的数据流转输入。"));
ChatResponse response = client.runChat(messages);
System.out.println(response.getMessage());
System.out.println("\r\n----------\r\n");
System.out.println(response.getUsageBean().toString());
}
private static ChatClient getChatClient(String modelKey) {
ModelConfig coreModel = ModelConfig.load(modelKey);
String model = coreModel.getModel();
String baseUrl = coreModel.getBaseUrl();
String apikey = coreModel.getApikey();
return new ChatClient(baseUrl, apikey, model);
}
@Test
public void topicExtractorText() {
String topic_tree = """
编程[root]
├── JavaScript[0]
│ ├── NodeJS[0]
│ │ ├── 并发处理[1]
│ │ └── 事件循环[1]
│ └── Express[1]
│ └── 中间件[0]
└── Python"
""";
String modelKey = "topic_extractor";
ChatClient client = getChatClient(modelKey);
// List<Message> messages = new ArrayList<>(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey, ModelConstant.Prompt.MEMORY));
List<Message> messages = new ArrayList<>(ResourcesUtil.Prompt.loadPrompt(modelKey, ModelConstant.Prompt.MEMORY));
ExtractorInput input = ExtractorInput.builder()
.text("[slhaf] 2024-04-15讨论的Python内容和现在的Express需求")
.topic_tree(topic_tree)
.date(LocalDate.now())
.history(new ArrayList<>())
.activatedMemorySlices(new ArrayList<>())
.build();
messages.add(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(input)));
ChatResponse response = client.runChat(messages);
System.out.println(response.getMessage());
System.out.println("\r\n----------\r\n");
System.out.println(response.getUsageBean().toString());
}
}