Files
Partner/Partner-Core/src/test/java/SelfAwarenessTest.java
slhafzjw dd10b00fb6 推进核心服务注册机制,并调整了Partner的模块结构
- 为了方便调试,将项目分为两个子模块,demo模块中进行新机制的开发工作,core模块为原来的Partner项目;
- 新增了多个注解,用于适配新的核心服务注册机制;
- 在`CapabilityRegisterFactory`中,将首先启动`statusCheck`,检查各个注解是否正常工作,包括以下内容:
   - `CapabilityCore`核心服务与`Capability`接口是否匹配
   - 核心服务中的`CapabilityMethod`是否与`Capability`接口中的方法匹配
   - 是否存在待协调方法`ToCoordinatedMethod`以及对应的存在于`BaseCognationManager`子类实现中
2025-07-15 16:48:27 +08:00

111 lines
5.2 KiB
Java

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.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());
}
}