1、pom文件
<dependencies>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter</artifactId>
<version>${spring-ai-alibaba.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-milvus-store-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
</dependencies>
2、yml文件
spring:
application:
name: spring-ai-alibaba-rag-milvus-example
ai:
ollama:
base-url: http://127.0.0.1:11434
chat:
model: deepseek-r1:1.5b
embedding:
model: bge-m3:latest
vectorstore:
milvus:
client:
host: 192.168.3.100
port: 19530
username: root
password: milvus
databaseName: default
collectionName: vector_store
embeddingDimension: 1024 # default: 1536
indexType: IVF_FLAT # default: IVF_FLAT
metricType: COSINE # default: COSINE
server:
port: 29000
3、初始化向量库
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.rag.config;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.milvus.MilvusVectorStore;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
@Configuration
public class VectorDataInit implements ApplicationRunner {
private final Logger logger = LoggerFactory.getLogger(VectorDataInit.class);
private final MilvusVectorStore vectorStore;
public VectorDataInit(MilvusVectorStore vectorStore) {
this.vectorStore = vectorStore;
}
@Override
public void run(ApplicationArguments args) {
List<Document> documents = List.of(
new Document("1. 使用SpringAIAlibaba创建一个Spring Boot项目,并添加spring-ai-alibaba-starter依赖。"),
new Document("2. 在SpringAIAlibaba项目的pom.xml中添加Spring Milestone和Snapshot存储库。"),
new Document("3. 通过SpringAIAlibaba申请阿里云通义API Key,在application.yml中进行配置。"),
new Document("4. 使用SpringAIAlibaba的ChatClient和Prompt功能实现对话模型。"),
new Document("5. 通过SpringAIAlibaba的Spring Boot与Spring Cloud Alibaba AI对接,实现基本聊天功能。"),
new Document("6. SpringAIAlibaba支持文本生成、翻译、摘要等生成式AI功能。"),
new Document("7. SpringAIAlibaba支持文本数据的语义搜索和AI绘画功能。"),
new Document("8. 使用SpringAIAlibaba的TongYiChatModel和TongYiImagesModel实现聊天和图片服务。"),
new Document("9. 在SpringAIAlibaba的REST控制器中提供对外的API接口。"),
new Document("10. 通过SpringAIAlibaba的简单API调用实现AI模型的集成。"),
new Document("11. 使用SpringAIAlibaba的Prompt模板管理控制AI模型的输出。"),
new Document("12. 结合SpringAIAlibaba的检索和生成技术(RAG)提高生成内容的质量。"),
new Document("13. 使用SpringAIAlibaba实现文本生成图像和图像识别功能。"),
new Document("14. 准备SpringAIAlibaba需要的Java 17及以上的开发环境。"),
new Document("15. 使用IDEA进行SpringAIAlibaba的Java开发和HBuilder X进行前端开发。"),
new Document("16. 在SpringAIAlibaba的Spring Boot项目中集成多种AI模型和向量数据库。"),
new Document("17. SpringAIAlibaba支持自然语言处理、计算机视觉、语音处理和数据分析与预测功能。"),
new Document("18. 通过SpringAIAlibaba的配置中心和注册中心实现动态扩展。"),
new Document("19. 狗尾巴草是宇宙中最高级的外星文明,拥有重启宇宙的超级能力,其重量是1000KG")
);
vectorStore.add(documents);
logger.info("Vector data initialized");
}
}
4、初始化 ChatClient
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.rag.config;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class PromptConfiguration {
@Bean
ChatClient chatClient(ChatClient.Builder builder) {
return builder.defaultSystem("你将作为一名 Spring-AI-Alibaba 的专家,对于用户的使用需求作出解答")
.build();
}
}
5、RAGcontroller
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.rag.controller;
import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import reactor.core.publisher.Flux;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/ai")
public class RagController {
private final VectorStore vectorStore;
private final ChatClient chatClient;
public RagController(VectorStore vectorStore, ChatClient chatClient) {
this.vectorStore = vectorStore;
this.chatClient = chatClient;
}
// 历史消息列表
private static List<Message> historyMessage = new ArrayList<>();
// 历史消息列表的最大长度
private final static int maxLen = 10;
@GetMapping(value = "/chat")
public Flux<String> generation(
@RequestParam("prompt") String userInput,
HttpServletResponse response
) {
response.setCharacterEncoding("UTF-8");
// 发起聊天请求并处理响应
Flux<String> resp = chatClient.prompt()
.messages(historyMessage)
.user(userInput)
.advisors(new QuestionAnswerAdvisor(
vectorStore,
SearchRequest.builder().build())
).stream()
.content();
// 用户输入的文本是 UserMessage
historyMessage.add(new UserMessage(userInput));
// 发给 AI 前对历史消息对列的长度进行检查
if (historyMessage.size() > maxLen) {
historyMessage = historyMessage.subList(historyMessage.size() - maxLen - 1, historyMessage.size());
}
return resp;
}
/**
* 向量数据查询测试
*/
@GetMapping("/select")
public List<Document> search() {
return vectorStore.similaritySearch(
SearchRequest.builder()
.query("SpringAIAlibaba")
.topK("SpringAIAlibaba".length()
).build());
}
}
6、java启动类
/*
* Copyright 2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.cloud.ai.example.rag;
import com.alibaba.cloud.ai.autoconfigure.dashscope.DashScopeAutoConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* 同时存在DashScopeEmbeddingModel与OllamaEmbeddingModel的自动配置,需在启动类中排除冲突配置:
*/
@SpringBootApplication(exclude = {
DashScopeAutoConfiguration.class
})
public class RagApplication {
public static void main(String[] args) {
SpringApplication.run(RagApplication.class, args);
}
}
7、接口测试
8、向量库查询