|
@@ -749,4 +749,86 @@ public class GRPCController extends BaseController {
|
|
|
// }
|
|
|
// }
|
|
|
// }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 手动调⽤大模型决策接口(用户自定义请求数据)
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ResponseBody
|
|
|
+ @PostMapping(value = "/decisionBySelfData")
|
|
|
+ public String decisionBySelfData(@RequestBody ChatReq chatReq)
|
|
|
+// public void decisionStream(HttpServletResponse response, ChatReq chatReq)
|
|
|
+ {
|
|
|
+ log.info("手动调⽤大模型决策接口(用户自定义请求数据)");
|
|
|
+ // 获取输出流
|
|
|
+ ManagedChannel channel = null;
|
|
|
+ //2024年5月29日14:15:58 新增逻辑,判断报警状态是否已经结束了,如果结束了就直接模拟一个report返回给前端,不再调用大模型
|
|
|
+// StringBuilder sb = new StringBuilder();
|
|
|
+ //大模型结果 放入一个结合中
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ //决策和问答不一样 没有历史的概念 所以sessionId都是新的 次数都是1
|
|
|
+ String sessionId = IdUtils.simpleUUID();
|
|
|
+ String feedback = chatReq.getFeedback();
|
|
|
+ String simulate = chatReq.getSimulate();
|
|
|
+ int type = 3;//仿真预测
|
|
|
+ if(StringUtils.isBlank(simulate) || "{}".equals(simulate))
|
|
|
+ type = 1;//决策
|
|
|
+ //决策请求的业务参数
|
|
|
+ String rows = chatReq.getRows();
|
|
|
+ String dataJson = "";
|
|
|
+ try {
|
|
|
+ channel = ManagedChannelBuilder.forAddress("10.0.0.24", 17070)
|
|
|
+ .usePlaintext()
|
|
|
+ .build();
|
|
|
+ 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 + "}}";
|
|
|
+ log.info("请求大模型的决策的参数为{}", dataJson);
|
|
|
+ PredictionsRequest request = PredictionsRequest.newBuilder()
|
|
|
+ .setModelName("slibra_bot")
|
|
|
+ .putInput("method", ByteString.copyFrom("decision_stream", "utf-8"))//推理
|
|
|
+ .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
|
|
|
+ .buildPartial();
|
|
|
+ Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
|
|
|
+ while (predictions.hasNext()) {
|
|
|
+ String responseStr = predictions.next().getPrediction().toStringUtf8();
|
|
|
+ log.info("决策流式返回的结果是{}", responseStr);
|
|
|
+ String message = JSON.parseObject(responseStr).getString("message");
|
|
|
+ //2024年5月25日16:37:16 按照大模型返回的类型解析数据
|
|
|
+ String biz = JSON.parseObject(responseStr).getString("biz");
|
|
|
+// String message = JSON.parseObject(responseStr).getString("message");
|
|
|
+// String message = JSON.parseObject(responseStr).getString("message");
|
|
|
+ if(BusinessEnum.BigModelBizEnum.OK.getCode().equals(biz)){
|
|
|
+ log.info("结尾语句并且是非JSON,无需处理,返回数据为{}", responseStr);
|
|
|
+ //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
|
|
|
+ /*outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();*/
|
|
|
+ }else if(BusinessEnum.BigModelBizEnum.DECISION_DEBUGGER.getCode().equals(biz)){
|
|
|
+ log.info("中间过程,目前只打印日志,不记录数据,也不返回给前端,返回数据为{}", responseStr);
|
|
|
+ //结束语句也流式输出,但是并不记录下来 2024年5月24日11:15:23 也不返回前端
|
|
|
+ /*outputStream.write(responseStr.getBytes());
|
|
|
+ outputStream.flush();*/
|
|
|
+ }else{//其他 要么错误 要么alert 要么出的报告
|
|
|
+// sb.append(responseStr);
|
|
|
+ if(StringUtils.isBlank(message)){
|
|
|
+ log.error("×××××××××××××××××××××××××××为空的数据暂时不返回×××××××××××××××××××××××××××");
|
|
|
+ }else {
|
|
|
+ if(BusinessEnum.BigModelBizEnum.DECISION_ALERT.getCode().equals(biz)
|
|
|
+ || BusinessEnum.BigModelBizEnum.DECISION_REPORT.getCode().equals(biz)){
|
|
|
+ sb.append(message).append(",");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+ log.error("处理大模型推理异常,异常信息为{}", JSON.toJSONString(e));
|
|
|
+ //出现异常 给前端返回一句话
|
|
|
+ return "大模型调用失败,异常原因为:" + e.getMessage();
|
|
|
+ } finally {
|
|
|
+ channel.shutdown();
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
}
|