|
@@ -1,5 +1,6 @@
|
|
package com.slibra.web.controller.business;
|
|
package com.slibra.web.controller.business;
|
|
|
|
|
|
|
|
+import com.alibaba.fastjson.serializer.SerializerFeature;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.alibaba.fastjson2.JSONWriter;
|
|
import com.alibaba.fastjson2.JSONWriter;
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSON;
|
|
@@ -921,6 +922,131 @@ public class GRPCController extends BaseController {
|
|
// return AjaxResult.success("ok");
|
|
// return AjaxResult.success("ok");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * RAG+⼤模型的调⽤参数
|
|
|
|
+ * 2024年6月17日09:40:38 工单新增 自定义工单处理
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ @PostMapping(value = "/inferStreamChat")
|
|
|
|
+ public void inferStreamChat(HttpServletRequest httpServletRequest, HttpServletResponse response, @RequestBody ChatStreamRequest chatReq) {
|
|
|
|
+
|
|
|
|
+ log.info("进入了调⽤RAG+⼤模型的调⽤参数");
|
|
|
|
+ long time1 = System.currentTimeMillis();
|
|
|
|
+ log.info("开始进来的时间是{}",time1);
|
|
|
|
+ // 获取输出流
|
|
|
|
+ OutputStream outputStream = null;
|
|
|
|
+ ManagedChannel channel = null;
|
|
|
|
+ response.setContentType("text/event-stream");
|
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
|
+
|
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
|
+ String sessionId = IdUtils.simpleUUID();
|
|
|
|
+ String ipAddr = IpUtils.getIpAddr();//获取用户的ip地址 传给大模型
|
|
|
|
+ String headerPort = httpServletRequest.getHeader(port);
|
|
|
|
+ long time2 = System.currentTimeMillis();
|
|
|
|
+ log.info("开始调用大模型的的时间是{}",time2);
|
|
|
|
+ log.info("调用大模型之前,处理历史会话等操作的时间为{}", (time2 - time1)/1000);
|
|
|
|
+ try {
|
|
|
|
+ channel = ManagedChannelBuilder.forAddress(bigModelConfig.getIp(), StringUtils.isBlank(headerPort) ? bigModelConfig.getPort() : Integer.parseInt(headerPort))
|
|
|
|
+ .usePlaintext()
|
|
|
|
+ .build();
|
|
|
|
+ InferenceAPIsServiceGrpc.InferenceAPIsServiceBlockingStub stub = InferenceAPIsServiceGrpc.newBlockingStub(channel);
|
|
|
|
+ String dataJson = buildBigModelReqForChatStreamReq(sessionId, ipAddr, chatReq);
|
|
|
|
+
|
|
|
|
+ log.info("******请求大模型的问答参数为{}", dataJson);
|
|
|
|
+ PredictionsRequest request = PredictionsRequest.newBuilder()
|
|
|
|
+ .setModelName("slibra_bot")
|
|
|
|
+ .putInput("method", ByteString.copyFrom("infer_stream", "utf-8"))//推理
|
|
|
|
+ .putInput("data", ByteString.copyFrom(dataJson, "utf-8"))
|
|
|
|
+ .buildPartial();
|
|
|
|
+
|
|
|
|
+ ChatStreamResponse resp = new ChatStreamResponse();
|
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ Iterator<PredictionResponse> predictions = stub.streamPredictions(request);
|
|
|
|
+ //将结果记录到问答表
|
|
|
|
+ while (predictions.hasNext()) {
|
|
|
|
+ String responseStr = predictions.next().getPrediction().toStringUtf8();
|
|
|
|
+// log.info("大模型问答返回的原始结果为{}", responseStr);
|
|
|
|
+ if (StringUtils.isBlank(responseStr)) {
|
|
|
|
+ log.error("大模型返回的是空,无法解析");
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ responseStr = JSON.parseObject(responseStr).getString("message");
|
|
|
|
+ if (!"complete".equals(responseStr)) {
|
|
|
|
+ sb.append(responseStr);
|
|
|
|
+ }
|
|
|
|
+ if (chatReq.getStream()) {
|
|
|
|
+ List<Map<String, String>> list = new ArrayList<>();
|
|
|
|
+ Map<String, String> member = new HashMap<>();
|
|
|
|
+ list.add(member);
|
|
|
|
+ if ("complete".equals(responseStr)) {
|
|
|
|
+ member.put("delta", "");
|
|
|
|
+ member.put("finish_reason", "stop");
|
|
|
|
+ } else {
|
|
|
|
+ member.put("delta", responseStr);
|
|
|
|
+ member.put("finish_reason", "");
|
|
|
|
+ }
|
|
|
|
+ resp.setChoices(JSON.toJSONString(list));
|
|
|
|
+ outputStream.write(JSON.toJSONString(resp).getBytes());
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (!chatReq.getStream()) {
|
|
|
|
+ resp.setContent(sb.toString());
|
|
|
|
+ outputStream.write(JSON.toJSONString(resp).getBytes());
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ }
|
|
|
|
+ }catch (Throwable e){
|
|
|
|
+ log.error("inferStreamChat:exception, session_id={}", sessionId, e);
|
|
|
|
+ resp.setStatus("failed");
|
|
|
|
+ resp.setReason("调用服务超时");
|
|
|
|
+ outputStream.write(JSON.toJSONString(resp).getBytes());
|
|
|
|
+ outputStream.flush();
|
|
|
|
+ }
|
|
|
|
+ long time3 = System.currentTimeMillis();
|
|
|
|
+ log.info("结束调用大模型的的时间是{}",time3);
|
|
|
|
+ log.info("调用的时间为{}", (time3 - time2)/1000);
|
|
|
|
+
|
|
|
|
+// //将问答更新到数据库中
|
|
|
|
+ List<ChatStreamDialogue> dialogues = chatReq.getDialogue();
|
|
|
|
+ ChatStreamDialogue lastDialogue = dialogues.size()>1 ? dialogues.get(dialogues.size()-1) : null;
|
|
|
|
+ chatReq.setQuestion(Objects.isNull(lastDialogue) ? "" : lastDialogue.getContent());
|
|
|
|
+ chatReq.setSessionId(sessionId);
|
|
|
|
+ chatReq.setAnswer(sb.toString());
|
|
|
|
+ chatReq.setType(0);//0问答 1决策 2本地 3仿真预测
|
|
|
|
+ chatReq.setModule(0);//0专家问答 1智能工单 2智能体助手 3告警 4简报
|
|
|
|
+ //2024年5月28日10:58:02 由于部分问题 展示的和调用大模型的不一样,所以这个由前端传过来
|
|
|
|
+// chatReq.setShowVal(question);
|
|
|
|
+ chatReq.setCounts(chatReq.getDialogue().size());//问答次数
|
|
|
|
+ String userId = SecurityUtils.getUserId().toString();
|
|
|
|
+ String username = SecurityUtils.getUsername();
|
|
|
|
+ chatReq.setUserId(userId);
|
|
|
|
+ chatReq.setCreateBy(username);
|
|
|
|
+ chatReq.setCreateTime(DateUtils.getNowDate());
|
|
|
|
+ this.chatRecordMapper.insertTXinyiChatRecord(chatReq);
|
|
|
|
+// outputStream.write((DEFAULT_ID_IDENTIFIER + chatReq.getId()).getBytes());
|
|
|
|
+// outputStream.flush();
|
|
|
|
+
|
|
|
|
+ long time4 = System.currentTimeMillis();
|
|
|
|
+ log.info("最后操作的时间是{}",time4);
|
|
|
|
+ log.info("最后操作的时间是{}", (time4 - time3)/1000);
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ } finally {
|
|
|
|
+ // 关闭输出流
|
|
|
|
+ try {
|
|
|
|
+ outputStream.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ throw new RuntimeException(e);
|
|
|
|
+ }finally {
|
|
|
|
+ channel.shutdown();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // return AjaxResult.success("ok");
|
|
|
|
+ }
|
|
|
|
|
|
private String buildGDCustomQuestion(String timeBegin, String timeEnd, ChatReq chatReq) {
|
|
private String buildGDCustomQuestion(String timeBegin, String timeEnd, ChatReq chatReq) {
|
|
//存放的JSON
|
|
//存放的JSON
|
|
@@ -1052,6 +1178,35 @@ public class GRPCController extends BaseController {
|
|
return JSON.toJSONString(map);
|
|
return JSON.toJSONString(map);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private String buildBigModelReqForChatStreamReq(String sessionId, String ipAddr, ChatStreamRequest req){
|
|
|
|
+ List<String> historyDatas = new ArrayList<>();
|
|
|
|
+ for (ChatStreamDialogue dialogue :req.getDialogue()){
|
|
|
|
+ historyDatas.add(dialogue.getContent());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ ChatRequest chatRequest = new ChatRequest();
|
|
|
|
+ chatRequest.setSessionId(sessionId);
|
|
|
|
+ chatRequest.setHistoryDia(historyDatas);
|
|
|
|
+ //2024年7月5日13:24:10 temperature做区分
|
|
|
|
+ GenerateArgs generateArgs = new GenerateArgs();
|
|
|
|
+ generateArgs.setTemperature(bigModelConfig.getTemperature());
|
|
|
|
+ generateArgs.setTopP(bigModelConfig.getTopP());
|
|
|
|
+ generateArgs.setMaxNewTokens(req.getMax_tokens());
|
|
|
|
+ chatRequest.setGenerateArgs(generateArgs);
|
|
|
|
+ Map<String, Object> extra = new HashMap<>();
|
|
|
|
+ extra.put("ip_address", ipAddr);
|
|
|
|
+ chatRequest.setExtra(extra);
|
|
|
|
+ chatRequest.setStrengthen(Boolean.FALSE);
|
|
|
|
+ chatRequest.setUseRag(Boolean.TRUE);
|
|
|
|
+ //2024年08月11日16:04:21 允许用户自定义prompt
|
|
|
|
+ chatRequest.setPrompt(DEFAULT_PROMPT);
|
|
|
|
+ //2025年02月24日11:41:38 新增的 区分模型的
|
|
|
|
+ chatRequest.setModelType(0);
|
|
|
|
+ //2025年02月27日10:58:16 新增的 是否使用在线搜索
|
|
|
|
+ chatRequest.setOnlineSearch(Boolean.FALSE);
|
|
|
|
+ return JSON.toJSONString(chatRequest);
|
|
|
|
+ }
|
|
|
|
+
|
|
private String buildBigModelReqForChat(String sessionId, List<String> historyDates, String ipAddr, int isStrong, Double topP, Double temperature, String tools, boolean useRag, String extraStr, String prompt, int modelType, Boolean onlineSearch) {
|
|
private String buildBigModelReqForChat(String sessionId, List<String> historyDates, String ipAddr, int isStrong, Double topP, Double temperature, String tools, boolean useRag, String extraStr, String prompt, int modelType, Boolean onlineSearch) {
|
|
ChatRequest chatRequest = new ChatRequest();
|
|
ChatRequest chatRequest = new ChatRequest();
|
|
chatRequest.setSessionId(sessionId);
|
|
chatRequest.setSessionId(sessionId);
|