|
|
|
@@ -135,10 +135,10 @@ public class WebSocketClient extends WebSocketListener { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
public void sendMsg(List<String> messages){ |
|
|
|
this.sendMsg(messages,false, null); |
|
|
|
public WebSocketClient sendMsg(List<String> messages){ |
|
|
|
return this.sendMsg(messages,false, "null"); |
|
|
|
} |
|
|
|
public void sendMsg(List<String> messages,boolean stream,String userId){ |
|
|
|
public WebSocketClient sendMsg(List<String> messages,boolean stream,String userId){ |
|
|
|
this.stream = stream; |
|
|
|
this.curUserId = userId; |
|
|
|
if (messages.size() / 2 > 0) { |
|
|
|
@@ -164,9 +164,10 @@ public class WebSocketClient extends WebSocketListener { |
|
|
|
ServerConfig.currentTraceMap.put(curUserId,wsc.traceId); |
|
|
|
System.out.println("wocket客户端:" + wsc.hashCode()); |
|
|
|
webSocket = okHttpClient.newWebSocket(request,wsc); |
|
|
|
|
|
|
|
return wsc; |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
//鉴权url |
|
|
|
@@ -283,148 +284,155 @@ public class WebSocketClient extends WebSocketListener { |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
ResponseData responseData = json.fromJson(text,ResponseData.class); |
|
|
|
try { |
|
|
|
// System.out.println("code:\n" + responseData.getHeader().get("code")); |
|
|
|
if (0 == responseData.getHeader().get("code").getAsInt()) { |
|
|
|
System.out.println("###########"); |
|
|
|
System.out.println("getStatus: " + responseData.getHeader().get("status").getAsInt()); |
|
|
|
if (2 != responseData.getHeader().get("status").getAsInt()) { |
|
|
|
System.out.println("****************"); |
|
|
|
Payload pl = json.fromJson(responseData.getPayload(), Payload.class); |
|
|
|
JsonArray temp = (JsonArray) pl.getChoices().get("text"); |
|
|
|
JsonObject jo = (JsonObject) temp.get(0); |
|
|
|
answer += jo.get("content").getAsString(); |
|
|
|
answerBuf += jo.get("content").getAsString(); |
|
|
|
synchronized (this) { |
|
|
|
ResponseData responseData = json.fromJson(text,ResponseData.class); |
|
|
|
try { |
|
|
|
// System.out.println("code:\n" + responseData.getHeader().get("code")); |
|
|
|
if (0 == responseData.getHeader().get("code").getAsInt()) { |
|
|
|
System.out.println("###########"); |
|
|
|
System.out.println("getStatus: " + responseData.getHeader().get("status").getAsInt()); |
|
|
|
if (2 != responseData.getHeader().get("status").getAsInt()) { |
|
|
|
System.out.println("****************"); |
|
|
|
Payload pl = json.fromJson(responseData.getPayload(), Payload.class); |
|
|
|
JsonArray temp = (JsonArray) pl.getChoices().get("text"); |
|
|
|
JsonObject jo = (JsonObject) temp.get(0); |
|
|
|
answer += jo.get("content").getAsString(); |
|
|
|
answerBuf += jo.get("content").getAsString(); |
|
|
|
// System.out.println(answer); |
|
|
|
} else { |
|
|
|
Payload pl1 = json.fromJson(responseData.getPayload(), Payload.class); |
|
|
|
JsonObject jsonObject = (JsonObject) pl1.getUsage().get("text"); |
|
|
|
int prompt_tokens = jsonObject.get("prompt_tokens").getAsInt(); |
|
|
|
JsonArray temp1 = (JsonArray) pl1.getChoices().get("text"); |
|
|
|
JsonObject jo = (JsonObject) temp1.get(0); |
|
|
|
answer += jo.get("content").getAsString(); |
|
|
|
answerBuf += jo.get("content").getAsString(); |
|
|
|
System.out.println("返回结果为:\n" + answer); |
|
|
|
|
|
|
|
if (INSTANCE.redisTemplate.hasKey("gpt:websocket:1")) { |
|
|
|
DmWebSocketMessageVo message = (DmWebSocketMessageVo) INSTANCE.redisTemplate.opsForValue().get("gpt:websocket:1"); |
|
|
|
if (message != null && StringUtils.isNotEmpty(message.getTemplate()) && message.getTemplate().equals("birthday")) { |
|
|
|
JSONObject birthdayJo = new JSONObject(); |
|
|
|
birthdayJo.put("content", answer); |
|
|
|
birthdayJo.put("timestamp", message.getFormat().get("timestamp")); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + message.getFormat().getString("orderId"), "birthday", birthdayJo.toString()); |
|
|
|
INSTANCE.redisTemplate.delete("gpt:websocket:1"); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (message!= null && StringUtils.isNotEmpty(message.getTemplate()) && message.getTemplate().equals("hireDate")) { |
|
|
|
JSONObject birthdayJo = new JSONObject(); |
|
|
|
birthdayJo.put("content", answer); |
|
|
|
birthdayJo.put("timestamp", message.getFormat().get("timestamp")); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + message.getFormat().getString("orderId"), "hireDate", birthdayJo.toString()); |
|
|
|
} else { |
|
|
|
Payload pl1 = json.fromJson(responseData.getPayload(), Payload.class); |
|
|
|
JsonObject jsonObject = (JsonObject) pl1.getUsage().get("text"); |
|
|
|
int prompt_tokens = jsonObject.get("prompt_tokens").getAsInt(); |
|
|
|
JsonArray temp1 = (JsonArray) pl1.getChoices().get("text"); |
|
|
|
JsonObject jo = (JsonObject) temp1.get(0); |
|
|
|
answer += jo.get("content").getAsString(); |
|
|
|
answerBuf += jo.get("content").getAsString(); |
|
|
|
System.out.println("返回结果为:\n" + answer); |
|
|
|
|
|
|
|
if (INSTANCE.redisTemplate.hasKey("gpt:websocket:1")) { |
|
|
|
DmWebSocketMessageVo message = (DmWebSocketMessageVo) INSTANCE.redisTemplate.opsForValue().get("gpt:websocket:1"); |
|
|
|
if (message != null && StringUtils.isNotEmpty(message.getTemplate()) && message.getTemplate().equals("birthday")) { |
|
|
|
JSONObject birthdayJo = new JSONObject(); |
|
|
|
birthdayJo.put("content", answer); |
|
|
|
birthdayJo.put("timestamp", message.getFormat().get("timestamp")); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + message.getFormat().getString("orderId"), "birthday", birthdayJo.toString()); |
|
|
|
INSTANCE.redisTemplate.delete("gpt:websocket:1"); |
|
|
|
this.notifyAll(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (message!= null && StringUtils.isNotEmpty(message.getTemplate()) && message.getTemplate().equals("hireDate")) { |
|
|
|
JSONObject birthdayJo = new JSONObject(); |
|
|
|
birthdayJo.put("content", answer); |
|
|
|
birthdayJo.put("timestamp", message.getFormat().get("timestamp")); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + message.getFormat().getString("orderId"), "hireDate", birthdayJo.toString()); |
|
|
|
INSTANCE.redisTemplate.delete("gpt:websocket:1"); |
|
|
|
this.notifyAll(); |
|
|
|
return; |
|
|
|
} |
|
|
|
if (message != null) { |
|
|
|
JSONObject preWebsocketJo = message.getFormat(); |
|
|
|
JSONObject meetingJo = new JSONObject(); |
|
|
|
meetingJo.put("timestamp",preWebsocketJo.get("timestamp")); |
|
|
|
meetingJo.put("content",answer); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + preWebsocketJo.getString("orderId"), "meeting", meetingJo.toString()); |
|
|
|
this.notifyAll(); |
|
|
|
return; |
|
|
|
} |
|
|
|
INSTANCE.redisTemplate.delete("gpt:websocket:1"); |
|
|
|
return; |
|
|
|
// 清除systemRole |
|
|
|
systemRole = ""; |
|
|
|
|
|
|
|
}else { |
|
|
|
// 添加上下文 |
|
|
|
INSTANCE.redisTemplate.opsForList().rightPush("group:nlp:null:-1", questions.get(questions.size() - 1)); |
|
|
|
INSTANCE.redisTemplate.opsForList().rightPush("group:nlp:null:-1", answer); |
|
|
|
// 添加缓存 |
|
|
|
INSTANCE.stringRedisTemplate.opsForValue().set("group:websocket:content", answer); |
|
|
|
} |
|
|
|
if (message != null) { |
|
|
|
JSONObject preWebsocketJo = message.getFormat(); |
|
|
|
JSONObject meetingJo = new JSONObject(); |
|
|
|
meetingJo.put("timestamp",preWebsocketJo.get("timestamp")); |
|
|
|
meetingJo.put("content",answer); |
|
|
|
INSTANCE.stringRedisTemplate.opsForHash().put("group:nlp" + ":" + preWebsocketJo.getString("orderId"), "meeting", meetingJo.toString()); |
|
|
|
this.notifyAll(); |
|
|
|
// webSocket.close(3,"客户端主动断开链接"); |
|
|
|
//webSocket.close(1000,"客户端主动断开链接"); |
|
|
|
} |
|
|
|
if (this.stream && !StringUtils.isEmpty(curUserId)) { |
|
|
|
Channel ch = ServerConfig.sessionMap.get(curUserId); |
|
|
|
logger.info("当前ch:{}",ch.id().asLongText()); |
|
|
|
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("。|?|!|\\?|\\!"); |
|
|
|
ttsList.add(temp[0] + answerBuf.charAt(temp[0].length())); |
|
|
|
answerBuf = answerBuf.substring(temp[0].length() + 1); |
|
|
|
} |
|
|
|
if (2 == responseData.getHeader().get("status").getAsInt() && CollectionUtils.isEmpty(ttsList)) { |
|
|
|
jo.put("tts",answerBuf); |
|
|
|
jo.put("status",2); |
|
|
|
logger.info("发送到client:{},id:{},内容:{}",curUserId,ch.id().asLongText(),jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
} else { |
|
|
|
for (int i = 0;i <ttsList.size();i++) { |
|
|
|
if (2 == responseData.getHeader().get("status").getAsInt() && i == ttsList.size() - 1) { |
|
|
|
jo.put("status",2); |
|
|
|
} else { |
|
|
|
jo.put("status",1); |
|
|
|
} |
|
|
|
jo.put("tts",ttsList.get(i)); |
|
|
|
String str = jo.toJSONString(); |
|
|
|
logger.info("发送到client:{},id:{},内容:{}",curUserId,ch.id().asLongText(),jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
INSTANCE.redisTemplate.delete("gpt:websocket:1"); |
|
|
|
// 清除systemRole |
|
|
|
systemRole = ""; |
|
|
|
|
|
|
|
}else { |
|
|
|
// 添加上下文 |
|
|
|
INSTANCE.redisTemplate.opsForList().rightPush("group:nlp:null:-1", questions.get(questions.size() - 1)); |
|
|
|
INSTANCE.redisTemplate.opsForList().rightPush("group:nlp:null:-1", answer); |
|
|
|
// 添加缓存 |
|
|
|
INSTANCE.stringRedisTemplate.opsForValue().set("group:websocket:content", answer); |
|
|
|
} |
|
|
|
|
|
|
|
// webSocket.close(3,"客户端主动断开链接"); |
|
|
|
//webSocket.close(1000,"客户端主动断开链接"); |
|
|
|
} |
|
|
|
if (this.stream && !StringUtils.isEmpty(curUserId)) { |
|
|
|
Channel ch = ServerConfig.sessionMap.get(curUserId); |
|
|
|
logger.info("当前ch:{}",ch.id().asLongText()); |
|
|
|
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("。|?|!|\\?|\\!"); |
|
|
|
ttsList.add(temp[0] + answerBuf.charAt(temp[0].length())); |
|
|
|
answerBuf = answerBuf.substring(temp[0].length() + 1); |
|
|
|
} |
|
|
|
if (2 == responseData.getHeader().get("status").getAsInt() && CollectionUtils.isEmpty(ttsList)) { |
|
|
|
jo.put("tts",answerBuf); |
|
|
|
} else { |
|
|
|
// 添加缓存 |
|
|
|
INSTANCE.stringRedisTemplate.opsForValue().set("group:websocket:content", "-1"); |
|
|
|
// 判断流式则返回结束状态 |
|
|
|
if (stream == true) { |
|
|
|
Channel ch = ServerConfig.sessionMap.get(curUserId); |
|
|
|
if (ch != null) { |
|
|
|
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(); |
|
|
|
logger.info("发送到client:{},id:{},内容:{}",curUserId,ch.id().asLongText(),jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(jo.toJSONString())); |
|
|
|
} else { |
|
|
|
for (int i = 0;i <ttsList.size();i++) { |
|
|
|
if (2 == responseData.getHeader().get("status").getAsInt() && i == ttsList.size() - 1) { |
|
|
|
jo.put("status",2); |
|
|
|
} else { |
|
|
|
jo.put("status",1); |
|
|
|
} |
|
|
|
jo.put("tts",ttsList.get(i)); |
|
|
|
String str = jo.toJSONString(); |
|
|
|
logger.info("发送到client:{},id:{},内容:{}",curUserId,ch.id().asLongText(),jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
} |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
} |
|
|
|
} |
|
|
|
System.out.println("返回结果错误:\n" + responseData.getHeader().get("code") + responseData.getHeader().get("message")); |
|
|
|
this.notifyAll(); |
|
|
|
} |
|
|
|
|
|
|
|
} else { |
|
|
|
// 添加缓存 |
|
|
|
INSTANCE.stringRedisTemplate.opsForValue().set("group:websocket:content", "-1"); |
|
|
|
// 判断流式则返回结束状态 |
|
|
|
if (stream == true) { |
|
|
|
} catch (Exception e) { |
|
|
|
if (StringUtils.isNotEmpty(curUserId)) { |
|
|
|
Channel ch = ServerConfig.sessionMap.get(curUserId); |
|
|
|
if (ch != null) { |
|
|
|
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(); |
|
|
|
logger.info("发送到client:{},id:{},内容:{}",curUserId,ch.id().asLongText(),jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
} |
|
|
|
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(); |
|
|
|
logger.info("发生异常client:{},内容:{}",curUserId,jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
} |
|
|
|
System.out.println("返回结果错误:\n" + responseData.getHeader().get("code") + responseData.getHeader().get("message")); |
|
|
|
LOCK.notifyAll(); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
if (StringUtils.isNotEmpty(curUserId)) { |
|
|
|
Channel ch = ServerConfig.sessionMap.get(curUserId); |
|
|
|
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(); |
|
|
|
logger.info("发生异常client:{},内容:{}",curUserId,jo.toJSONString()); |
|
|
|
ch.writeAndFlush(new TextWebSocketFrame(str)); |
|
|
|
e.printStackTrace(); |
|
|
|
this.notifyAll(); |
|
|
|
} |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|