feat(llm): 添加知识库文档预览功能
- 新增 KnowledgeBasePreviewReqVO 和 KnowledgeBasePreviewRespVO 类用于知识库文档预览请求和响应 - 在 KnowledgeBaseController 中添加 previewKnowledgeBaseDocument 方法处理预览请求 - 在 KnowledgeBaseService 接口中定义 previewKnowledgeBaseDocument 方法 - 在 KnowledgeBaseServiceImpl 中实现预览逻辑,模拟分页数据并返回 - 新增 PageUtil 工具类用于列表分页处理 - 在 PageResult 类中添加 pageNo 和 pageSize 字段
This commit is contained in:
parent
b5b67db5da
commit
505fc8ee06
@ -17,6 +17,12 @@ public final class PageResult<T> implements Serializable {
|
||||
@Schema(description = "总量", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private Long total;
|
||||
|
||||
@Schema(description = "页码")
|
||||
private Integer pageNo;
|
||||
|
||||
@Schema(description = "每页大小")
|
||||
private Integer pageSize;
|
||||
|
||||
public PageResult() {
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ import cn.iocoder.yudao.module.llm.service.knowledgebase.KnowledgeBaseService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
@ -104,6 +105,13 @@ public class KnowledgeBaseController {
|
||||
return success(KnowledgeBaseRespVO);
|
||||
}
|
||||
|
||||
@GetMapping("/preview")
|
||||
@Operation(summary = "知识库文档预览")
|
||||
public CommonResult<PageResult<KnowledgeBasePreviewRespVO>> previewKnowledgeBaseDocument(@RequestBody KnowledgeBasePreviewReqVO reqVO) {
|
||||
PageResult<KnowledgeBasePreviewRespVO> pageResult = knowledgeBaseService.previewKnowledgeBaseDocument(reqVO);
|
||||
return success(BeanUtils.toBean(pageResult,KnowledgeBasePreviewRespVO.class));
|
||||
}
|
||||
|
||||
@GetMapping("/page")
|
||||
@Operation(summary = "获得知识库分页")
|
||||
// @PreAuthorize("@ss.hasPermission('llm:knowledge-base:query')")
|
||||
|
@ -0,0 +1,25 @@
|
||||
package cn.iocoder.yudao.module.llm.controller.admin.knowledgebase.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Description
|
||||
* @Author 知识库文档预览请求参数
|
||||
* @Date 2025/3/15 14:33
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "知识库文档预览请求参数")
|
||||
public class KnowledgeBasePreviewReqVO {
|
||||
@Schema(description = "知识库编号", example = "1024")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "文件编号", example = "2048")
|
||||
private Long fileId;
|
||||
|
||||
@Schema(description = "页码(从 1 开始)", example = "1")
|
||||
private Integer pageNo;
|
||||
|
||||
@Schema(description = "每页大小", example = "10")
|
||||
private Integer pageSize;
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package cn.iocoder.yudao.module.llm.controller.admin.knowledgebase.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Description
|
||||
* @Author Liu Yang
|
||||
* @Date 2025/3/15 14:31
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "知识库文档预览响应信息")
|
||||
public class KnowledgeBasePreviewRespVO {
|
||||
@Schema(description = "文件内容", example = "这是文件的内容")
|
||||
private String pageContent;
|
||||
|
||||
@Schema(description = "文档字数", example = "文档字数")
|
||||
private Integer wordCount;
|
||||
}
|
@ -87,4 +87,11 @@ public interface KnowledgeBaseService {
|
||||
* @param updateReqVO
|
||||
*/
|
||||
void updateHitParam (@Valid KnowledgeBaseSaveReqVO updateReqVO);
|
||||
|
||||
/**
|
||||
* 知识库文档预览
|
||||
* @param reqVO
|
||||
* @return
|
||||
*/
|
||||
PageResult<KnowledgeBasePreviewRespVO> previewKnowledgeBaseDocument (KnowledgeBasePreviewReqVO reqVO);
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.ErrorCode;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.object.PageUtils;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
|
||||
import cn.iocoder.yudao.module.llm.controller.admin.knowledgebase.vo.*;
|
||||
import cn.iocoder.yudao.module.llm.controller.admin.knowledgedocuments.vo.KnowledgeDocumentsRespVO;
|
||||
@ -14,8 +15,10 @@ import cn.iocoder.yudao.module.llm.dal.mysql.knowledgebase.KnowledgeBaseMapper;
|
||||
import cn.iocoder.yudao.module.llm.dal.mysql.knowledgedocuments.KnowledgeDocumentsMapper;
|
||||
import cn.iocoder.yudao.module.llm.service.application.ApplicationService;
|
||||
import cn.iocoder.yudao.module.llm.service.async.AsyncKnowledgeBase;
|
||||
import cn.iocoder.yudao.module.llm.utils.PageUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.ehcache.shadow.org.terracotta.offheapstore.paging.Page;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@ -166,9 +169,9 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||
documents.forEach(doc -> {
|
||||
KnowledgeDocumentsDO docDO = BeanUtils.toBean(doc, KnowledgeDocumentsDO.class);
|
||||
docDO.setKnowledgeBaseId(knowledgeBaseId);
|
||||
// if (doc.getId() == null) {
|
||||
newDocuments.add(docDO);
|
||||
// }
|
||||
// if (doc.getId() == null) {
|
||||
newDocuments.add(docDO);
|
||||
// }
|
||||
docDO.setId(null);
|
||||
knowledgeDocumentsMapper.insertOrUpdate(docDO);
|
||||
});
|
||||
@ -335,8 +338,10 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 知识库Embedding
|
||||
*
|
||||
* @param request
|
||||
*/
|
||||
@Override
|
||||
@ -354,6 +359,7 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||
|
||||
/**
|
||||
* 更新知识库命中测试参数
|
||||
*
|
||||
* @param updateReqVO
|
||||
*/
|
||||
@Override
|
||||
@ -365,10 +371,53 @@ public class KnowledgeBaseServiceImpl implements KnowledgeBaseService {
|
||||
updateObj.setId(updateReqVO.getId())
|
||||
.setTopK(updateReqVO.getTopK())
|
||||
.setScore(updateReqVO.getScore())
|
||||
;
|
||||
;
|
||||
knowledgeBaseMapper.updateById(updateObj);
|
||||
}
|
||||
|
||||
/**
|
||||
* 知识库文档预览
|
||||
*
|
||||
* @param reqVO
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public PageResult<KnowledgeBasePreviewRespVO> previewKnowledgeBaseDocument (KnowledgeBasePreviewReqVO reqVO) {
|
||||
// 模拟分页数据
|
||||
List<KnowledgeBasePreviewRespVO> pageData = new ArrayList<>();
|
||||
|
||||
// 第一页数据
|
||||
KnowledgeBasePreviewRespVO page1 = new KnowledgeBasePreviewRespVO();
|
||||
page1.setPageContent("这是第一页的内容");
|
||||
page1.setWordCount(100);
|
||||
pageData.add(page1);
|
||||
|
||||
// 第二页数据
|
||||
KnowledgeBasePreviewRespVO page2 = new KnowledgeBasePreviewRespVO();
|
||||
page2.setPageContent("这是第二页的内容");
|
||||
page2.setWordCount(150);
|
||||
pageData.add(page2);
|
||||
|
||||
// 第三页数据
|
||||
KnowledgeBasePreviewRespVO page3 = new KnowledgeBasePreviewRespVO();
|
||||
page3.setPageContent("这是第三页的内容");
|
||||
page3.setWordCount(200);
|
||||
pageData.add(page3);
|
||||
|
||||
// 使用 PageUtil 进行分页处理
|
||||
List<KnowledgeBasePreviewRespVO> pagedList = PageUtil.page(reqVO.getPageNo(), reqVO.getPageSize(), pageData);
|
||||
|
||||
// 设置分页结果
|
||||
PageResult<KnowledgeBasePreviewRespVO> pageResult = new PageResult<>();
|
||||
pageResult.setList(pagedList);
|
||||
pageResult.setTotal((long) pageData.size());
|
||||
pageResult.setPageNo(reqVO.getPageNo());
|
||||
pageResult.setPageSize(reqVO.getPageSize());
|
||||
|
||||
log.info("知识库文档预览成功,返回分页结果:{}", pageResult);
|
||||
return pageResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验知识库是否存在
|
||||
*
|
||||
|
@ -0,0 +1,32 @@
|
||||
package cn.iocoder.yudao.module.llm.utils;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class PageUtil {
|
||||
|
||||
/**
|
||||
* 对列表进行分页处理
|
||||
*
|
||||
* @param pageNo 页码(从 1 开始)
|
||||
* @param pageSize 每页大小
|
||||
* @param list 原始列表
|
||||
* @param <T> 泛型类型
|
||||
* @return 分页后的子列表
|
||||
*/
|
||||
public static <T> List<T> page(int pageNo, int pageSize, List<T> list) {
|
||||
if (list == null || list.isEmpty()) {
|
||||
return list;
|
||||
}
|
||||
|
||||
int total = list.size();
|
||||
int fromIndex = (pageNo - 1) * pageSize;
|
||||
int toIndex = Math.min(fromIndex + pageSize, total);
|
||||
|
||||
if (fromIndex >= total || fromIndex < 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return list.subList(fromIndex, toIndex);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user