Dict.CN 在线词典, 英语学习, 在线翻译 ------------- MyGitee My腾云code My51cto

Happy_EveryDay

可以平凡 不可以平庸 无爱则无忧,无欲则无求,无怒而无敌,无怨才是佛。所有烦恼,都是放不下的执著 开源技群 328035181 MyGitee

博客园 首页 管理

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>

<!-- Spring AI JDBC 聊天记忆核心依赖 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-jdbc</artifactId>
</dependency>
<!-- Spring Boot JDBC Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</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-8718a83408d7443b9544cXXXXXXXX
chat:
memory:
repository:
jdbc:
initialize-schema: always
schema: classpath:/sql/schema-mysql.sql
datasource:
url: jdbc:mysql://192.168.91.165:3306/springai?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor: debug
#org.springframework.ai.chat.client.advisor: debug

 


 
3、config
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.memory.ChatMemoryRepository;
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class config {

@Bean
ChatMemory chatMemoryJdbc(JdbcChatMemoryRepository chatMemoryRepository) {
return MessageWindowChatMemory
.builder()
// 自定义最大消息数,覆盖默认的20条 (2 轮用户 + 助手对话)
//保留最近 4 条消息。若超出则删除最早的消息
.maxMessages(4)
// 绑定JDBC持久化仓库(支持多用户会话隔离)
.chatMemoryRepository(chatMemoryRepository)
.build();
}


}




4、resources/sql/schema-mysql.sql
CREATE TABLE IF NOT EXISTS SPRING_AI_CHAT_MEMORY (
`conversation_id` VARCHAR(36) NOT NULL COMMENT '会话ID,区分不同对话',
`content` TEXT NOT NULL COMMENT '序列化后的对话消息内容(JSON格式)',
`type` VARCHAR(10) NOT NULL COMMENT '消息类型(USER/ASSISTANT/SYSTEM等)',
`timestamp` TIMESTAMP NOT NULL COMMENT '消息时间戳,用于排序',
INDEX `SPRING_AI_CHAT_MEMORY_CONVERSATION_ID_TIMESTAMP_IDX` (`conversation_id`, `timestamp`)
);

 

 




5、controller
 

import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.sb.dashscope18081.tool.ReReadingAdvisor;
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
DashScopeChatModel chatModel;



@Autowired
//保留最近 10 条消息。若超出则删除最早的消息
//JDBC chatMemoryJdbc
private ChatMemory chatMemoryJdbc;




/**
* 基于 ChatMemoryJdbc 历史对话长度
*/
@GetMapping("/simple/chatmemoryjdbc")
public String chatmemoryjdbc () {
String conversationId = "jdbc001";
ChatClient chatClient = ChatClient.builder(chatModel)
.defaultAdvisors(
PromptChatMemoryAdvisor.builder(chatMemoryJdbc).build()
)
.build();

String content= chatClient.prompt()
.user("你好,我叫谢安!")
.advisors(new ReReadingAdvisor())
.advisors(advisorSpec->advisorSpec.param(ChatMemory.CONVERSATION_ID,conversationId))
.call()
.content();
System.out.println("content:" + content);

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



}






6、 http://localhost:18081/openai/simple/chatmemoryjdbc

 imageimage

 
posted on 2026-05-11 16:27  cn2025  阅读(6)  评论(0)    收藏  举报