springAi集成es向量库

1. pom

xml 复制代码
<!-- Vector Store for RAG -->
        <dependency>
            <groupId>org.springframework.ai</groupId>
            <artifactId>spring-ai-starter-vector-store-elasticsearch</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>co.elastic.clients</groupId>
                    <artifactId>elasticsearch-java</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 与自己的es版本对应 -->
        <dependency>
            <groupId>co.elastic.clients</groupId>
            <artifactId>elasticsearch-java</artifactId>
            <version>8.18.8</version>
        </dependency>

2. yaml

yaml 复制代码
spring:
    dashscope:
      api-key: sk-4xxx
      # 生成"结构化大输出/工具调用多轮"时,默认读超时过短会触发 timeout
      # 单位为毫秒(Integer),按需调整
      # 连接建立超时(毫秒)
      connect-timeout: 30000
      read-timeout: 1200000
      embedding:
        options:
          model: text-embedding-v3

3. EsRagConfig 配置类

java 复制代码
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStore;
import org.springframework.ai.vectorstore.elasticsearch.ElasticsearchVectorStoreOptions;
import org.springframework.ai.vectorstore.elasticsearch.SimilarityFunction;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class EsRagConfig {

    @Bean
    public RestClient restClient() {
        return RestClient.builder(new HttpHost("localhost", 9200, "http")) // 配置 ES 地址和端口
                .build();
    }

    @Bean
    public VectorStore vectorStore(RestClient restClient, EmbeddingModel dashscopeEmbeddingModel) {
        ElasticsearchVectorStoreOptions options = new ElasticsearchVectorStoreOptions();
        options.setIndexName("testRag"); // 设置索引名称
        options.setSimilarity(SimilarityFunction.cosine);
        options.setDimensions(1024); // 设置向量维度
        return ElasticsearchVectorStore.builder(restClient, dashscopeEmbeddingModel)
                .options(options)
                .initializeSchema(true) // 没有索引时自动创建索引
                .batchingStrategy(new TokenCountBatchingStrategy())
                .build();
    }

}

然后就可以看官网示例进行rag相关操作了比如:

java 复制代码
// 向量搜索
vectorStore.similaritySearch(
            SearchRequest.builder()
                    .query(userQuestion)
                    .topK(topK)
                    .similarityThreshold(0.3)
                    .build()
        )
相关推荐
Coder_Boy_5 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
九河云6 小时前
5秒开服,你的应用部署还卡在“加载中”吗?
大数据·人工智能·安全·机器学习·华为云
Gain_chance6 小时前
36-学习笔记尚硅谷数仓搭建-DWS层数据装载脚本
大数据·数据仓库·笔记·学习
每日新鲜事7 小时前
热销复盘:招商林屿缦岛203套售罄背后的客户逻辑分析
大数据·人工智能
AI架构全栈开发实战笔记8 小时前
Eureka 在大数据环境中的性能优化技巧
大数据·ai·eureka·性能优化
AI架构全栈开发实战笔记8 小时前
Eureka 对大数据领域服务依赖关系的梳理
大数据·ai·云原生·eureka
自挂东南枝�9 小时前
政企舆情大数据服务平台的“全域洞察中枢”
大数据
Dragon~Snow9 小时前
Linux Centos9 安装 Elasticsearch
linux·elasticsearch·jenkins
熊延9 小时前
麒麟V10系统安装部署elasticsearch
linux·运维·服务器·elasticsearch·搜索引擎·全文检索