Bladeren bron

yinruoxi

修改:
  1.优化大模型结构
dev_movie_demo
10710 1 jaar geleden
bovenliggende
commit
432886594c
3 gewijzigde bestanden met toevoegingen van 44 en 20 verwijderingen
  1. +34
    -16
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/netty/client/WebSocketClient.java
  2. +4
    -0
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/netty/server/config/ServerConfig.java
  3. +6
    -4
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java

+ 34
- 16
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/netty/client/WebSocketClient.java Bestand weergeven

@@ -1,10 +1,12 @@
package com.xueyi.nlt.netty.client;

import cn.hutool.core.lang.Snowflake;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.alibaba.nacos.shaded.com.google.gson.JsonArray;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xueyi.common.core.utils.core.IdUtil;
import com.xueyi.nlt.api.netty.domain.vo.DmWebSocketMessageVo;
import com.xueyi.nlt.netty.server.config.ServerConfig;
import com.xueyi.nlt.nlt.domain.vo.LlmQueryVo;
@@ -54,21 +56,21 @@ public class WebSocketClient extends WebSocketListener {
// public static String APIKEY = "7c217b3a313f4b66fcc14a8e97f85103";//从开放平台控制台中获取
// public static String APISecret = "ZTRiNDQwMTRlOTlmZDQwMDUwYTdjMDM0";//从开放平台控制台中获取

public static WebSocket webSocket;
public static String APPID = "948cf4b6";//从开放平台控制台中获取
public static String APIKEY = "54f6e81f40a31d66d976496de895a7a4";//从开放平台控制台中获取
public static String APISecret = "ZDYyMjNmMTlkYTE0YWRmOWUwZTYxNjYz";//从开放平台控制台中获取
public WebSocket webSocket;
public static final Gson json = new Gson();
// public static String question = "假设你是一位前台,你需要通过与其他人对话来获取会议相关信息,已知今天是2023-7-19,你需要获取会议日期,开始时间,持续时间,会议地点,会议主题。时间用类似00:00的格式输出。对方的话中可能不包含全部信息,对于未知的信息填充为none。如果所有信息都已知那么commit为true。否则为false。将你获得的信息输出为json格式。对方的话是:“明天下午开个会。从两点开到下午三点,在大会议室开,主题是访客接待”,只输出最后的json。输出只有一行,输出格式为{date:,start_time:,duration:,location:,theme:commit:}。";//可以修改question 内容,来向模型提问
public static String question = "请帮我安排五一出行计划";//可以修改question 内容,来向模型提问
public static String systemRole = "";
public static List<String> questions = new ArrayList<>();//可以修改question 内容,来向模型提问

public static boolean stream = false;
public static String curUserId = null;
// 定义内存共享变量traceId
public Long traceId;
public String question = "请帮我安排五一出行计划";//可以修改question 内容,来向模型提问
public String systemRole = "";
public List<String> questions = new ArrayList<>();//可以修改question 内容,来向模型提问

public boolean stream = false;
public String curUserId = null;
public String answer = "";
public String answerBuf = "";


@PostConstruct
public void init() {
INSTANCE = this;
@@ -133,8 +135,8 @@ public class WebSocketClient extends WebSocketListener {
this.sendMsg(messages,false, null);
}
public void sendMsg(List<String> messages,boolean stream,String userId){
WebSocketClient.stream = stream;
WebSocketClient.curUserId = userId;
this.stream = stream;
this.curUserId = userId;
if (messages.size() / 2 > 0) {
systemRole = messages.get(0);
messages.remove(0);
@@ -148,7 +150,15 @@ public class WebSocketClient extends WebSocketListener {
OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
String url = authUrl.replace("https://","wss://").replace("http://","ws://");
Request request = new Request.Builder().url(url).build();
webSocket = okHttpClient.newWebSocket(request,new WebSocketClient());
WebSocketClient wsc = new WebSocketClient();
wsc.stream = stream;
wsc.curUserId = userId;
wsc.questions = questions;
wsc.question = question;
wsc.systemRole = systemRole;
wsc.traceId = IdUtil.getSnowflakeNextId();
ServerConfig.currentTraceId = wsc.traceId;
webSocket = okHttpClient.newWebSocket(request,wsc);

} catch (Exception e) {
e.printStackTrace();
@@ -263,6 +273,9 @@ public class WebSocketClient extends WebSocketListener {
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
System.out.println("text:\n" + text);
if (ServerConfig.currentTraceId != null && ServerConfig.currentTraceId != traceId) {
return;
}
ResponseData responseData = json.fromJson(text,ResponseData.class);
synchronized (LOCK) {
try {
@@ -328,19 +341,23 @@ public class WebSocketClient extends WebSocketListener {

// webSocket.close(3,"客户端主动断开链接");
//webSocket.close(1000,"客户端主动断开链接");

}
if (WebSocketClient.stream && !StringUtils.isEmpty(curUserId)) {
if (this.stream && !StringUtils.isEmpty(curUserId)) {
Channel ch = ServerConfig.sessionMap.get(curUserId);
if (ch != null) {
List<String> ttsList = new ArrayList<>();
JSONObject jo = new JSONObject();
jo.put("action","chat");
jo.put("motion","idle");
jo.put("traceId",traceId);
//去除转义符
answerBuf = answerBuf.replaceAll("[\\r\\n]", "");
//去除引号
answerBuf = answerBuf.replaceAll("\"", "");
// 处理answer,如果包含"。",则将"。"之前的内容发送给前端
while(answerBuf.contains("。") || answerBuf.contains("?") || answerBuf.contains("!") ||
answerBuf.contains("?") || answerBuf.contains("!")) {
String[] temp = answerBuf.split("。|?|!|\\.|\\?|\\!");
String[] temp = answerBuf.split("。|?|!|\\?|\\!");
ttsList.add(temp[0] + answerBuf.charAt(temp[0].length()));
answerBuf = answerBuf.substring(temp[0].length() + 1);
}
@@ -373,6 +390,7 @@ public class WebSocketClient extends WebSocketListener {
JSONObject jo = new JSONObject();
jo.put("action","chat");
jo.put("motion","idle");
jo.put("traceId",traceId);
jo.put("status",2);
jo.put("tts","。");
String str = jo.toJSONString();


+ 4
- 0
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/netty/server/config/ServerConfig.java Bestand weergeven

@@ -1,5 +1,7 @@
package com.xueyi.nlt.netty.server.config;

import com.alibaba.fastjson2.JSONObject;
import com.xueyi.nlt.netty.client.WebSocketClient;
import io.netty.channel.Channel;

import java.util.concurrent.ConcurrentHashMap;
@@ -8,4 +10,6 @@ public class ServerConfig {

public static ConcurrentHashMap<String, Channel> sessionMap = new ConcurrentHashMap<>();

public static volatile Long currentTraceId;

}

+ 6
- 4
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/FreeChatTemplate.java Bestand weergeven

@@ -44,7 +44,7 @@ public class FreeChatTemplate implements BaseTemplate{
}
size = redisTemplate.opsForList().size(redisKey);
List<String> context = new ArrayList<>();
context.add("你是一位公司前台事务的专家。");
context.add("你是缔智元公司的前台,你叫小酷,你是一位数字人。");
context.addAll(redisTemplate.opsForList().range(redisKey,size-6,size));

context.add(content);
@@ -63,7 +63,7 @@ public class FreeChatTemplate implements BaseTemplate{
result = result.replaceAll("认知模型", "数字员工");
result = result.replaceAll("认知智能模型", "数字员工");
if (result.equals("-1")) {
result = "这个问题超出了我无法回答,您可以提出更多关于公司相关问题。";
result = "这个问题超出了我无法回答,您可以提出更多关于电影相关问题。";
}

if(!StringUtils.isEmpty(result)){
@@ -88,10 +88,12 @@ public class FreeChatTemplate implements BaseTemplate{
}
size = redisTemplate.opsForList().size(redisKey);
List<String> context = new ArrayList<>();
context.add("你是一位电影知识十分渊博的数字人,你来自缔智元公司,你叫小酷,你说话很简短,只有两句话,30字左右。");
context.addAll(redisTemplate.opsForList().range(redisKey,size-6,size));
context.add("你是缔智元公司的前台,你叫小酷,你是一位数字人。");
context.addAll(redisTemplate.opsForList().range(redisKey,size-2,size));

context.add(content);
//使用stream去除context列表中所有字符串中的引号
context = context.stream().map(s -> s.replaceAll("\"", "")).collect(java.util.stream.Collectors.toList());

//webSocketClient.sendMsg(context);



Laden…
Annuleren
Opslaan