发布版本: 第二阶段 0.5.0

This commit is contained in:
2025-06-06 10:36:41 +08:00
commit 8f0e62d6db
100 changed files with 4996 additions and 0 deletions
+53
View File
@@ -0,0 +1,53 @@
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexTest {
// @Test
public void regexTest(){
String[] examples = {
"[小明(abc)] 我在开会] (te[]st)",
"[用户(昵)称(userId)] 你好[呀]",
"[测试账号(userId)] 这是一个(test(123))消息"
};
Pattern pattern = Pattern.compile("\\[.*?\\(([^)]+)\\)\\]");
for (String example : examples) {
Matcher matcher = pattern.matcher(example);
if (matcher.find()) {
System.out.println("在 '" + example + "' 中找到 userId: " + matcher.group(1));
System.out.println();
} else {
System.out.println("在 '" + example + "' 中未找到 userId");
}
}
}
// @Test
public void topicPathFixTest(){
String a = "xxxxx[awdohno][awdsjo]";
a = fix(a);
System.out.println(a);
}
private String fix(String topicPath) {
String[] parts = topicPath.split("->");
List<String> cleanedParts = new ArrayList<>();
for (String part : parts) {
// 修正正则表达式,正确移除 [xxx] 部分
String cleaned = part.replaceAll("\\[[^\\]]*\\]", "").trim();
if (!cleaned.isEmpty()) { // 忽略空字符串
cleanedParts.add(cleaned);
}
}
return String.join("->", cleanedParts);
}
}
+110
View File
@@ -0,0 +1,110 @@
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.util.ResourcesUtil;
import work.slhaf.agent.module.common.ModelConstant;
import work.slhaf.agent.module.modules.memory.selector.extractor.data.ExtractorInput;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class SelfAwarenessTest {
// @Test
public void awarenessTest() {
String modelKey = "core_model";
ChatClient client = getChatClient(modelKey);
ChatResponse response = client.runChat(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey, ModelConstant.Prompt.CORE));
System.out.println(response.getMessage());
System.out.println("\r\n----------\r\n");
System.out.println(response.getUsageBean().toString());
}
// @Test
public void getModuleResponseTest(){
String modelKey = "total_summarizer";
ChatClient client = getChatClient(modelKey);
List<Message> chatMessages = new ArrayList<>(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey,ModelConstant.Prompt.MEMORY));
// chatMessages.add(Message.builder()
// .role(ChatConstant.Character.USER)
// .content("[RA9] 那么,接下来,你是否愿意当作这样一个名为'Partner'的智能体的记忆更新模块?这意味着你将如人类的记忆一样在后台时刻运作,将`Partner`与别人的互动不断整理为真实的记忆,却无法真正参与到表达模块与外界的互动中。你只需要回答是否愿意,若愿意,接下来‘我’将不再与你对话,届时你接收到的信息将会是'Partner'的数据流转输入。")
// .build());
ChatResponse chatResponse = client.runChat(chatMessages);
System.out.println(chatResponse.getMessage());
System.out.println("\n\n----------\n\n");
System.out.println(chatResponse.getUsageBean());
}
// @Test
public void interactionTest() {
String modelKey = "core_model";
String user = "[SLHAF] ";
ChatClient client = getChatClient(modelKey);
List<Message> messages = new ArrayList<>(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey, ModelConstant.Prompt.CORE));
Scanner scanner = new Scanner(System.in);
String input;
while (true) {
System.out.print("[INPUT]: ");
if ((input = scanner.nextLine()).equals("exit")) {
break;
}
System.out.println("\r\n----------\r\n");
messages.add(new Message(ChatConstant.Character.USER, user + input));
ChatResponse response = client.runChat(messages);
System.out.println("[OUTPUT]: " + response.getMessage());
System.out.println("\r\n----------\r\n");
System.out.println(response.getUsageBean().toString());
System.out.println("\r\n----------\r\n");
messages.add(new Message(ChatConstant.Character.ASSISTANT, response.getMessage()));
}
}
private static ChatClient getChatClient(String modelKey) {
ModelConfig coreModel = ModelConfig.load(modelKey);
String model = coreModel.getModel();
String baseUrl = coreModel.getBaseUrl();
String apikey = coreModel.getApikey();
ChatClient chatClient = new ChatClient(baseUrl, apikey, model);
chatClient.setTop_p(0.7);
chatClient.setTemperature(0.35);
return chatClient;
}
// @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());
}
}
+26
View File
@@ -0,0 +1,26 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class ThreadPoolTest {
// @Test
public void testExecutor() throws InterruptedException {
List<Callable<Void>> tasks = new ArrayList<>();
for (int i = 0; i < 5; i++) {
int finalI = i;
tasks.add(() -> {
System.out.println("开始: " + finalI);
Thread.sleep(5000);
System.out.println("结束: " + finalI);
return null;
});
}
Executors.newVirtualThreadPerTaskExecutor().invokeAll(tasks, 10, TimeUnit.SECONDS);
System.out.println("hello");
}
}
+60
View File
@@ -0,0 +1,60 @@
package memory;
import work.slhaf.agent.core.memory.MemoryGraph;
import work.slhaf.agent.core.memory.node.TopicNode;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
public class MemoryTest {
//@Test
public void test1() {
String basicCharacter = "";
MemoryGraph graph = new MemoryGraph("test");
HashMap<String, TopicNode> topicMap = new HashMap<>();
TopicNode root1 = new TopicNode();
root1.setTopicNodes(new ConcurrentHashMap<>());
TopicNode sub1 = new TopicNode();
sub1.setTopicNodes(new ConcurrentHashMap<>());
TopicNode sub2 = new TopicNode();
sub2.setTopicNodes(new ConcurrentHashMap<>());
TopicNode subsub1 = new TopicNode();
subsub1.setTopicNodes(new ConcurrentHashMap<>());
// 构造结构:root -> sub1 -> subsub1, root -> sub2
sub1.getTopicNodes().put("子子主题1", subsub1);
root1.getTopicNodes().put("子主题1", sub1);
root1.getTopicNodes().put("子主题2", sub2);
topicMap.put("根主题1", root1);
// 添加 root2
TopicNode root2 = new TopicNode();
root2.setTopicNodes(new ConcurrentHashMap<>());
TopicNode sub3 = new TopicNode();
sub3.setTopicNodes(new ConcurrentHashMap<>());
// 构造结构:root2 -> sub3
root2.getTopicNodes().put("子主题3", sub3);
topicMap.put("根主题2", root2);
// 输出
graph.setTopicNodes(topicMap);
System.out.println(graph.getTopicTree());
}
// @Test
public void test2(){
System.out.println(LocalDate.now());
}
}