|
|
@@ -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(); |
|
|
|