feat(ai): 实现AI聊天功能并集成数据库工具

- 新增 AiChatController 支持流式聊天响应
- 创建 AIResponse 和 ChatMessageDTO 用于数据传输
- 开发 AiDBTools 提供用户相关的增删改查及封禁功能- 配置 ChatClient 支持默认工具调用
- 调整 User 实体类时间字段为 OffsetDateTime 并格式化- 添加 jackson-datatype-jsr310 依赖以支持 JSR310 时间序列化
- 修改 PostgreSQL 连接字符串时区配置
- 启用 Jackson 日期写入为字符串而非时间戳
This commit is contained in:
2025-10-25 17:27:53 +08:00
parent 177dfff3c7
commit 5c0feab211
8 changed files with 181 additions and 3 deletions

View File

@@ -0,0 +1,36 @@
package com.hanserwei.chat.controller;
import com.hanserwei.chat.model.dto.ChatMessageDTO;
import com.hanserwei.chat.model.vo.AIResponse;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
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 reactor.core.publisher.Flux;
@RestController
@RequestMapping("/ai")
public class AiChatController {
@Resource
private ChatClient dashScopeChatClient;
@PostMapping(path = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<AIResponse> chatWithAi(@RequestBody ChatMessageDTO chatMessageDTO) {
return dashScopeChatClient.prompt()
.user(chatMessageDTO.getMessage())
.advisors(p -> p.param(ChatMemory.CONVERSATION_ID, chatMessageDTO.getConversionId()))
.stream()
.chatResponse()
.mapNotNull(chatResponse -> AIResponse.builder()
.v(chatResponse.getResult().getOutput().getText())
.build());
}
}