Bladeren bron

技淘百问本地后台版开发

anderx 2 jaren geleden
bovenliggende
commit
6b392a9351

+ 25 - 0
src/main/java/com/goafanti/baiduAI/bo/InputsendGLM.java

@@ -0,0 +1,25 @@
+package com.goafanti.baiduAI.bo;
+
+import java.util.List;
+
+public class InputsendGLM {
+
+    private String query;
+    private List<Object>  history;
+
+    public String getQuery() {
+        return query;
+    }
+
+    public void setQuery(String query) {
+        this.query = query;
+    }
+
+    public List<Object> getHistory() {
+        return history;
+    }
+
+    public void setHistory(List<Object> history) {
+        this.history = history;
+    }
+}

+ 6 - 4
src/main/java/com/goafanti/baiduAI/controller/BaiduAiController.java

@@ -1,10 +1,7 @@
 package com.goafanti.baiduAI.controller;
 
 import com.alibaba.fastjson.JSON;
-import com.goafanti.baiduAI.bo.InputSendChat;
-import com.goafanti.baiduAI.bo.Messages;
-import com.goafanti.baiduAI.bo.SseMap;
-import com.goafanti.baiduAI.bo.SseResult;
+import com.goafanti.baiduAI.bo.*;
 import com.goafanti.baiduAI.service.BaiduAiService;
 import com.goafanti.common.bo.Result;
 import com.goafanti.common.bo.SseEmitterUTF8;
@@ -41,6 +38,11 @@ public class BaiduAiController {
         return baiduAiService.sendChat(in);
     }
 
+    @RequestMapping(value = "/sendGLM")
+    public Object sendGLM(@RequestBody InputsendGLM in){
+        return baiduAiService.sendGLM(in);
+    }
+
 
 
     @RequestMapping("/start")

+ 2 - 0
src/main/java/com/goafanti/baiduAI/service/BaiduAiService.java

@@ -1,6 +1,7 @@
 package com.goafanti.baiduAI.service;
 
 import com.goafanti.baiduAI.bo.InputSendChat;
+import com.goafanti.baiduAI.bo.InputsendGLM;
 import com.goafanti.common.bo.SseEmitterUTF8;
 import com.goafanti.common.model.ErnieBotCommon;
 
@@ -11,6 +12,7 @@ public interface BaiduAiService {
 
     SseEmitterUTF8 sendChat(InputSendChat in);
 
+    SseEmitterUTF8 sendGLM(InputsendGLM in);
 
     List<ErnieBotCommon> selectErnieBotCommonAll(Integer count);
 

+ 18 - 1
src/main/java/com/goafanti/baiduAI/service/impl/BaiduAiServiceImpl.java

@@ -9,6 +9,7 @@ import com.goafanti.common.dao.ErnieBotCommonDao;
 import com.goafanti.common.error.BusinessException;
 import com.goafanti.common.model.ErnieBotCommon;
 import com.goafanti.common.utils.*;
+import com.goafanti.core.mybatis.JDBCIdGenerator;
 import com.goafanti.core.shiro.cache.ShiroRedisCache;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -27,7 +28,9 @@ public class BaiduAiServiceImpl implements BaiduAiService {
     @Autowired
     private BaiduChatUtils baiduChatUtils;
     @Autowired
-    private RedisUtil redisUtil;
+    private SseEmitterUtils sseEmitterUtils;
+    @Autowired
+    private JDBCIdGenerator jdbcIdGenerator;
 
 
     @Override
@@ -46,6 +49,20 @@ public class BaiduAiServiceImpl implements BaiduAiService {
     }
 
     @Override
+    public SseEmitterUTF8 sendGLM(InputsendGLM in) {
+        SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(3600_000L);
+        String id=jdbcIdGenerator.generateId().toString();
+        SseMap.sseEmitterMap.put(id, new SseResult(id, System.currentTimeMillis(), sseEmitter));
+        try {
+            sseEmitterUtils.sseEmitterSend(id,in);
+        } catch (IOException e) {
+            e.printStackTrace();
+            throw new BusinessException("ChatGLM调用失败"+e.getLocalizedMessage());
+        }
+        return sseEmitter;
+    }
+
+    @Override
 //    @Cacheable(value = "selectErnieBotCommonAll#300",key = "'page:'+'All'")
     public List<ErnieBotCommon> selectErnieBotCommonAll(Integer count) {
         return ernieBotCommonDao.selectAll(count);

+ 99 - 0
src/main/java/com/goafanti/common/utils/SseEmitterUtils.java

@@ -0,0 +1,99 @@
+package com.goafanti.common.utils;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.goafanti.baiduAI.BaiduChatErrorEnums;
+import com.goafanti.baiduAI.bo.InputsendGLM;
+import com.goafanti.baiduAI.bo.OutChatER;
+import com.goafanti.baiduAI.bo.SseMap;
+import com.goafanti.baiduAI.bo.SseResult;
+import com.goafanti.common.bo.SseEmitterUTF8;
+
+import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+
+public class SseEmitterUtils {
+
+
+    public void sseEmitterSend(String id, InputsendGLM in) throws IOException {
+        SseResult res =  SseMap.sseEmitterMap.get(id);
+        BufferedReader reader=null;
+        InputStreamReader inputStreamReader=null;
+        OutputStream outputStream=null;
+        try {
+            String local_url="http://172.16.1.160:8800/stream";
+            URL url = new URL(local_url);
+            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}";
+            String requestBody=JSON.toJSONString(in);
+            System.out.println(requestBody);
+            byte[] postData = requestBody.getBytes(StandardCharsets.UTF_8);
+            connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
+            outputStream =connection.getOutputStream();
+            outputStream.write(postData);
+            InputStream responseStream = connection.getInputStream();
+            inputStreamReader = new InputStreamReader(responseStream, "UTF-8");
+            reader = new BufferedReader(inputStreamReader);
+            String line;
+
+            while ((line = reader.readLine())!= null) {
+                // 每行数据中以 "data:" 开头的部分即为实际的响应数据
+                if (StringUtils.isNotBlank(line)){
+                    if (line.startsWith("data:")) {
+                        String data = line.substring("data:".length()).trim();
+                        System.out.println(data);
+                        JSONObject jsonObject = JSONObject.parseObject(data);
+                        Boolean isEnd = jsonObject.getBoolean("finished");
+                        res.sseEmitter.send(line);
+                        if(isEnd){
+                            break;
+                        }
+                        //错误返回格式{"error_code":110,"error_msg":"Access token invalid or no longer valid"}
+                    }else if(line.startsWith("{")) {
+                        JSONObject jsonObject = JSONObject.parseObject(line);
+                        Integer errorCode = jsonObject.getInteger("error_code");
+                        OutChatER out=new OutChatER();
+                        if (errorCode!=null){
+                            out.setError_code(errorCode);
+                            if (errorCode.equals("336003")){
+                                out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode)+jsonObject.getString("error_msg"));
+                            }else {
+                                out.setError_msg(BaiduChatErrorEnums.BycodeGetMsg(errorCode));
+                            }
+                        }
+                        String errorStr= JSON.toJSONString(out);
+                        res.sseEmitter.send(errorStr);
+                    }
+                }
+            }
+            res.sseEmitter.complete();
+        } catch (MalformedURLException e) {
+            e.printStackTrace();
+        } catch (ProtocolException e) {
+            e.printStackTrace();
+        }catch (IllegalStateException e){
+            e.printStackTrace();
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        } catch (IOException e) {
+            e.printStackTrace();
+        } finally {
+            inputStreamReader.close();
+            outputStream.close();
+            reader.close();
+            res.sseEmitter.complete();
+            SseMap.sseEmitterMap.remove(id);
+        }
+    }
+
+
+}

+ 2 - 2
src/main/resources/props/config_local.properties

@@ -39,7 +39,7 @@ app.mobile.website=http://m.jishutao.com/1.0/template/index.html
 template.cacheable=false
 
 static.host=//ss.jishutao.com/client/1.2.11
-#portal.host=//ss.jishutao.com/portal/2.2.21
+portal.host=//ss.jishutao.com/portal/2.2.22
 skjt.host=//ss.jishutao.com/skjt/1.3
 
 
@@ -47,7 +47,7 @@ skjt.host=//ss.jishutao.com/skjt/1.3
 #static.host=//172.16.1.187/client/1.2.8
 #static.host=//172.16.0.255:3000/jitao/client/1.2.9
 #portal.host=//172.16.0.255:3000/jitao/portal/1.2.18
-portal.host=//172.16.1.187/portal/2.2.22
+#portal.host=//172.16.1.187/portal/2.2.22
 
 avatar.host=//ss.jishutao.com
 avatar.upload.host=//ss.jishutao.com/upload

+ 1 - 0
src/main/resources/spring/spring-shiro.xml

@@ -65,6 +65,7 @@
 
 	<!--文心一言-->
 	<bean id="BaiduChatUtils" class="com.goafanti.common.utils.BaiduChatUtils"/>
+	<bean id="SseEmitterUtils" class="com.goafanti.common.utils.SseEmitterUtils"/>
 
 	<!-- 环信 -->
 <!--    <bean id="jedisQueueListener" class="com.goafanti.easemob.queue.EasemobRedisQueueListener"/>-->