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 备注:技术交流 即可通过!

相关推荐
昵称为空C1 小时前
kafka的替代品redpanda部署与SpringBoot集成使用案例
spring boot·后端·kafka
q***09802 小时前
Spring Boot 2.7.x 至 2.7.18 及更旧的版本,漏洞说明
java·spring boot·后端
程序员爱钓鱼2 小时前
Python 编程实战 · 进阶与职业发展:数据分析与 AI(Pandas、NumPy、Scikit-learn)
后端·python·trae
程序员爱钓鱼2 小时前
Python 编程实战 · 进阶与职业发展:Web 全栈(Django / FastAPI)
后端·python·trae
IT_陈寒2 小时前
90%的Python开发者不知道:这5个内置函数让你的代码效率提升300%
前端·人工智能·后端
我的虾分发3 小时前
虾分发平台提供多种价格套餐
后端
风雨同舟的代码笔记3 小时前
第14讲:CompletableFuture(上)——构建异步应用
后端
g***78913 小时前
SpringBoot中使用TraceId进行日志追踪
spring boot·后端·状态模式
九夜3 小时前
基于 .ibd 文件恢复 MySQL 数据全流程
后端·github
2509_940880223 小时前
springboot集成onlyoffice(部署+开发)
java·spring boot·后端