Commit 04b8aa04222d6fbe516c657221b3532c4146c480
1 parent
3e8596ee
reactor:【AI 大模型】优化消息发送后,读取文档的逻辑,避免 N 次读取
Showing
4 changed files
with
13 additions
and
27 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/knowledge/AiKnowledgeSegmentMapper.java
| ... | ... | @@ -30,9 +30,9 @@ public interface AiKnowledgeSegmentMapper extends BaseMapperX<AiKnowledgeSegment |
| 30 | 30 | .orderByDesc(AiKnowledgeSegmentDO::getId)); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - default List<AiKnowledgeSegmentDO> selectListByVectorIds(List<String> vectorIdList) { | |
| 33 | + default List<AiKnowledgeSegmentDO> selectListByVectorIds(List<String> vectorIds) { | |
| 34 | 34 | return selectList(new LambdaQueryWrapperX<AiKnowledgeSegmentDO>() |
| 35 | - .in(AiKnowledgeSegmentDO::getVectorId, vectorIdList) | |
| 35 | + .in(AiKnowledgeSegmentDO::getVectorId, vectorIds) | |
| 36 | 36 | .orderByDesc(AiKnowledgeSegmentDO::getId)); |
| 37 | 37 | } |
| 38 | 38 | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/chat/AiChatMessageServiceImpl.java
| ... | ... | @@ -121,11 +121,11 @@ public class AiChatMessageServiceImpl implements AiChatMessageService { |
| 121 | 121 | String newContent = chatResponse.getResult().getOutput().getText(); |
| 122 | 122 | chatMessageMapper.updateById(new AiChatMessageDO().setId(assistantMessage.getId()).setContent(newContent)); |
| 123 | 123 | // 3.4 响应结果 |
| 124 | + Map<Long, AiKnowledgeDocumentDO> documentMap = knowledgeDocumentService.getKnowledgeDocumentMap( | |
| 125 | + convertSet(knowledgeSegments, AiKnowledgeSegmentSearchRespBO::getDocumentId)); | |
| 124 | 126 | List<AiChatMessageRespVO.KnowledgeSegment> segments = BeanUtils.toBean(knowledgeSegments, |
| 125 | - AiChatMessageRespVO.KnowledgeSegment.class, | |
| 126 | - segment -> { | |
| 127 | - AiKnowledgeDocumentDO document = knowledgeDocumentService | |
| 128 | - .getKnowledgeDocument(segment.getDocumentId()); | |
| 127 | + AiChatMessageRespVO.KnowledgeSegment.class, segment -> { | |
| 128 | + AiKnowledgeDocumentDO document = documentMap.get(segment.getDocumentId()); | |
| 129 | 129 | segment.setDocumentName(document != null ? document.getName() : null); |
| 130 | 130 | }); |
| 131 | 131 | return new AiChatMessageSendRespVO() |
| ... | ... | @@ -172,12 +172,13 @@ public class AiChatMessageServiceImpl implements AiChatMessageService { |
| 172 | 172 | // 处理知识库的返回,只有首次才有 |
| 173 | 173 | List<AiChatMessageRespVO.KnowledgeSegment> segments = null; |
| 174 | 174 | if (StrUtil.isEmpty(contentBuffer)) { |
| 175 | - segments = BeanUtils.toBean(knowledgeSegments, AiChatMessageRespVO.KnowledgeSegment.class, | |
| 176 | - segment -> TenantUtils.executeIgnore(() -> { | |
| 177 | - AiKnowledgeDocumentDO document = knowledgeDocumentService | |
| 178 | - .getKnowledgeDocument(segment.getDocumentId()); | |
| 179 | - segment.setDocumentName(document != null ? document.getName() : null); | |
| 180 | - })); | |
| 175 | + Map<Long, AiKnowledgeDocumentDO> documentMap = TenantUtils.executeIgnore(() -> | |
| 176 | + knowledgeDocumentService.getKnowledgeDocumentMap( | |
| 177 | + convertSet(knowledgeSegments, AiKnowledgeSegmentSearchRespBO::getDocumentId))); | |
| 178 | + segments = BeanUtils.toBean(knowledgeSegments, AiChatMessageRespVO.KnowledgeSegment.class, segment -> { | |
| 179 | + AiKnowledgeDocumentDO document = documentMap.get(segment.getDocumentId()); | |
| 180 | + segment.setDocumentName(document != null ? document.getName() : null); | |
| 181 | + }); | |
| 181 | 182 | } |
| 182 | 183 | // 响应结果 |
| 183 | 184 | String newContent = chunk.getResult() != null ? chunk.getResult().getOutput().getText() : null; | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/knowledge/AiKnowledgeDocumentService.java
| ... | ... | @@ -68,13 +68,6 @@ public interface AiKnowledgeDocumentService { |
| 68 | 68 | void updateKnowledgeDocumentStatus(AiKnowledgeDocumentUpdateStatusReqVO reqVO); |
| 69 | 69 | |
| 70 | 70 | /** |
| 71 | - * 更新文档检索次数(增加 +1) | |
| 72 | - * | |
| 73 | - * @param ids 文档编号列表 | |
| 74 | - */ | |
| 75 | - void updateKnowledgeDocumentRetrievalCountIncr(Collection<Long> ids); | |
| 76 | - | |
| 77 | - /** | |
| 78 | 71 | * 删除文档 |
| 79 | 72 | * |
| 80 | 73 | * @param id 文档编号 | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/knowledge/AiKnowledgeDocumentServiceImpl.java
| ... | ... | @@ -162,14 +162,6 @@ public class AiKnowledgeDocumentServiceImpl implements AiKnowledgeDocumentServic |
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | @Override |
| 165 | - public void updateKnowledgeDocumentRetrievalCountIncr(Collection<Long> ids) { | |
| 166 | - if (CollUtil.isEmpty(ids)) { | |
| 167 | - return; | |
| 168 | - } | |
| 169 | - knowledgeDocumentMapper.updateRetrievalCountIncr(ids); | |
| 170 | - } | |
| 171 | - | |
| 172 | - @Override | |
| 173 | 165 | public AiKnowledgeDocumentDO validateKnowledgeDocumentExists(Long id) { |
| 174 | 166 | AiKnowledgeDocumentDO knowledgeDocument = knowledgeDocumentMapper.selectById(id); |
| 175 | 167 | if (knowledgeDocument == null) { | ... | ... |