【代码优化】AI:知识库的索引过程

This commit is contained in:
YunaiV 2025-02-28 18:57:17 +08:00
parent 7918ba7d29
commit e5cc9d2ad8
19 changed files with 151 additions and 49 deletions

View File

@ -9,6 +9,7 @@ import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnow
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO; import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO;
import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeService; import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.validation.Valid; import jakarta.validation.Valid;
@ -17,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// TODO @芋艿增加权限标识
@Tag(name = "管理后台 - AI 知识库") @Tag(name = "管理后台 - AI 知识库")
@RestController @RestController
@RequestMapping("/ai/knowledge") @RequestMapping("/ai/knowledge")
@ -34,6 +36,14 @@ public class AiKnowledgeController {
return success(BeanUtils.toBean(pageResult, AiKnowledgeRespVO.class)); return success(BeanUtils.toBean(pageResult, AiKnowledgeRespVO.class));
} }
@GetMapping("/get")
@Operation(summary = "获得知识库")
@Parameter(name = "id", description = "编号", required = true, example = "1024")
public CommonResult<AiKnowledgeRespVO> getKnowledge(@RequestParam("id") Long id) {
AiKnowledgeDO knowledge = knowledgeService.getKnowledge(id);
return success(BeanUtils.toBean(knowledge, AiKnowledgeRespVO.class));
}
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "创建知识库") @Operation(summary = "创建知识库")
public CommonResult<Long> createKnowledge(@RequestBody @Valid AiKnowledgeSaveReqVO createReqVO) { public CommonResult<Long> createKnowledge(@RequestBody @Valid AiKnowledgeSaveReqVO createReqVO) {

View File

@ -6,7 +6,7 @@ import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentRespVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentRespVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateListReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentCreateListReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO; import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeDocumentService; import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeDocumentService;
@ -21,6 +21,7 @@ import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// TODO @芋艿增加权限标识
@Tag(name = "管理后台 - AI 知识库文档") @Tag(name = "管理后台 - AI 知识库文档")
@RestController @RestController
@RequestMapping("/ai/knowledge/document") @RequestMapping("/ai/knowledge/document")
@ -38,6 +39,13 @@ public class AiKnowledgeDocumentController {
return success(BeanUtils.toBean(pageResult, AiKnowledgeDocumentRespVO.class)); return success(BeanUtils.toBean(pageResult, AiKnowledgeDocumentRespVO.class));
} }
@GetMapping("/get")
@Operation(summary = "获取文档详情")
public CommonResult<AiKnowledgeDocumentRespVO> getKnowledgeDocument(@RequestParam("id") Long id) {
AiKnowledgeDocumentDO document = documentService.getKnowledgeDocument(id);
return success(BeanUtils.toBean(document, AiKnowledgeDocumentRespVO.class));
}
@PostMapping("/create") @PostMapping("/create")
@Operation(summary = "新建文档") @Operation(summary = "新建文档")
public CommonResult<Long> createKnowledgeDocument(@RequestBody @Valid AiKnowledgeDocumentCreateReqVO reqVO) { public CommonResult<Long> createKnowledgeDocument(@RequestBody @Valid AiKnowledgeDocumentCreateReqVO reqVO) {

View File

@ -18,6 +18,7 @@ import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
// TODO @芋艿增加权限标识
@Tag(name = "管理后台 - AI 知识库段落") @Tag(name = "管理后台 - AI 知识库段落")
@RestController @RestController
@RequestMapping("/ai/knowledge/segment") @RequestMapping("/ai/knowledge/segment")

View File

@ -1,4 +1,4 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge; package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
@ -17,6 +17,10 @@ public class AiKnowledgeDocumentCreateListReqVO {
@NotNull(message = "知识库编号不能为空") @NotNull(message = "知识库编号不能为空")
private Long knowledgeId; private Long knowledgeId;
@Schema(description = "分段的最大 Token 数", requiredMode = Schema.RequiredMode.REQUIRED, example = "800")
@NotNull(message = "分段的最大 Token 数不能为空")
private Integer segmentMaxTokens;
@Schema(description = "文档列表", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "文档列表", requiredMode = Schema.RequiredMode.REQUIRED)
@NotEmpty(message = "文档列表不能为空") @NotEmpty(message = "文档列表不能为空")
private List<Document> list; private List<Document> list;
@ -33,10 +37,6 @@ public class AiKnowledgeDocumentCreateListReqVO {
@URL(message = "文档 URL 格式不正确") @URL(message = "文档 URL 格式不正确")
private String url; private String url;
@Schema(description = "分段的最大 Token 数", requiredMode = Schema.RequiredMode.REQUIRED, example = "800")
@NotNull(message = "分段的最大 Token 数不能为空")
private Integer segmentMaxTokens;
} }
} }

View File

@ -8,6 +8,9 @@ import lombok.Data;
@Data @Data
public class AiKnowledgeDocumentPageReqVO extends PageParam { public class AiKnowledgeDocumentPageReqVO extends PageParam {
@Schema(description = "知识库编号", example = "1")
private Long knowledgeId;
@Schema(description = "文档名称", example = "Java 开发手册") @Schema(description = "文档名称", example = "Java 开发手册")
private String name; private String name;

View File

@ -1,38 +1,45 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document; package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
@Schema(description = "管理后台 - AI 知识库-文档 Response VO") import java.time.LocalDateTime;
@Data
public class AiKnowledgeDocumentRespVO extends PageParam {
@Schema(description = "编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24790") @Schema(description = "管理后台 - AI 知识库文档 Response VO")
@Data
public class AiKnowledgeDocumentRespVO {
@Schema(description = "文档编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24790")
private Long id; private Long id;
@Schema(description = "知识库编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24790") @Schema(description = "知识库编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "24790")
private Long knowledgeId; private Long knowledgeId;
@Schema(description = "名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "Java 开发手册") @Schema(description = "文档名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "Java 开发手册")
private String name; private String name;
@Schema(description = "内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "Java 是一门面向对象的语言.....") @Schema(description = "文档 URL", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://doc.iocoder.cn")
private String content;
@Schema(description = "文档 url", requiredMode = Schema.RequiredMode.REQUIRED, example = "https://doc.iocoder.cn")
private String url; private String url;
@Schema(description = "token 数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024") @Schema(description = "文档内容", requiredMode = Schema.RequiredMode.REQUIRED, example = "Java 是一门面向对象的语言.....")
private String content;
@Schema(description = "文档内容长度", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
private Integer contentLength;
@Schema(description = "文档 Token 数量", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
private Integer tokens; private Integer tokens;
@Schema(description = "字符数", requiredMode = Schema.RequiredMode.REQUIRED, example = "1008") @Schema(description = "分片最大 Token ", requiredMode = Schema.RequiredMode.REQUIRED, example = "512")
private Integer wordCount; private Integer segmentMaxTokens;
@Schema(description = "切片状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "召回次数", requiredMode = Schema.RequiredMode.REQUIRED, example = "10")
private Integer sliceStatus; private Integer retrievalCount;
@Schema(description = "文档状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1") @Schema(description = "文档状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "0")
private Integer status; private Integer status;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
} }

View File

@ -6,8 +6,8 @@ import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
// TODO @芋艿稍后优化
@Schema(description = "管理后台 - AI 更新 知识库-文档 Request VO") @Schema(description = "管理后台 - AI 知识库文档更新 Request VO")
@Data @Data
public class AiKnowledgeDocumentUpdateReqVO { public class AiKnowledgeDocumentUpdateReqVO {

View File

@ -1,14 +1,29 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge; package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
@Schema(description = "管理后台 - AI 知识库的分页 Request VO") @Schema(description = "管理后台 - AI 知识库的分页 Request VO")
@Data @Data
public class AiKnowledgePageReqVO extends PageParam { public class AiKnowledgePageReqVO extends PageParam {
@Schema(description = "知识库名称", example = "Java 开发手册") @Schema(description = "知识库名称", example = "芋艿")
private String name; private String name;
@Schema(description = "是否启用", example = "1")
@InEnum(CommonStatusEnum.class)
private Integer status;
@Schema(description = "创建时间")
@DateTimeFormat(pattern = FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)
private LocalDateTime[] createTime;
} }

View File

@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime;
@Schema(description = "管理后台 - AI 知识库 Response VO") @Schema(description = "管理后台 - AI 知识库 Response VO")
@Data @Data
@ -17,10 +18,22 @@ public class AiKnowledgeRespVO {
@Schema(description = "知识库描述", example = "帮助你快速构建系统") @Schema(description = "知识库描述", example = "帮助你快速构建系统")
private String description; private String description;
@Schema(description = "模型编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "14") @Schema(description = "向量模型编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "14")
private Long modelId; private Long embeddingModelId;
@Schema(description = "模型标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "qwen-72b-chat") @Schema(description = "向量模型标识", requiredMode = Schema.RequiredMode.REQUIRED, example = "qwen-72b-chat")
private String model; private String embeddingModel;
@Schema(description = "topK", requiredMode = Schema.RequiredMode.REQUIRED, example = "3")
private Integer topK;
@Schema(description = "相似度阈值", requiredMode = Schema.RequiredMode.REQUIRED, example = "0.7")
private Double similarityThreshold;
@Schema(description = "状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
private Integer status;
@Schema(description = "创建时间", requiredMode = Schema.RequiredMode.REQUIRED)
private LocalDateTime createTime;
} }

View File

@ -1,5 +1,7 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge; package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.validation.InEnum;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
@ -31,6 +33,9 @@ public class AiKnowledgeSaveReqVO {
@NotNull(message = "相似性阈值不能为空") @NotNull(message = "相似性阈值不能为空")
private Double similarityThreshold; private Double similarityThreshold;
// TODO @芋艿status @Schema(description = "是否启用", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "是否启用不能为空")
@InEnum(CommonStatusEnum.class)
private Integer status;
} }

View File

@ -29,7 +29,7 @@ public class AiKnowledgeDocumentDO extends BaseDO {
*/ */
private Long knowledgeId; private Long knowledgeId;
/** /**
* 名称 * 名称
*/ */
private String name; private String name;
/** /**
@ -50,10 +50,15 @@ public class AiKnowledgeDocumentDO extends BaseDO {
*/ */
private Integer tokens; private Integer tokens;
/** /**
* 每个文本块的目标 token * 分片最大 Token
*/ */
private Integer segmentMaxTokens; private Integer segmentMaxTokens;
/**
* 召回次数
*/
private Integer retrievalCount;
/** /**
* 状态 * 状态
* <p> * <p>

View File

@ -57,6 +57,11 @@ public class AiKnowledgeSegmentDO extends BaseDO {
*/ */
private Integer tokens; private Integer tokens;
/**
* 召回次数
*/
private Integer retrievalCount;
/** /**
* 状态 * 状态
* <p> * <p>

View File

@ -8,7 +8,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
/** /**
* AI 知识库-文档 Mapper * AI 知识库文档 Mapper
* *
* @author xiaoxin * @author xiaoxin
*/ */
@ -17,6 +17,7 @@ public interface AiKnowledgeDocumentMapper extends BaseMapperX<AiKnowledgeDocume
default PageResult<AiKnowledgeDocumentDO> selectPage(AiKnowledgeDocumentPageReqVO reqVO) { default PageResult<AiKnowledgeDocumentDO> selectPage(AiKnowledgeDocumentPageReqVO reqVO) {
return selectPage(reqVO, new LambdaQueryWrapperX<AiKnowledgeDocumentDO>() return selectPage(reqVO, new LambdaQueryWrapperX<AiKnowledgeDocumentDO>()
.eqIfPresent(AiKnowledgeDocumentDO::getKnowledgeId, reqVO.getKnowledgeId())
.likeIfPresent(AiKnowledgeDocumentDO::getName, reqVO.getName()) .likeIfPresent(AiKnowledgeDocumentDO::getName, reqVO.getName())
.orderByDesc(AiKnowledgeDocumentDO::getId)); .orderByDesc(AiKnowledgeDocumentDO::getId));
} }

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.ai.dal.mysql.knowledge; package cn.iocoder.yudao.module.ai.dal.mysql.knowledge;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
@ -9,7 +8,7 @@ import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
/** /**
* AI 知识库基础信息 Mapper * AI 知识库 Mapper
* *
* @author xiaoxin * @author xiaoxin
*/ */
@ -18,7 +17,10 @@ public interface AiKnowledgeMapper extends BaseMapperX<AiKnowledgeDO> {
default PageResult<AiKnowledgeDO> selectPage(AiKnowledgePageReqVO pageReqVO) { default PageResult<AiKnowledgeDO> selectPage(AiKnowledgePageReqVO pageReqVO) {
return selectPage(pageReqVO, new LambdaQueryWrapperX<AiKnowledgeDO>() return selectPage(pageReqVO, new LambdaQueryWrapperX<AiKnowledgeDO>()
.eq(AiKnowledgeDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) .likeIfPresent(AiKnowledgeDO::getName, pageReqVO.getName())
.likeIfPresent(AiKnowledgeDO::getName, pageReqVO.getName())); .eqIfPresent(AiKnowledgeDO::getStatus, pageReqVO.getStatus())
.betweenIfPresent(AiKnowledgeDO::getCreateTime, pageReqVO.getCreateTime())
.orderByDesc(AiKnowledgeDO::getId));
} }
} }

View File

@ -10,7 +10,7 @@ import org.apache.ibatis.annotations.Mapper;
import java.util.List; import java.util.List;
/** /**
* AI 知识库-分片 Mapper * AI 知识库分片 Mapper
* *
* @author xiaoxin * @author xiaoxin
*/ */

View File

@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateListReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentCreateListReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO; import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
import java.util.List; import java.util.List;
@ -40,6 +40,14 @@ public interface AiKnowledgeDocumentService {
*/ */
PageResult<AiKnowledgeDocumentDO> getKnowledgeDocumentPage(AiKnowledgeDocumentPageReqVO pageReqVO); PageResult<AiKnowledgeDocumentDO> getKnowledgeDocumentPage(AiKnowledgeDocumentPageReqVO pageReqVO);
/**
* 获取文档详情
*
* @param id 文档编号
* @return 文档详情
*/
AiKnowledgeDocumentDO getKnowledgeDocument(Long id);
/** /**
* 更新文档 * 更新文档
* *

View File

@ -8,7 +8,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentPageReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentUpdateReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateListReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.document.AiKnowledgeDocumentCreateListReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgeDocumentCreateReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO; import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeDocumentMapper; import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeDocumentMapper;
@ -84,15 +84,17 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic
for (int i = 0; i < createListReqVO.getList().size(); i++) { for (int i = 0; i < createListReqVO.getList().size(); i++) {
AiKnowledgeDocumentCreateListReqVO.Document documentVO = createListReqVO.getList().get(i); AiKnowledgeDocumentCreateListReqVO.Document documentVO = createListReqVO.getList().get(i);
String content = contents.get(i); String content = contents.get(i);
documentDOs.add(BeanUtils.toBean(documentVO, AiKnowledgeDocumentDO.class).setKnowledgeId(createListReqVO.getKnowledgeId()) documentDOs.add(BeanUtils.toBean(documentVO, AiKnowledgeDocumentDO.class)
.setContent(content).setContentLength(content.length()).setTokens(tokenCountEstimator.estimate(content)) .setKnowledgeId(createListReqVO.getKnowledgeId())
.setContent(content).setContentLength(content.length())
.setTokens(tokenCountEstimator.estimate(content))
.setStatus(CommonStatusEnum.ENABLE.getStatus())); .setStatus(CommonStatusEnum.ENABLE.getStatus()));
} }
knowledgeDocumentMapper.insertBatch(documentDOs); knowledgeDocumentMapper.insertBatch(documentDOs);
// 4. 批量创建文档切片异步 // 4. 批量创建文档切片异步
documentDOs.forEach(documentDO -> documentDOs.forEach(documentDO -> knowledgeSegmentService
knowledgeSegmentService.createKnowledgeSegmentBySplitContentAsync(documentDO.getId(), documentDO.getContent())); .createKnowledgeSegmentBySplitContentAsync(documentDO.getId(), documentDO.getContent()));
return convertList(documentDOs, AiKnowledgeDocumentDO::getId); return convertList(documentDOs, AiKnowledgeDocumentDO::getId);
} }
@ -101,6 +103,11 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic
return knowledgeDocumentMapper.selectPage(pageReqVO); return knowledgeDocumentMapper.selectPage(pageReqVO);
} }
@Override
public AiKnowledgeDocumentDO getKnowledgeDocument(Long id) {
return knowledgeDocumentMapper.selectById(id);
}
@Override @Override
public void updateKnowledgeDocument(AiKnowledgeDocumentUpdateReqVO reqVO) { public void updateKnowledgeDocument(AiKnowledgeDocumentUpdateReqVO reqVO) {
// 1. 校验文档是否存在 // 1. 校验文档是否存在

View File

@ -27,6 +27,14 @@ public interface AiKnowledgeService {
*/ */
void updateKnowledge(AiKnowledgeSaveReqVO updateReqVO); void updateKnowledge(AiKnowledgeSaveReqVO updateReqVO);
/**
* 获得知识库
*
* @param id 编号
* @return 知识库
*/
AiKnowledgeDO getKnowledge(Long id);
/** /**
* 校验知识库是否存在 * 校验知识库是否存在
* *

View File

@ -1,6 +1,5 @@
package cn.iocoder.yudao.module.ai.service.knowledge; package cn.iocoder.yudao.module.ai.service.knowledge;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgePageReqVO; import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnowledgePageReqVO;
@ -8,11 +7,9 @@ import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.knowledge.AiKnow
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO; import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO;
import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeMapper; import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeMapper;
import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService;
import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; import cn.iocoder.yudao.module.ai.service.model.AiChatModelService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
@ -40,7 +37,7 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
// 2. 插入知识库 // 2. 插入知识库
AiKnowledgeDO knowledge = BeanUtils.toBean(createReqVO, AiKnowledgeDO.class) AiKnowledgeDO knowledge = BeanUtils.toBean(createReqVO, AiKnowledgeDO.class)
.setEmbeddingModel(model.getModel()).setStatus(CommonStatusEnum.ENABLE.getStatus()); .setEmbeddingModel(model.getModel());
knowledgeMapper.insert(knowledge); knowledgeMapper.insert(knowledge);
return knowledge.getId(); return knowledge.getId();
} }
@ -56,6 +53,13 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService {
AiKnowledgeDO updateObj = BeanUtils.toBean(updateReqVO, AiKnowledgeDO.class) AiKnowledgeDO updateObj = BeanUtils.toBean(updateReqVO, AiKnowledgeDO.class)
.setEmbeddingModel(model.getModel()); .setEmbeddingModel(model.getModel());
knowledgeMapper.updateById(updateObj); knowledgeMapper.updateById(updateObj);
// TODO @芋艿如果模型变化需要 reindex 所有的文档
}
@Override
public AiKnowledgeDO getKnowledge(Long id) {
return knowledgeMapper.selectById(id);
} }
@Override @Override