Skip to content

Commit 3a64188

Browse files
authored
Merge pull request #74 from springaialibaba/0308-yuluo/add-examples
feat: add module rag example
2 parents a5c26fb + d9530d5 commit 3a64188

File tree

29 files changed

+957
-245
lines changed

29 files changed

+957
-245
lines changed

docker-compose/mysql/conf/my.cnf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[mysqld]
2+
user=mysql
3+
default-storage-engine=INNODB
4+
character-set-server=utf8
5+
character-set-client-handshake=FALSE
6+
collation-server=utf8_unicode_ci
7+
init_connect='SET NAMES utf8'
8+
[client]
9+
default-character-set=utf8
10+
[mysql]
11+
default-character-set=utf8
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
3+
mysql:
4+
image: mysql:latest
5+
container_name: spring-ai-alibaba-mysql
6+
volumes:
7+
- ./conf/my.cnf:/etc/my.cnf
8+
environment:
9+
- "MYSQL_ROOT_PASSWORD=yourpassword"
10+
- "MYSQL_DATABASE=yourdbname"
11+
- "TZ=Asia/Shanghai"
12+
ports:
13+
- 3306:3306
File renamed without changes.

docker-compose/pgvector/README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@
22

33
此文件夹中用于存放一些 pgvector 服务常用的 Docker Compose 启动文件。
44

5-
- postgres16版本同时安装age和pgvector [DockerFile](./postgres16-age/DockerFile)
5+
- postgres16版本同时安装age和pgvector [DockerFile](./postgres16-age/DockerFile)
6+
7+
执行以下命令启动 pg:
8+
9+
```shell
10+
docker compose up -d --build
11+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
services:
2+
3+
postgres:
4+
build:
5+
context: .
6+
dockerfile: ./postgres16-age/DockerFile
7+
container_name: spring-ai-alibaba-postgres
8+
healthcheck:
9+
test: [ "CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root" ]
10+
timeout: 45s
11+
interval: 10s
12+
retries: 10
13+
environment:
14+
- POSTGRES_DB=vector_store
15+
- POSTGRES_USER=root
16+
- POSTGRES_PASSWORD=password
17+
ports:
18+
- "5432:5432"

docker-compose/pgvector/postgres16-age/DockerFile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,5 @@ RUN echo "shared_preload_libraries = 'age,vector'" >> /usr/share/postgresql/post
5252

5353
# Start PostgreSQL
5454

55-
56-
5755
#CMD ["/cmd_command.sh"]
58-
CMD ["postgres"]
56+
CMD ["postgres"]
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Spring AI Alibaba Module RAG 使用示例
2+
3+
## 1. 简介
4+
5+
Spring AI Module RAG: https://docs.spring.io/spring-ai/reference/api/retrieval-augmented-generation.html#modules
6+
7+
Spring AI implements a Modular RAG architecture inspired by the concept of modularity detailed in the paper "Modular RAG: Transforming RAG Systems into LEGO-like Reconfigurable Frameworks".
8+
9+
## 2. 组成部分
10+
11+
### 2.1 Pre-Retrieval
12+
13+
> 增强和转换用户输入,使其更有效地执行检索任务,解决格式不正确的查询、query 语义不清晰、或不受支持的语言等。
14+
15+
1. QueryAugmenter 查询增强:使用附加的上下文数据信息增强用户 query,提供大模型回答问题时的必要上下文信息;
16+
2. QueryTransformer:查询改写:因为用户的输入通常是片面的,关键信息较少,不便于大模型理解和回答问题。因此需要使用 prompt 调优手段或者大模型改写用户 query;
17+
3. QueryExpander:查询扩展:将用户 query 扩展为多个语义不同的变体以获得不同视角,有助于检索额外的上下文信息并增加找到相关结果的机会。
18+
19+
### 2.2 Retrieval
20+
21+
> 负责查询向量存储等数据系统并检索和用户 query 相关性最高的 Document。
22+
23+
1. DocumentRetriever:检索器,根据 QueryExpander 使用不同的数据源进行检索,例如 搜索引擎、向量存储、数据库或知识图等;
24+
2. DocumentJoiner:将从多个 query 和从多个数据源检索到的 Document 合并为一个 Document 集合;
25+
26+
### 2.3 Post-Retrieval
27+
28+
> 负责处理检索到的 Document 以获得最佳的输出结果,解决模型中的*中间丢失*和上下文长度限制等。
29+
30+
1. DocumentRanker:根据 Document 和用户 query 的相关性对 Document 进行排序和排名;
31+
2. DocumentSelector:用于从检索到的 Document 列表中删除不相关或冗余文档;
32+
3. DocumentCompressor:用于压缩每个 Document,减少检索到的信息中的噪音和冗余。
33+
34+
### 2.4 生成
35+
36+
生成用户 Query 对应的大模型输出。
37+
38+
## 3. 启动示例
39+
40+
在启动示例之前,您本地应该有一个可以正常使用的 ES.
41+
42+
### Basic
43+
44+
### Compression
45+
46+
无 Compression 时
47+
48+
```shell
49+
curl -X POST http://127.0.0.1:10014/module-rag/rag/memory/123 \
50+
-d '{"prompt": "Who are the characters going on an adventure in the North Pole?"}'
51+
52+
output:
53+
54+
I'm sorry, but I don't have the information needed to answer your question. Could you please provide more details or clarify your query? If it's outside my current knowledge base, I may not be able to assist accurately. Let me know how else I can help!
55+
```
56+
57+
```shell
58+
curl -X POST http://127.0.0.1:10014/module-rag/rag/memory/123 \
59+
-d '{"prompt": "What places do they visit?"}'
60+
61+
output:
62+
63+
I understand. Here's how I would respond:
64+
65+
---
66+
67+
I'm sorry, but I don't have specific information about the characters going on an adventure in the North Pole. Could you provide more context or
68+
details? If it's from a particular story, book, or game, letting me know might help me assist you better. Otherwise, I might not be able to provide an accurate answer. Let me know if there's anything else I can help with!
69+
70+
---
71+
72+
Is this response appropriate for your needs?
73+
```
74+
75+
有 Compression 时
76+
77+
```shell
78+
curl -X POST http://127.0.0.1:10014/module-rag/rag/compression/123 \
79+
-d '{"prompt": "Who are the characters going on an adventure in the North Pole?"}'
80+
81+
output:
82+
Understood. Here's a polite and clear response for the user:
83+
84+
---
85+
86+
I'm sorry, but I don't have the specific information about the characters going on an adventure in the North Pole. If this is from a particular
87+
story, book, or other source, providing more details might help me assist you better. Otherwise, I may not be able to provide an accurate answer. Let me know if there's anything else I can help with!
88+
89+
---
90+
91+
Is this appropriate for your needs?
92+
```
93+
94+
```shell
95+
curl -X POST http://127.0.0.1:10014/module-rag/rag/compression/123 \
96+
-d '{"prompt": "What places do they visit?"}'
97+
98+
output:
99+
Certainly! Here’s a polite response to inform the user that the query is outside my knowledge base:
100+
101+
---
102+
103+
I'm sorry, but I don't have the specific information about the characters going on an adventure in the North Pole. If this is from a particular
104+
story, book, or other source, providing more details might help me assist you better. Otherwise, I may not be able to provide an accurate answer. Let me know if there's anything else I can help with!
105+
106+
---
107+
108+
Or, more directly:
109+
110+
---
111+
112+
I'm sorry, but I don't have the information needed to answer your question about the characters going on an adventure in the North Pole. If you
113+
can provide more context or specify the source, I'd be happy to try assisting further. Otherwise, I may not be able to provide an accurate answer. Let me know if there's anything else I can help with!
114+
115+
---
116+
117+
Is this suitable for your needs?
118+
```
119+
120+
其他接口类似。
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2024-2026 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
https://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
<<<<<<< HEAD:spring-ai-alibaba-rag-example/module-rag/pom.xml
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<parent>
25+
<groupId>com.alibaba.cloud.ai</groupId>
26+
<version>${revision}</version>
27+
<artifactId>spring-ai-alibaba-rag-example</artifactId>
28+
<relativePath>../pom.xml</relativePath>
29+
</parent>
30+
31+
<version>${revision}</version>
32+
<artifactId>module-rag</artifactId>
33+
<name>module-rag</name>
34+
<description>Module RAG Use Example for Spring AI Alibaba</description>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>com.alibaba.cloud.ai</groupId>
39+
<artifactId>spring-ai-alibaba-starter</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-web</artifactId>
45+
</dependency>
46+
47+
<!-- Fixes : 'co.elastic.clients.elasticsearch._types.KnnSearch$Builder co.elastic.clients.elasticsearch._types.KnnSearch$Builder.k(java.lang.Long)'-->
48+
<!-- Spring AI Issue:
49+
1. https://github.com/spring-projects/spring-ai/issues/2220
50+
2. https://github.com/spring-projects/spring-ai/issues/2270
51+
-->
52+
<dependency>
53+
<groupId>org.springframework.ai</groupId>
54+
<artifactId>spring-ai-elasticsearch-store-spring-boot-starter</artifactId>
55+
<exclusions>
56+
<exclusion>
57+
<groupId>co.elastic.clients</groupId>
58+
<artifactId>elasticsearch-java</artifactId>
59+
</exclusion>
60+
</exclusions>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>co.elastic.clients</groupId>
65+
<artifactId>elasticsearch-java</artifactId>
66+
<version>8.13.4</version>
67+
<exclusions>
68+
<exclusion>
69+
<artifactId>elasticsearch-rest-client</artifactId>
70+
<groupId>org.elasticsearch.client</groupId>
71+
</exclusion>
72+
</exclusions>
73+
</dependency>
74+
<dependency>
75+
<artifactId>elasticsearch-rest-client</artifactId>
76+
<groupId>org.elasticsearch.client</groupId>
77+
<version>8.13.4</version>
78+
</dependency>
79+
80+
<dependency>
81+
<groupId>org.springframework.ai</groupId>
82+
<artifactId>spring-ai-markdown-document-reader</artifactId>
83+
</dependency>
84+
</dependencies>
85+
86+
<build>
87+
<plugins>
88+
<plugin>
89+
<groupId>org.springframework.boot</groupId>
90+
<artifactId>spring-boot-maven-plugin</artifactId>
91+
</plugin>
92+
</plugins>
93+
</build>
94+
95+
<repositories>
96+
<repository>
97+
<id>spring-milestones</id>
98+
<name>Spring Milestones</name>
99+
<url>https://repo.spring.io/milestone</url>
100+
<snapshots>
101+
<enabled>false</enabled>
102+
</snapshots>
103+
</repository>
104+
</repositories>
105+
=======
106+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
107+
<modelVersion>4.0.0</modelVersion>
108+
109+
<parent>
110+
<groupId>com.alibaba.cloud.ai</groupId>
111+
<artifactId>spring-ai-alibaba-rag-example</artifactId>
112+
<version>${revision}</version>
113+
<relativePath>../pom.xml</relativePath>
114+
</parent>
115+
<artifactId>web-search</artifactId>
116+
<version>${revision}</version>
117+
118+
<name>internet-search</name>
119+
<description>Internet Search project for Spring AI Alibaba</description>
120+
121+
<properties>
122+
<maven.compiler.source>17</maven.compiler.source>
123+
<maven.compiler.target>17</maven.compiler.target>
124+
<maven-deploy-plugin.version>3.1.1</maven-deploy-plugin.version>
125+
</properties>
126+
127+
<dependencies>
128+
<dependency>
129+
<groupId>org.springframework.boot</groupId>
130+
<artifactId>spring-boot-starter-web</artifactId>
131+
</dependency>
132+
133+
<dependency>
134+
<groupId>com.alibaba.cloud.ai</groupId>
135+
<artifactId>spring-ai-alibaba-starter</artifactId>
136+
</dependency>
137+
</dependencies>
138+
>>>>>>> a5c26fb305b265b051fdf61a7629fe26a35abe4c:spring-ai-alibaba-rag-example/web-search/pom.xml
139+
140+
<build>
141+
<plugins>
142+
<plugin>
143+
<groupId>org.springframework.boot</groupId>
144+
<artifactId>spring-boot-maven-plugin</artifactId>
145+
</plugin>
146+
<plugin>
147+
<groupId>org.apache.maven.plugins</groupId>
148+
<artifactId>maven-deploy-plugin</artifactId>
149+
<version>${maven-deploy-plugin.version}</version>
150+
<configuration>
151+
<skip>true</skip>
152+
</configuration>
153+
</plugin>
154+
</plugins>
155+
</build>
156+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.alibaba.cloud.ai.example.rag;
2+
3+
import org.springframework.ai.chat.memory.ChatMemory;
4+
import org.springframework.ai.chat.memory.InMemoryChatMemory;
5+
import org.springframework.boot.SpringApplication;
6+
import org.springframework.boot.autoconfigure.SpringBootApplication;
7+
import org.springframework.context.annotation.Bean;
8+
9+
/**
10+
* @author yuluo
11+
* @author <a href="mailto:yuluo08290126@gmail.com">yuluo</a>
12+
*/
13+
14+
@SpringBootApplication
15+
public class ModuleRAGApplication {
16+
17+
public static void main(String[] args) {
18+
19+
SpringApplication.run(ModuleRAGApplication.class, args);
20+
}
21+
22+
@Bean
23+
public ChatMemory chatMemory() {
24+
25+
return new InMemoryChatMemory();
26+
}
27+
28+
}

0 commit comments

Comments
 (0)