|
@@ -69,6 +69,7 @@ public class BaiduChatUtils {
|
|
|
// 构造请求体
|
|
// 构造请求体
|
|
|
// String requestBody = "{\"messages\":[{\"role\":\"user\",\"content\":\"给我介绍一条从四川自驾到拉萨的路线\"}],\"stream\":true}";
|
|
// String requestBody = "{\"messages\":[{\"role\":\"user\",\"content\":\"给我介绍一条从四川自驾到拉萨的路线\"}],\"stream\":true}";
|
|
|
String requestBody=JSON.toJSONString(inputSendChat);
|
|
String requestBody=JSON.toJSONString(inputSendChat);
|
|
|
|
|
+ System.out.println(requestBody);
|
|
|
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
|
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
|
|
|
outputStream =connection.getOutputStream();
|
|
outputStream =connection.getOutputStream();
|
|
@@ -80,14 +81,18 @@ public class BaiduChatUtils {
|
|
|
|
|
|
|
|
while ((line = reader.readLine())!= null) {
|
|
while ((line = reader.readLine())!= null) {
|
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
// 每行数据中以 "data:" 开头的部分即为实际的响应数据
|
|
|
|
|
+ System.out.println(line);
|
|
|
if (StringUtils.isNotBlank(line)){
|
|
if (StringUtils.isNotBlank(line)){
|
|
|
if (line.startsWith("data:")) {
|
|
if (line.startsWith("data:")) {
|
|
|
String data = line.substring("data:".length()).trim();
|
|
String data = line.substring("data:".length()).trim();
|
|
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
|
|
- Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
|
|
|
- res.sseEmitter.send(data);
|
|
|
|
|
- if(isEnd){
|
|
|
|
|
- break;
|
|
|
|
|
|
|
+ String result=jsonObject.getString("result");
|
|
|
|
|
+ if (StringUtils.isNotBlank(result)){
|
|
|
|
|
+ Boolean isEnd = jsonObject.getBoolean("is_end");
|
|
|
|
|
+ res.sseEmitter.send(data);
|
|
|
|
|
+ if(isEnd){
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
//错误返回格式{"error_code":110,"error_msg":"Access token invalid or no longer valid"}
|
|
//错误返回格式{"error_code":110,"error_msg":"Access token invalid or no longer valid"}
|
|
|
}else if(line.startsWith("{")) {
|
|
}else if(line.startsWith("{")) {
|