使用Spring-AI的chatMemoryAdvisor实现多轮会话

代码

controller/ZhipuChatMemoryController.java

java 复制代码
package org.example.controller;

import org.example.advisor.CallAdvisor1;
import org.example.advisor.CallAdvisor2;
import org.example.advisor.SimpleMessageChatMemoryAdvisor;
import org.example.entity.Book;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.zhipuai.ZhiPuAiChatOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chatMemory")
public class ZhipuChatMemoryController {

    // 声明 ChatClient 实例,用于与 AI 模型进行交互
    private final ChatClient chatClient;

    /**
     * 构造函数,初始化 ChatClient 并配置聊天内存顾问
     * @param builder ChatClient.Builder 实例,用于构建 ChatClient
     */
    public ZhipuChatMemoryController(ChatClient.Builder builder) {

        // 创建 MessageWindowChatMemory,用于管理对话历史消息窗口
        MessageWindowChatMemory windowChatMemory = MessageWindowChatMemory.builder()
                .build();
        // 创建 MessageChatMemoryAdvisor,用于管理对话内存
        MessageChatMemoryAdvisor chatMemoryAdvisor = MessageChatMemoryAdvisor.builder(windowChatMemory)
                .build();

        // 构建 ChatClient,设置默认Advisors
        this.chatClient = builder
                .defaultAdvisors(chatMemoryAdvisor)
                .build();
    }

    /**
     * 处理聊天请求
     * @param question 用户问题
     * @param sessionId 会话ID,用于区分不同用户的对话上下文
     * @return AI 模型的响应内容
     */
    @GetMapping("/messageChatMemoryAdvisor")
    public String messageChatMemoryAdvisor(@RequestParam(name = "question") String question,
                                           @RequestParam(name = "sessionId") String sessionId) {

        String content = chatClient.prompt()
                .user(question)
                // 把会话ID存入上下文,确保对话记忆与特定会话关联
                .advisors(advisorSpec -> advisorSpec.param(ChatMemory.CONVERSATION_ID, sessionId))
                .call()
                .content();
        return content;
    }
}

效果

AI可以记住我的名字

相关推荐
u0103055273 分钟前
使用BufferedReader读取控制台输入
人工智能·1024程序员节
ck-joker3 分钟前
M1 Pro跑LLM实测:Ollama+LangChain4j零成本本地大模型开发,比云端API快在哪?
java·语言模型
测试开发技术10 分钟前
AI 测试提效 | 告别手工写脚本,分享我的 Playwright + Skill 批量生成 UI 自动化脚本方案
自动化测试·人工智能·ui·自动化·agent·skill·ai测试
乐思智能科技有限公司23 分钟前
PLECS软件学习使用(二)直流电机基本系统模型
人工智能·算法·机器学习·面试·职场和发展
极连AI24 分钟前
极连AI平台解读、Codex5.6仅需0.01倍率,无需Token焦虑,极速响应
人工智能·gpt·chatgpt·aigc·ai编程·ai写作·gpu算力
MartinYeung530 分钟前
[论文学习]PACT:溯源感知能力合约——面向智能体安全的参数级溯源
人工智能·学习·安全
带娃的IT创业者33 分钟前
Inkling:当开源模型开始思考“如何思考”
人工智能·开源·大语言模型·多模态·moe·开源模型·inkling
丙氨酸長鏈36 分钟前
[Bukkit插件开发]手持发射器箭矢机枪 教学文档 面向Python/C#开发者入门Java与Bukkit API
java·python·c#
Nontee38 分钟前
设计模式:模板方法与策略,从“每个字都认识“到能说清它们在干嘛
java·数据库·设计模式
我是唐青枫40 分钟前
Java ReentrantLock 实战详解:比 synchronized 更灵活的可重入锁
java·开发语言