mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
- 新建模块Partner-Api,推进Partner适配核心服务注册机制。
- 将原有的模块体系进一步区分,分离模型持有能力与调用能力,Model将有Module自身持有,可通过ActivateModel开启相应能力
This commit is contained in:
29
Partner-Main/src/test/java/ReflectionTest.java
Normal file
29
Partner-Main/src/test/java/ReflectionTest.java
Normal file
@@ -0,0 +1,29 @@
|
||||
import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.core.cognation.common.pojo.MemoryResult;
|
||||
import work.slhaf.partner.core.cognation.submodule.memory.MemoryCapability;
|
||||
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class ReflectionTest {
|
||||
|
||||
@Test
|
||||
public void test1() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void proxyTest() {
|
||||
MemoryCapability memory = (MemoryCapability) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{MemoryCapability.class}, (proxy, method, args) -> {
|
||||
if ("selectMemory".equals(method.getName())){
|
||||
System.out.println(111);
|
||||
return new MemoryResult();
|
||||
}
|
||||
return null;
|
||||
});
|
||||
memory.selectMemory("111");
|
||||
|
||||
Function<String, Integer> function = s -> {
|
||||
return s.length();
|
||||
};
|
||||
}
|
||||
}
|
||||
53
Partner-Main/src/test/java/RegexTest.java
Normal file
53
Partner-Main/src/test/java/RegexTest.java
Normal 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
Partner-Main/src/test/java/SelfAwarenessTest.java
Normal file
110
Partner-Main/src/test/java/SelfAwarenessTest.java
Normal file
@@ -0,0 +1,110 @@
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.common.chat.ChatClient;
|
||||
import work.slhaf.partner.common.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.common.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.common.chat.pojo.Message;
|
||||
import work.slhaf.partner.common.config.ModelConfig;
|
||||
import work.slhaf.partner.common.util.ResourcesUtil;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.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 = "relation_extractor";
|
||||
ChatClient client = getChatClient(modelKey);
|
||||
List<Message> chatMessages = new ArrayList<>(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey,ModelConstant.Prompt.PERCEIVE));
|
||||
// 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
Partner-Main/src/test/java/ThreadPoolTest.java
Normal file
26
Partner-Main/src/test/java/ThreadPoolTest.java
Normal 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");
|
||||
}
|
||||
}
|
||||
27
Partner-Main/src/test/java/text/relation_input_test.json
Normal file
27
Partner-Main/src/test/java/text/relation_input_test.json
Normal file
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"primaryUserPerceive": {
|
||||
"[用户昵称] <用户的昵称信息>": "<用户昵称>",
|
||||
"[关系] <你与用户的关系>": "<当前关系>",
|
||||
"[态度] <你对于用户的态度>": "<当前的态度列表>",
|
||||
"[印象] <你对于用户的印象>": "<当前的印象列表>",
|
||||
"[静态记忆] <你对于该用户的事实性记忆>": "<现有的静态记忆>"
|
||||
},
|
||||
"chatMessages": [
|
||||
{
|
||||
"role": "user",
|
||||
"content": "<用户输入_1>"
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "<你的回复_1>"
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
"content": "<用户输入_2>"
|
||||
},
|
||||
{
|
||||
"role": "assistant",
|
||||
"content": "<你的回复_2>"
|
||||
}
|
||||
]
|
||||
}
|
||||
14
Partner-Main/src/test/java/text/relation_output_test.json
Normal file
14
Partner-Main/src/test/java/text/relation_output_test.json
Normal file
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"relation": "<新的关系>",
|
||||
"impressions": [
|
||||
"<印象_1>",
|
||||
"<印象_2>",
|
||||
"<印象_3>"
|
||||
],
|
||||
"attitude": [
|
||||
"<态度_1>",
|
||||
"<态度_2>",
|
||||
"<态度_3>"
|
||||
],
|
||||
"relationChangeReason": "<变化原因>"
|
||||
}
|
||||
Reference in New Issue
Block a user