|
|
@@ -3,10 +3,14 @@ package com.goafanti.common.utils;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.goafanti.baiduAI.bo.InputSendChat;
|
|
|
+import com.goafanti.baiduAI.bo.OutSendChatOK;
|
|
|
+import com.goafanti.baiduAI.bo.SseMap;
|
|
|
+import com.goafanti.baiduAI.bo.SseResult;
|
|
|
import com.goafanti.common.error.BusinessException;
|
|
|
import okhttp3.*;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
|
|
|
|
|
|
@@ -16,6 +20,7 @@ import java.io.InputStream;
|
|
|
import java.io.InputStreamReader;
|
|
|
import java.net.HttpURLConnection;
|
|
|
import java.net.MalformedURLException;
|
|
|
+import java.net.ProtocolException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
import java.util.Calendar;
|
|
|
@@ -80,7 +85,9 @@ public class BaiduChatUtils {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
- public void sendBaiduAiStream() throws IOException {
|
|
|
+ @Async
|
|
|
+ public void sendBaiduAiStream(InputSendChat inputSendChat) throws IOException {
|
|
|
+ System.out.println("异步调用获取数据");
|
|
|
try {
|
|
|
String baidu_url=BAIDU_CHAT_WXYY_URL+getRedisBaiduAccessToken();
|
|
|
URL url = new URL(baidu_url);
|
|
|
@@ -90,15 +97,15 @@ public class BaiduChatUtils {
|
|
|
connection.setDoInput(true);
|
|
|
connection.setDoOutput(true);
|
|
|
// 构造请求体
|
|
|
- String requestBody = "{\"messages\":[{\"role\":\"user\",\"content\":\"给我介绍一条从四川自驾到拉萨的路线\"}],\"stream\":true}";
|
|
|
+// String requestBody = "{\"messages\":[{\"role\":\"user\",\"content\":\"给我介绍一条从四川自驾到拉萨的路线\"}],\"stream\":true}";
|
|
|
+ String requestBody=JSON.toJSONString(inputSendChat);
|
|
|
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
|
connection.getOutputStream().write(postData);
|
|
|
-
|
|
|
InputStream responseStream = connection.getInputStream();
|
|
|
- BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream));
|
|
|
-
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(responseStream,"UTF-8"));
|
|
|
String line;
|
|
|
+ SseResult res = SseMap.sseEmitterMap.get(inputSendChat.getUserId());
|
|
|
while ((line = reader.readLine())!= null) {
|
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
|
if (line.startsWith("data:")) {
|
|
|
@@ -109,12 +116,18 @@ public class BaiduChatUtils {
|
|
|
if(isEnd){
|
|
|
break;
|
|
|
}
|
|
|
- System.out.println("result: " + result.replaceAll("\n", ""));
|
|
|
+// OutSendChatOK out =jsonObject.toJavaObject(OutSendChatOK.class);
|
|
|
+ System.out.println(data);
|
|
|
+ res.sseEmitter.send(data, org.springframework.http.MediaType.APPLICATION_JSON);
|
|
|
}
|
|
|
}
|
|
|
+ SseMap.sseEmitterMap.remove(inputSendChat.getUserId());
|
|
|
+ res.sseEmitter.complete();
|
|
|
reader.close();
|
|
|
} catch (MalformedURLException e) {
|
|
|
e.printStackTrace();
|
|
|
+ } catch (ProtocolException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -133,14 +146,14 @@ public class BaiduChatUtils {
|
|
|
redisUtil.deleteString("baiduAccessToken");
|
|
|
redisUtil.deleteString("baiduAccessTime");
|
|
|
redisAccessToken=pushRedisBaiduAccessToken();
|
|
|
- LoggerUtils.debug(getClass(),"accessToken过期,重新获取");
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken过期,重新获取!");
|
|
|
}else {
|
|
|
redisAccessToken= redisUtil.getString("baiduAccessToken");
|
|
|
- LoggerUtils.debug(getClass(),"accessToken从redis获取");
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken从redis获取成功!");
|
|
|
}
|
|
|
}else {
|
|
|
redisAccessToken=pushRedisBaiduAccessToken();
|
|
|
- LoggerUtils.debug(getClass(),"accessToken不存在,从百度获取");
|
|
|
+ LoggerUtils.debug(getClass(),"accessToken不存在,从百度获取!");
|
|
|
}
|
|
|
return redisAccessToken;
|
|
|
}
|