[update] Prompt模版查询优化 --1

This commit is contained in:
Liuyang 2025-01-14 15:21:34 +08:00
parent 2963e4ad88
commit a7353b90ae
6 changed files with 337 additions and 280 deletions

View File

@ -1,11 +1,14 @@
package cn.iocoder.yudao.module.llm.service.application;
import java.util.*;
import javax.validation.*;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.application.ApplicationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationRespVO;
import cn.iocoder.yudao.module.llm.controller.admin.application.vo.ApplicationSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.application.ApplicationDO;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* 大模型应用 Service 接口
@ -20,21 +23,21 @@ public interface ApplicationService {
* @param createReqVO 创建信息
* @return 编号
*/
Long createApplication(@Valid ApplicationSaveReqVO createReqVO);
Long createApplication (@Valid ApplicationSaveReqVO createReqVO);
/**
* 更新大模型应用
*
* @param updateReqVO 更新信息
*/
void updateApplication(@Valid ApplicationSaveReqVO updateReqVO);
void updateApplication (@Valid ApplicationSaveReqVO updateReqVO);
/**
* 删除大模型应用
*
* @param id 编号
*/
void deleteApplication(Long id);
void deleteApplication (Long id);
/**
* 获得大模型应用
@ -42,7 +45,7 @@ public interface ApplicationService {
* @param id 编号
* @return 大模型应用
*/
ApplicationRespVO getApplication(Long id);
ApplicationRespVO getApplication (Long id);
/**
* 获得大模型应用分页
@ -50,11 +53,18 @@ public interface ApplicationService {
* @param pageReqVO 分页查询
* @return 大模型应用分页
*/
PageResult<ApplicationRespVO> getApplicationPage(ApplicationPageReqVO pageReqVO);
PageResult<ApplicationRespVO> getApplicationPage (ApplicationPageReqVO pageReqVO);
List<ApplicationRespVO> getApplicationList(ApplicationPageReqVO pageReqVO);
List<ApplicationRespVO> getApplicationList (ApplicationPageReqVO pageReqVO);
List<ApplicationDO> getList();
List<ApplicationDO> getList ();
ApplicationDO getByApiKey(String apiKey);
ApplicationDO getByApiKey (String apiKey);
/**
* 获取应用Map
*
* @return Map<Long, String>
*/
Map<Long, String> getApplicatioMap ();
}

View File

@ -19,14 +19,13 @@ import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -214,4 +213,20 @@ public class ApplicationServiceImpl implements ApplicationService {
return applicationMapper.selectOne(queryWrapper);
}
/**
* 获取应用Map
*
* @return Map<Long, String>
*/
@Override
public Map<Long, String> getApplicatioMap () {
List<ApplicationDO> appList = this.getList();
if (CollectionUtils.isEmpty(appList)){
return Collections.emptyMap();
}
return appList.stream()
.collect(Collectors.toMap(ApplicationDO::getId, ApplicationDO::getAppName));
}
}

View File

@ -8,6 +8,7 @@ import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;
/**
* 标签管理 Service 接口
@ -61,6 +62,14 @@ public interface LabelService {
*/
List<LabelDO> getLabelList ();
/**
* 获取标签Map
*
* @param allLabel 是否获取所有标签 true:获取所有标签 false:获取已启用标签
* @return 标签列表
*/
Map<Long, String> getLabelMap (boolean allLabel);
/**
* 获取所有已开启的标签
*

View File

@ -1,27 +1,26 @@
package cn.iocoder.yudao.module.llm.service.label;
import cn.iocoder.yudao.module.llm.dal.dataobject.dataset.DatasetDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.llm.controller.admin.label.vo.LabelPageReqVO;
import cn.iocoder.yudao.module.llm.controller.admin.label.vo.LabelSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.llm.dal.mysql.label.LabelMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.stream.Collectors;
import cn.iocoder.yudao.module.llm.controller.admin.label.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.llm.dal.mysql.label.LabelMapper;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.LABEL_NAME_EXISTS;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.LABEL_NOT_EXISTS;
/**
* 标签管理 Service 实现类
@ -36,7 +35,7 @@ public class LabelServiceImpl implements LabelService {
private LabelMapper labelMapper;
@Override
public Long createLabel(LabelSaveReqVO createReqVO) {
public Long createLabel (LabelSaveReqVO createReqVO) {
validateLabelNameExists(createReqVO);
// 插入
LabelDO label = BeanUtils.toBean(createReqVO, LabelDO.class);
@ -46,7 +45,7 @@ public class LabelServiceImpl implements LabelService {
}
@Override
public void updateLabel(LabelSaveReqVO updateReqVO) {
public void updateLabel (LabelSaveReqVO updateReqVO) {
// 校验存在
validateLabelExists(updateReqVO.getId());
validateLabelNameExists(updateReqVO);
@ -56,44 +55,70 @@ public class LabelServiceImpl implements LabelService {
}
@Override
public void deleteLabel(Long id) {
public void deleteLabel (Long id) {
// 校验存在
validateLabelExists(id);
// 删除
labelMapper.deleteById(id);
}
private void validateLabelExists(Long id) {
private void validateLabelExists (Long id) {
if (labelMapper.selectById(id) == null) {
throw exception(LABEL_NOT_EXISTS);
}
}
private void validateLabelNameExists(LabelSaveReqVO updateReqVO) {
private void validateLabelNameExists (LabelSaveReqVO updateReqVO) {
LambdaQueryWrapper<LabelDO> wrapper = new LambdaQueryWrapper<LabelDO>()
.eq(LabelDO::getLabelName, updateReqVO.getLabelName());
if (updateReqVO.getId() != null){
if (updateReqVO.getId() != null) {
wrapper.ne(LabelDO::getId, updateReqVO.getId());
}
List<LabelDO> labelDOS = labelMapper.selectList(wrapper);
if (CollectionUtils.isNotEmpty(labelDOS)){
if (CollectionUtils.isNotEmpty(labelDOS)) {
throw exception(LABEL_NAME_EXISTS);
}
}
@Override
public LabelDO getLabel(Long id) {
public LabelDO getLabel (Long id) {
return labelMapper.selectById(id);
}
@Override
public PageResult<LabelDO> getLabelPage(LabelPageReqVO pageReqVO) {
public PageResult<LabelDO> getLabelPage (LabelPageReqVO pageReqVO) {
return labelMapper.selectPage(pageReqVO);
}
@Override
public List<LabelDO> getLabelList() {
return labelMapper.selectList();
public List<LabelDO> getLabelList () {
List<LabelDO> result = labelMapper.selectList();
if (result == null) {
return new ArrayList<>();
}
return result;
}
/**
* 获取标签Map
*
* @param allLabel 是否获取所有标签 true:获取所有标签 false:获取已启用标签
* @return 标签列表
*/
@Override
public Map<Long, String> getLabelMap (boolean allLabel) {
List<LabelDO> labels = labelMapper.selectList();
if (CollectionUtils.isEmpty(labels)) {
return Collections.emptyMap();
}
if (allLabel) {
return labels.stream().collect(Collectors.toMap(LabelDO::getId, LabelDO::getLabelName));
} else {
List<LabelDO> enableLabels = labels.stream().filter(labelDO -> labelDO.getStatus() == 0).collect(Collectors.toList());
return enableLabels.stream().collect(Collectors.toMap(LabelDO::getId, LabelDO::getLabelName));
}
}
/**
@ -104,7 +129,7 @@ public class LabelServiceImpl implements LabelService {
@Override
public List<LabelDO> getEnableLabelList () {
List<LabelDO> labels = labelMapper.selectList();
if (CollectionUtils.isEmpty(labels)){
if (CollectionUtils.isEmpty(labels)) {
return Collections.emptyList();
}

View File

@ -1,14 +1,20 @@
package cn.iocoder.yudao.module.llm.service.prompttemplates;
import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.llm.controller.admin.prompttemplates.vo.*;
import cn.iocoder.yudao.module.llm.controller.admin.prompttemplatesbackup.vo.PromptTemplatesBackupSaveReqVO;
import cn.iocoder.yudao.module.llm.dal.dataobject.application.ApplicationDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.label.LabelDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplatesapplications.PromptTemplatesApplicationsDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplatesbackup.PromptTemplatesBackupDO;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplatestags.PromptTemplatesTagsDO;
import cn.iocoder.yudao.module.llm.dal.mysql.prompttemplates.PromptTemplatesMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.prompttemplatesapplications.PromptTemplatesApplicationsMapper;
import cn.iocoder.yudao.module.llm.dal.mysql.prompttemplatestags.PromptTemplatesTagsMapper;
import cn.iocoder.yudao.module.llm.framework.backend.config.LLMBackendProperties;
@ -24,22 +30,13 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;
import cn.iocoder.yudao.module.llm.controller.admin.prompttemplates.vo.*;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.llm.dal.mysql.prompttemplates.PromptTemplatesMapper;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;
@ -67,34 +64,34 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
private ApplicationService applicationService;
@Override
public Long createPromptTemplates(PromptTemplatesSaveReqVO createReqVO) {
public Long createPromptTemplates (PromptTemplatesSaveReqVO createReqVO) {
this.isNameDuplicate(createReqVO.getName());
PromptTemplatesDO promptTemplates = BeanUtils.toBean(createReqVO, PromptTemplatesDO.class);
// 暂时先去掉这个逻辑演示用 -- zhangtao
// promptTemplates.setTemplateType(2);
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(createReqVO
// .getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// BriefFramework briefFramework = createReqVO.getBriefFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(briefFramework));
// break;
// case "角色能力框架":
// AbilityFramework abilityFramework = createReqVO.getAbilityFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(abilityFramework));
// break;
// case "样例提示框架":
// SampleFramework sampleFramework = createReqVO.getSampleFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(sampleFramework));
// break;
// default:
// break;
// }
// promptTemplates.setTemplateType(2);
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(createReqVO
// .getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// BriefFramework briefFramework = createReqVO.getBriefFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(briefFramework));
// break;
// case "角色能力框架":
// AbilityFramework abilityFramework = createReqVO.getAbilityFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(abilityFramework));
// break;
// case "样例提示框架":
// SampleFramework sampleFramework = createReqVO.getSampleFramework();
// promptTemplates.setTemplateText(JSON.toJSONString(sampleFramework));
// break;
// default:
// break;
// }
promptTemplatesMapper.insert(promptTemplates);
List<String> tagIds = createReqVO.getTagIds();
if(tagIds!=null && !tagIds.isEmpty()) {
if (tagIds != null && !tagIds.isEmpty()) {
List<PromptTemplatesTagsDO> tagDOList = tagIds.stream()
.map(p->PromptTemplatesTagsDO.builder()
.map(p -> PromptTemplatesTagsDO.builder()
.tagId(Long.valueOf(p))
.promptTemplateId(promptTemplates.getId())
.build())
@ -102,9 +99,9 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
this.promptTemplatesTagsMapper.insertBatch(tagDOList, 1);
}
List<String> applicationIds = createReqVO.getApplicationIds();
if(applicationIds!=null && !applicationIds.isEmpty()) {
if (applicationIds != null && !applicationIds.isEmpty()) {
List<PromptTemplatesApplicationsDO> applicationDOList = applicationIds.stream()
.map(p->PromptTemplatesApplicationsDO.builder()
.map(p -> PromptTemplatesApplicationsDO.builder()
.applicationId(Long.valueOf(p))
.promptTemplateId(promptTemplates.getId())
.build())
@ -115,41 +112,41 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void updatePromptTemplates(PromptTemplatesSaveReqVO updateReqVO) {
public void updatePromptTemplates (PromptTemplatesSaveReqVO updateReqVO) {
PromptTemplatesDO templatesDo = this.promptTemplatesMapper.selectById(updateReqVO.getId());
if(templatesDo == null) {
if (templatesDo == null) {
throw exception(PROMPT_TEMPLATES_NOT_EXISTS);
}
if(!templatesDo.getName().equals(updateReqVO.getName())) {
if (!templatesDo.getName().equals(updateReqVO.getName())) {
this.isNameDuplicate(updateReqVO.getName());
}
PromptTemplatesDO updateObj = BeanUtils.toBean(updateReqVO, PromptTemplatesDO.class);
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(updateReqVO
// .getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// BriefFramework briefFramework = updateReqVO.getBriefFramework();
// updateObj.setTemplateText(JSON.toJSONString(briefFramework));
// break;
// case "角色能力框架":
// AbilityFramework abilityFramework = updateReqVO.getAbilityFramework();
// updateObj.setTemplateText(JSON.toJSONString(abilityFramework));
// break;
// case "样例提示框架":
// SampleFramework sampleFramework = updateReqVO.getSampleFramework();
// updateObj.setTemplateText(JSON.toJSONString(sampleFramework));
// break;
// default:
// break;
// }
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(updateReqVO
// .getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// BriefFramework briefFramework = updateReqVO.getBriefFramework();
// updateObj.setTemplateText(JSON.toJSONString(briefFramework));
// break;
// case "角色能力框架":
// AbilityFramework abilityFramework = updateReqVO.getAbilityFramework();
// updateObj.setTemplateText(JSON.toJSONString(abilityFramework));
// break;
// case "样例提示框架":
// SampleFramework sampleFramework = updateReqVO.getSampleFramework();
// updateObj.setTemplateText(JSON.toJSONString(sampleFramework));
// break;
// default:
// break;
// }
promptTemplatesMapper.updateById(updateObj);
Map<String, Object> deleteCondition = new HashMap<>(1);
deleteCondition.put("prompt_template_id", updateReqVO.getId());
this.promptTemplatesTagsMapper.deleteByMap(deleteCondition);
List<String> tagIds = updateReqVO.getTagIds();
if(tagIds!=null && !tagIds.isEmpty()) {
if (tagIds != null && !tagIds.isEmpty()) {
List<PromptTemplatesTagsDO> tagDOList = tagIds.stream()
.map(p->PromptTemplatesTagsDO.builder()
.map(p -> PromptTemplatesTagsDO.builder()
.tagId(Long.valueOf(p))
.promptTemplateId(updateReqVO.getId())
.build())
@ -158,9 +155,9 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
this.promptTemplatesApplicationsMapper.deleteByMap(deleteCondition);
List<String> applicationIds = updateReqVO.getApplicationIds();
if(applicationIds!=null && !applicationIds.isEmpty()) {
if (applicationIds != null && !applicationIds.isEmpty()) {
List<PromptTemplatesApplicationsDO> applicationDOList = applicationIds.stream()
.map(p->PromptTemplatesApplicationsDO.builder()
.map(p -> PromptTemplatesApplicationsDO.builder()
.applicationId(Long.valueOf(p))
.promptTemplateId(updateReqVO.getId())
.build())
@ -170,7 +167,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void deletePromptTemplates(Long id) {
public void deletePromptTemplates (Long id) {
validatePromptTemplatesExists(id);
promptTemplatesMapper.deleteById(id);
Map<String, Object> condition = new HashMap<>(1);
@ -179,165 +176,166 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
this.promptTemplatesApplicationsMapper.deleteByMap(condition);
}
private void validatePromptTemplatesExists(Long id) {
private void validatePromptTemplatesExists (Long id) {
if (promptTemplatesMapper.selectById(id) == null) {
throw exception(PROMPT_TEMPLATES_NOT_EXISTS);
}
}
private void isNameDuplicate(String name) {
private void isNameDuplicate (String name) {
List<PromptTemplatesDO> templateDOList = this.promptTemplatesMapper.selectList("name", name);
if(templateDOList!=null && !templateDOList.isEmpty()) {
if (templateDOList != null && !templateDOList.isEmpty()) {
throw exception(PROMPT_TEMPLATES_EXISTS);
}
}
@Override
public PromptTemplatesRespVO getPromptTemplates(Long id) {
public PromptTemplatesRespVO getPromptTemplates (Long id) {
PromptTemplatesDO templatesDO = this.promptTemplatesMapper.selectById(id);
if(templatesDO == null) {
return null;
if (templatesDO == null) {
throw exception(PROMPT_TEMPLATES_NOT_EXISTS);
}
PromptTemplatesRespVO promptTemplates = BeanUtils.toBean(templatesDO, PromptTemplatesRespVO.class);
// 标签
getPromptTemplatesLabelResult(id, promptTemplates);
// 应用
getPromptTemplatesApplicationsResult(id, promptTemplates);
return promptTemplates;
}
/**
* 标签
*
* @param id 模版ID
* @param promptTemplates 模版
*/
private void getPromptTemplatesLabelResult (Long id, PromptTemplatesRespVO promptTemplates) {
// 标签
List<PromptTemplatesTagsDO> tagDOList = this.promptTemplatesTagsMapper
.selectList("prompt_template_id", id);
List<LabelDO> labelList = labelService.getLabelList();
if (CollectionUtils.isEmpty(labelList)){
labelList=new ArrayList<>();
}
Map<Long, String> labelMap = new HashMap<>();
labelList.forEach(labelDO -> labelMap.put(labelDO.getId(), labelDO.getLabelName()));
Map<Long, String> labelMap = labelService.getLabelMap(false);
if (tagDOList!= null &&!tagDOList.isEmpty()) {
if (CollectionUtils.isNotEmpty(tagDOList)) {
// 过滤 tagDOList labelMap 中不存在的 tagIds只保留存在的
List<String> tagIds = tagDOList.stream()
.filter(tagsDO -> labelMap.containsKey(tagsDO.getTagId()))
.map(p -> String.valueOf(p.getTagId()))
.collect(Collectors.toList());
promptTemplates.setTagIds(tagIds);
// 获取对应的标签名称
List<String> labelNameList = tagIds.stream()
.map(tagId -> labelMap.computeIfAbsent(Long.parseLong(tagId), k -> null))
.collect(Collectors.toList());
promptTemplates.setTagNames(labelNameList);
}
// 应用
}
/**
* 应用
*
* @param id 模版ID
* @param promptTemplates 模版
*/
private void getPromptTemplatesApplicationsResult (Long id, PromptTemplatesRespVO promptTemplates) {
// 获取当前模版的应用
List<PromptTemplatesApplicationsDO> applicationDOList = this.promptTemplatesApplicationsMapper
.selectList("prompt_template_id", id);
List<ApplicationDO> appList = applicationService.getList();
if (CollectionUtils.isEmpty(appList)){
appList=new ArrayList<>();
}
Map<Long, String> appMap = applicationService.getApplicatioMap();
labelList.forEach(labelDO -> labelMap.put(labelDO.getId(), labelDO.getLabelName()));
HashMap<Long,String> appMap = new HashMap<>();
appList.forEach(applicationDO -> appMap.put(applicationDO.getId(),applicationDO.getAppName()));
if(applicationDOList!=null && !applicationDOList.isEmpty()) {
if (CollectionUtils.isNotEmpty(applicationDOList)) {
// 过滤 applicationDOList appMap 中不存在的 applicationIds只保留存在的
List<String> applicationIds = applicationDOList.stream()
.map(p->p.getApplicationId().toString())
.filter(applicationDO -> appMap.containsKey(applicationDO.getApplicationId()))
.map(p -> p.getApplicationId().toString())
.collect(Collectors.toList());
promptTemplates.setApplicationIds(applicationIds);
// 获取对应的应用名称
List<String> appNameList = applicationIds.stream()
.map(appId -> appMap.computeIfAbsent(Long.parseLong(appId), k -> null))
.collect(Collectors.toList());
promptTemplates.setApplicationNames(appNameList);
}
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(templatesDO.getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// promptTemplates.setBriefFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), BriefFramework.class));
// promptTemplates.setTemplateText("");
// break;
// case "角色能力框架":
// promptTemplates.setAbilityFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), AbilityFramework.class));
// promptTemplates.setTemplateText("");
// break;
// case "样例提示框架":
// promptTemplates.setSampleFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), SampleFramework.class));
// promptTemplates.setTemplateText("");
// break;
// default:
// break;
// }
return promptTemplates;
}
@Override
public PageResult<PromptTemplatesRespVO> getPromptTemplatesPage(PromptTemplatesPageReqVO pageReqVO) {
public PageResult<PromptTemplatesRespVO> getPromptTemplatesPage (PromptTemplatesPageReqVO pageReqVO) {
PageResult<PromptTemplatesDO> pageDoList = this.promptTemplatesMapper.selectPage(pageReqVO);
PageResult<PromptTemplatesRespVO> pageRespList = BeanUtils.toBean(pageDoList, PromptTemplatesRespVO.class);
List<PromptTemplatesRespVO> templatesRespList = pageRespList.getList();
List<LabelDO> labelList = labelService.getLabelList();
HashMap<Long,String> labelMap = new HashMap<>();
for (LabelDO labelDO : labelList) {
labelMap.put(labelDO.getId(),labelDO.getLabelName());
}
List<ApplicationDO> appList = applicationService.getList();
HashMap<Long,String> appMap = new HashMap<>();
for (ApplicationDO applicationDO : appList) {
appMap.put(applicationDO.getId(),applicationDO.getAppName());
}
for(PromptTemplatesRespVO respVO : templatesRespList) {
for (PromptTemplatesRespVO respVO : templatesRespList) {
try {
// 标签
List<PromptTemplatesTagsDO> tagDOList = this.promptTemplatesTagsMapper
.selectList("prompt_template_id", respVO.getId());
if(tagDOList!=null && !tagDOList.isEmpty()) {
List<String> tagIds = tagDOList.stream()
.map(p->p.getTagId().toString())
.collect(Collectors.toList());
List<String> labelNameList = new ArrayList<>();
for (String tagId : tagIds) {
labelNameList.add(labelMap.get(Long.parseLong(tagId)));
}
Map<Long, String> labelMap = labelService.getLabelMap(false);
if (CollectionUtils.isNotEmpty(tagDOList)) {
// 过滤 tagDOList labelMap 中不存在的 tagIds只保留存在的
List<String> tagIds = tagDOList.stream()
.filter(tagsDO -> labelMap.containsKey(tagsDO.getTagId()))
.map(p -> String.valueOf(p.getTagId()))
.collect(Collectors.toList());
respVO.setTagIds(tagIds);
// 获取对应的标签名称
List<String> labelNameList = tagIds.stream()
.map(tagId -> labelMap.computeIfAbsent(Long.parseLong(tagId), k -> null))
.collect(Collectors.toList());
respVO.setTagNames(labelNameList);
}
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
try {
List<PromptTemplatesApplicationsDO> applicationIds = this.promptTemplatesApplicationsMapper.selectList(
new QueryWrapper<PromptTemplatesApplicationsDO>()
.eq("prompt_template_id", respVO.getId()));
// 获取当前模版的应用
List<PromptTemplatesApplicationsDO> applicationDOList = this.promptTemplatesApplicationsMapper
.selectList("prompt_template_id", respVO.getId());
List<String> appNames = new ArrayList<>();
for (PromptTemplatesApplicationsDO applicationId : applicationIds) {
appNames.add(appMap.get(applicationId.getApplicationId()));
Map<Long, String> appMap = applicationService.getApplicatioMap();
if (CollectionUtils.isNotEmpty(applicationDOList)) {
// 过滤 applicationDOList appMap 中不存在的 applicationIds只保留存在的
List<String> applicationIds = applicationDOList.stream()
.filter(applicationDO -> appMap.containsKey(applicationDO.getApplicationId()))
.map(p -> p.getApplicationId().toString())
.collect(Collectors.toList());
respVO.setApplicationIds(applicationIds);
// 获取对应的应用名称
List<String> appNameList = applicationIds.stream()
.map(appId -> appMap.computeIfAbsent(Long.parseLong(appId), k -> null))
.collect(Collectors.toList());
respVO.setApplicationNames(appNameList);
respVO.setApplicationCount(applicationIds.size());
}
respVO.setApplicationCount(applicationIds.size());
respVO.setApplicationNames(appNames);
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
try {
PromptTemplatesBackupDO backupDO = this.promptTemplatesBackupService.getPromptTemplatesBackupBySrcId(respVO.getId());
if(backupDO == null) {
if (backupDO == null) {
respVO.setIsBackup(false);
} else {
respVO.setIsBackup(true);
respVO.setBackupId(backupDO.getId());
}
} catch(Exception e) {
} catch (Exception e) {
respVO.setIsBackup(false);
}
}
@ -345,29 +343,29 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void exportPromptTemplatesExcel(PromptTemplatesPageReqVO pageReqVO,
HttpServletResponse response) throws Exception {
public void exportPromptTemplatesExcel (PromptTemplatesPageReqVO pageReqVO,
HttpServletResponse response) throws Exception {
pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
List<PromptTemplatesRespVO> list = this.getPromptTemplatesPage(pageReqVO).getList();
List<PromptTemplatesExcelVO> excelList = BeanUtils.toBean(list, PromptTemplatesExcelVO.class);
for(PromptTemplatesExcelVO excelVO : excelList){
for (PromptTemplatesExcelVO excelVO : excelList) {
try {
excelVO.setTemplateCategoryDisplay(this.dictDataApi
.getDictDataLabel(Long.valueOf(excelVO.getTemplateCategory().toString())));
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
try {
List<PromptTemplatesTagsDO> tagDOList = this.promptTemplatesTagsMapper
.selectList("prompt_template_id", excelVO.getId());
if(tagDOList!=null && !tagDOList.isEmpty()) {
if (tagDOList != null && !tagDOList.isEmpty()) {
excelVO.setTag(tagDOList.stream()
.map(p->this.labelService.getLabel(p.getTagId()))
.map(p -> this.labelService.getLabel(p.getTagId()))
.filter(Objects::nonNull)
.map(LabelDO::getLabelName)
.collect(Collectors.joining(";")));
}
} catch(Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
@ -376,13 +374,13 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void backupPromptTemplates(PromptTemplatesSaveReqVO updateReqVO) {
public void backupPromptTemplates (PromptTemplatesSaveReqVO updateReqVO) {
PromptTemplatesDO templatesDo = this.promptTemplatesMapper.selectById(updateReqVO.getId());
if(templatesDo == null) {
if (templatesDo == null) {
throw exception(PROMPT_TEMPLATES_NOT_EXISTS);
}
if(templatesDo.getBackupId() != null && templatesDo.getBackupId() != 0){
throw exception(new ErrorCode(500,"已存在备份,不能再备份"));
if (templatesDo.getBackupId() != null && templatesDo.getBackupId() != 0) {
throw exception(new ErrorCode(500, "已存在备份,不能再备份"));
}
this.promptTemplatesBackupService.isBackup(updateReqVO.getId());
PromptTemplatesBackupSaveReqVO templatesBackupDo = BeanUtils.toBean(templatesDo, PromptTemplatesBackupSaveReqVO.class);
@ -396,7 +394,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void unbackupPromptTemplates(PromptTemplatesSaveReqVO updateReqVO) {
public void unbackupPromptTemplates (PromptTemplatesSaveReqVO updateReqVO) {
PromptTemplatesDO templatesDo = this.promptTemplatesMapper.selectById(updateReqVO.getId());
Long backupId = templatesDo.getBackupId();
promptTemplatesBackupService.deletePromptTemplatesBackup(backupId);
@ -407,7 +405,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
}
@Override
public void updatePromptTemplatesById(PromptTemplatesDO promptTemplatesDO) {
public void updatePromptTemplatesById (PromptTemplatesDO promptTemplatesDO) {
promptTemplatesMapper.updateById(promptTemplatesDO);
}
@ -418,24 +416,24 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
* @return 优化后的内容
*/
@Override
public String optimizePrompt(OptimizePromptReqVO reqVO) {
public String optimizePrompt (OptimizePromptReqVO reqVO) {
String resultData = HttpUtils.post(llmBackendProperties.getOptimizePrompt(), null, JSON.toJSONString(reqVO));
if (StringUtils.isBlank(resultData)) {
log.error("prompt优化失败:{}",OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
log.error("prompt优化失败:{}", OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
throw exception(OPTIMIZE_PROMPT_NOT_EXISTS);
}
JSONObject jsonData = JSONObject.parseObject(resultData);
JSONObject optimizedPrompt = jsonData.getJSONObject("optimized_prompt");
if (optimizedPrompt==null) {
log.error("prompt优化失败:{}",OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
if (optimizedPrompt == null) {
log.error("prompt优化失败:{}", OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
throw exception(OPTIMIZE_PROMPT_NOT_EXISTS);
}
String content = optimizedPrompt.getString("content");
if (StringUtils.isBlank(content)) {
log.error("prompt优化失败:{}",OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
log.error("prompt优化失败:{}", OPTIMIZE_PROMPT_NOT_EXISTS.getMsg());
throw exception(OPTIMIZE_PROMPT_NOT_EXISTS);
}

View File

@ -50,7 +50,7 @@ import static cn.iocoder.yudao.module.llm.enums.ErrorCodeConstants.*;
*/
@Service
@Validated
public class PromptTemplatesBackupServiceImpl implements PromptTemplatesBackupService {
public class PromptTemplatesBackupServiceImpl implements PromptTemplatesBackupService {
@Resource
private PromptTemplatesMapper promptTemplatesMapper;
@ -137,84 +137,78 @@ public class PromptTemplatesBackupServiceImpl implements PromptTemplatesBackupSe
@Override
public PromptTemplatesBackupRespVO getPromptTemplatesBackup(Long id) {
PromptTemplatesBackupDO templatesDO = this.promptTemplatesBackupMapper.selectById(id);
if(templatesDO == null) {
return null;
if (templatesDO == null) {
throw exception(PROMPT_TEMPLATES_BACKUP_NOT_EXISTS);
}
PromptTemplatesBackupRespVO promptTemplates = BeanUtils.toBean(templatesDO, PromptTemplatesBackupRespVO.class);
// 标签
List<PromptTemplatesTagsDO> tagDOList = this.promptTemplatesTagsMapper
.selectList("prompt_template_id", templatesDO.getSrcId());
getPromptTemplatesLabelResult(id, promptTemplates);
List<LabelDO> labelList = labelService.getLabelList();
if (CollectionUtils.isEmpty(labelList)){
labelList=new ArrayList<>();
}
// 应用
getPromptTemplatesApplicationsResult(id, promptTemplates);
Map<Long, String> labelMap = new HashMap<>();
labelList.forEach(labelDO -> labelMap.put(labelDO.getId(), labelDO.getLabelName()));
return promptTemplates;
}
/**
* 标签
*
* @param id 模版ID
* @param promptTemplates 模版
*/
private void getPromptTemplatesLabelResult (Long id, PromptTemplatesBackupRespVO promptTemplates) {
// 标签
List<PromptTemplatesTagsBackupDO> tagDOList = this.promptTemplatesTagsBackupMapper
.selectList("prompt_template_id", id);
if (tagDOList!= null &&!tagDOList.isEmpty()) {
Map<Long, String> labelMap = labelService.getLabelMap(false);
if (CollectionUtils.isNotEmpty(tagDOList)) {
// 过滤 tagDOList labelMap 中不存在的 tagIds只保留存在的
List<String> tagIds = tagDOList.stream()
.filter(tagsDO -> labelMap.containsKey(tagsDO.getTagId()))
.map(p -> String.valueOf(p.getTagId()))
.collect(Collectors.toList());
promptTemplates.setTagIds(tagIds);
// 获取对应的标签名称
List<String> labelNameList = tagIds.stream()
.map(tagId -> labelMap.computeIfAbsent(Long.parseLong(tagId), k -> null))
.collect(Collectors.toList());
promptTemplates.setTagNames(labelNameList);
}
}
// 应用
List<PromptTemplatesApplicationsDO> applicationDOList = this.promptTemplatesApplicationsMapper
.selectList("prompt_template_id", templatesDO.getSrcId());
/**
* 应用
*
* @param id 模版ID
* @param promptTemplates 模版
*/
private void getPromptTemplatesApplicationsResult (Long id, PromptTemplatesBackupRespVO promptTemplates) {
// 获取当前模版的应用
List<PromptTemplatesApplicationsBackupDO> applicationDOList = this.promptTemplatesApplicationsBackupMapper
.selectList("prompt_template_id", id);
List<ApplicationDO> appList = applicationService.getList();
if (CollectionUtils.isEmpty(appList)){
appList=new ArrayList<>();
}
Map<Long, String> appMap = applicationService.getApplicatioMap();
labelList.forEach(labelDO -> labelMap.put(labelDO.getId(), labelDO.getLabelName()));
HashMap<Long,String> appMap = new HashMap<>();
appList.forEach(applicationDO -> appMap.put(applicationDO.getId(),applicationDO.getAppName()));
if(applicationDOList!=null && !applicationDOList.isEmpty()) {
if (CollectionUtils.isNotEmpty(applicationDOList)) {
// 过滤 applicationDOList appMap 中不存在的 applicationIds只保留存在的
List<String> applicationIds = applicationDOList.stream()
.map(p->p.getApplicationId().toString())
.filter(applicationDO -> appMap.containsKey(applicationDO.getApplicationId()))
.map(p -> p.getApplicationId().toString())
.collect(Collectors.toList());
promptTemplates.setApplicationIds(applicationIds);
// 获取对应的应用名称
List<String> appNameList = applicationIds.stream()
.map(appId -> appMap.computeIfAbsent(Long.parseLong(appId), k -> null))
.collect(Collectors.toList());
promptTemplates.setApplicationNames(appNameList);
}
// String categoryName = this.dictDataApi.getDictDataLabel("llm_prompt_template_framework",Integer.valueOf(templatesDO.getTemplateFramework()));
// switch (categoryName) {
// case "简介框架":
// promptTemplates.setBriefFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), BriefFramework.class));
// promptTemplates.setTemplateText("");
// break;
// case "角色能力框架":
// promptTemplates.setAbilityFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), AbilityFramework.class));
// promptTemplates.setTemplateText("");
// break;
// case "样例提示框架":
// promptTemplates.setSampleFramework(JSON.parseObject(promptTemplates
// .getTemplateText(), SampleFramework.class));
// promptTemplates.setTemplateText("");
// break;
// default:
// break;
// }
return promptTemplates;
}
@Override
@ -223,17 +217,6 @@ public class PromptTemplatesBackupServiceImpl implements PromptTemplatesBackupSe
PageResult<PromptTemplatesBackupRespVO> pageRespList = BeanUtils.toBean(pageDoList, PromptTemplatesBackupRespVO.class);
List<PromptTemplatesBackupRespVO> templatesRespList = pageRespList.getList();
List<LabelDO> labelList = labelService.getLabelList();
HashMap<Long,String> labelMap = new HashMap<>();
for (LabelDO labelDO : labelList) {
labelMap.put(labelDO.getId(),labelDO.getLabelName());
}
List<ApplicationDO> appList = applicationService.getList();
HashMap<Long,String> appMap = new HashMap<>();
for (ApplicationDO applicationDO : appList) {
appMap.put(applicationDO.getId(),applicationDO.getAppName());
}
for(PromptTemplatesBackupRespVO respVO : templatesRespList) {
// TODO 使用量查询暂时设置为0
@ -241,32 +224,49 @@ public class PromptTemplatesBackupServiceImpl implements PromptTemplatesBackupSe
try {
List<PromptTemplatesTagsBackupDO> tagDOList = this.promptTemplatesTagsBackupMapper
.selectList("prompt_template_id", respVO.getId());
if(tagDOList!=null && !tagDOList.isEmpty()) {
List<String> tagIds = tagDOList.stream()
.map(p->p.getTagId().toString())
.collect(Collectors.toList());
List<String> labelNameList = new ArrayList<>();
for (String tagId : tagIds) {
labelNameList.add(labelMap.get(Long.parseLong(tagId)));
}
// 标签
Map<Long, String> labelMap = labelService.getLabelMap(true);
if (CollectionUtils.isNotEmpty(tagDOList)) {
// 过滤 tagDOList labelMap 中不存在的 tagIds只保留存在的
List<String> tagIds = tagDOList.stream()
.filter(tagsDO -> labelMap.containsKey(tagsDO.getTagId()))
.map(p -> String.valueOf(p.getTagId()))
.collect(Collectors.toList());
respVO.setTagIds(tagIds);
// 获取对应的标签名称
List<String> labelNameList = tagIds.stream()
.map(tagId -> labelMap.computeIfAbsent(Long.parseLong(tagId), k -> null))
.collect(Collectors.toList());
respVO.setTagNames(labelNameList);
}
} catch(Exception e) {
e.printStackTrace();
}
try {
List<PromptTemplatesApplicationsBackupDO> applicationIds = this.promptTemplatesApplicationsBackupMapper.selectList(
new QueryWrapper<PromptTemplatesApplicationsBackupDO>()
.eq("prompt_template_id", respVO.getId()));
// 获取当前模版的应用
List<PromptTemplatesApplicationsBackupDO> applicationDOList = this.promptTemplatesApplicationsBackupMapper
.selectList("prompt_template_id", respVO.getId());
List<String> appNames = new ArrayList<>();
for (PromptTemplatesApplicationsBackupDO applicationId : applicationIds) {
appNames.add(appMap.get(applicationId.getApplicationId()));
Map<Long, String> appMap = applicationService.getApplicatioMap();
if (CollectionUtils.isNotEmpty(applicationDOList)) {
// 过滤 applicationDOList appMap 中不存在的 applicationIds只保留存在的
List<String> applicationIds = applicationDOList.stream()
.filter(applicationDO -> appMap.containsKey(applicationDO.getApplicationId()))
.map(p -> p.getApplicationId().toString())
.collect(Collectors.toList());
respVO.setApplicationIds(applicationIds);
// 获取对应的应用名称
List<String> appNameList = applicationIds.stream()
.map(appId -> appMap.computeIfAbsent(Long.parseLong(appId), k -> null))
.collect(Collectors.toList());
respVO.setApplicationNames(appNameList);
}
respVO.setApplicationCount(applicationIds.size());
respVO.setApplicationNames(appNames);
} catch(Exception e) {
e.printStackTrace();
}