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()
        )
相关推荐
那就学有所成吧(˵¯͒¯͒˵)1 天前
大数据项目(一):Hadoop 云网盘管理系统开发实践
大数据·hadoop·分布式
KKKlucifer1 天前
数据资产地图构建:文档安全可视化与主动防御
大数据·安全
2501_943695331 天前
高职工业大数据应用专业,怎么找智能制造企业的数据岗?
大数据·信息可视化·制造
得赢科技1 天前
智能菜谱研发公司推荐 适配中小型餐饮
大数据·运维·人工智能
好好沉淀1 天前
Elasticsearch 中获取返回匹配记录总数
开发语言·elasticsearch
Hello.Reader1 天前
Flink 内存与资源调优从 Process Memory 到 Fine-Grained Resource Management
大数据·flink
有代理ip1 天前
成功请求的密码:HTTP 2 开头响应码深度解析
java·大数据·python·算法·php
jl48638211 天前
打造医疗设备的“可靠视窗”:医用控温仪专用屏从抗菌设计到EMC兼容的全链路解析
大数据·运维·人工智能·物联网·人机交互
好好沉淀1 天前
ES 脚本核心语法:ctx._source [‘group_id‘]
java·elasticsearch·script
刺客xs1 天前
git 入门常用命令
大数据·git·elasticsearch