|
@@ -4,6 +4,8 @@ import cn.hutool.core.util.IdUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.util.StringUtils;
|
|
@@ -12,21 +14,19 @@ import space.anyi.BI.entity.Chart;
|
|
|
import space.anyi.BI.entity.dto.ChartDTO;
|
|
|
import space.anyi.BI.entity.vo.ChartVO;
|
|
|
import space.anyi.BI.entity.vo.PageVO;
|
|
|
-import space.anyi.BI.entity.xinghuo.HttpRequestData;
|
|
|
-import space.anyi.BI.entity.xinghuo.HttpResponseData;
|
|
|
+import space.anyi.BI.entity.deepseek.DeepSeekHttpRequestData;
|
|
|
+import space.anyi.BI.entity.deepseek.DeepSeekHttpResponseData;
|
|
|
import space.anyi.BI.exception.SystemException;
|
|
|
import space.anyi.BI.service.ChartService;
|
|
|
import space.anyi.BI.mapper.ChartMapper;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
-import space.anyi.BI.util.AiUtil;
|
|
|
-import space.anyi.BI.util.BeanCopyUtil;
|
|
|
-import space.anyi.BI.util.ExcelUtils;
|
|
|
-import space.anyi.BI.util.SecurityUtils;
|
|
|
+import space.anyi.BI.util.*;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.IOException;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
import java.util.concurrent.RejectedExecutionException;
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
|
|
|
|
@@ -41,6 +41,8 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
|
private final static Logger log = LoggerFactory.getLogger(ChartServiceImpl.class);
|
|
|
@Resource
|
|
|
private ThreadPoolExecutor threadPoolExecutor;
|
|
|
+ @Resource
|
|
|
+ private ObjectMapper objectMapper;
|
|
|
|
|
|
/**
|
|
|
* 异步使用AI生成图表
|
|
@@ -90,24 +92,31 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
|
message.append(chartDTO.getAnalysisTarget());
|
|
|
message.append("\n.使用").append(chartDTO.getChartType()).append("进行可视化分析.\n");
|
|
|
//配置prompt向AI发送请求
|
|
|
- HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
|
- HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
|
- //解析AI返回的数据
|
|
|
- String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
- log.info("AI返回的数据为:{}", content);
|
|
|
- int index = content.indexOf("```");
|
|
|
- int endIndex = content.lastIndexOf("```");
|
|
|
- if (index == -1 || endIndex == -1){
|
|
|
+ DeepSeekHttpRequestData requestData = DeepSeekAIUtil.createDefaultRequestData(message.toString());
|
|
|
+
|
|
|
+ //AI生成的指定内容
|
|
|
+ Map contentMap = null;
|
|
|
+ try {
|
|
|
+ DeepSeekHttpResponseData responseData = DeepSeekAIUtil.doChat(requestData);
|
|
|
+ //解析AI返回的数据
|
|
|
+ String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
+ log.info("AI返回的数据为:{}", content);
|
|
|
+ contentMap = objectMapper.readValue(content, Map.class);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
chart.setState("失败");
|
|
|
chart.setExecuteMessage("AI生成图表失败");
|
|
|
updateById(chart);
|
|
|
throw new SystemException(500, "AI生成图表失败");
|
|
|
+ }catch (SystemException e){
|
|
|
+ chart.setState("失败");
|
|
|
+ chart.setExecuteMessage("AI生成图表失败");
|
|
|
+ updateById(chart);
|
|
|
+ throw e;
|
|
|
}
|
|
|
//数据可视化,Echarts的option代码
|
|
|
- chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
- index = endIndex;
|
|
|
+ chart.setGeneratedChartData(contentMap.get("option").toString());
|
|
|
//分析结论
|
|
|
- chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
+ chart.setAnalysisConclusion(contentMap.get("conclusion").toString());
|
|
|
//保存到数据库
|
|
|
chart.setState("成功");
|
|
|
chart.setExecuteMessage("AI生成图表成功");
|
|
@@ -177,25 +186,30 @@ public class ChartServiceImpl extends ServiceImpl<ChartMapper, Chart>
|
|
|
chart.setExecuteMessage("AI正在生成图表");
|
|
|
updateById(chart);
|
|
|
//配置prompt向AI发送请求
|
|
|
- HttpRequestData requestData = AiUtil.createDefaultRequestData(message.toString());
|
|
|
- HttpResponseData responseData = AiUtil.doChat(requestData);
|
|
|
- //解析AI返回的数据
|
|
|
- //ChartVO chartVO = BeanCopyUtil.copyBean(chartDTO, ChartVO.class);
|
|
|
- String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
- log.info("AI返回的数据为:{}", content);
|
|
|
- int index = content.indexOf("```");
|
|
|
- int endIndex = content.lastIndexOf("```");
|
|
|
- if (index == -1 || endIndex == -1){
|
|
|
+ DeepSeekHttpRequestData requestData = DeepSeekAIUtil.createDefaultRequestData(message.toString());
|
|
|
+ //AI生成的指定内容
|
|
|
+ Map contentMap = null;
|
|
|
+ try {
|
|
|
+ DeepSeekHttpResponseData responseData = DeepSeekAIUtil.doChat(requestData);
|
|
|
+ //解析AI返回的数据
|
|
|
+ String content = responseData.getChoices().get(0).getMessage().getContent();
|
|
|
+ log.info("AI返回的数据为:{}", content);
|
|
|
+ contentMap = objectMapper.readValue(content, Map.class);
|
|
|
+ } catch (JsonProcessingException e) {
|
|
|
chart.setState("失败");
|
|
|
chart.setExecuteMessage("AI生成图表失败");
|
|
|
updateById(chart);
|
|
|
throw new SystemException(500, "AI生成图表失败");
|
|
|
+ }catch (SystemException e){
|
|
|
+ chart.setState("失败");
|
|
|
+ chart.setExecuteMessage("AI生成图表失败");
|
|
|
+ updateById(chart);
|
|
|
+ throw e;
|
|
|
}
|
|
|
//数据可视化,Echarts的option代码
|
|
|
- chart.setGeneratedChartData(content.substring(index+7, endIndex).trim());
|
|
|
- index = endIndex;
|
|
|
+ chart.setGeneratedChartData(contentMap.get("option").toString());
|
|
|
//分析结论
|
|
|
- chart.setAnalysisConclusion(content.substring(index+3).trim());
|
|
|
+ chart.setAnalysisConclusion(contentMap.get("conclusion").toString());
|
|
|
|
|
|
//更新数据库
|
|
|
chart.setState("成功");
|