java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring AI ectorStore

Spring AI ectorStore的使用流程

作者:王小工

SpringAI中的VectorStore是一种用于存储和检索高维向量数据的数据库或存储解决方案,它在AI应用中发挥着至关重要的作用,本文给大家介绍Spring AI ectorStore的使用流程,感兴趣的朋友一起看看吧

Spring AI中的VectorStore是一种用于存储和检索高维向量数据的数据库或存储解决方案,它在AI应用中扮演着至关重要的角色。以下是对Spring AI VectorStore的详细解析:

一、VectorStore的基本概念

定义:VectorStore特别适用于处理那些经过嵌入模型转化后的数据。在VectorStore中,查询与传统关系数据库不同,它执行的是相似性搜索,而非精确匹配。当给定向量作为查询时,它会返回与查询向量“相似”的向量。
应用场景:VectorStore主要用于将数据与AI模型集成。它存储并支持对这些向量的相似性搜索,为AI模型提供丰富的上下文信息,从而实现更精确、更智能的回复。这种技术被称为检索增强生成(Retrieval Augmented Generation,RAG)。

二、VectorStore的核心接口

Spring AI框架通过VectorStore接口为向量数据库交互提供了抽象化的API。VectorStore接口定义了以下核心操作:

三、VectorStore的使用流程

四、Spring AI与VectorStore的集成案例

以基于Spring AI框架的聊天机器人项目为例,该项目需要实现根据用户提供的文档数据进行回复的功能。由于对话有最大Token的限制,无法直接将所有的数据发给AI模型进行处理。因此,决定采用数据向量化的方式,将文档数据存储到VectorStore中,并在用户发起对话之前从VectorStore中检索一组相似的文档作为上下文信息。具体实现步骤如下:

五、VectorStore&ES8

1、添加依赖

首先,在Spring Boot项目的构建文件中(如pom.xml对于Maven项目,或build.gradle对于Gradle项目)添加Elasticsearch客户端的依赖。由于Spring AI框架可能不直接支持Elasticsearch作为VectorStore,需要使用Elasticsearch的Java客户端库来与Elasticsearch进行交互。

<!-- Maven 示例 -->
<dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-elasticsearch-store</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-elasticsearch-store-spring-boot-starter</artifactId>
            <version>${spring-ai.version}</version>
        </dependency>

2、配置Elasticsearch连接

在Spring Boot的配置文件中(如application.properties或application.yml),配置Elasticsearch的连接信息,包括集群地址、端口和可能的认证信息。

spring:
  profiles:
    active: druid
  elasticsearch:
    uris: http://127.0.0.1:9200/      #请改成自己的路径
  ai:
    ollama:
      base-url: http://localhost:11434
      embedding:
        model: llama3.2
    vectorstore:
      elasticsearch:
        initialize-schema: true         #请不要修改此配置
        index-name: zixiai           #这是 zixiai 默认的索引,请不要修改或删除
        dimensions: 2048                #不要修改这个配置,与具体大模型本身的维度参数有关系
        similarity: cosine
        batching-strategy: TOKEN_COUNT 

3、业务代码

    @Override
    public String embed(String msg, Set<String> fileIds) {
        log.debug("embedding... {}", msg);
        Set<String> finalFileIds = (fileIds == null) ? new HashSet<>() : fileIds;
        List<Document> st = vectorStore.similaritySearch(SearchRequest.builder().query(msg).topK(5).build());
//                .similaritySearch(SearchRequest.query(msg).withTopK(5));
        // 首先查询向量库
        String promptContent = null;
        if (!CollectionUtils.isEmpty(st)) {
            promptContent = st.stream()
                    .filter(doc -> {
                        if (CollectionUtils.isEmpty(finalFileIds)) {
                            return true;
                        }
                        Object fileIdObject = doc.getMetadata().get("file_id");
                        String docFileId = fileIdObject != null ? fileIdObject.toString() : null;
                        return finalFileIds.contains(docFileId);
                    })
                    .map(Document::getText)
                    .filter(StringUtils::hasText)
                    .collect(Collectors.joining(" "));
        }
        // 确保 promptContent 不为空
        if (!StringUtils.hasText(promptContent)) {
            promptContent = "No information found in the database.";
        }
        log.debug("Prompt content: {}", promptContent);
        return chatClient
                .prompt(promptContent)
                .user(msg)
                .call()
                .content();
    }

详细样例代码样例

综上所述,Spring AI中的
VectorStore为开发者提供了高效、灵活的向量数据存储与检索解决方案。通过集成VectorStore,开发者可以轻松实现AI应用中的相似性搜索功能,从而提升应用的智能化水平和用户体验。

到此这篇关于Spring AI ectorStore的文章就介绍到这了,更多相关Spring AI ectorStore内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

阅读全文