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

推荐订阅源

钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
云风的 BLOG
云风的 BLOG
IT之家
IT之家
C
Check Point Blog
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
人人都是产品经理
人人都是产品经理
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
M
MIT News - Artificial intelligence
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
F
Fortinet All Blogs
V
Visual Studio Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
MyScale Blog
MyScale Blog
爱范儿
爱范儿
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)

博客园 - 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
ChatmemoryConversationId 多用户对话记忆 20260508
cn2025 · 2026-05-08 · 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.SimpleLoggerAdvisor: debug
#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.Message;
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;
import java.util.List;

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

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

/**
* 多用户对话记忆 conversationId
*/
@GetMapping("/simple/chatmemoryconversationId")
public String chatmemoryconversationId () {
String conversationId = "lk001"; // 对话唯一标识
String conversationId2 = "lk002"; // 对话唯一标识
ChatClient chatClient = chatClientBuilder
.defaultAdvisors(
PromptChatMemoryAdvisor.builder(chatMemory).build()
)
.build();

// 2. 第一次对话:自我介绍
// 第一次对话
String content1= chatClient.prompt()
.user("我叫卢卡")
.advisors(advisorSpec->advisorSpec.param(ChatMemory.CONVERSATION_ID,conversationId))
.call()
.content();
System.out.println(content1);
System.out.println("------------------------");

String content2= chatClient.prompt()
.user("我叫什么?")
.advisors(advisorSpec->advisorSpec.param(ChatMemory.CONVERSATION_ID,conversationId))
.call()
.content();
System.out.println(content2);
System.out.println("------------------------");

String content3= chatClient.prompt()
.user("我叫什么?")
.advisors(advisorSpec->advisorSpec.param(ChatMemory.CONVERSATION_ID,conversationId2))
.call()
.content();
System.out.println(content3);

return "【多用户对话记忆 conversationId】content1【conversationId:lk001】:"+content1+"------------"
+"content2【conversationId:lk001】:"+content2+"------------"
+"content3【conversationId:lk002】:"+content3;
}

}

4、http://localhost:18081/openai/simple/chatmemoryconversationId

image