[update] 标签管理 使用状态从启用变为停用时检查Prompt模版是否使用该标签

This commit is contained in:
Liuyang 2025-01-16 11:09:08 +08:00
parent 1c7104c862
commit 76e2076be2
6 changed files with 75 additions and 7 deletions

View File

@ -19,6 +19,7 @@ import cn.iocoder.yudao.module.llm.service.conversation.ConversationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.context.annotation.Lazy;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
@ -41,6 +42,7 @@ public class ApplicationController {
@Resource
private ApplicationService applicationService;
@Lazy
@Resource
private ConversationService conversationService;

View File

@ -6,8 +6,10 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.llm.controller.admin.prompttemplates.vo.*;
import org.apache.ibatis.annotations.Param;
/**
* Prompt 模板表用于记录每个模板的信息 Mapper
@ -29,4 +31,11 @@ public interface PromptTemplatesMapper extends BaseMapperX<PromptTemplatesDO> {
.orderByDesc(PromptTemplatesDO::getId));
}
/**
* 根据 标签id 获取 模版
*
* @param labelId 标签id
* @return 模版
*/
List<PromptTemplatesDO> getPromptTemplateByLabelId (@Param("labelId") Long labelId);
}

View File

@ -6,11 +6,13 @@ 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.dataobject.prompttemplates.PromptTemplatesDO;
import cn.iocoder.yudao.module.llm.dal.mysql.label.LabelMapper;
import cn.iocoder.yudao.module.llm.service.application.ApplicationService;
import cn.iocoder.yudao.module.llm.service.prompttemplates.PromptTemplatesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
@ -40,6 +42,10 @@ public class LabelServiceImpl implements LabelService {
@Resource
private ApplicationService applicationService;
@Lazy
@Resource
private PromptTemplatesService promptTemplatesService;
@Override
public Long createLabel (LabelSaveReqVO createReqVO) {
validateLabelNameExists(createReqVO);
@ -92,9 +98,9 @@ public class LabelServiceImpl implements LabelService {
// 检查标签是否在 应用中心 有使用
validateLabelUsesInApplicationCenter(id, labelName);
// 检查标签是否在 Prompt 模版中有使用
// validateLabelUsesInPromptTemplate(id);
validateLabelUsesInPromptTemplate(id, labelName);
// 检查标签是否在 Prompt 模版备份中有使用
// validateLabelUsesInPromptTemplateBackup(id);
// validateLabelUsesInPromptTemplateBackup(id, labelName);
}
/**
@ -111,6 +117,20 @@ public class LabelServiceImpl implements LabelService {
}
}
/**
* 检查标签是否在 Prompt模版 有使用
*
* @param id 标签id
* @param name 标签名称
*/
private void validateLabelUsesInPromptTemplate (Long id, String name) {
Map<Long, String> template = promptTemplatesService.getPromptTemplateByLabelId(id);
if (CollectionUtils.isNotEmpty(template)) {
String msg = String.format("标签【%s】在 Prompt模版 %s 有使用请先删除Prompt模版中的标签", name, template.values());
throw exception(new ErrorCode(10004_002, msg));
}
}
@Override
public void deleteLabel (Long id) {
// 校验存在

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplate
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;
public interface PromptTemplatesService {
Long createPromptTemplates (@Valid PromptTemplatesSaveReqVO createReqVO);
@ -38,4 +39,11 @@ public interface PromptTemplatesService {
*/
String optimizePrompt (@Valid OptimizePromptReqVO reqVO);
/**
* 根据 标签id 获取 模版
*
* @param labelId 标签id
* @return 模版
*/
Map<Long,String> getPromptTemplateByLabelId (Long labelId);
}

View File

@ -8,7 +8,6 @@ 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;
@ -24,7 +23,6 @@ import cn.iocoder.yudao.module.llm.service.prompttemplatesbackup.PromptTemplates
import cn.iocoder.yudao.module.system.api.dict.DictDataApi;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import lombok.extern.slf4j.Slf4j;
@ -53,6 +51,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
@Resource
private DictDataApi dictDataApi;
@Resource
@Lazy
private LabelService labelService;
@Resource
private PromptTemplatesBackupService promptTemplatesBackupService;
@ -250,7 +249,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
List<PromptTemplatesApplicationsDO> applicationDOList = this.promptTemplatesApplicationsMapper
.selectList("prompt_template_id", id);
Map<Long, String> appMap = applicationService.getApplicatioMap();
Map<Long, String> appMap = applicationService.getApplicatioMap();
if (CollectionUtils.isNotEmpty(applicationDOList)) {
@ -306,7 +305,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
List<PromptTemplatesApplicationsDO> applicationDOList = this.promptTemplatesApplicationsMapper
.selectList("prompt_template_id", respVO.getId());
Map<Long, String> appMap = applicationService.getApplicatioMap();
Map<Long, String> appMap = applicationService.getApplicatioMap();
if (CollectionUtils.isNotEmpty(applicationDOList)) {
// 过滤 applicationDOList appMap 中不存在的 applicationIds只保留存在的
@ -439,4 +438,23 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService {
return content;
}
/**
* 根据 标签id 获取 模版
*
* @param labelId 标签id
* @return 模版
*/
@Override
public Map<Long, String> getPromptTemplateByLabelId (Long labelId) {
if (labelId == null) {
return Collections.emptyMap();
}
List<PromptTemplatesDO> templatesDO = this.promptTemplatesMapper.getPromptTemplateByLabelId(labelId);
if (CollectionUtils.isEmpty(templatesDO)) {
return Collections.emptyMap();
}
return templatesDO.stream().collect(Collectors.toMap(PromptTemplatesDO::getId, PromptTemplatesDO::getName));
}
}

View File

@ -9,4 +9,15 @@
文档可见https://www.iocoder.cn/MyBatis/x-plugins/
-->
<!-- 根据 标签id 获取 模版 -->
<select id="getPromptTemplateByLabelId"
resultType="cn.iocoder.yudao.module.llm.dal.dataobject.prompttemplates.PromptTemplatesDO">
SELECT lpt.id AS id,
lpt.name AS name
FROM llm_prompt_templates lpt
LEFT JOIN llm_prompt_templates_tags lptt ON lpt.id = lptt.prompt_template_id
WHERE lptt.tag_id = #{labelId}
AND lpt.deleted = FALSE
AND lptt.deleted = FALSE
</select>
</mapper>