From 76e2076be263753df5a316748ef90291cb223d35 Mon Sep 17 00:00:00 2001 From: Liuyang <2746366019@qq.com> Date: Thu, 16 Jan 2025 11:09:08 +0800 Subject: [PATCH] =?UTF-8?q?[update]=20=E6=A0=87=E7=AD=BE=E7=AE=A1=E7=90=86?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8=E7=8A=B6=E6=80=81=E4=BB=8E=E5=90=AF?= =?UTF-8?q?=E7=94=A8=E5=8F=98=E4=B8=BA=E5=81=9C=E7=94=A8=E6=97=B6=E6=A3=80?= =?UTF-8?q?=E6=9F=A5Prompt=E6=A8=A1=E7=89=88=E6=98=AF=E5=90=A6=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=E8=AF=A5=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/ApplicationController.java | 2 ++ .../PromptTemplatesMapper.java | 9 +++++++ .../llm/service/label/LabelServiceImpl.java | 26 ++++++++++++++++--- .../PromptTemplatesService.java | 8 ++++++ .../PromptTemplatesServiceImpl.java | 26 ++++++++++++++++--- .../prompttemplates/PromptTemplatesMapper.xml | 11 ++++++++ 6 files changed, 75 insertions(+), 7 deletions(-) diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/application/ApplicationController.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/application/ApplicationController.java index 5a88f157d..05ee89ebd 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/application/ApplicationController.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/controller/admin/application/ApplicationController.java @@ -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; diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/dal/mysql/prompttemplates/PromptTemplatesMapper.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/dal/mysql/prompttemplates/PromptTemplatesMapper.java index 6cb4f8104..86b737982 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/dal/mysql/prompttemplates/PromptTemplatesMapper.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/dal/mysql/prompttemplates/PromptTemplatesMapper.java @@ -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 { .orderByDesc(PromptTemplatesDO::getId)); } + /** + * 根据 标签id 获取 模版 + * + * @param labelId 标签id + * @return 模版 + */ + List getPromptTemplateByLabelId (@Param("labelId") Long labelId); } diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/label/LabelServiceImpl.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/label/LabelServiceImpl.java index 3185cfde2..6db485294 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/label/LabelServiceImpl.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/label/LabelServiceImpl.java @@ -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 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) { // 校验存在 diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesService.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesService.java index 69745d971..7234a084f 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesService.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesService.java @@ -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 getPromptTemplateByLabelId (Long labelId); } diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesServiceImpl.java b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesServiceImpl.java index 53a090ea8..ac3b29a58 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesServiceImpl.java +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/java/cn/iocoder/yudao/module/llm/service/prompttemplates/PromptTemplatesServiceImpl.java @@ -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 applicationDOList = this.promptTemplatesApplicationsMapper .selectList("prompt_template_id", id); - Map appMap = applicationService.getApplicatioMap(); + Map appMap = applicationService.getApplicatioMap(); if (CollectionUtils.isNotEmpty(applicationDOList)) { @@ -306,7 +305,7 @@ public class PromptTemplatesServiceImpl implements PromptTemplatesService { List applicationDOList = this.promptTemplatesApplicationsMapper .selectList("prompt_template_id", respVO.getId()); - Map appMap = applicationService.getApplicatioMap(); + Map 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 getPromptTemplateByLabelId (Long labelId) { + if (labelId == null) { + return Collections.emptyMap(); + } + List templatesDO = this.promptTemplatesMapper.getPromptTemplateByLabelId(labelId); + if (CollectionUtils.isEmpty(templatesDO)) { + return Collections.emptyMap(); + } + + return templatesDO.stream().collect(Collectors.toMap(PromptTemplatesDO::getId, PromptTemplatesDO::getName)); + } } diff --git a/yudao-module-llm/yudao-module-llm-biz/src/main/resources/mapper/prompttemplates/PromptTemplatesMapper.xml b/yudao-module-llm/yudao-module-llm-biz/src/main/resources/mapper/prompttemplates/PromptTemplatesMapper.xml index bb4427bce..e3058ce71 100644 --- a/yudao-module-llm/yudao-module-llm-biz/src/main/resources/mapper/prompttemplates/PromptTemplatesMapper.xml +++ b/yudao-module-llm/yudao-module-llm-biz/src/main/resources/mapper/prompttemplates/PromptTemplatesMapper.xml @@ -9,4 +9,15 @@ 文档可见:https://www.iocoder.cn/MyBatis/x-plugins/ --> + +