MessageChatMemoryAdvisor + MongoDB 完整配置指南

MessageChatMemoryAdvisor 默认使用内存存储,生产环境需配置 MongoDB 实现持久化。以下是完整配置方案


步骤1:添加 Maven 依赖

pom.xml 中添加 Spring AI 官方提供的 MongoDB 存储 starter:

复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-model-chat-memory-repository-mongodb</artifactId>
</dependency>

同时确保已添加 Spring Data MongoDB 依赖:

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

步骤2:配置 MongoDB 连接

application.yml 中配置 MongoDB 连接信息:

复制代码
spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/chat_memory_db
      # 或分步配置
      # host: localhost
      # port: 27017
      # database: chat_memory_db
      # username: your_username
      # password: your_password

步骤3:配置 ChatMemory Bean

提供 两种配置方式(推荐自动配置):

方式A:自动配置(推荐)

Spring AI 会自动配置 MongoChatMemoryRepository,只需在配置文件中开启:

复制代码
spring:
  ai:
    chat:
      memory:
        repository:
          mongo:
            create-indices: true  # 启动时自动创建索引
            ttl: 86400           # 可选:消息TTL(秒),86400=24小时,0=永久存储

然后在 Java 配置类中注入即可:

复制代码
@Configuration
public class ChatMemoryConfig {

    @Autowired
    private MongoChatMemoryRepository mongoChatMemoryRepository; // 自动注入

    @Bean
    public ChatMemory chatMemory() {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(mongoChatMemoryRepository)
                .maxMessages(30)  // 保留最近30条消息
                .build();
    }
}

方式B:手动配置

如需自定义,可手动创建 MongoChatMemoryRepository

复制代码
@Configuration
public class ChatMemoryConfig {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Bean
    public ChatMemory chatMemory() {
        // 手动创建 Repository
        MongoChatMemoryRepository repository = MongoChatMemoryRepository.builder()
                .mongoTemplate(mongoTemplate)
                .build();

        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(repository)
                .maxMessages(30)
                .build();
    }
}

步骤4:配置 MessageChatMemoryAdvisor 并集成到 ChatClient

复制代码
@Configuration
public class AIConfig {

    @Bean
    public ChatClient chatClient(ChatModel chatModel, ChatMemory chatMemory) {
        return ChatClient.builder(chatModel)
                .defaultSystem("你是专业的智能客服助手")
                .defaultAdvisors(
                    MessageChatMemoryAdvisor.builder(chatMemory)
                            .conversationId("default")
                            .build()
                )
                .build();
    }
}

步骤5:Controller 中使用(关键:传递 conversationId)

复制代码
@RestController
@RequestMapping("/chat")
public class ChatController {
    
    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder, ChatMemory chatMemory) {
        this.chatClient = builder
                .defaultAdvisors(
                    MessageChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }

    @PostMapping
    public String chat(@RequestBody ChatRequest request) {
        return chatClient.prompt(request.getMessage())
                .advisors(advisor -> 
                    advisor.param(ChatMemory.CONVERSATION_ID, request.getChatId())
                )
                .call()
                .content();
    }
}

@Data
class ChatRequest {
    private String chatId;    // 用户会话ID(必须传递)
    private String message;   // 用户消息
}

步骤6:高级配置与注意事项

6.1 数据存储结构

MongoDB 中会自动创建集合 ai_chat_memory ,文档格式如下:

复制代码
{
  "_id": "conversationId::timestamp",
  "conversationId": "user123",
  "messages": [
    {
      "type": "user",
      "text": "你好",
      "metadata": {}
    },
    {
      "type": "assistant", 
      "text": "你好!有什么可以帮助你的?",
      "metadata": {}
    }
  ],
  "createdAt": ISODate("2025-12-01T10:30:00.000Z")
}

6.2 索引配置问题

重要 :根据 GitHub Issue #4839,当前版本(1.1.0)的 TTL 索引配置可能存在问题。建议:

  1. 手动创建索引:在 MongoDB Shell 中执行:

    use chat_memory_db;
    db.ai_chat_memory.createIndex(
    { "createdAt": 1 },
    { expireAfterSeconds: 86400 }
    );

  2. 验证索引是否生效

    db.ai_chat_memory.getIndexes();

6.3 最大消息数建议

根据实际场景设置 maxMessages

场景 推荐值 说明
简单问答 10-20 节省 token 成本
复杂任务 30-50 保留更多上下文
长文档分析 50-100 需监控 token 消耗

6.4 性能优化

  • 批量操作MongoChatMemoryRepository 支持批量插入,减少网络开销

  • 索引优化 :确保 conversationIdcreatedAt 字段有索引

  • 连接池:调整 Spring Data MongoDB 的连接池大小


完整配置示例

复制代码
// 完整配置类示例
@Configuration
public class AIApplicationConfig {

    // 1. 配置 MongoDB 存储的 ChatMemory
    @Bean
    public ChatMemory chatMemory(MongoChatMemoryRepository repository) {
        return MessageWindowChatMemory.builder()
                .chatMemoryRepository(repository)
                .maxMessages(50)
                .build();
    }

    // 2. 配置 ChatClient
    @Bean
    public ChatClient chatClient(ChatModel chatModel, ChatMemory chatMemory) {
        return ChatClient.builder(chatModel)
                .defaultAdvisors(
                    MessageChatMemoryAdvisor.builder(chatMemory)
                            .conversationId("default")
                            .order(-2147482648)  // 确保优先执行
                            .build()
                )
                .build();
    }

    // 3. 可选:配置 TTL 索引(如果自动配置失效)
    @Bean
    public CommandLineRunner createMongoIndexes(MongoTemplate mongoTemplate) {
        return args -> {
            mongoTemplate.indexOps("ai_chat_memory")
                .ensureIndex(
                    new Index().on("createdAt", Direction.ASC)
                              .expire(86400)  // 24小时过期
                );
        };
    }
}

验证是否生效

发送几条消息后,在 MongoDB 中查询:

复制代码
// 查看所有会话
db.ai_chat_memory.find().pretty()

// 查看特定会话
db.ai_chat_memory.find({conversationId: "user123"}).pretty()

// 查看集合统计
db.ai_chat_memory.stats()
相关推荐
Y***89081 小时前
【数据库】MySQL的安装与卸载
数据库·mysql·adb
Dwzun1 小时前
基于SpringBoot的共享单车调度系统【附源码+文档+部署视频+讲解)
java·数据库·vue.js·spring boot·后端·毕业设计·maven
little_kid_pea1 小时前
Oracle:从收费明细中扣减退费数据
java·服务器·数据库
无盐海1 小时前
Redis底层数据结构
数据结构·数据库·redis
Coder-coco1 小时前
选题管理|基于springboot + vue毕业设计选题管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
长河1 小时前
Record-API 性能优化实战:从“锁”到“快”的深度治理
数据库·性能优化
吃喝不愁霸王餐APP开发者1 小时前
霸王餐试吃资格发放:Redis HyperLogLog亿级去重与Lua脚本原子性
数据库·redis·lua
q***48411 小时前
【MySQL】视图
数据库·mysql·oracle
007php0071 小时前
nginx加速缓存导致Event-Stream消息延迟问题的解决方案
运维·网络·数据库·nginx·缓存·面试·职场和发展