Commit ec23cc35 authored by alex yao's avatar alex yao

feat[智能问数]:

1.修改对话接口流程
2.新增生成洞察报告和查询接口
3.智能问数示例接口[猜你想问,数据库示例,文件示例]
parent 1a46b7b2
...@@ -18,6 +18,38 @@ public interface AiBiService { ...@@ -18,6 +18,38 @@ public interface AiBiService {
* @param databaseIds 数据库ids * @param databaseIds 数据库ids
* @param userId 用户id * @param userId 用户id
*/ */
@Deprecated
void call(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception; void call(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception;
/**
* 调用智能问数生成式BI服务
*
* @param dialoguesId 对话id
* @param input 问题
* @param fileUrl 文件地址
* @param knowledgeIds 知识库ids
* @param databaseIds 数据库ids
* @param userId 用户id
*/
void callV2(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception;
/**
* 生成洞察报告
*
* @param dialoguesId
* @param fileUrl
* @param SQL
* @param result
* @param question
*/
void generateInsightReport(String dialoguesId, String fileUrl, String SQL, String result, String question) throws Exception;
/**
* 获取洞察报告
*
* @param dialoguesId
*/
String getInsightReport(String dialoguesId);
} }
...@@ -4,21 +4,46 @@ import cn.com.poc.agent_application.aggregate.AgentApplicationService; ...@@ -4,21 +4,46 @@ import cn.com.poc.agent_application.aggregate.AgentApplicationService;
import cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants; import cn.com.poc.agent_application.constant.AgentApplicationDialoguesRecordConstants;
import cn.com.poc.agent_application.entity.*; import cn.com.poc.agent_application.entity.*;
import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService; import cn.com.poc.agent_application.service.BizAgentApplicationDialoguesRecordService;
import cn.com.poc.agent_application.service.BizAgentApplicationGcConfigService;
import cn.com.poc.agent_application.service.BizAgentApplicationPublishService; import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.agent_application.utils.AgentApplicationTools; import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.ai_bi.aggregate.AiBiService; import cn.com.poc.ai_bi.aggregate.AiBiService;
import cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity; import cn.com.poc.ai_dialogues.entity.BizAiDialoguesEntity;
import cn.com.poc.ai_dialogues.service.BizAiDialoguesService; import cn.com.poc.ai_dialogues.service.BizAiDialoguesService;
import cn.com.poc.common.constant.CommonConstant; import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.service.RedisService;
import cn.com.poc.common.utils.DocumentLoad;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.common.utils.SSEUtil;
import cn.com.poc.common.utils.StringUtils; import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.knowledge.aggregate.KnowledgeService; import cn.com.poc.knowledge.aggregate.KnowledgeService;
import cn.com.poc.knowledge.entity.BizKnowledgeDatabaseEntity;
import cn.com.poc.knowledge.service.BizKnowledgeDatabaseService;
import cn.com.poc.long_document.domain.LongtextDialoguesResult;
import cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum; import cn.com.poc.thirdparty.resource.demand.ai.constants.KnowledgeSearchTypeEnum;
import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.csvchain.CSVChainResponse;
import cn.com.poc.thirdparty.resource.demand.ai.entity.csvchain.CSVChainResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResponse;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message; import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool; import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.ToolFunction;
import cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResult; import cn.com.poc.thirdparty.resource.demand.ai.entity.function.FunctionCallResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDemandResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractFunctionResult;
import cn.com.poc.thirdparty.resource.demand.ai.function.echart.EChartGenerateFunction;
import cn.com.poc.thirdparty.service.ChainService;
import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.frame.service.FmxParamConfigService; import cn.com.yict.framemax.frame.service.FmxParamConfigService;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
...@@ -27,8 +52,11 @@ import org.springframework.web.context.request.ServletRequestAttributes; ...@@ -27,8 +52,11 @@ import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* @author alex.yao * @author alex.yao
...@@ -40,6 +68,9 @@ public class AiBiServiceImpl implements AiBiService { ...@@ -40,6 +68,9 @@ public class AiBiServiceImpl implements AiBiService {
private final Logger logger = LoggerFactory.getLogger(AiBiServiceImpl.class); private final Logger logger = LoggerFactory.getLogger(AiBiServiceImpl.class);
private final String INSIGHT_REPORT_REDIS_PREFIX = "INSIGHT_REPORT:";
private final String FMX_PARAM_AGENT_ID_CODE = "ai_bi.agent_id"; private final String FMX_PARAM_AGENT_ID_CODE = "ai_bi.agent_id";
@Resource @Resource
...@@ -60,6 +91,24 @@ public class AiBiServiceImpl implements AiBiService { ...@@ -60,6 +91,24 @@ public class AiBiServiceImpl implements AiBiService {
@Resource @Resource
private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService; private BizAgentApplicationDialoguesRecordService bizAgentApplicationDialoguesRecordService;
@Resource
private ChainService chainService;
@Resource
private BizKnowledgeDatabaseService bizKnowledgeDatabaseService;
@Resource
private LLMService llmService;
@Resource
private RedisService redisService;
@Resource
private EChartGenerateFunction eChartGenerateFunction;
@Resource
private BizAgentApplicationGcConfigService bizAgentApplicationGcConfigService;
@Override @Override
public void call(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception { public void call(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception {
...@@ -160,4 +209,306 @@ public class AiBiServiceImpl implements AiBiService { ...@@ -160,4 +209,306 @@ public class AiBiServiceImpl implements AiBiService {
outputRecord.setReasoningContent(agentResultEntity.getReasoningContent()); outputRecord.setReasoningContent(agentResultEntity.getReasoningContent());
bizAgentApplicationDialoguesRecordService.save(outputRecord); bizAgentApplicationDialoguesRecordService.save(outputRecord);
} }
@Override
public void callV2(String dialoguesId, String input, String fileUrl, Integer[] knowledgeIds, Integer[] databaseIds, Long userId) throws Exception {
if (databaseIds.length > 1) {
throw new BusinessException("仅支持单个数据库");
}
long inputTime = System.currentTimeMillis();
SSEUtil sseUtil = new SSEUtil();
CSVChainResult csvChainResult = new CSVChainResult();
List<DBChainResult> dbChainResults = new ArrayList<>();
String functionRecord = StringUtils.EMPTY;
BizAgentApplicationGcConfigEntity gcConfigEntity = bizAgentApplicationGcConfigService.getByConfigCode("AIBIPrompt");
if (gcConfigEntity == null) {
throw new BusinessException("无法找到【智能问数】配置");
}
//1. 文件/数据库 chain
if (ArrayUtils.isNotEmpty(databaseIds)) {
for (Integer databaseId : databaseIds) {
BizKnowledgeDatabaseEntity bizKnowledgeDatabaseEntity = bizKnowledgeDatabaseService.get(databaseId.longValue());
if (bizKnowledgeDatabaseEntity == null) {
continue;
}
DBChainResponse dbChainResponse = new DBChainResponse();
dbChainResponse.setQuestion(input);
dbChainResponse.setContext(null);
dbChainResponse.setPrompt(null);
dbChainResponse.setTableFilters(null);
dbChainResponse.setMysqlUser(bizKnowledgeDatabaseEntity.getDbUsername());
dbChainResponse.setMysqlPassword(bizKnowledgeDatabaseEntity.getDbPassword());
dbChainResponse.setMysqlHost(bizKnowledgeDatabaseEntity.getDbHost());
dbChainResponse.setMysqlPort(bizKnowledgeDatabaseEntity.getDbPort());
dbChainResponse.setMysqlDatabase(bizKnowledgeDatabaseEntity.getDbName());
DBChainResult dbChainResult = chainService.dbChain(dbChainResponse);
if (dbChainResult != null) {
dbChainResult.setSql("```SQL\n" + dbChainResult.getSql() + "\n```");
dbChainResults.add(dbChainResult);
}
}
//2. 若有数据, 执行[EChart Agent] 判断并生成EChart Option
if (CollectionUtils.isNotEmpty(dbChainResults)) {
DBChainResult dbChainResult = dbChainResults.get(0);
//输出SQL
LargeModelDemandResult result = new LargeModelDemandResult();
result.setCode("0");
result.setFunction(null);
result.setDbChainResult(dbChainResults);
result.setKnowledgeContentResult(null);
sseUtil.send(JsonUtils.serialize(result));
JSONObject echartJSONObject = new JSONObject();
echartJSONObject.put("sql", dbChainResult.getSql());
echartJSONObject.put("sql_result", dbChainResult.getSqlResult());
echartJSONObject.put("question", input);
AbstractFunctionResult<String> functionResult = eChartGenerateFunction.doFunction(echartJSONObject.toJSONString(), null, null, null);
String eChartOption = functionResult.getFunctionResult();
//输出EChart
if (eChartOption != null) {
JSONObject jsonObject = JSONObject.parseObject(eChartOption);
if (!jsonObject.containsKey("skip")) {
ToolFunction toolFunction = new ToolFunction();
toolFunction.setName("echart_function");
toolFunction.setResult(eChartOption);
toolFunction.setDisplayFormat("json");
toolFunction.setArguments(null);
result = new LargeModelDemandResult();
result.setCode("0");
result.setFunction(toolFunction);
result.setDbChainResult(null);
result.setKnowledgeContentResult(null);
String toolFunctionJson = JsonUtils.serialize(result);
sseUtil.send(toolFunctionJson);
functionRecord = toolFunctionJson;
}
}
}
} else if (StringUtils.isNotBlank(fileUrl)) {
CSVChainResponse csvChainResponse = new CSVChainResponse();
csvChainResponse.setQuestion(input);
csvChainResponse.setContext(null);
csvChainResponse.setFilePath(fileUrl);
csvChainResult = chainService.csvChain(csvChainResponse);
if (csvChainResult != null) {
String[] csvRes = JsonUtils.deSerialize(csvChainResult.getResult(), String[].class);
if (ArrayUtils.isNotEmpty(csvRes)) {
JSONObject echartJSONObject = new JSONObject();
echartJSONObject.put("sql", null);
echartJSONObject.put("sql_result", csvRes.toString());
echartJSONObject.put("question", input);
AbstractFunctionResult<String> functionResult = eChartGenerateFunction.doFunction(echartJSONObject.toJSONString(), null, null, null);
String eChartOption = functionResult.getFunctionResult();
functionRecord = outputECharts(eChartOption, sseUtil, functionRecord);
}
}
}
//3. 执行对话大模型
List<Message> messages = buildMessage(userId, dialoguesId, gcConfigEntity.getConfigSystem(), input, dbChainResults, csvChainResult);
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(gcConfigEntity.getLargeModel());
largeModelResponse.setMessages(messages.toArray(new Message[0]));
largeModelResponse.setStream(true);
largeModelResponse.setUser("AI_BI_CHAT");
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
LongtextDialoguesResult longtextDialoguesResult = textOutputStream(sseUtil, bufferedReader);
//保存对话记录
BizAgentApplicationDialoguesRecordEntity inputRecord = new BizAgentApplicationDialoguesRecordEntity();
inputRecord.setMemberId(userId);
inputRecord.setContent(input);
if (StringUtils.isNotBlank(fileUrl)) {
inputRecord.setFileUrl(fileUrl);
}
inputRecord.setDialogsId(dialoguesId);
inputRecord.setRole("user");
inputRecord.setTimestamp(inputTime);
// 保存AI回复记录
BizAgentApplicationDialoguesRecordEntity assistantRecord = new BizAgentApplicationDialoguesRecordEntity();
assistantRecord.setMemberId(userId);
assistantRecord.setContent(longtextDialoguesResult.getMessage());
assistantRecord.setReasoningContent(longtextDialoguesResult.getReasoningContent());
assistantRecord.setDialogsId(dialoguesId);
assistantRecord.setFunction(functionRecord);
assistantRecord.setRole("assistant");
assistantRecord.setTimestamp(System.currentTimeMillis());
bizAgentApplicationDialoguesRecordService.save(inputRecord);
bizAgentApplicationDialoguesRecordService.save(assistantRecord);
}
private String outputECharts(String eChartOption, SSEUtil sseUtil, String functionRecord) throws IOException {
//输出EChart
if (eChartOption != null) {
JSONObject jsonObject = JSONObject.parseObject(eChartOption);
if (!jsonObject.containsKey("skip")) {
ToolFunction toolFunction = new ToolFunction();
toolFunction.setName("echart_function");
toolFunction.setResult(eChartOption);
toolFunction.setDisplayFormat("json");
toolFunction.setArguments(null);
LargeModelDemandResult result = new LargeModelDemandResult();
result.setCode("0");
result.setFunction(toolFunction);
result.setDbChainResult(null);
result.setKnowledgeContentResult(null);
String toolFunctionJson = JsonUtils.serialize(result);
sseUtil.send(toolFunctionJson);
functionRecord = toolFunctionJson;
}
}
return functionRecord;
}
private List<Message> buildMessage(Long userId, String dialoguesId, String prompt, String input, List<DBChainResult> dbChainResults, CSVChainResult csvChainResult) throws Exception {
List<Message> messages = new ArrayList<>();
String databaseResult = StringUtils.EMPTY, csvResult = StringUtils.EMPTY;
if (CollectionUtils.isNotEmpty(dbChainResults)) {
databaseResult = JsonUtils.serialize(dbChainResults);
}
if (csvChainResult != null && csvChainResult.getResult() != null) {
csvResult = JsonUtils.serialize(csvChainResult);
}
prompt = prompt.replace("${csvResult}", csvResult)
.replace("${databaseResult}", databaseResult)
.replace("${question}", input);
Message systemMessage = new Message();
systemMessage.setRole(LLMRoleEnum.SYSTEM.getRole());
systemMessage.setContent(prompt);
messages.add(systemMessage);
messages.addAll(AgentApplicationTools.buildMessage(dialoguesId, null, userId, input));
return messages;
}
/**
* 文本输出结果
*
* @param sseUtil
* @param bufferedReader
* @throws IOException
*/
private LongtextDialoguesResult textOutputStream(SSEUtil sseUtil, BufferedReader bufferedReader) throws IOException {
String res = "";
StringBuilder output = new StringBuilder();
StringBuilder reasoningContent = new StringBuilder();
try {
while ((res = bufferedReader.readLine()) != null) {
if (org.apache.commons.lang3.StringUtils.isEmpty(res)) {
continue;
}
res = org.apache.commons.lang3.StringUtils.replace(res, "data: ", org.apache.commons.lang3.StringUtils.EMPTY);
LargeModelDemandResult result = JsonUtils.deSerialize(res, LargeModelDemandResult.class);
if (!"0".equals(result.getCode())) {
logger.error("LLM Error,code:{}", result.getCode());
BusinessException ex = new BusinessException("exception/call.failure");
result.setMessage(ex.getMessage());
sseUtil.send(JsonUtils.serialize(result));
sseUtil.send("[DONE]");
sseUtil.complete();
throw ex;
}
result.setFunction(null);
result.setDbChainResult(null);
result.setKnowledgeContentResult(null);
sseUtil.send(JsonUtils.serialize(result));
if (org.apache.commons.lang3.StringUtils.isNotEmpty(result.getMessage())) {
output.append(result.getMessage());
}
if (org.apache.commons.lang3.StringUtils.isNotEmpty(result.getReasoningContent())) {
reasoningContent.append(result.getReasoningContent());
}
}
// 关闭资源
sseUtil.send("[DONE]");
sseUtil.complete();
LongtextDialoguesResult longtextDialoguesResult = new LongtextDialoguesResult();
longtextDialoguesResult.setMessage(output.toString());
longtextDialoguesResult.setReasoningContent(reasoningContent.toString());
return longtextDialoguesResult;
} catch (IOException e) {
logger.error("连接断开,code:{}", e.getMessage());
sseUtil.completeByError("连接断开");
throw new BusinessException("连接断开");
} finally {
bufferedReader.close();
sseUtil.complete();
}
}
@Override
public void generateInsightReport(String dialoguesId, String fileUrl, String SQL, String result, String question) throws Exception {
String redisKey = INSIGHT_REPORT_REDIS_PREFIX + dialoguesId;
BizAgentApplicationGcConfigEntity gcConfigEntity = bizAgentApplicationGcConfigService.getByConfigCode("AIBIInsightReport");
if (gcConfigEntity == null) {
throw new BusinessException("无法找到【洞察报告生成】配置");
}
String prompt = gcConfigEntity.getConfigSystem();
if (StringUtils.isNotBlank(fileUrl)) {
// 获取文件表头
File file = DocumentLoad.downloadURLDocument(fileUrl);
try (Reader reader = new FileReader(file)) {
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim());
Map<String, Integer> headerMap = csvParser.getHeaderMap();
String headers = StringUtils.EMPTY;
if (MapUtils.isNotEmpty(headerMap)) {
Set<String> headerSet = headerMap.keySet();
headers = JsonUtils.serialize(headerSet);
}
prompt = prompt.replace("${question}", question)
.replace("${index}", headers)
.replace("${sql}", StringUtils.EMPTY)
.replace("${result}", result);
} catch (Exception e) {
throw new BusinessException("获取文件失败");
}
} else {
prompt = prompt.replace("${question}", question)
.replace("${index}", StringUtils.EMPTY)
.replace("${sql}", SQL)
.replace("${result}", result);
}
//生成洞察报告
String insightReport = StringUtils.EMPTY;
Message message = new Message();
message.setRole(LLMRoleEnum.USER.getRole());
message.setContent(prompt);
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setModel(gcConfigEntity.getLargeModel());
largeModelResponse.setMessages(new Message[]{message});
largeModelResponse.setStream(true);
largeModelResponse.setUser("GENERATE_INSIGHT_REPORT");
BufferedReader bufferedReader = llmService.chatChunk(largeModelResponse);
LongtextDialoguesResult longtextDialoguesResult = textOutputStream(new SSEUtil(), bufferedReader);
insightReport = longtextDialoguesResult.getMessage();
//保存
redisService.set(redisKey, insightReport);
}
@Override
public String getInsightReport(String dialoguesId) {
String redisKey = INSIGHT_REPORT_REDIS_PREFIX + dialoguesId;
if (redisService.hasKey(redisKey)) {
return redisService.get(redisKey).toString();
}
return StringUtils.EMPTY;
}
} }
package cn.com.poc.ai_bi.convert;
import cn.com.poc.ai_bi.model.BizAiBiExampleModel;
import cn.com.poc.ai_bi.entity.BizAiBiExampleEntity;
import cn.com.poc.ai_bi.dto.BizAiBiExampleDto;
public class BizAiBiExampleConvert {
public static BizAiBiExampleEntity modelToEntity(BizAiBiExampleModel model){
BizAiBiExampleEntity entity = new BizAiBiExampleEntity();
entity.setId(model.getId());
entity.setTitle(model.getTitle());
entity.setDataType(model.getDataType());
entity.setDataRelation(model.getDataRelation());
entity.setExampleDesc(model.getExampleDesc());
entity.setQuestion(model.getQuestion());
return entity;
}
public static BizAiBiExampleModel entityToModel(BizAiBiExampleEntity entity){
BizAiBiExampleModel model = new BizAiBiExampleModel();
model.setId(entity.getId());
model.setTitle(entity.getTitle());
model.setDataType(entity.getDataType());
model.setDataRelation(entity.getDataRelation());
model.setExampleDesc(entity.getExampleDesc());
model.setQuestion(entity.getQuestion());
return model;
}
public static BizAiBiExampleDto entityToDto(BizAiBiExampleEntity entity){
BizAiBiExampleDto dto = new BizAiBiExampleDto();
dto.setId(entity.getId());
dto.setTitle(entity.getTitle());
dto.setDataType(entity.getDataType());
dto.setDataRelation(entity.getDataRelation());
dto.setExampleDesc(entity.getExampleDesc());
dto.setQuestion(entity.getQuestion());
return dto;
}
public static BizAiBiExampleEntity dtoToEntity(BizAiBiExampleDto dto){
BizAiBiExampleEntity entity = new BizAiBiExampleEntity();
entity.setId(dto.getId());
entity.setTitle(dto.getTitle());
entity.setDataType(dto.getDataType());
entity.setDataRelation(dto.getDataRelation());
entity.setExampleDesc(dto.getExampleDesc());
entity.setQuestion(dto.getQuestion());
return entity;
}
}
\ No newline at end of file
package cn.com.poc.ai_bi.dto;
import java.util.List;
/**
* @author 52747
* @date 2025/8/20
*/
public class AIBiExampleDto {
private List<BizAiBiExampleDto> featuredExamples;
private List<BizAiBiExampleDto> databaseExamples;
private List<BizAiBiExampleDto> fileExamples;
public List<BizAiBiExampleDto> getFeaturedExamples() {
return featuredExamples;
}
public void setFeaturedExamples(List<BizAiBiExampleDto> featuredExamples) {
this.featuredExamples = featuredExamples;
}
public List<BizAiBiExampleDto> getDatabaseExamples() {
return databaseExamples;
}
public void setDatabaseExamples(List<BizAiBiExampleDto> databaseExamples) {
this.databaseExamples = databaseExamples;
}
public List<BizAiBiExampleDto> getFileExamples() {
return fileExamples;
}
public void setFileExamples(List<BizAiBiExampleDto> fileExamples) {
this.fileExamples = fileExamples;
}
}
package cn.com.poc.ai_bi.dto;
/**
* @author 52747
* @date 2025/8/20
*/
public class AiBiGenerateInsightReportDto {
private String fileUrl, SQL, result, question, dialoguesId;
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getDialoguesId() {
return dialoguesId;
}
public void setDialoguesId(String dialoguesId) {
this.dialoguesId = dialoguesId;
}
public String getSQL() {
return SQL;
}
public void setSQL(String SQL) {
this.SQL = SQL;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
package cn.com.poc.ai_bi.dto;
public class BizAiBiExampleDto {
private static final long serialVersionUID = 1L;
/** id
*
*/
private java.lang.Long id;
public java.lang.Long getId(){
return this.id;
}
public void setId(java.lang.Long id){
this.id = id;
}
/** title
*标题
*/
private java.lang.String title;
public java.lang.String getTitle(){
return this.title;
}
public void setTitle(java.lang.String title){
this.title = title;
}
/** data_type
*
*/
private java.lang.String dataType;
public java.lang.String getDataType(){
return this.dataType;
}
public void setDataType(java.lang.String dataType){
this.dataType = dataType;
}
/** data_relation
*
*/
private java.lang.String dataRelation;
public java.lang.String getDataRelation(){
return this.dataRelation;
}
public void setDataRelation(java.lang.String dataRelation){
this.dataRelation = dataRelation;
}
/** example_desc
*
*/
private java.lang.String exampleDesc;
public java.lang.String getExampleDesc(){
return this.exampleDesc;
}
public void setExampleDesc(java.lang.String exampleDesc){
this.exampleDesc = exampleDesc;
}
/** question
*
*/
private java.lang.String question;
public java.lang.String getQuestion(){
return this.question;
}
public void setQuestion(java.lang.String question){
this.question = question;
}
}
\ No newline at end of file
package cn.com.poc.ai_bi.entity;
public class BizAiBiExampleEntity {
private static final long serialVersionUID = 1L;
/** id
*
*/
private java.lang.Long id;
public java.lang.Long getId(){
return this.id;
}
public void setId(java.lang.Long id){
this.id = id;
}
/** title
*标题
*/
private java.lang.String title;
public java.lang.String getTitle(){
return this.title;
}
public void setTitle(java.lang.String title){
this.title = title;
}
/** data_type
*
*/
private java.lang.String dataType;
public java.lang.String getDataType(){
return this.dataType;
}
public void setDataType(java.lang.String dataType){
this.dataType = dataType;
}
/** data_relation
*
*/
private java.lang.String dataRelation;
public java.lang.String getDataRelation(){
return this.dataRelation;
}
public void setDataRelation(java.lang.String dataRelation){
this.dataRelation = dataRelation;
}
/** example_desc
*
*/
private java.lang.String exampleDesc;
public java.lang.String getExampleDesc(){
return this.exampleDesc;
}
public void setExampleDesc(java.lang.String exampleDesc){
this.exampleDesc = exampleDesc;
}
/** question
*
*/
private java.lang.String question;
public java.lang.String getQuestion(){
return this.question;
}
public void setQuestion(java.lang.String question){
this.question = question;
}
}
\ No newline at end of file
package cn.com.poc.ai_bi.model;
import java.io.Serializable;
import cn.com.yict.framemax.data.model.BaseModelClass;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Id;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
/**
* Model class for biz_ai_bi_example
* AIBI示例表
*/
@Entity
@Table(name = "biz_ai_bi_example")
@DynamicInsert
@DynamicUpdate
public class BizAiBiExampleModel extends BaseModelClass implements Serializable {
private static final long serialVersionUID = 1L;
/** id
*
*/
private java.lang.Long id;
@Column(name = "id",length = 19)
@Id @GeneratedValue(strategy = GenerationType.AUTO)
public java.lang.Long getId(){
return this.id;
}
public void setId(java.lang.Long id){
this.id = id;
super.addValidField("id");
}
/** title
*标题
*/
private java.lang.String title;
@Column(name = "title",length = 100)
public java.lang.String getTitle(){
return this.title;
}
public void setTitle(java.lang.String title){
this.title = title;
super.addValidField("title");
}
/** data_type
*
*/
private java.lang.String dataType;
@Column(name = "data_type",length = 100)
public java.lang.String getDataType(){
return this.dataType;
}
public void setDataType(java.lang.String dataType){
this.dataType = dataType;
super.addValidField("dataType");
}
/** data_relation
*
*/
private java.lang.String dataRelation;
@Column(name = "data_relation",length = 100)
public java.lang.String getDataRelation(){
return this.dataRelation;
}
public void setDataRelation(java.lang.String dataRelation){
this.dataRelation = dataRelation;
super.addValidField("dataRelation");
}
/** example_desc
*
*/
private java.lang.String exampleDesc;
@Column(name = "example_desc",length = 2147483647)
public java.lang.String getExampleDesc(){
return this.exampleDesc;
}
public void setExampleDesc(java.lang.String exampleDesc){
this.exampleDesc = exampleDesc;
super.addValidField("exampleDesc");
}
/** question
*
*/
private java.lang.String question;
@Column(name = "question",length = 100)
public java.lang.String getQuestion(){
return this.question;
}
public void setQuestion(java.lang.String question){
this.question = question;
super.addValidField("question");
}
}
\ No newline at end of file
package cn.com.poc.ai_bi.repository;
import cn.com.yict.framemax.data.repository.Repository;
import cn.com.poc.ai_bi.model.BizAiBiExampleModel;
public interface BizAiBiExampleRepository extends Repository<BizAiBiExampleModel,java.lang.Long> {
}
\ No newline at end of file
package cn.com.poc.ai_bi.rest; package cn.com.poc.ai_bi.rest;
import cn.com.poc.ai_bi.dto.AIBiExampleDto;
import cn.com.poc.ai_bi.dto.AiBiDialoguesDto; import cn.com.poc.ai_bi.dto.AiBiDialoguesDto;
import cn.com.poc.ai_bi.dto.AiBiGenerateInsightReportDto;
import cn.com.poc.common.utils.SSEUtil;
import cn.com.yict.framemax.core.rest.BaseRest; import cn.com.yict.framemax.core.rest.BaseRest;
import cn.com.yict.framemax.web.permission.Access; import cn.com.yict.framemax.web.permission.Access;
import cn.com.yict.framemax.web.permission.Permission; import cn.com.yict.framemax.web.permission.Permission;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.io.IOException;
/** /**
* 智数:智能问数生成式BI服务接口 * 智数:智能问数生成式BI服务接口
...@@ -21,4 +28,26 @@ public interface AiBiRest extends BaseRest { ...@@ -21,4 +28,26 @@ public interface AiBiRest extends BaseRest {
*/ */
void call(@RequestBody AiBiDialoguesDto dto) throws Exception; void call(@RequestBody AiBiDialoguesDto dto) throws Exception;
/**
* 生成洞察报告
* <p>
* sse
*
* @param dto
*/
void generateInsightReport(@RequestBody AiBiGenerateInsightReportDto dto) throws Exception;
/**
* 获取洞察报告
*
* @param dialoguesId
*/
String getInsightReport(@RequestParam String dialoguesId);
/**
* 获取示例
* @return
* @throws Exception
*/
AIBiExampleDto getExample() throws Exception;
} }
package cn.com.poc.ai_bi.rest.impl; package cn.com.poc.ai_bi.rest.impl;
import cn.com.poc.ai_bi.aggregate.AiBiService; import cn.com.poc.ai_bi.aggregate.AiBiService;
import cn.com.poc.ai_bi.convert.BizAiBiExampleConvert;
import cn.com.poc.ai_bi.dto.AIBiExampleDto;
import cn.com.poc.ai_bi.dto.AiBiDialoguesDto; import cn.com.poc.ai_bi.dto.AiBiDialoguesDto;
import cn.com.poc.ai_bi.dto.AiBiGenerateInsightReportDto;
import cn.com.poc.ai_bi.dto.BizAiBiExampleDto;
import cn.com.poc.ai_bi.rest.AiBiRest; import cn.com.poc.ai_bi.rest.AiBiRest;
import cn.com.poc.ai_bi.service.BizAiBiExampleService;
import cn.com.poc.common.utils.Assert; import cn.com.poc.common.utils.Assert;
import cn.com.poc.common.utils.BlContext; import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity; import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.yict.framemax.core.exception.BusinessException; import cn.com.yict.framemax.core.exception.BusinessException;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils; import org.springframework.util.ObjectUtils;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/** /**
* @author alex.yao * @author alex.yao
...@@ -23,6 +31,10 @@ public class AiBiRestImpl implements AiBiRest { ...@@ -23,6 +31,10 @@ public class AiBiRestImpl implements AiBiRest {
@Resource @Resource
private AiBiService aiBiService; private AiBiService aiBiService;
@Resource
private BizAiBiExampleService bizAiBiExampleService;
@Override @Override
public void call(AiBiDialoguesDto dto) throws Exception { public void call(AiBiDialoguesDto dto) throws Exception {
Assert.notNull(dto); Assert.notNull(dto);
...@@ -33,11 +45,40 @@ public class AiBiRestImpl implements AiBiRest { ...@@ -33,11 +45,40 @@ public class AiBiRestImpl implements AiBiRest {
if (ObjectUtils.isEmpty(userBaseEntity)) { if (ObjectUtils.isEmpty(userBaseEntity)) {
throw new BusinessException("用户未登录"); throw new BusinessException("用户未登录");
} }
aiBiService.call(dto.getDialoguesId(), aiBiService.callV2(dto.getDialoguesId(),
dto.getInput(), dto.getInput(),
dto.getFileUrl(), dto.getFileUrl(),
dto.getKnowledgeIds(), dto.getKnowledgeIds(),
dto.getDatabaseIds(), dto.getDatabaseIds(),
userBaseEntity.getUserId()); userBaseEntity.getUserId());
} }
@Override
public void generateInsightReport(AiBiGenerateInsightReportDto dto) throws Exception {
Assert.notBlank(dto.getDialoguesId(), "对话id不能为空");
Assert.notBlank(dto.getResult(), "数据集不能为空");
Assert.notBlank(dto.getQuestion(), "问题不能为空");
Assert.isTrue(StringUtils.isNotBlank(dto.getFileUrl()) || StringUtils.isNotBlank(dto.getSQL()), "SQL 或 fileUrl 不能同时为空");
aiBiService.generateInsightReport(dto.getDialoguesId(), dto.getFileUrl(), dto.getSQL(), dto.getResult(), dto.getQuestion());
}
@Override
public String getInsightReport(String dialoguesId) {
Assert.notBlank(dialoguesId,"对话id不能为空");
return aiBiService.getInsightReport(dialoguesId);
}
@Override
public AIBiExampleDto getExample() throws Exception {
List<BizAiBiExampleDto> exampleDtos = bizAiBiExampleService.findByExample(BizAiBiExampleConvert.dtoToEntity(new BizAiBiExampleDto()), null)
.stream()
.map(BizAiBiExampleConvert::entityToDto)
.collect(Collectors.toList());
AIBiExampleDto aiBiExampleDto = new AIBiExampleDto();
aiBiExampleDto.setDatabaseExamples(exampleDtos.stream().filter(dto -> dto.getDataType().equals("FILE")).collect(Collectors.toList()));
aiBiExampleDto.setFileExamples(exampleDtos.stream().filter(dto -> dto.getDataType().equals("DATA_BASE")).collect(Collectors.toList()));
aiBiExampleDto.setFeaturedExamples(exampleDtos.stream().filter(dto -> dto.getDataType().equals("FEATURED")).collect(Collectors.toList()));
return aiBiExampleDto;
}
} }
package cn.com.poc.ai_bi.service;
import cn.com.yict.framemax.core.service.BaseService;
import cn.com.poc.ai_bi.entity.BizAiBiExampleEntity;
import cn.com.yict.framemax.data.model.PagingInfo;
import java.util.Collection;
import java.util.List;
public interface BizAiBiExampleService extends BaseService {
BizAiBiExampleEntity get(java.lang.Long id) throws Exception;
List<BizAiBiExampleEntity> findByExample(BizAiBiExampleEntity example,PagingInfo pagingInfo) throws Exception;
BizAiBiExampleEntity save(BizAiBiExampleEntity entity) throws Exception;
BizAiBiExampleEntity update(BizAiBiExampleEntity entity) throws Exception;
void deletedById(java.lang.Long id) throws Exception;
}
\ No newline at end of file
package cn.com.poc.ai_bi.service.impl;
import cn.com.yict.framemax.core.service.impl.BaseServiceImpl;
import cn.com.poc.ai_bi.service.BizAiBiExampleService;
import cn.com.poc.ai_bi.model.BizAiBiExampleModel;
import cn.com.poc.ai_bi.entity.BizAiBiExampleEntity;
import cn.com.poc.ai_bi.convert.BizAiBiExampleConvert;
import cn.com.poc.ai_bi.repository.BizAiBiExampleRepository;
import cn.com.yict.framemax.data.model.PagingInfo;
import org.springframework.stereotype.Service;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.util.Assert;
@Service
public class BizAiBiExampleServiceImpl extends BaseServiceImpl
implements BizAiBiExampleService {
@Resource
private BizAiBiExampleRepository repository;
public BizAiBiExampleEntity get(java.lang.Long id) throws Exception{
Assert.notNull(id);
BizAiBiExampleModel model = this.repository.get(id);
if (model == null){
return null;
}
return BizAiBiExampleConvert.modelToEntity(model);
}
public List<BizAiBiExampleEntity> findByExample(BizAiBiExampleEntity example,PagingInfo pagingInfo) throws Exception{
List<BizAiBiExampleEntity> result = new ArrayList<BizAiBiExampleEntity>();
BizAiBiExampleModel model = new BizAiBiExampleModel();
if (example != null){
model = BizAiBiExampleConvert.entityToModel(example);
}
List<BizAiBiExampleModel> models = this.repository.findByExample(model,pagingInfo);
if (CollectionUtils.isNotEmpty(models)) {
result = models.stream().map(BizAiBiExampleConvert::modelToEntity).collect(Collectors.toList());
}
return result;
}
public BizAiBiExampleEntity save(BizAiBiExampleEntity entity) throws Exception{
Assert.notNull(entity);
entity.setId(null);
BizAiBiExampleModel model = BizAiBiExampleConvert.entityToModel(entity);
BizAiBiExampleModel saveModel = this.repository.save(model);
return BizAiBiExampleConvert.modelToEntity(saveModel);
}
public BizAiBiExampleEntity update(BizAiBiExampleEntity entity) throws Exception{
Assert.notNull(entity);
Assert.notNull(entity.getId(),"update pk can not be null");
BizAiBiExampleModel model = this.repository.get(entity.getId());
if (entity.getTitle() != null){
model.setTitle(entity.getTitle());
}
if (entity.getDataType() != null){
model.setDataType(entity.getDataType());
}
if (entity.getDataRelation() != null){
model.setDataRelation(entity.getDataRelation());
}
if (entity.getExampleDesc() != null){
model.setExampleDesc(entity.getExampleDesc());
}
if (entity.getQuestion() != null){
model.setQuestion(entity.getQuestion());
}
BizAiBiExampleModel saveModel = this.repository.save(model);
return BizAiBiExampleConvert.modelToEntity(saveModel);
}
public void deletedById(java.lang.Long id) throws Exception{
Assert.notNull(id);
BizAiBiExampleModel model = this.repository.get(id);
if (model != null){
}
}
}
\ No newline at end of file
...@@ -6,6 +6,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.chart_generate.ChartGen ...@@ -6,6 +6,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.function.chart_generate.ChartGen
import cn.com.poc.thirdparty.resource.demand.ai.function.csv_data_analysis.CSVFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.csv_data_analysis.CSVFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.document_reader.DocumentReaderFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.document_reader.DocumentReaderFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.document_understanding.DocumentUnderstandIngFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.document_understanding.DocumentUnderstandIngFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.echart.EChartGenerateFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.ContractExtractionFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.extraction.ContractExtractionFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.html_reader.HtmlReaderFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.html_reader.HtmlReaderFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.image_ocr.ImageOCRFunction; import cn.com.poc.thirdparty.resource.demand.ai.function.image_ocr.ImageOCRFunction;
...@@ -54,6 +55,8 @@ public enum LargeModelFunctionEnum { ...@@ -54,6 +55,8 @@ public enum LargeModelFunctionEnum {
chart_generate(ChartGenerateFunction.class), chart_generate(ChartGenerateFunction.class),
echart_generate(EChartGenerateFunction.class),
csv_data_analysis(CSVFunction.class), csv_data_analysis(CSVFunction.class),
completion_of_travel_v2_form(TravelFormV2Function.class), completion_of_travel_v2_form(TravelFormV2Function.class),
......
package cn.com.poc.thirdparty.resource.demand.ai.function.echart;
import cn.com.poc.agent_application.entity.KnowledgeContentResult;
import cn.com.poc.agent_application.entity.Variable;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDemandResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.Thinking;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractFunctionResult;
import cn.com.poc.thirdparty.resource.demand.ai.function.AbstractLargeModelFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.chart_generate.ChartGenerateFunction;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.FunctionLLMConfig;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Parameters;
import cn.com.poc.thirdparty.resource.demand.ai.function.entity.Properties;
import cn.com.poc.thirdparty.service.LLMService;
import cn.hutool.core.collection.ListUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.List;
/**
* @author 52747
* @date 2025/8/19
*/
@Component
public class EChartGenerateFunction extends AbstractLargeModelFunction {
@Resource
private LLMService llmService;
private final Logger logger = LoggerFactory.getLogger(ChartGenerateFunction.class);
private final String DESC = "根据用户问题、SQL和SQL执行结果,生成折线图,饼图,柱状图";
private final FunctionLLMConfig functionLLMConfig = new FunctionLLMConfig.FunctionLLMConfigBuilder()
.name("eChart_generate")
.description(DESC)
.parameters(new Parameters("object")
.addProperties("question", new Properties("string", "用户问题优化后的问题"))
.addProperties("sql", new Properties("string", "如果用户问题中存在数据则解析或截取对应的数据"))
.addProperties("sql_result", new Properties("string", "根据用户问题和上下文生成的图表主题")))
.build();
private static String PROMPT = "## 角色\n" +
"你的任务是根据数据和用户提问生成EChart 的Option Json\n" +
"\n" +
"## 限制\n" +
"- 输出格式: 适用JSON格式输出\n" +
"- 仅输出JSON内容\n" +
"- 图表限制: 折线图、饼图、柱状图\n" +
"- 如果数据与问题不能生成图表,则输出 : { \"skip\" : true }\n" +
"\n" +
"## 用户提问\n" +
"${QUESTION}\n" +
"\n" +
"## SQL\n" +
"${SQL}\n" +
"\n" +
"## SQL Result\n" +
"${SQL_RESULT}";
@Override
public AbstractFunctionResult<String> doFunction(String content, String identifier, List<DBChainResult> dbChainResults, List<KnowledgeContentResult> knowledgeContentResults) {
AbstractFunctionResult<String> result = new AbstractFunctionResult<>();
result.setFunctionResult(StringUtils.EMPTY);
result.setPromptContent(StringUtils.EMPTY);
if (StringUtils.isBlank(content)) {
return result;
}
JSONObject jsonObject = JSON.parseObject(content);
if (!jsonObject.containsKey("question") || !jsonObject.containsKey("sql") || !jsonObject.containsKey("sql_result")) {
logger.warn("缺少必要参数,无法生成图表,content:{}", content);
return result;
}
String question = jsonObject.getString("question");
String sql = jsonObject.getString("sql");
String sqlResult = jsonObject.getString("sql_result");
String option = generateEChart(sql, sqlResult, question);
JSONObject optionJSONObject = JSON.parseObject(option);
if (optionJSONObject.containsKey("skip")) {
return result;
}
result.setPromptContent(option);
result.setFunctionResult(option);
return result;
}
@Override
public String getDesc() {
return DESC;
}
@Override
public List<String> getLLMConfig() {
return ListUtil.toList(JsonUtils.serialize(functionLLMConfig));
}
@Override
public List<String> getLLMConfig(List<Variable> variableStructure) {
return this.getLLMConfig();
}
private String generateEChart(String SQL, String SQLResult, String question) {
String model = "ep-20250814152748-6jlnx";
String prompt = PROMPT.replace("${QUESTION}", question)
.replace("${SQL}", SQL)
.replace("${SQL_RESULT}", SQLResult);
Message systemMessage = new Message();
systemMessage.setRole(LLMRoleEnum.SYSTEM.getRole());
systemMessage.setContent(prompt);
Message userMessage = new Message();
userMessage.setRole(LLMRoleEnum.USER.getRole());
userMessage.setContent(question);
LargeModelResponse largeModelResponse = new LargeModelResponse();
largeModelResponse.setThinking(new Thinking() {{
setType("disabled");
}});
largeModelResponse.setModel(model);
largeModelResponse.setMessages(new Message[]{systemMessage, userMessage});
largeModelResponse.setStream(false);
largeModelResponse.setUser("GENERATE_E_CHART");
LargeModelDemandResult largeModelDemandResult = llmService.chat(largeModelResponse);
return largeModelDemandResult.getMessage();
}
}
...@@ -9,6 +9,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult; ...@@ -9,6 +9,7 @@ import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult;
import cn.com.poc.thirdparty.service.ChainService; import cn.com.poc.thirdparty.service.ChainService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
...@@ -22,6 +23,10 @@ public class ChainServiceImpl implements ChainService { ...@@ -22,6 +23,10 @@ public class ChainServiceImpl implements ChainService {
private Logger logger = LoggerFactory.getLogger(ChainServiceImpl.class); private Logger logger = LoggerFactory.getLogger(ChainServiceImpl.class);
@Value(value = "${large-model.apikey}")
private String API_KEY;
@Resource @Resource
private AIDialogueService aiDialogueService; private AIDialogueService aiDialogueService;
...@@ -43,6 +48,7 @@ public class ChainServiceImpl implements ChainService { ...@@ -43,6 +48,7 @@ public class ChainServiceImpl implements ChainService {
@Override @Override
public CSVChainResult csvChain(CSVChainResponse response) { public CSVChainResult csvChain(CSVChainResponse response) {
logger.info("csvChain response : {}", response); logger.info("csvChain response : {}", response);
response.setApiKey(API_KEY);
CSVChainResult csvChainResult = aiDialogueService.csvChain(response); CSVChainResult csvChainResult = aiDialogueService.csvChain(response);
if (csvChainResult == null || csvChainResult.getStatus().equals("error")) { if (csvChainResult == null || csvChainResult.getStatus().equals("error")) {
logger.error("csvChain result error : {} , response:{}", csvChainResult, response); logger.error("csvChain result error : {} , response:{}", csvChainResult, response);
......
package cn.com.poc.ai_bi;
import cn.com.poc.common.utils.DocumentLoad;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.core.spring.SingleContextInitializer;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
import java.util.Map;
import java.util.Set;
/**
* @author 52747
* @date 2025/8/21
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = SingleContextInitializer.class)
@WebAppConfiguration
public class AIBiTest {
@Test
public void test_readCSVHeader() {
String fileUrl = "https://gsst-poe-sit.gz.bcebos.com/v1/weather_district_id.csv";
File file = DocumentLoad.downloadURLDocument(fileUrl);
try (Reader reader = new FileReader(file)) {
CSVParser csvParser = new CSVParser(reader, CSVFormat.DEFAULT
.withFirstRecordAsHeader()
.withIgnoreHeaderCase()
.withTrim());
Map<String, Integer> headerMap = csvParser.getHeaderMap();
Set<String> headerSet = headerMap.keySet();
System.out.println(JsonUtils.serialize(headerSet));
} catch (Exception e) {
throw new BusinessException("获取文件失败");
}
}
}
package cn.com.poc.thirdparty.resource.demand.ai.service; package cn.com.poc.thirdparty.resource.demand.ai.service;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.thirdparty.resource.demand.ai.entity.csvchain.CSVChainResponse;
import cn.com.poc.thirdparty.resource.demand.ai.entity.csvchain.CSVChainResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResponse; import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResponse;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult; import cn.com.poc.thirdparty.resource.demand.ai.entity.dbchain.DBChainResult;
import cn.com.poc.thirdparty.service.ChainService; import cn.com.poc.thirdparty.service.ChainService;
...@@ -27,7 +30,7 @@ public class ChainServiceTest { ...@@ -27,7 +30,7 @@ public class ChainServiceTest {
@Test @Test
public void test_dbChain(){ public void test_dbChain() {
DBChainResponse response = new DBChainResponse(); DBChainResponse response = new DBChainResponse();
response.setQuestion("查询百度插件详情"); response.setQuestion("查询百度插件详情");
...@@ -43,4 +46,17 @@ public class ChainServiceTest { ...@@ -43,4 +46,17 @@ public class ChainServiceTest {
} }
@Test
public void test_csvChain() {
CSVChainResponse csvChainResponse = new CSVChainResponse();
csvChainResponse.setQuestion("张家市口的所有地区经纬度");
csvChainResponse.setContext("");
csvChainResponse.setFilePath("https://gsst-poe-sit.gz.bcebos.com/v1/weather_district_id.csv");
csvChainResponse.setApiKey("sk-bNzgFwFtw3k4jR7CE3575a19C3304a53Bb803cCeDeC154F2");
CSVChainResult csvChainResult = chainService.csvChain(csvChainResponse);
System.out.println(JsonUtils.serialize(csvChainResult));
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment