spring-ai使用Xinference(openai)和milvus

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-openai-spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.ai</groupId>
                    <artifactId>spring-ai-openai</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-openai</artifactId>
            <version>1.0.0-M6-XIN</version>
        </dependency>


    </dependencies>

2、yml

spring:
  application:
    name: spring-ai-alibaba-rag-milvus-example

  ai:
    openai:
      api-key: "not empty"
      base-url: http://192.168.3.100:9997
      #  兼容其他OpenAI格式的大模型配置示例
      chat:
        options:
          # 模型ID,需要替换为实际的接入点ID
          model: qwen2-instruct
      embedding:
        options:
          model: bge-m3
          dimensions: 1024
    vectorstore:
      milvus:
        client:
          host: xxx.xxx.xxx.xxx
          port: 19530
          username: root
          password: xxxxxx
        databaseName: default
        collectionName: vector_store1
        initializeSchema: true
        embeddingDimension: 1024
        indexType: IVF_FLAT
        metricType: 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 io.milvus.client.MilvusServiceClient;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.milvus.MilvusVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
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();
    }

    @Autowired
    private MilvusServiceClient milvusServiceClient;
    @Autowired
    EmbeddingModel embeddingModel;


    @Bean
    public MilvusVectorStore vectorStore() {
        MilvusVectorStore customVectorStore =
                MilvusVectorStore.builder(milvusServiceClient, embeddingModel)
                        .databaseName("default")
                        .collectionName("vector_store1")
                        .metricType(MetricType.COSINE)
                        .indexType(IndexType.IVF_FLAT)
                        .indexParameters("{\"nlist\":1024}")
                        //初始化创建collection
                        .initializeSchema(true)
                        .embeddingDimension(1024)
                        .batchingStrategy(new TokenCountBatchingStrategy())
                        .initializeSchema(true).build();
        return customVectorStore;
    }
}

4、初始化插入数据到milvus

/*
 * 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 java.util.Map;

import io.milvus.client.MilvusServiceClient;
import io.milvus.param.IndexType;
import io.milvus.param.MetricType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.milvus.MilvusVectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;

@Configuration
public class VectorDataInit implements ApplicationRunner {

    private final Logger logger = LoggerFactory.getLogger(VectorDataInit.class);

	@Autowired
	private  MilvusVectorStore 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")
        );

        List<Document> document1 = List.of(
                new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
                new Document("The World is Big and Salvation Lurks Around the Corner"),
                new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

        vectorStore.add(documents);
        vectorStore.add(document1);
        logger.info("Vector data initialized");
    }

}

5、controller

/*
 * 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、collection结构如下

### spring-ai-milvus-store-spring-boot-starter 的使用说明 `spring-ai-milvus-store-spring-boot-starter` 是一个用于将 Milvus 集成到 Spring Boot 项目中的依赖。它提供了对 Milvus 向量数据库的支持,便于开发者在应用中实现向量存储检索功能。以下是关于如何配置集成该依赖的详细说明。 #### 1. 添加依赖 在项目的 `pom.xml` 文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.ai</groupId> <artifactId>spring-ai-milvus-store-spring-boot-starter</artifactId> </dependency> ``` 此依赖项会自动引入 Milvus 客户端以及其他必要的组件[^1]。 #### 2. 配置文件设置 在 `application.yml` 或 `application.properties` 文件中配置 Milvus 相关参数。以下是一个典型的配置示例: ```yaml spring: application: name: spring-ai-milvus-example ai: vectorstore: milvus: client: host: xxx.xxx.xxx.xxx port: 19530 username: root password: xxxxxx collectionName: vector_store1 initializeSchema: true embeddingDimension: 1024 indexType: IVF_FLAT metricType: COSINE ``` - **host**: Milvus 数据库的主机地址。 - **port**: Milvus 数据库的端口号,默认为 `19530`。 - **username** **password**: 如果启用了认证,则需要提供用户名密码。 - **collectionName**: 向量存储的集合名称。 - **initializeSchema**: 是否初始化 Milvus 表结构。 - **embeddingDimension**: 嵌入向量的维度。 - **indexType**: 索引类型,例如 `IVF_FLAT`、`HNSW` 等。 - **metricType**: 度量类型,例如 `COSINE` 或 `L2`[^4]。 #### 3. 使用示例 以下是一个简单的代码示例,展示如何在项目中使用 Milvus 存储检索向量数据。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ai.vectorstore.milvus.MilvusVectorStore; import org.springframework.stereotype.Service; @Service public class VectorService { @Autowired private MilvusVectorStore milvusVectorStore; public void storeVector(String id, double[] vector) { milvusVectorStore.add(id, vector); } public double[] searchVector(double[] queryVector, int topK) { return milvusVectorStore.search(queryVector, topK); } } ``` - **storeVector**: 将向量数据存储到 Milvus 中。 - **searchVector**: 根据查询向量检索最相似的向量。 #### 4. 测试与验证 完成上述步骤后,可以通过单元测试或集成测试验证 Milvus 的集成是否成功。可以参考 GitHub 上的示例项目进行进一步学习[^3]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

非ban必选

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值