Browse Source

新增技淘百问处理

anderx 2 years ago
parent
commit
d8a760d949
1 changed files with 48 additions and 0 deletions
  1. 48 0
      src/main/java/com/goafanti/common/utils/BaiduChatUtils.java

+ 48 - 0
src/main/java/com/goafanti/common/utils/BaiduChatUtils.java

@@ -1,6 +1,7 @@
 package com.goafanti.common.utils;
 
 import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
 import com.goafanti.baiduAI.bo.InputSendChat;
 import com.goafanti.common.error.BusinessException;
 import okhttp3.*;
@@ -9,7 +10,14 @@ import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
 
+import java.io.BufferedReader;
 import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.concurrent.TimeUnit;
@@ -72,6 +80,44 @@ public class BaiduChatUtils {
         return result;
     }
 
+    public void sendBaiduAiStream() throws IOException {
+        URL url = null;
+        try {
+            url = new URL("https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions?access_token=xxx");
+        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+            connection.setRequestMethod("POST");
+            connection.setRequestProperty("Content-Type", "application/json");
+            connection.setDoInput(true);
+            connection.setDoOutput(true);
+            // 构造请求体
+            String requestBody = "{\"messages\":[{\"role\":\"user\",\"content\":\"给我介绍一条从四川自驾到拉萨的路线\"}],\"stream\":true}";
+            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));
+
+            String line;
+            while ((line = reader.readLine())!= null) {
+                // 每行数据中以 "data:" 开头的部分即为实际的响应数据
+                if (line.startsWith("data:")) {
+                    String data = line.substring("data:".length()).trim();
+                    JSONObject jsonObject = JSONObject.parseObject(data);
+                    Boolean isEnd = jsonObject.getBoolean("is_end");
+                    String result = jsonObject.getString("result");
+                    if(isEnd){
+                        break;
+                    }
+                    System.out.println("result: " + result.replaceAll("\n", ""));
+                }
+            }
+            reader.close();
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        }
+    }
+
 
 
 
@@ -116,4 +162,6 @@ public class BaiduChatUtils {
         redisUtil.setString("baiduAccessTime",baiduAccessTime.toString());
         return baiduAccessToken;
     }
+
+
 }