【新增】AI 知识库管理

This commit is contained in:
xiaoxin 2024-08-15 11:28:02 +08:00
parent b453856864
commit b4af042c64
21 changed files with 497 additions and 23 deletions

View File

@ -50,4 +50,8 @@ public interface ErrorCodeConstants {
ErrorCode WRITE_NOT_EXISTS = new ErrorCode(1_022_007_000, "作文不存在!"); ErrorCode WRITE_NOT_EXISTS = new ErrorCode(1_022_007_000, "作文不存在!");
ErrorCode WRITE_STREAM_ERROR = new ErrorCode(1_022_07_001, "写作生成异常!"); ErrorCode WRITE_STREAM_ERROR = new ErrorCode(1_022_07_001, "写作生成异常!");
// ========== API 知识库 1-022-008-000 ==========
ErrorCode KNOWLEDGE_NOT_EXISTS = new ErrorCode(1_022_008_000, "知识库不存在!");
} }

View File

@ -0,0 +1,39 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeCreateMyReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeUpdateMyReqVO;
import cn.iocoder.yudao.module.ai.service.knowledge.AiKnowledgeBaseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@Tag(name = "管理后台 - AI 知识库")
@RestController
@RequestMapping("/ai/knowledge")
public class AiKnowledgeController {
@Resource
private AiKnowledgeBaseService knowledgeBaseService;
@PostMapping("/create-my")
@Operation(summary = "创建【我的】知识库")
public CommonResult<Long> createKnowledgeMy(@RequestBody @Valid AiKnowledgeCreateMyReqVO createReqVO) {
return success(knowledgeBaseService.createKnowledgeMy(createReqVO, getLoginUserId()));
}
@PutMapping("/update-my")
@Operation(summary = "更新【我的】知识库")
public CommonResult<Boolean> updateKnowledgeMy(@RequestBody @Valid AiKnowledgeUpdateMyReqVO updateReqVO) {
knowledgeBaseService.updateKnowledgeMy(updateReqVO, getLoginUserId());
return success(true);
}
}

View File

@ -0,0 +1,31 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.util.List;
/**
* @author xiaoxin
*/
@Schema(description = "管理后台 - AI 知识库创建【我的】 Request VO")
@Data
public class AiKnowledgeCreateMyReqVO {
@Schema(description = "知识库名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "")
@NotBlank(message = "知识库名称不能为空")
private String name;
@Schema(description = "知识库描述", requiredMode = Schema.RequiredMode.REQUIRED, example = "")
private String description;
@Schema(description = "可见权限,只能选择哪些人可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1]")
private List<Long> visibilityPermissions;
@Schema(description = "嵌入模型 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "嵌入模型不能为空")
private Long modelId;
}

View File

@ -0,0 +1,36 @@
package cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.util.List;
/**
* @author xiaoxin
*/
@Schema(description = "管理后台 - AI 知识库创建【我的】 Request VO")
@Data
public class AiKnowledgeUpdateMyReqVO {
@Schema(description = "对话编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1204")
@NotNull(message = "知识库编号不能为空")
private Long id;
@Schema(description = "知识库名称", requiredMode = Schema.RequiredMode.REQUIRED, example = "")
@NotBlank(message = "知识库名称不能为空")
private String name;
@Schema(description = "知识库描述", requiredMode = Schema.RequiredMode.REQUIRED, example = "")
private String description;
@Schema(description = "可见权限,只能选择哪些人可见", requiredMode = Schema.RequiredMode.REQUIRED, example = "[1]")
private List<Long> visibilityPermissions;
@Schema(description = "嵌入模型 id", requiredMode = Schema.RequiredMode.REQUIRED, example = "1")
@NotNull(message = "嵌入模型不能为空")
private Long modelId;
}

View File

@ -0,0 +1,56 @@
package cn.iocoder.yudao.module.ai.dal.dataobject.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.List;
/**
* AI 知识库 DO
*
* @author xiaoxin
*/
@TableName(value = "ai_knowledge_base")
@Data
public class AiKnowledgeBaseDO extends BaseDO {
/**
* 编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 用户编号
* <p>
* 关联 AdminUserDO userId 字段
*/
private Long userId;
/**
* 知识库名称
*/
private String name;
/**
* 知识库描述
*/
private String description;
/**
* 可见权限,只能选择哪些人可见
*/
private List<String> visibilityPermissions;
/**
* 嵌入模型编号高质量模式时维护
*/
private Long modelId;
/**
* 模型标识
*/
private String model;
/**
* 是否启用
*/
private Boolean status;
}

View File

@ -0,0 +1,55 @@
package cn.iocoder.yudao.module.ai.dal.dataobject.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* AI 知识库-文档 DO
*
* @author xiaoxin
*/
@TableName(value = "ai_knowledge_document")
@Data
public class AiKnowledgeDocumentDO extends BaseDO {
/**
* 编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 知识库编号
*/
private Long knowledgeId;
/**
* 文件名称
*/
private String name;
/**
* 内容
*/
private String content;
/**
* 文件 URL
*/
private String url;
/**
* token数量
*/
private Integer tokens;
/**
* 字符数
*/
private Integer wordCount;
/**
* 切片状态
*/
private Integer sliceStatus;
/**
* 是否启用
*/
private Boolean status;
}

View File

@ -0,0 +1,48 @@
package cn.iocoder.yudao.module.ai.dal.dataobject.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* AI 知识库-文档分段 DO
*
* @author xiaoxin
*/
@TableName(value = "ai_knowledge_segment")
@Data
public class AiKnowledgeSegmentDO extends BaseDO {
/**
* 编号
*/
@TableId(type = IdType.AUTO)
private Long id;
/**
* 向量库的id
*/
private String vectorId;
/**
* 文档编号
*/
private Long documentId;
/**
* 切片内容
*/
private String content;
/**
* 字符数
*/
private Integer wordCount;
/**
* token数量
*/
private Integer tokens;
/**
* 是否启用
*/
private Boolean status;
}

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.module.ai.dal.mysql.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeBaseDO;
/**
* AI 知识库基础信息 Mapper
*
* @author xiaoxin
*/
public interface AiKnowledgeBaseMapper extends BaseMapperX<AiKnowledgeBaseDO> {
}

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.module.ai.dal.mysql.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDocumentDO;
/**
* AI 知识库-文档 Mapper
*
* @author xiaoxin
*/
public interface AiKnowledgeDocumentMapper extends BaseMapperX<AiKnowledgeDocumentDO> {
}

View File

@ -0,0 +1,12 @@
package cn.iocoder.yudao.module.ai.dal.mysql.knowledge;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeSegmentDO;
/**
* AI 知识库-分片 Mapper
*
* @author xiaoxin
*/
public interface AiKnowledgeSegmentMapper extends BaseMapperX<AiKnowledgeSegmentDO> {
}

View File

@ -0,0 +1,26 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import org.springframework.ai.document.Document;
import java.util.List;
/**
* AI 嵌入 Service 接口
*
* @author xiaoxin
*/
public interface AiEmbeddingService {
/**
* 向量化文档
*/
void embeddingDoc();
/**
* 相似查询
*
* @param content 查询内容
*/
List<Document> similaritySearch(String content);
}

View File

@ -1,24 +1,23 @@
package cn.iocoder.yudao.module.ai.service.knowledge; package cn.iocoder.yudao.module.ai.service.knowledge;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.document.Document; import org.springframework.ai.document.Document;
import org.springframework.ai.reader.tika.TikaDocumentReader; import org.springframework.ai.reader.tika.TikaDocumentReader;
import org.springframework.ai.transformer.splitter.TokenTextSplitter; import org.springframework.ai.transformer.splitter.TokenTextSplitter;
import org.springframework.ai.vectorstore.RedisVectorStore; import org.springframework.ai.vectorstore.RedisVectorStore;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
/** /**
* AI 知识库 Service 实现类 * AI 嵌入 Service 实现类
* *
* @author xiaoxin * @author xiaoxin
*/ */
@Service @Service
@Slf4j public class AiEmbeddingServiceImpl implements AiEmbeddingService {
public class DocServiceImpl implements DocService {
@Resource @Resource
private RedisVectorStore vectorStore; private RedisVectorStore vectorStore;
@ -40,4 +39,9 @@ public class DocServiceImpl implements DocService {
vectorStore.add(segments); vectorStore.add(segments);
} }
@Override
public List<Document> similaritySearch(String content) {
SearchRequest request = SearchRequest.query(content);
return vectorStore.similaritySearch(request);
}
} }

View File

@ -0,0 +1,30 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeCreateMyReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeUpdateMyReqVO;
/**
* AI 知识库-基础信息 Service 接口
*
* @author xiaoxin
*/
public interface AiKnowledgeBaseService {
/**
* 创建我的知识库
*
* @param createReqVO 创建信息
* @param userId 用户编号
* @return 编号
*/
Long createKnowledgeMy(AiKnowledgeCreateMyReqVO createReqVO, Long userId);
/**
* 创建我的知识库
*
* @param updateReqVO 更新信息
* @param userId 用户编号
*/
void updateKnowledgeMy(AiKnowledgeUpdateMyReqVO updateReqVO, Long userId);
}

View File

@ -0,0 +1,72 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeCreateMyReqVO;
import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeUpdateMyReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeBaseDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO;
import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeBaseMapper;
import cn.iocoder.yudao.module.ai.service.model.AiChatModelService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.KNOWLEDGE_NOT_EXISTS;
/**
* AI 知识库-基础信息 Service 实现类
*
* @author xiaoxin
*/
@Service
@Slf4j
public class AiKnowledgeBaseServiceImpl implements AiKnowledgeBaseService {
@Resource
private AiKnowledgeBaseMapper knowledgeBaseMapper;
@Resource
private AiChatModelService chatModalService;
@Override
public Long createKnowledgeMy(AiKnowledgeCreateMyReqVO createReqVO, Long userId) {
AiChatModelDO model = validateChatModel(createReqVO.getModelId());
AiKnowledgeBaseDO knowledgeBaseDO = BeanUtils.toBean(createReqVO, AiKnowledgeBaseDO.class);
knowledgeBaseDO.setModel(model.getModel()).setUserId(userId);
knowledgeBaseMapper.insert(knowledgeBaseDO);
return knowledgeBaseDO.getId();
}
@Override
public void updateKnowledgeMy(AiKnowledgeUpdateMyReqVO updateReqVO, Long userId) {
AiKnowledgeBaseDO knowledgeBaseDO = validateKnowledgeExists(updateReqVO.getId());
if (ObjUtil.notEqual(knowledgeBaseDO.getUserId(), userId)) {
throw exception(KNOWLEDGE_NOT_EXISTS);
}
AiChatModelDO model = validateChatModel(updateReqVO.getModelId());
AiKnowledgeBaseDO updateDO = BeanUtils.toBean(updateReqVO, AiKnowledgeBaseDO.class);
updateDO.setModel(model.getModel());
knowledgeBaseMapper.updateById(updateDO);
}
private AiChatModelDO validateChatModel(Long id) {
AiChatModelDO model = chatModalService.validateChatModel(id);
Assert.notNull(model, "未找到对应嵌入模型");
return model;
}
public AiKnowledgeBaseDO validateKnowledgeExists(Long id) {
AiKnowledgeBaseDO knowledgeBase = knowledgeBaseMapper.selectById(id);
if (knowledgeBase == null) {
throw exception(KNOWLEDGE_NOT_EXISTS);
}
return knowledgeBase;
}
}

View File

@ -0,0 +1,10 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
/**
* AI 知识库-文档 Service 接口
*
* @author xiaoxin
*/
public interface AiKnowledgeDocumentService {
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* AI 知识库-文档 Service 实现类
*
* @author xiaoxin
*/
@Service
@Slf4j
public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentService {
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
/**
* AI 知识库-分片 Service 接口
*
* @author xiaoxin
*/
public interface AiKnowledgeSegmentService {
}

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
/**
* AI 知识库-基础信息 Service 实现类
*
* @author xiaoxin
*/
@Service
@Slf4j
public class AiKnowledgeSegmentServiceImpl implements AiKnowledgeSegmentService {
}

View File

@ -1,15 +0,0 @@
package cn.iocoder.yudao.module.ai.service.knowledge;
/**
* AI 知识库 Service 接口
*
* @author xiaoxin
*/
public interface DocService {
/**
* 向量化文档
*/
void embeddingDoc();
}

View File

@ -57,11 +57,9 @@
<version>${spring-ai.version}</version> <version>${spring-ai.version}</version>
</dependency> </dependency>
<!-- TODO @xin引入我们项目的 starter -->
<dependency> <dependency>
<groupId>org.springframework.data</groupId> <groupId>cn.iocoder.boot</groupId>
<artifactId>spring-data-redis</artifactId> <artifactId>yudao-spring-boot-starter-redis</artifactId>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -31,6 +31,7 @@ import redis.clients.jedis.JedisPooled;
* TODO @xin 先拿 spring-ai 最新代码覆盖1.0.0-M1 redis 自动配置会冲突 * TODO @xin 先拿 spring-ai 最新代码覆盖1.0.0-M1 redis 自动配置会冲突
* *
* TODO 这个官方有说啥时候 fix * TODO 这个官方有说啥时候 fix
* TODO 看着是列在1.0.0-M2版本
* *
* @author Christian Tzolov * @author Christian Tzolov
* @author Eddú Meléndez * @author Eddú Meléndez