Spring AI存储向量数据

背景:打造企业级AI应用,满足客户需求

此次为演示demo版,采用若依plus框架,SpringBoot版本3.4.1,JDK17,需安装Ollama!

步骤一:引入依赖

[1]pom.xml

xml 复制代码
<spring-ai.version>1.0.0</spring-ai.version>
<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
xml 复制代码
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama</artifactId>
    <version>${spring-ai.version}</version>
</dependency>
xml 复制代码
<dependency>
    <groupId>com.alibaba.cloud.ai</groupId>
    <artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
    <version>${spring-ai-alibaba.version}</version>
</dependency>

步骤二:配置信息

[2]application.yml

yaml 复制代码
spring:
  application:
    name: ruoyi-plus
  ai:
    ollama:
      base-url: http://localhost:11434
      embedding:
        model: nomic-embed-text:latest
    dashscope:
      api-key: 个人秘钥
      chat:
        options:
          model: qwen-max

步骤三:接口开发

[1]VectorStoreController

scala 复制代码
@RestController
@RequestMapping("/vector")
public class VectorStoreController extends BaseController {
    @Autowired
    private VectorStoreService vectorStoreService;

    /**
     * spring ai向量库
     *
     * @return
     */
    @GetMapping("/simple")
    public R<Void> simpleVectorStore() {
        return toAjax(vectorStoreService.simpleVectorStore());
    }
}

[2]VectorStoreService

csharp 复制代码
public interface VectorStoreService {
    /**
     * spring ai向量库
     *
     * @return
     */
    int simpleVectorStore();
}

[3]VectorStoreServiceImpl

java 复制代码
@Service
@Slf4j
public class VectorStoreServiceImpl implements VectorStoreService {
    private static final String VECTOR_STORE_DOCUMENT_ID = "documentId";

    @Autowired
    private EmbeddingModel embeddingModel;

    /**
     * spring ai向量库
     *
     * @return
     */
    @Override
    public int simpleVectorStore() {
        String content = "这是一段测试文本";
        Document document = new Document(content);
        VectorStore vectorStore = buildSimpleVectorStore(embeddingModel);

        // 存储数据 业务关联
        document.getMetadata().put(VECTOR_STORE_DOCUMENT_ID, "c7e8354c-af69-721b-dac0-cc0d8833b2b4");
        vectorStore.add(List.of(document));
        log.info("vector store success...");
        return 1;
    }

    /**
     * 构建spring ai向量库
     *
     * @param embeddingModel
     * @return
     */
    private VectorStore buildSimpleVectorStore(EmbeddingModel embeddingModel) {
        SimpleVectorStore vectorStore = SimpleVectorStore.builder(embeddingModel).build();
        // 本地存储文件
        File file = new File(StrUtil.format("{}/data_store/simple_{}.json", FileUtil.getUserHomePath(), embeddingModel.getClass().getSimpleName()));
        if (!file.exists()) {
            FileUtil.mkParentDirs(file);
            try {
                file.createNewFile();
            } catch (IOException e) {
                log.info("file create error...");
            }
        } else if (file.length() > 0) {
            vectorStore.load(file);
        }
        // 定时器
        Timer timer = new Timer("SimpleVectorStoreTimer-" + file.getAbsolutePath());
        log.info("timer task start...");
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                vectorStore.save(file);
            }
        }, Duration.ofMinutes(1).toMillis(), Duration.ofMinutes(1).toMillis());
        // 持久化
        RuntimeUtil.addShutdownHook(() -> vectorStore.save(file));
        return vectorStore;
    }


}

接口测试

验证

文件内容

可以将业务数据,如文档id存储在元数据中,方便检索

以上所用的在spring-ai-alibaba-starter-dashscope依赖中含有rag功能,无需引入spring-ai-ollama依赖!

源码解析

由于引入ollama,改写为ollama向量模型解析并存储

[4]ModelFactory

scss 复制代码
@Component
public class ModelFactory {
    @Value("${spring.ai.ollama.base-url}")
    private String url;

    @Value("${spring.ai.ollama.embedding.model}")
    private String model;

    /**
     * 构建ollama向量模型
     *
     * @return
     */
    public OllamaEmbeddingModel buildOllamaEmbeddingModel() {
        OllamaApi ollamaApi = OllamaApi.builder().baseUrl(url).build();
        OllamaOptions ollamaOptions = OllamaOptions.builder().model(model).build();
        return OllamaEmbeddingModel.builder()
            .ollamaApi(ollamaApi)
            .defaultOptions(ollamaOptions)
            .build();
    }
}

改写VectorStoreServiceImpl

java 复制代码
@Autowired
private ModelFactory modelFactory;
ini 复制代码
// 构建向量模型 ollama示例
OllamaEmbeddingModel ollamaEmbeddingModel = modelFactory.buildOllamaEmbeddingModel();
VectorStore vectorStore = buildSimpleVectorStore(ollamaEmbeddingModel);

接口测试

验证

文件内容

至此,Spring AI实现文本转向量存储已完成,由于用的本地文件,大家可以使用向量库pgvector/Milvus来实现!

本人正在打造技术交流群,欢迎志同道合的朋友一起探讨,一起努力,通过自己的努力,在技术岗位这条道路上走得更远。QQ群号:925317809 备注:技术交流 即可通过!

相关推荐
Tony Bai2 小时前
“我曾想付钱给 Google 去工作”—— Russ Cox 深度访谈:Go 的诞生、演进与未来
开发语言·后端·golang
serendipity_hky4 小时前
【SpringCloud | 第2篇】OpenFeign远程调用
java·后端·spring·spring cloud·openfeign
嘟嘟MD4 小时前
程序员副业 | 2025年11月复盘
后端·创业
SadSunset4 小时前
(15)抽象工厂模式(了解)
java·笔记·后端·spring·抽象工厂模式
汝生淮南吾在北4 小时前
SpringBoot+Vue养老院管理系统
vue.js·spring boot·后端·毕业设计·毕设
李慕婉学姐4 小时前
【开题答辩过程】以《基于springboot的地铁综合服务管理系统的设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·spring boot·后端
期待のcode5 小时前
Springboot配置属性绑定
java·spring boot·后端
海上彼尚5 小时前
Go之路 - 6.go的指针
开发语言·后端·golang
LYFlied5 小时前
在AI时代,前端开发者如何构建全栈开发视野与核心竞争力
前端·人工智能·后端·ai·全栈
用户47949283569155 小时前
我只是给Typescript提个 typo PR,为什么还要签协议?
前端·后端·开源