惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
罗磊的独立博客
M
MIT News - Artificial intelligence
G
Google Developers Blog
V
V2EX
D
Docker
博客园_首页
The Cloudflare Blog
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
WordPress大学
WordPress大学
T
Tailwind CSS Blog
博客园 - 司徒正美
J
Java Code Geeks
L
LangChain Blog
博客园 - 三生石上(FineUI控件)
B
Blog RSS Feed
博客园 - 【当耐特】
小众软件
小众软件
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - Franky

博客园 - cn2025

K8s -sentinel-dashboard【nacos版本】集 20260815 kubeSphere【ks-controller-manager-webhook-cert】证书 20260814 K8s -nacos集 20260813 K8s - 安装部署redis集群-哨兵sentinels集(支持从K8s外部访问)20260812 k8s-deployment发布测试(pv-pvc-deployment-service) 20260810 K8s - 安装部署Kafka、Zookeeper集群教程(支持从K8s外部访问)20260808 k8s-关机及启动脚本工具 【kubeSphere发布ruoyi-pro前端】工具脚本20260731 【k8s】 etcd服务端及客户端版本及快照【定时】备份 20260730 harbor【https启用】及k8节点部证书20260728 kube-flannel.yml k8s集群-安装helm 【kubeShpere】 官网 20260710 kubesphere-KDP 工具脚本【kubeSphere发布ruoyi-pro前端】20260720 HbuilderX 内置终端转为 gitBash 20260720 kubeSphere发布ruoyi-pro后端-用到脚本 kubeSphere发布ruoyi-web前端 20260711 kubeSphere发布ruoyi-pro后端 k8s集群-kubeShpere 20260710 安装Helm 20260710 k8s-portainer docker 镜像查询 k8集群一键重置 docker run OceanBase spring ai alibaba doc AI2.0 【多模态】 20260615 世界只有一个墨脱 AI2.0 【Mcp-client】 20260611 AI2.0 【Mcp-server】 20260611 GoLand 配Go SDK
Chatmemory 对话记忆20260506
cn2025 · 2026-05-06 · via 博客园 - cn2025

1、pom

<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.2.0</spring-boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 阿里云通义千问(DashScope)starter -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>
<!--对话记忆 chat-memory-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-memory</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 统一管理Spring AI依赖版本 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>${spring-ai-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Spring AI 里程碑/快照仓库(必须配置,否则依赖无法下载) -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

2、yml


#server.port=18081
#spring.ai.dashscope.api-key="sk-72d9fd311cde487eb948bcd999ea4a65"
#spring.ai.dashscope.chat.options.model= sk-8718a83408d7443b9544cdfbf259280f
#sk-72d9fd311cde487eb948bcd999ea4a65
server:
port: 18081
spring:
ai:
dashscope:
api-key: sk-8718a83408d7443bxxxxxxx
logging: #####日志级别
level:
   org.springframework.ai.chat.client.advisor: debug



3、controller
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/openai")
@ResponseBody
@Controller
public class ChatMemoryController {
@Autowired
private ChatClient.Builder chatClientBuilder;

@Autowired
private ChatMemory chatMemory; // 自动注入的MessageWindowChatMemory

@Autowired
DashScopeChatModel chatModel;

@GetMapping("/simple/chatmemory")
public String chatmemory () {

ChatClient chatClient = chatClientBuilder
.defaultAdvisors(
PromptChatMemoryAdvisor.builder(chatMemory).build()
)
.build();
// 2. 第一次对话:自我介绍
String content1 = chatClient.prompt()
.user("我叫宋武帝刘裕")
.call()
.content();
System.out.println("content1:" + content1);

System.out.println("------------------------");

// 3. 第二次对话:提问自己的名字(模型会记住上下文)
String content2 = chatClient.prompt()
.user("我叫什么?")
.call()
.content();
System.out.println("content2:" + content2);
return "content1:"+content1+"------------" +"content2:"+content2;
}

/**
*手动拼接上下文
*/
@GetMapping("/simple/chatmemory2")
public String chatmemory2 () {
ChatClient chatClient = chatClientBuilder
.defaultAdvisors(
PromptChatMemoryAdvisor.builder(chatMemory).build()
)
.build();
// 2. 第一次对话:自我介绍
// 第一次对话
String chatHis="我叫秦始皇";
String content1 = chatClient.prompt()
.user(chatHis)
.call()
.content();
System.out.println("content1:" + content1);

System.out.println("------------------------");
// 拼接历史对话
chatHis+=content1;
chatHis+="我叫什么?";
// 3. 第二次对话:提问自己的名字(模型会记住上下文)
String content2 = chatClient.prompt()
.user(chatHis)
.call()
.content();
System.out.println("content2:" + content2);
return "【手动拼接上下文】content1:"+content1+"------------" +"content2:"+content2;
}

/**
* 基于 ChatMemory 的标准
*/
@GetMapping("/simple/chatmemory3")
public String chatmemory3 () {
// 1. 创建对话记忆(滑动窗口实现)
ChatMemory chatMemory = MessageWindowChatMemory.builder().build();
String conversationId = "ly001"; // 对话唯一标识

// First interaction
UserMessage userMessage1 = new UserMessage("我叫前秦符坚");
chatMemory.add(conversationId, userMessage1);
ChatResponse response1 = chatModel.call(new Prompt(chatMemory.get(conversationId)));
chatMemory.add(conversationId, response1.getResult().getOutput());
String content1 = String.valueOf(response1.getResult().getOutput());
System.out.println("content1:" + content1);
//Second interaction
UserMessage userMessage2 = new UserMessage("我叫什么?");
chatMemory.add(conversationId, userMessage2);
ChatResponse response2 = chatModel.call(new Prompt(chatMemory.get(conversationId)));
chatMemory.add(conversationId, response2.getResult().getOutput());
String content2 = String.valueOf(response2.getResult().getOutput());
System.out.println("content2:" + content2);
return "【基于 ChatMemory】content1:"+content1+"------------" +"content2:"+content2;
}
}

4、浏览器
http://localhost:18081/openai/simple/chatmemory

image

 

image

 

 http://localhost:18081/openai/simple/chatmemory2

image

image

http://localhost:18081/openai/simple/chatmemory3

image