mirror of
https://github.com/slhaf/Partner.git
synced 2026-05-12 16:53:04 +08:00
refactor(chat): replace custom client with OpenAI runtime and remove file-based module prompt loading logic, prompt will be provided by each module
This commit is contained in:
@@ -2,10 +2,9 @@ package experimental;
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import work.slhaf.partner.api.chat.ChatClient;
|
||||
import work.slhaf.partner.api.chat.constant.ChatConstant;
|
||||
import work.slhaf.partner.api.chat.pojo.ChatResponse;
|
||||
import work.slhaf.partner.api.chat.pojo.Message;
|
||||
import work.slhaf.partner.api.chat.runtime.OpenAiChatRuntime;
|
||||
import work.slhaf.partner.common.util.ResourcesUtil;
|
||||
import work.slhaf.partner.module.common.model.ModelConstant;
|
||||
import work.slhaf.partner.module.modules.memory.selector.extractor.entity.ExtractorInput;
|
||||
@@ -16,46 +15,41 @@ import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SelfAwarenessTest {
|
||||
private static ChatClient getChatClient(String modelKey) {
|
||||
private static OpenAiChatRuntime getChatRuntime(String modelKey) {
|
||||
String model = "";
|
||||
String baseUrl = "";
|
||||
String apikey = "";
|
||||
ChatClient chatClient = new ChatClient(baseUrl, apikey, model);
|
||||
chatClient.setTop_p(0.7);
|
||||
chatClient.setTemperature(0.35);
|
||||
return chatClient;
|
||||
return new OpenAiChatRuntime(baseUrl, apikey, model);
|
||||
}
|
||||
|
||||
@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());
|
||||
OpenAiChatRuntime client = getChatRuntime(modelKey);
|
||||
String response = client.chat(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey, ModelConstant.Prompt.CORE), false);
|
||||
System.out.println(response);
|
||||
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);
|
||||
OpenAiChatRuntime client = getChatRuntime(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());
|
||||
String chatResponse = client.chat(chatMessages, false);
|
||||
System.out.println(chatResponse);
|
||||
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);
|
||||
OpenAiChatRuntime client = getChatRuntime(modelKey);
|
||||
List<Message> messages = new ArrayList<>(ResourcesUtil.Prompt.loadPromptWithSelfAwareness(modelKey, ModelConstant.Prompt.CORE));
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String input;
|
||||
@@ -66,12 +60,10 @@ public class SelfAwarenessTest {
|
||||
}
|
||||
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());
|
||||
String response = client.chat(messages, false);
|
||||
System.out.println("[OUTPUT]: " + response);
|
||||
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()));
|
||||
messages.add(new Message(ChatConstant.Character.ASSISTANT, response));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -89,7 +81,7 @@ public class SelfAwarenessTest {
|
||||
└── Python"
|
||||
""";
|
||||
String modelKey = "topic_extractor";
|
||||
ChatClient client = getChatClient(modelKey);
|
||||
OpenAiChatRuntime client = getChatRuntime(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()
|
||||
@@ -101,9 +93,8 @@ public class SelfAwarenessTest {
|
||||
.build();
|
||||
messages.add(new Message(ChatConstant.Character.USER, JSONUtil.toJsonPrettyStr(input)));
|
||||
|
||||
ChatResponse response = client.runChat(messages);
|
||||
System.out.println(response.getMessage());
|
||||
String response = client.chat(messages, false);
|
||||
System.out.println(response);
|
||||
System.out.println("\r\n----------\r\n");
|
||||
System.out.println(response.getUsageBean().toString());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user