整个开发过程将分为三个关键部分逐步推进,以下是详细内容
1、调试大模型回复信息
2、对接微信机器人
3、整合1,2并测试整体流程
测试大模型回复信息
设置系统信息以及提前将用户对话发送给模型
@Bean("qwChatClient")
public ChatClient qwChatClient(ChatModel chatModel, LocalChatMemory localChatMemory) {
ChatClient weChatClient = ChatClient.builder(chatModel)
// 实现 Chat Memory 的 Advisor
// 在使用 Chat Memory 时,需要指定对话 ID,以便 Spring AI 处理上下文。
.defaultAdvisors(
new MessageChatMemoryAdvisor(localChatMemory, WechatEnums.WechatMemoryKey.getValue(), 100))
// 实现 Logger 的 Advisor
.defaultAdvisors(
new SimpleLoggerAdvisor()
)
// 设置 ChatClient 中 ChatModel 的 Options 参数
.defaultOptions(
DashScopeChatOptions.builder()
.withTopP(0.7)
.build()
)
.build();
try {
persona = getFilesWithPaths("data/人设/");
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
ChatClient.CallResponseSpec call = weChatClient.prompt().system(Files.readString(Path.of(SYSTEM_COMMON_PATH)))
.user(Files.readString(persona.get(wechatGirlProperty.getPersona()))).call();
log.debug(call.content());
} catch (IOException e) {
log.warn("请查看路径是否存在以及中文路径是否乱码!!!");
throw new RuntimeException(e);
}
return weChatClient;
}
对话人设:林悦.txt,详细描述了女友机器人的人物设定,为交互提供了角色背景。
公共配置:common.txt,包含一些通用的配置信息,为系统提供基础设置。
保存对话:chat_history.txt,用于记录与女友机器人的对话历史,方便后续分析和优化。
对接微信机器人
该步骤利用 wechatbox
对微信进行操作,实现与微信的交互功能。
public synchronized Client getInstance(ChatClient chatClient) {
if (chatClient==null){
this.chatClient = chatClient;
}
if (client == null) {
// 本地启动 RPC
client = new Client(); // 默认 10086 端口
// Client client = new Client(10088,true); // 也可以指定端口
// 是否已登录
log.info("isLogin: {}", client.isLogin());
// 登录账号 wxid
log.info("wxid: {}", client.getSelfWxid());
// 接收消息,并调用 printWxMsg 处理
client.enableRecvMsg(100);
thread = new Thread(() -> {
while (client.getIsReceivingMsg()) {
Wcf.WxMsg clientMsg = client.getMsg();
System.out.println("nxx:"+clientMsg.getContent());
String content = chatClient.prompt(clientMsg.getContent()).call().content();
int i = client.sendText(content, clientMsg.getSender(), "");
System.out.println("sender"+clientMsg.getSender());
System.out.println("getRoomid"+clientMsg.getRoomid());
System.out.println("id"+clientMsg.getId());
System.out.println("i"+i);
client.printWxMsg(clientMsg);
}
});
thread.start();
// client.diableRecvMsg(); // 需要停止时调用
client.keepRunning();
}
return client;
}
整合1,2并测试整体流程
将调试大模型回复信息和对接微信机器人两个步骤进行整合,并对整个系统进行测试。然而,目前测试结果显示,女友机器人的交互表现还有待提升,回话还是有些呆。