Explorar el Código

切片接口,预设问题,提词接口联调修改。

S0025136190 hace 11 meses
padre
commit
32195da130

+ 22 - 8
takai-admin/src/main/java/com/takai/web/controller/bigmodel/BigModelController.java

@@ -161,7 +161,7 @@ public class BigModelController extends BaseController {
     @GetMapping("/slice_info/{requestId}")
     public AjaxResult sliceInfo(@PathVariable String requestId)
     {
-        List<Map<String, Object>> json = bigModelService.getSliceInfo(requestId);
+        String json = bigModelService.getSliceInfo(requestId);
         return AjaxResult.success(json);
     }
 
@@ -172,7 +172,7 @@ public class BigModelController extends BaseController {
      */
     @PostMapping("/async/completions")
     public AjaxResult getAsyncCompletions(@RequestBody CompletionsParams params){
-        AsyncCompletions json = bigModelService.getAsyncCompletions(params);
+        List<String> json = bigModelService.getAsyncCompletions(params);
         return success(json);
     }
 
@@ -184,7 +184,7 @@ public class BigModelController extends BaseController {
     @GetMapping("/async_result/{id}")
     public AjaxResult getAsyncResult(@PathVariable String id)
     {
-        AsyncResult json = bigModelService.getAsyncResult(id);
+        JSONObject json = bigModelService.getAsyncResult(id);
         return success(json);
     }
 
@@ -203,11 +203,25 @@ public class BigModelController extends BaseController {
      */
     @GetMapping("/presets")
     public AjaxResult presets(){
-        List<String> list = new ArrayList();
-        list.add("预设问题1");
-        list.add("预设问题2");
-        list.add("预设问题3");
-        list.add("预设问题4");
+        List<Map<String, Object>> list = new ArrayList();
+        Map<String, Object> map = new HashMap();
+        map.put("key", "question_1");
+        map.put("title", "预设问题1");
+        map.put("content", "公司福利有哪些?");
+        list.add(map);
+
+        Map<String, Object> map1 = new HashMap();
+        map1.put("key", "question_2");
+        map1.put("title", "预设问题2");
+        map1.put("content", "新员工入职流程");
+        list.add(map1);
+
+        Map<String, Object> map2 = new HashMap();
+        map2.put("key", "question_3");
+        map2.put("title", "预设问题3");
+        map2.put("content", "培训新员工");
+        list.add(map2);
+
         return success(list);
     }
 

+ 1 - 1
takai-bigmodel/src/main/java/com/takai/bigmodel/mapper/DialogMapper.java

@@ -39,7 +39,7 @@ public interface DialogMapper
     /**
      * 新增对话消息头表信息
      * 
-     * @param dialogId
+     * @param dto
      * @return 结果
      */
     public void insertDialog(DialogReqDTO dto);

+ 3 - 3
takai-bigmodel/src/main/java/com/takai/bigmodel/service/IBigModelService.java

@@ -70,11 +70,11 @@ public interface IBigModelService
 
     SseEmitter getCompletions(CompletionsParams params);
 
-    List<Map<String, Object>> getSliceInfo(String requestId);
+    String getSliceInfo(String requestId);
 
-    AsyncCompletions getAsyncCompletions(CompletionsParams params);
+    List<String> getAsyncCompletions(CompletionsParams params);
 
-    AsyncResult getAsyncResult(String id);
+    JSONObject getAsyncResult(String id);
 
     SseEmitter assistant(AssistantParams params);
 

+ 96 - 24
takai-bigmodel/src/main/java/com/takai/bigmodel/service/impl/BigModelServiceImpl.java

@@ -25,7 +25,6 @@ import javax.validation.constraints.NotNull;
 import java.io.IOException;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
-import java.time.format.DateTimeFormatter;
 import java.time.temporal.ChronoUnit;
 import java.util.*;
 import java.util.concurrent.TimeUnit;
@@ -88,6 +87,18 @@ public class BigModelServiceImpl implements IBigModelService
     public static final String END_SIGN = "】";
     public static final String SYMBOL = "【示意图序号";
 
+    public static final String MODEL = "glm-4-plus";
+
+    public static final String DATA_STR = "\n\n---\n\n答案引用自文档:";
+
+    public static final String QUESTION = "总结三个建议,二十个字内,固定格式";
+
+    public static final String USER_STR = "user";
+
+    public static final String TODAY = "今天";
+
+    public static final String BEFOR = "天前";
+
     @Override
     public SseEmitter sseInvoke(SseParams sseParams) {
         SseEmitter sseEmitter = new SseEmitter(0L);
@@ -143,12 +154,24 @@ public class BigModelServiceImpl implements IBigModelService
 //                            threadPoolTaskExecutor.execute(() -> send(sseEmitter,json));
                         }
                     } else {
+                        if(com.takai.common.utils.StringUtils.isNotEmpty(type) && "finish".equals(type)){
+                            String sliceInfo = getSliceInfo(id);
+                            if(com.takai.common.utils.StringUtils.isNotEmpty(sliceInfo)){
+                                JSONObject json = new JSONObject();
+                                json.put("id", id);
+                                json.put("event", "add");
+                                json.put("data", sliceInfo);
+                                log.info("智谱返回切片消息:" + sliceInfo);
+                                send(sseEmitter, json);
+                            }
+                        }
                         JSONObject json = new JSONObject();
                         json.put("id", id);
                         json.put("event", type);
                         json.put("data", newData);
                         log.info("智谱返回信息:" + json);
                         send(sseEmitter,json);
+
 //                        threadPoolTaskExecutor.execute(() -> send(sseEmitter,json));
                     }
                     //sseEmitter.send(json);
@@ -421,7 +444,7 @@ public class BigModelServiceImpl implements IBigModelService
                             .temperature("0.05")
                             .knowledgeIds("["+uuid+"]")
                             .sliceCount(4)
-                            .model("glm-4-plus")
+                            .model(MODEL)
                             .iconColor("blue")
                             .iconType("robot_assist")
                             .build();
@@ -561,11 +584,11 @@ public class BigModelServiceImpl implements IBigModelService
                 m.put("key", System.currentTimeMillis());
                 m.put("type", "group");
                 if(daysBetween == 0l){
-                    m.put("label", "今天");
+                    m.put("label", TODAY);
                     m.put("children", setValue(entry.getValue()));
                     obj.add(m);
                 }else{
-                    m.put("label", -daysBetween+"天前");
+                    m.put("label", -daysBetween+BEFOR);
                     m.put("children", setValue(entry.getValue()));
                     obj.add(m);
                 }
@@ -587,7 +610,7 @@ public class BigModelServiceImpl implements IBigModelService
         SseEmitter sseEmitter = new SseEmitter(0L);
         JSONObject json = new JSONObject();
         json.put("model",params.getModel());
-        json.put("messages",params.getMessages());
+        json.put("messages", params.getMessages());
         json.put("stream", params.getStream());
         json.put("request_id", params.getRequest_id());
 //        json.put("tools", params.getTools());
@@ -662,8 +685,7 @@ public class BigModelServiceImpl implements IBigModelService
     }
 
     @Override
-    public List<Map<String, Object>> getSliceInfo(String requestId) {
-        List<Map<String, Object>> list = new ArrayList();
+    public String getSliceInfo(String requestId) {
         String url = bigModelConfig.getBaseurl() + bigModelConfig.getSliceInfo().replace("{request_id}",requestId);
         Request request = buildGetRequest(url);
         OkHttpClient client = buildOkHttpClient();
@@ -673,73 +695,123 @@ public class BigModelServiceImpl implements IBigModelService
                 String body = response.body().string();
                 boolean status = isJsonObject(body);
                 if(!status){
-                    return list;
+                    return null;
                 }else{
                     JSONObject jsonObj = JSON.parseObject(body);
                     JSONObject jb = jsonObj.getJSONObject("data");
                     JSONArray array = jb.getJSONArray("document_slices");
                     if(array != null){
+                        String data = DATA_STR;
                         for(int i=0; i<array.size(); i++){
                             Map<String, Object> map = new HashMap();
                             JSONObject detailObj = array.getJSONObject(i);
                             JSONObject docObj = detailObj.getJSONObject("document");
                             String name = String.valueOf(docObj.get("name"));
                             String urlStr = String.valueOf(docObj.get("url"));
-                            map.put("name", name);
-                            map.put("url", urlStr);
-                            list.add(map);
+                            data += "["+name+"]"+"("+urlStr +"),";
                         }
+                        data = data.substring(0, data.length()-1);
+                        return data;
                     }
                 }
             }
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
-        return list;
+        return null;
     }
 
     @Override
-    public AsyncCompletions getAsyncCompletions(CompletionsParams params) {
+    public List<String> getAsyncCompletions(CompletionsParams params) {
+        log.info("---question---", params.getMessages());
+        PromptObject vo = new PromptObject();
+        vo.setContent(QUESTION);
+        vo.setRole(USER_STR);
+        params.getMessages().add(vo);
         String url = bigModelConfig.getAsyncCompletions();
         JSONObject json = new JSONObject();
-        json.put("model",params.getModel());
+        json.put("model", MODEL);
         json.put("messages",params.getMessages());
+        json.put("stream", true);
         RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8") , json.toJSONString());
 
         Request request = buildPostRequest(url,requestBody);
         OkHttpClient client = buildOkHttpClient();
-        AsyncCompletions result = null;
+        List<String> resultList = new ArrayList();
         try {
             Response response = client.newCall(request).execute();
             if(response.isSuccessful()){
                 String body = response.body().string();
-                JSONObject jsonObj = JSON.parseObject(body);
-                result = JSONObject.parseObject(String.valueOf(jsonObj), AsyncCompletions.class);
+                boolean status = isJsonObject(body);
+                if(status){
+                    JSONObject jsonObj = JSON.parseObject(body);
+                    AsyncCompletions result = JSONObject.parseObject(String.valueOf(jsonObj), AsyncCompletions.class);
+                    if(result != null){
+                        for (int i=0; i<5; i++){
+                            try {
+                                Thread.sleep(i*1000); // 时间递增调用
+                                // 用异步返回的数据
+                                JSONObject obj = getAsyncResult(result.getId());
+                                if(obj != null){
+                                    JSONArray array = obj.getJSONArray("choices");
+                                    if(array != null){
+                                        for(int j=0; j<array.size(); j++){
+                                            JSONObject megObj = array.getJSONObject(j);
+                                            JSONObject message = megObj.getJSONObject("message");
+                                            String content = String.valueOf(message.get("content"));
+                                            if(com.takai.common.utils.StringUtils.isNotEmpty(content)){
+                                                content = content.replace("*", "").replace("1.", "")
+                                                        .replace("2.", "").replace("3.", "");
+                                                if(content.contains("\n")){
+                                                    String[] strings = content.split("\n");
+                                                    List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
+                                                    resultList.addAll(listFromArray);
+                                                }else if(content.contains("、")){
+                                                    String[] strings = content.split("、");
+                                                    List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
+                                                    resultList.addAll(listFromArray);
+                                                }else if(content.contains(",")){
+                                                    String[] strings = content.split(",");
+                                                    List<String> listFromArray = new ArrayList<>(Arrays.asList(strings));
+                                                    resultList.addAll(listFromArray);
+                                                }
+                                            }
+                                        }
+                                        break;
+                                    }
+                                }
+                            } catch (InterruptedException e) {
+                                throw new RuntimeException(e);
+                            }
+                        }
+                    }
+                }
             }
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
-        return result;
+        return resultList;
     }
 
     @Override
-    public AsyncResult getAsyncResult(String id) {
+    public JSONObject getAsyncResult(String id) {
         String url = bigModelConfig.getAsyncResult().replace("{id}", id);
         Request request = buildGetRequest(url);
         OkHttpClient client = buildOkHttpClient();
-        AsyncResult vo = null;
         try {
             Response response = client.newCall(request).execute();
             if(response.isSuccessful()){
                 String body = response.body().string();
-                JSONObject jsonObj = JSON.parseObject(body);
-                vo = JSONObject.parseObject(String.valueOf(jsonObj), AsyncResult.class);
+                if(isJsonObject(body)){
+                    JSONObject jsonObj = JSON.parseObject(body);
+                    return jsonObj;
+                    //vo = JSONObject.parseObject(String.valueOf(jsonObj), AsyncResult.class);
+                }
             }
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
-
-        return vo;
+        return null;
     }
 
     @Override