Skip to content

Commit b0edf7c

Browse files
committed
Merge branch 'main' into studio-dsl
2 parents 491b37f + 5a88ea3 commit b0edf7c

File tree

68 files changed

+3797
-353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+3797
-353
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
# Spring AI Alibaba Elasticsearch Memory 模块
2+
3+
[English](./README.md)
4+
5+
## 简介
6+
7+
Spring AI Alibaba Elasticsearch Memory 模块是 Spring AI Alibaba 项目的核心组件之一,
8+
专门提供基于 Elasticsearch 的存储解决方案。该模块利用 Elasticsearch 的全文检索和分布式特性,为 AI 应用提供快速、可靠的对话历史和上下文数据存储服务,使 AI 系统能够"记住"之前的交互,从而提供更连贯、更个性化的用户体验。
9+
10+
## 主要特性
11+
12+
- **Elasticsearch 存储**:利用 Elasticsearch 的高性能和分布式特性,实现对话历史和上下文数据的快速存取
13+
- **与 Spring 生态无缝集成**:完美兼容 Spring 框架和 Spring Boot 应用
14+
- **灵活的记忆管理**:支持设置对话记忆大小限制,自动清理过期对话
15+
16+
## 快速开始
17+
18+
### Maven 依赖
19+
20+
将以下依赖添加到你的项目中:
21+
22+
```xml
23+
<dependency>
24+
<groupId>com.alibaba.cloud.ai</groupId>
25+
<artifactId>spring-ai-alibaba-starter-memory-elasticsearch</artifactId>
26+
<version>${latest.version}</version>
27+
</dependency>
28+
```
29+
30+
### 基本配置
31+
32+
`application.properties``application.yml`中添加 Elasticsearch 配置:
33+
34+
```yaml
35+
elasticsearch:
36+
host: localhost
37+
port: 9200
38+
scheme: http
39+
# 如果有认证需求
40+
# username: elastic
41+
# password: password
42+
```
43+
44+
### 示例代码
45+
46+
```java
47+
import org.springframework.ai.chat.client.ChatClient;
48+
import org.springframework.beans.factory.annotation.Autowired;
49+
import org.springframework.web.bind.annotation.*;
50+
import reactor.core.publisher.Flux;
51+
import com.alibaba.cloud.ai.memory.elasticsearch.ElasticsearchChatMemoryRepository;
52+
import org.springframework.ai.chat.memory.ChatMemory;
53+
import org.springframework.ai.chat.memory.MessageWindowChatMemory;
54+
55+
import javax.servlet.http.HttpServletResponse;
56+
57+
@RestController
58+
public class ChatController {
59+
60+
@Autowired
61+
private ElasticsearchChatMemoryRepository elasticsearchChatMemoryRepository;
62+
63+
@Autowired
64+
private ChatClient chatClient;
65+
66+
/**
67+
* 流式聊天接口(基于 Elasticsearch 存储对话历史)
68+
*
69+
* @param prompt 用户输入的问题或提示
70+
* @param chatId 对话 ID,用于标识当前会话
71+
* @param response HttpServletResponse 对象,用于设置响应编码
72+
* @return 返回流式响应内容(Flux<String>),逐步输出 AI 回答
73+
*/
74+
@GetMapping("/chat")
75+
public Flux<String> elasticsearchChat(
76+
@RequestParam("prompt") String prompt,
77+
@RequestParam("chatId") String chatId,
78+
HttpServletResponse response) {
79+
80+
// 设置响应字符编码为 UTF-8,确保中文等字符正确显示
81+
response.setCharacterEncoding("UTF-8");
82+
83+
// 构建带消息窗口的记忆组件,最多保留最近 10 条消息
84+
ChatMemory chatMemory = MessageWindowChatMemory.builder()
85+
.chatMemoryRepository(elasticsearchChatMemoryRepository)
86+
.maxMessages(10)
87+
.build();
88+
89+
// 发起 AI 模型调用,并启用记忆功能
90+
return chatClient.prompt(prompt)
91+
.advisors(new MessageChatMemoryAdvisor(chatMemory))
92+
.advisors(a -> a
93+
.param("chatMemoryConversationId", chatId)
94+
.param("chatMemoryRetrieveSize", 100)
95+
)
96+
.stream() // 使用流式响应
97+
.content(); // 获取内容流
98+
}
99+
}
100+
```
101+
102+
## 高级特性
103+
104+
### 自定义 Elasticsearch 配置
105+
106+
可以通过`ElasticsearchConfig`类来自定义更复杂的 Elasticsearch 配置:
107+
108+
```java
109+
ElasticsearchConfig config = new ElasticsearchConfig();
110+
config.setHost("localhost");
111+
config.setPort(9200);
112+
config.setScheme("https"); // 使用HTTPS
113+
config.setUsername("elastic");
114+
config.setPassword("password");
115+
116+
// 使用集群多节点配置
117+
List<String> nodes = new ArrayList<>();
118+
nodes.add("node1:9200");
119+
nodes.add("node2:9200");
120+
config.setNodes(nodes);
121+
122+
ElasticsearchChatMemoryRepository repository = new ElasticsearchChatMemoryRepository(config);
123+
```
124+
125+
### 管理记忆大小
126+
127+
```java
128+
// 为特定对话清理过期消息,保留最新的消息
129+
// 参数:对话ID,最大消息数量,要删除的消息数量
130+
repository.clearOverLimit("conversation-123", 10, 5);
131+
```
Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
# Spring AI Alibaba Elasticsearch Memory 模块
1+
# Spring AI Alibaba Elasticsearch Memory Module
22

3-
## 简介
3+
[中文版本](./README-zh.md)
44

5-
Spring AI Alibaba Elasticsearch Memory 模块是 Spring AI Alibaba 项目的核心组件之一,
6-
专门提供基于 Elasticsearch 的存储解决方案。该模块利用 Elasticsearch 的全文检索和分布式特性,为 AI 应用提供快速、可靠的对话历史和上下文数据存储服务,使 AI 系统能够"记住"之前的交互,从而提供更连贯、更个性化的用户体验。
5+
## Introduction
76

8-
## 主要特性
7+
The Spring AI Alibaba Elasticsearch Memory Module is a core component of the Spring AI Alibaba project,
8+
specifically designed to provide an Elasticsearch-based storage solution. Leveraging Elasticsearch's full-text search and distributed capabilities, this module delivers fast and reliable storage services for conversational history and contextual data in AI applications. It enables AI systems to remember past interactions, thereby facilitating more coherent and personalized user experiences.
99

10-
- **Elasticsearch 存储**:利用 Elasticsearch 的高性能和分布式特性,实现对话历史和上下文数据的快速存取
11-
- **与 Spring 生态无缝集成**:完美兼容 Spring 框架和 Spring Boot 应用
12-
- **灵活的记忆管理**:支持设置对话记忆大小限制,自动清理过期对话
10+
## Core Features
1311

14-
## 快速开始
12+
- **Elasticsearch Storage**: Leverages Elasticsearch's high performance and distributed architecture to enable rapid storage and retrieval of conversational history and contextual data.
13+
- **Seamless Integration with Spring Ecosystem**: Provides full compatibility with the Spring Framework and Spring Boot applications for effortless adoption.
14+
- **Flexible Memory Management**: Supports configurable conversation memory size limits with automatic cleanup of expired dialogues.
1515

16-
### Maven 依赖
16+
## Get Started
1717

18-
将以下依赖添加到你的项目中:
18+
### Maven Dependency
19+
20+
Add the following dependency to your project:
1921

2022
```xml
2123
<dependency>
@@ -25,21 +27,21 @@ Spring AI Alibaba Elasticsearch Memory 模块是 Spring AI Alibaba 项目的核
2527
</dependency>
2628
```
2729

28-
### 基本配置
30+
### Basic Configuration
2931

30-
`application.properties``application.yml`中添加 Elasticsearch 配置:
32+
Add the following Elasticsearch configuration to your `application.properties` or `application.yml`:
3133

3234
```yaml
3335
elasticsearch:
3436
host: localhost
3537
port: 9200
3638
scheme: http
37-
# 如果有认证需求
39+
# If authentication is required
3840
# username: elastic
3941
# password: password
4042
```
4143

42-
### 示例代码
44+
### Sample Code
4345

4446
```java
4547
import org.springframework.ai.chat.client.ChatClient;
@@ -62,56 +64,56 @@ public class ChatController {
6264
private ChatClient chatClient;
6365

6466
/**
65-
* 流式聊天接口(基于 Elasticsearch 存储对话历史)
67+
* Stream-based chat interface (with conversation history stored in Elasticsearch).
6668
*
67-
* @param prompt 用户输入的问题或提示
68-
* @param chatId 对话 ID,用于标识当前会话
69-
* @param response HttpServletResponse 对象,用于设置响应编码
70-
* @return 返回流式响应内容(Flux<String>),逐步输出 AI 回答
69+
* @param prompt User's input question or prompt.
70+
* @param chatId Conversation ID used to identify the current session.
71+
* @param response HttpServletResponse object for setting response encoding.
72+
* @return Streamed response content (Flux<String>), gradually output AI responses
7173
*/
7274
@GetMapping("/chat")
7375
public Flux<String> elasticsearchChat(
7476
@RequestParam("prompt") String prompt,
7577
@RequestParam("chatId") String chatId,
7678
HttpServletResponse response) {
7779

78-
// 设置响应字符编码为 UTF-8,确保中文等字符正确显示
80+
// Sets the response character encoding to UTF-8 to ensure proper display of Chinese and other Unicode characters
7981
response.setCharacterEncoding("UTF-8");
8082

81-
// 构建带消息窗口的记忆组件,最多保留最近 10 条消息
83+
// Constructs a message window-based chat memory component retaining up to 10 recent messages
8284
ChatMemory chatMemory = MessageWindowChatMemory.builder()
8385
.chatMemoryRepository(elasticsearchChatMemoryRepository)
8486
.maxMessages(10)
8587
.build();
8688

87-
// 发起 AI 模型调用,并启用记忆功能
89+
// Initiates AI model invocation with memory capabilities enabled
8890
return chatClient.prompt(prompt)
8991
.advisors(new MessageChatMemoryAdvisor(chatMemory))
9092
.advisors(a -> a
9193
.param("chatMemoryConversationId", chatId)
9294
.param("chatMemoryRetrieveSize", 100)
9395
)
94-
.stream() // 使用流式响应
95-
.content(); // 获取内容流
96+
.stream() // Enables streaming response
97+
.content(); // Retrieves the content stream
9698
}
9799
}
98100
```
99101

100-
## 高级特性
102+
## Advanced features
101103

102-
### 自定义 Elasticsearch 配置
104+
### Custom Elasticsearch Configuration
103105

104-
可以通过`ElasticsearchConfig`类来自定义更复杂的 Elasticsearch 配置:
106+
Custom, more complex Elasticsearch configurations can be defined through the `ElasticsearchConfig` class.
105107

106108
```java
107109
ElasticsearchConfig config = new ElasticsearchConfig();
108110
config.setHost("localhost");
109111
config.setPort(9200);
110-
config.setScheme("https"); // 使用HTTPS
112+
config.setScheme("https"); // Use HTTPS
111113
config.setUsername("elastic");
112114
config.setPassword("password");
113115

114-
// 使用集群多节点配置
116+
// Uses multi-node cluster configuration
115117
List<String> nodes = new ArrayList<>();
116118
nodes.add("node1:9200");
117119
nodes.add("node2:9200");
@@ -120,10 +122,10 @@ config.setNodes(nodes);
120122
ElasticsearchChatMemoryRepository repository = new ElasticsearchChatMemoryRepository(config);
121123
```
122124

123-
### 管理记忆大小
125+
### Managing Memory Size
124126

125127
```java
126-
// 为特定对话清理过期消息,保留最新的消息
127-
// 参数:对话ID,最大消息数量,要删除的消息数量
128+
// Cleans up expired messages for a specific conversation while retaining the most recent ones
129+
// Parameters: conversation ID, maximum number of messages to keep, number of messages to delete
128130
repository.clearOverLimit("conversation-123", 10, 5);
129131
```

0 commit comments

Comments
 (0)