Przeglądaj źródła

所有调用大模型的接口全部改成对象 不再拼接JSON字符串

wangmiaomiao 9 miesięcy temu
rodzic
commit
bd175da0dc

+ 43 - 27
slibra-admin/src/main/java/com/slibra/web/controller/business/GRPCController.java

@@ -118,8 +118,8 @@ public class GRPCController extends BaseController {
         //决策请求的业务参数
         List<DecisionReq> decisionReqs = getDecisionReqs();
 //        String rows = JSON.toJSONString(decisionReqs, JSONWriter.Feature.WriteNulls);
-        String dataJson = "";
         boolean needAdd = true;//标识变量是否可以保存
+        String dataJson = "";
         try {
             channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
                     .usePlaintext()
@@ -127,20 +127,7 @@ public class GRPCController extends BaseController {
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
 //            dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + chatReq.getCategory() + "\",\"feedback\":" + feedback + ",\"simulate\":" + simulate + ",\"session_id\":" + "\"" + sessionId + "\"" + ",\"generate_args\":{\"max_new_tokens\":1024,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{\"rows\":" +  rows + "}}";
             //2024年6月24日17:59:17  优化,不再拼接JSON字符串
-            PolicyReq policyReq = new PolicyReq();
-            policyReq.setNorm(chatReq.getCategory());
-            policyReq.setFeedback(StringUtils.isBlank(feedback) ? null : JSON.parseObject(feedback, Map.class));
-            policyReq.setSimulate(StringUtils.isBlank(simulate) ? null : JSON.parseObject(simulate, Map.class));
-            policyReq.setSessionId(sessionId);
-            HashMap<String, Object> map = new HashMap<>();
-            map.put("rows", decisionReqs);
-            //2024年6月25日14:16:05 增加报警是管控值报警还是标准值报警
-            if(WARNING_LEVEL_ONE.equals(xinyiWarningRecord.getLevel()) || WARNING_LEVEL_TWO.equals(xinyiWarningRecord.getLevel()))
-                map.put("source", "bzz");
-            else
-                map.put("source", "gkz");
-            policyReq.setExtra(map);
-            dataJson = JSON.toJSONString(policyReq, JSONWriter.Feature.WriteNulls);
+            dataJson = buildBigModelReqForDecision(chatReq, feedback, simulate, sessionId, decisionReqs, xinyiWarningRecord);
             log.info("请求大模型的决策的参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
@@ -220,7 +207,24 @@ public class GRPCController extends BaseController {
         }
     }
 
-    private void addChatRecord(ChatReq chatReq, String sessionId, int type, String warningId, String dataJson, List<String> resultData) {
+    private static String buildBigModelReqForDecision(ChatReq chatReq, String feedback, String simulate, String sessionId, List<DecisionReq> decisionReqs, TXinyiWarningRecord xinyiWarningRecord) {
+        PolicyReq policyReq = new PolicyReq();
+        policyReq.setNorm(chatReq.getCategory());
+        policyReq.setFeedback(StringUtils.isBlank(feedback) ? null : JSON.parseObject(feedback, Map.class));
+        policyReq.setSimulate(StringUtils.isBlank(simulate) ? null : JSON.parseObject(simulate, Map.class));
+        policyReq.setSessionId(sessionId);
+        HashMap<String, Object> map = new HashMap<>();
+        map.put("rows", decisionReqs);
+        //2024年6月25日14:16:05 增加报警是管控值报警还是标准值报警
+        if(WARNING_LEVEL_ONE.equals(xinyiWarningRecord.getLevel()) || WARNING_LEVEL_TWO.equals(xinyiWarningRecord.getLevel()))
+            map.put("source", "bzz");
+        else
+            map.put("source", "gkz");
+        policyReq.setExtra(map);
+        return JSON.toJSONString(policyReq, JSONWriter.Feature.WriteNulls);
+    }
+
+    private void addChatRecord(ChatReq chatReq, String sessionId, int type, String warningId, String question, List<String> resultData) {
         chatReq.setSessionId(sessionId);
         chatReq.setType(type);//类型(0问答 1决策 2本地 3仿真预测)
         chatReq.setModule(3);//0=专家问答,1=智能工单,2=智能体助手,3.告警
@@ -230,7 +234,7 @@ public class GRPCController extends BaseController {
         List<TXinyiChatRecord> tXinyiChatRecords = this.xinyiChatRecordMapper.selectTXinyiChatRecordList(TXinyiChatRecord.builder().warningId(warningId).build());
         if(!CollectionUtils.isEmpty(tXinyiChatRecords))
             chatReq.setShowVal(tXinyiChatRecords.get(0).getShowVal());
-        chatReq.setQuestion(dataJson);
+        chatReq.setQuestion(question);
         chatReq.setAnswer(JSON.toJSONString(resultData));
         chatReq.setWarningId(warningId);
         chatReq.setCounts(1);//问答次数
@@ -409,14 +413,7 @@ public class GRPCController extends BaseController {
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
 //            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是LibraAI水务大模型,由红杉天枰开发的水务垂直大语言模型,能够提供水务行业专家问答、智能决策、报表分析、智能工单管理等一系列功能,作为水务人的AI助手,你会竭尽全力帮助我处理工作问题。\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (isStrong == 1) + "}";
             //2024年6月25日18:12:23 优化,不再使用拼接JSON字符串
-            ChatRequest chatRequest = new ChatRequest();
-            chatRequest.setSessionId(sessionId);
-            chatRequest.setHistoryDia(historyDates);
-            Map<String, Object> extra = new HashMap<>();
-            extra.put("ip_address", ipAddr);
-            chatRequest.setExtra(extra);
-            chatRequest.setStrengthen(isStrong == 1);
-            String dataJson = JSON.toJSONString(chatRequest);
+            String dataJson = buildBigModelReqForChat(sessionId, historyDates, ipAddr, isStrong);
             log.info("请求大模型的问答参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
@@ -475,6 +472,16 @@ public class GRPCController extends BaseController {
 //        return AjaxResult.success("ok");
     }
 
+    private static String buildBigModelReqForChat(String sessionId, List<String> historyDates, String ipAddr, int isStrong) {
+        ChatRequest chatRequest = new ChatRequest();
+        chatRequest.setSessionId(sessionId);
+        chatRequest.setHistoryDia(historyDates);
+        Map<String, Object> extra = new HashMap<>();
+        extra.put("ip_address", ipAddr);
+        chatRequest.setExtra(extra);
+        chatRequest.setStrengthen(isStrong == 1);
+        return JSON.toJSONString(chatRequest);
+    }
 
 
     private String buildGDQuestionCustom(List<WorkOrderRes> workOrderRes, WorkOrderReq workOrderReq) {
@@ -717,8 +724,10 @@ public class GRPCController extends BaseController {
                     .usePlaintext()
                     .build();
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
-            String dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"出水氨氮\",\"session_id\":\" " + IdUtils.simpleUUID() + "  \",\"extra\":{}}";
-            log.info("请求大模型的问答参数为{}", dataJson);
+//            String dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"出水氨氮\",\"session_id\":\" " + IdUtils.simpleUUID() + "  \",\"extra\":{}}";
+            //2024年6月27日13:23:25  优化:改成非拼接JSON字符串
+            String dataJson = buildBigModelReqForPredictor("出水氨氮");
+            log.info("请求大模型的预测的参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
                     .putInput("method", ByteString.copyFrom("predictor", "utf-8"))//推理
@@ -739,5 +748,12 @@ public class GRPCController extends BaseController {
         }
     }
 
+    private static String buildBigModelReqForPredictor(String type) {
+        PredictorRequest predictorRequest = new PredictorRequest();
+        predictorRequest.setNorm(type);
+        predictorRequest.setSessionId(IdUtils.simpleUUID());
+        return JSON.toJSONString(predictorRequest);
+    }
+
 
 }

+ 16 - 1
slibra-quartz/src/main/java/com/slibra/quartz/task/AsyncTask.java

@@ -7,6 +7,7 @@ import com.google.protobuf.ByteString;
 import com.slibra.business.domain.*;
 import com.slibra.business.mapper.*;
 import com.slibra.business.req.ChatReq;
+import com.slibra.business.req.ChatRequest;
 import com.slibra.business.req.DecisionReq;
 import com.slibra.business.req.PolicyReq;
 import com.slibra.business.res.ShowValueCSBasic;
@@ -800,7 +801,9 @@ public class AsyncTask {
                     .usePlaintext()
                     .build();
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
-            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+//            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+            //2024年6月27日13:29:18 优化,不再使用拼接JSON字符串
+            String dataJson = buildBigModelReqForChat(sessionId, historyDates, ipAddr, true);
             log.info("请求大模型的问答参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
@@ -841,6 +844,18 @@ public class AsyncTask {
         }
     }
 
+
+    private static String buildBigModelReqForChat(String sessionId, List<String> historyDates, String ipAddr, Boolean strengthen) {
+        ChatRequest chatRequest = new ChatRequest();
+        chatRequest.setSessionId(sessionId);
+        chatRequest.setHistoryDia(historyDates);
+        Map<String, Object> extra = new HashMap<>();
+        extra.put("ip_address", ipAddr);
+        chatRequest.setExtra(extra);
+        chatRequest.setStrengthen(strengthen);
+        return JSON.toJSONString(chatRequest);
+    }
+
     /**
      * 调用大模型获取决策结果 并同时记录对应信息到聊天记录表中
      *

+ 30 - 3
slibra-quartz/src/main/java/com/slibra/quartz/task/RyTask.java

@@ -11,6 +11,8 @@ import com.google.protobuf.ByteString;
 import com.slibra.business.domain.*;
 import com.slibra.business.mapper.*;
 import com.slibra.business.req.ChatReq;
+import com.slibra.business.req.ChatRequest;
+import com.slibra.business.req.PredictorRequest;
 import com.slibra.business.res.ChartBasic;
 import com.slibra.business.res.ShowValueChartBasic;
 import com.slibra.business.service.ITXinyiForecastComparisonService;
@@ -19,6 +21,7 @@ import com.slibra.common.constant.MyConstants;
 import com.slibra.common.enums.BusinessEnum;
 import com.slibra.common.enums.DataSourceType;
 import com.slibra.common.utils.DateUtils;
+import com.slibra.common.utils.ip.IpUtils;
 import com.slibra.common.utils.uuid.IdUtils;
 import com.slibra.framework.datasource.DynamicDataSourceContextHolder;
 import inference.InferenceAPIsServiceGrpc;
@@ -1222,7 +1225,9 @@ public class RyTask
                     .usePlaintext()
                     .build();
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
-            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+//            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+            //2024年6月27日10:51:47  优化:不再手动拼接JSON字符串
+            String dataJson = buildBigModelReqForChat(sessionId, historyDates);
             log.info("请求大模型的问答参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
@@ -1263,6 +1268,17 @@ public class RyTask
         }
     }
 
+    private static String buildBigModelReqForChat(String sessionId, List<String> historyDates) {
+        ChatRequest chatRequest = new ChatRequest();
+        chatRequest.setSessionId(sessionId);
+        chatRequest.setHistoryDia(historyDates);
+        Map<String, Object> extra = new HashMap<>();
+        extra.put("ip_address", IpUtils.getIpAddr());
+        chatRequest.setExtra(extra);
+        chatRequest.setStrengthen(true);
+        return JSON.toJSONString(chatRequest);
+    }
+
 
     public static String getPredictor(String type){
         // 获取输出流
@@ -1272,7 +1288,9 @@ public class RyTask
                     .usePlaintext()
                     .build();
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
-            String dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + type + "\",\"session_id\":\" " + IdUtils.simpleUUID() + "  \",\"extra\":{}}";
+//            String dataJson = "{\"bot_id\":\"b00001\",\"exp_id\":\"721\",\"norm\":\"" + type + "\",\"session_id\":\" " + IdUtils.simpleUUID() + "  \",\"extra\":{}}";
+            //2024年6月27日13:23:25  优化:改成非拼接JSON字符串
+            String dataJson = buildBigModelReqForPredictor(type);
             log.info("请求大模型的预测的参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")
@@ -1297,6 +1315,13 @@ public class RyTask
         }
     }
 
+    private static String buildBigModelReqForPredictor(String type) {
+        PredictorRequest predictorRequest = new PredictorRequest();
+        predictorRequest.setNorm(type);
+        predictorRequest.setSessionId(IdUtils.simpleUUID());
+        return JSON.toJSONString(predictorRequest);
+    }
+
     /**
      *
      * 2022/01/01 转成2022年01月01日 数据
@@ -1330,7 +1355,9 @@ public class RyTask
                     .usePlaintext()
                     .build();
             InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
-            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+//            String dataJson = "{\"bot_id\":\"721\",\"exp_id\":\"721\",\"session_id\":\"" + sessionId + "\",\"use_rag\":\"true\",\"prompt\":\"你是⼀个资深⽔务领域专家,能回答各种⽔务相关问题\",\"history_dia\":" + JSON.toJSONString(historyDates) + ",\"generate_args\":{\"max_new_tokens\":2048,\"max_length\":4096,\"num_beams\":1,\"do_sample\":true,\"top_p\":0.7,\"temperature\":0.95},\"extra\":{ \"ip_address\": \"" + ipAddr  + "\" },\"strengthen\":" + (true) + "}";
+            //2024年6月27日10:51:47  优化:不再手动拼接JSON字符串
+            String dataJson = buildBigModelReqForChat(sessionId, historyDates);
             log.info("请求大模型的问答参数为{}", dataJson);
             PredictionsRequest request = PredictionsRequest.newBuilder()
                     .setModelName("slibra_bot")

+ 37 - 0
slibra-system/src/main/java/com/slibra/business/req/PredictorRequest.java

@@ -0,0 +1,37 @@
+package com.slibra.business.req;
+
+import com.alibaba.fastjson2.annotation.JSONField;
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+@Data
+@Builder
+@AllArgsConstructor
+@NoArgsConstructor
+public class PredictorRequest {
+
+    @JSONField(name = "bot_id")
+    private String botId = "b00001";
+
+    @JSONField(name = "exp_id")
+    private String expId = "721";
+
+    @JSONField(name = "norm")
+    private String norm;
+
+    @JSONField(name = "session_id")
+    private String sessionId;
+
+    @JSONField(name = "extra")
+    private Map<String, Object> extra = new HashMap<>();
+
+
+
+}