refactor(chat):重构AI助手功能并集成文档读取能力
- 移除原有的手机号识别与消息发送逻辑 - 删除RabbitMQ和邮件相关配置及代码 - 引入PDF、HTML、JSON等多种文档读取器 - 集成向量存储与检索功能支持问答 - 更新Spring AI依赖并调整内存存储方式 - 添加新的工具类用于保存文档到向量库- 修改提示词模板去除强制附加句规则 - 调整Cassandra和PgVector相关配置项- 新增多种文件格式读取组件实现类
This commit is contained in:
@@ -2,21 +2,19 @@ package com.hanserwei.chat.controller;
|
||||
|
||||
import com.hanserwei.chat.model.dto.ChatMessageDTO;
|
||||
import com.hanserwei.chat.model.vo.AIResponse;
|
||||
import com.hanserwei.chat.tools.SendMQMessageTools;
|
||||
import com.hanserwei.chat.reader.MyPdfReader;
|
||||
import com.hanserwei.chat.utils.ConversationContext;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.chat.client.ChatClient;
|
||||
import org.springframework.ai.chat.memory.ChatMemory;
|
||||
import org.springframework.ai.document.Document;
|
||||
import org.springframework.ai.vectorstore.VectorStore;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.List;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@@ -26,15 +24,14 @@ public class AiChatController {
|
||||
@Resource
|
||||
private ChatClient dashScopeChatClient;
|
||||
@Resource
|
||||
private SendMQMessageTools sendMQMessageTools;
|
||||
|
||||
private static final Pattern PHONE_PATTERN = Pattern.compile("(?<!\\d)(1[3-9]\\d{9})(?!\\d)");
|
||||
private MyPdfReader myPdfReader;
|
||||
@Resource
|
||||
private VectorStore vectorStore;
|
||||
|
||||
@PostMapping(path = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
|
||||
public Flux<AIResponse> chatWithAi(@RequestBody ChatMessageDTO chatMessageDTO) {
|
||||
log.info("会话ID:{}", chatMessageDTO.getConversionId());
|
||||
ConversationContext.setConversationId(chatMessageDTO.getConversionId());
|
||||
triggerSendMessageIfPhonePresent(chatMessageDTO);
|
||||
|
||||
return dashScopeChatClient.prompt()
|
||||
.user(chatMessageDTO.getMessage())
|
||||
@@ -48,21 +45,10 @@ public class AiChatController {
|
||||
.doFinally(signalType -> ConversationContext.clear());
|
||||
}
|
||||
|
||||
private void triggerSendMessageIfPhonePresent(ChatMessageDTO chatMessageDTO) {
|
||||
String message = chatMessageDTO.getMessage();
|
||||
if (message == null || message.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Matcher matcher = PHONE_PATTERN.matcher(message);
|
||||
if (!matcher.find()) {
|
||||
return;
|
||||
}
|
||||
|
||||
String phoneNumber = matcher.group(1);
|
||||
log.info("检测到手机号:{},会话ID:{}", phoneNumber, chatMessageDTO.getConversionId());
|
||||
sendMQMessageTools.sendMQMessage(phoneNumber)
|
||||
.doOnError(error -> log.error("触发发送消息工具失败,手机号:{},错误:{}", phoneNumber, error.getMessage(), error))
|
||||
.subscribe(result -> log.info("已触发发送消息工具,手机号:{},结果:{}", phoneNumber, result));
|
||||
@GetMapping("/readpdf")
|
||||
public String readPdf() {
|
||||
List<Document> docsFromPdf = myPdfReader.getDocsFromPdf();
|
||||
vectorStore.add(docsFromPdf);
|
||||
return "ok!";
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user