Commit 024109dac9237d06766daf8f1aa0e5347b5bf361
1 parent
ed2296e4
【优化】AI 知识库: 从url下载文档资源
Showing
4 changed files
with
19 additions
and
11 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/knowledge/AiKnowledgeBaseMapper.java renamed to yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/knowledge/AiKnowledgeMapper.java
| @@ -10,5 +10,5 @@ import org.apache.ibatis.annotations.Mapper; | @@ -10,5 +10,5 @@ import org.apache.ibatis.annotations.Mapper; | ||
| 10 | * @author xiaoxin | 10 | * @author xiaoxin |
| 11 | */ | 11 | */ |
| 12 | @Mapper | 12 | @Mapper |
| 13 | -public interface AiKnowledgeBaseMapper extends BaseMapperX<AiKnowledgeDO> { | 13 | +public interface AiKnowledgeMapper extends BaseMapperX<AiKnowledgeDO> { |
| 14 | } | 14 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/knowledge/AiKnowledgeDocumentServiceImpl.java
| 1 | package cn.iocoder.yudao.module.ai.service.knowledge; | 1 | package cn.iocoder.yudao.module.ai.service.knowledge; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.http.HttpUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 5 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 5 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | 6 | import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; |
| 6 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; | 7 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| @@ -17,7 +18,7 @@ import org.springframework.ai.reader.tika.TikaDocumentReader; | @@ -17,7 +18,7 @@ import org.springframework.ai.reader.tika.TikaDocumentReader; | ||
| 17 | import org.springframework.ai.tokenizer.TokenCountEstimator; | 18 | import org.springframework.ai.tokenizer.TokenCountEstimator; |
| 18 | import org.springframework.ai.transformer.splitter.TokenTextSplitter; | 19 | import org.springframework.ai.transformer.splitter.TokenTextSplitter; |
| 19 | import org.springframework.ai.vectorstore.RedisVectorStore; | 20 | import org.springframework.ai.vectorstore.RedisVectorStore; |
| 20 | -import org.springframework.beans.factory.annotation.Value; | 21 | +import org.springframework.core.io.ByteArrayResource; |
| 21 | import org.springframework.stereotype.Service; | 22 | import org.springframework.stereotype.Service; |
| 22 | import org.springframework.transaction.annotation.Transactional; | 23 | import org.springframework.transaction.annotation.Transactional; |
| 23 | 24 | ||
| @@ -46,17 +47,14 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic | @@ -46,17 +47,14 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic | ||
| 46 | private RedisVectorStore vectorStore; | 47 | private RedisVectorStore vectorStore; |
| 47 | 48 | ||
| 48 | 49 | ||
| 49 | - // TODO xiaoxin 临时测试用,后续删 | ||
| 50 | - @Value("classpath:/webapp/test/Fel.pdf") | ||
| 51 | - private org.springframework.core.io.Resource data; | ||
| 52 | - | ||
| 53 | // TODO 芋艿:需要 review 下,代码格式; | 50 | // TODO 芋艿:需要 review 下,代码格式; |
| 54 | @Override | 51 | @Override |
| 55 | @Transactional(rollbackFor = Exception.class) | 52 | @Transactional(rollbackFor = Exception.class) |
| 56 | public Long createKnowledgeDocument(AiKnowledgeDocumentCreateReqVO createReqVO) { | 53 | public Long createKnowledgeDocument(AiKnowledgeDocumentCreateReqVO createReqVO) { |
| 57 | - // TODO xiaoxin 后续从 url 加载 | ||
| 58 | - TikaDocumentReader loader = new TikaDocumentReader(data); | ||
| 59 | - // 1.1 加载文档 | 54 | + // 1.1 下载文档 |
| 55 | + String url = createReqVO.getUrl(); | ||
| 56 | + TikaDocumentReader loader = new TikaDocumentReader(downloadFile(url)); | ||
| 57 | + // 1.2 加载文档 | ||
| 60 | List<Document> documents = loader.get(); | 58 | List<Document> documents = loader.get(); |
| 61 | Document document = CollUtil.getFirst(documents); | 59 | Document document = CollUtil.getFirst(documents); |
| 62 | // TODO @xin:是不是不存在,就抛出异常呀;厚泽 return 呀; | 60 | // TODO @xin:是不是不存在,就抛出异常呀;厚泽 return 呀; |
| @@ -86,4 +84,14 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic | @@ -86,4 +84,14 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic | ||
| 86 | return documentId; | 84 | return documentId; |
| 87 | } | 85 | } |
| 88 | 86 | ||
| 87 | + private org.springframework.core.io.Resource downloadFile(String url) { | ||
| 88 | + try { | ||
| 89 | + byte[] bytes = HttpUtil.downloadBytes(url); | ||
| 90 | + return new ByteArrayResource(bytes); | ||
| 91 | + } catch (Exception e) { | ||
| 92 | + log.error("[downloadFile][url({}) 下载失败]", url, e); | ||
| 93 | + throw new RuntimeException(e); | ||
| 94 | + } | ||
| 95 | + } | ||
| 96 | + | ||
| 89 | } | 97 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/knowledge/AiKnowledgeServiceImpl.java
| @@ -7,7 +7,7 @@ import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeCreat | @@ -7,7 +7,7 @@ import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeCreat | ||
| 7 | import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeUpdateMyReqVO; | 7 | import cn.iocoder.yudao.module.ai.controller.admin.knowledge.vo.AiKnowledgeUpdateMyReqVO; |
| 8 | import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO; | 8 | import cn.iocoder.yudao.module.ai.dal.dataobject.knowledge.AiKnowledgeDO; |
| 9 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; | 9 | import cn.iocoder.yudao.module.ai.dal.dataobject.model.AiChatModelDO; |
| 10 | -import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeBaseMapper; | 10 | +import cn.iocoder.yudao.module.ai.dal.mysql.knowledge.AiKnowledgeMapper; |
| 11 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; | 11 | import cn.iocoder.yudao.module.ai.service.model.AiChatModelService; |
| 12 | import jakarta.annotation.Resource; | 12 | import jakarta.annotation.Resource; |
| 13 | import lombok.extern.slf4j.Slf4j; | 13 | import lombok.extern.slf4j.Slf4j; |
| @@ -29,7 +29,7 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService { | @@ -29,7 +29,7 @@ public class AiKnowledgeServiceImpl implements AiKnowledgeService { | ||
| 29 | private AiChatModelService chatModalService; | 29 | private AiChatModelService chatModalService; |
| 30 | 30 | ||
| 31 | @Resource | 31 | @Resource |
| 32 | - private AiKnowledgeBaseMapper knowledgeBaseMapper; | 32 | + private AiKnowledgeMapper knowledgeBaseMapper; |
| 33 | 33 | ||
| 34 | @Override | 34 | @Override |
| 35 | public Long createKnowledgeMy(AiKnowledgeCreateMyReqVO createReqVO, Long userId) { | 35 | public Long createKnowledgeMy(AiKnowledgeCreateMyReqVO createReqVO, Long userId) { |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/resources/webapp/test/Fel.pdf deleted
No preview for this file type