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()
        )
相关推荐
workflower3 小时前
使用大语言模型处理用户需求
大数据·人工智能·设计模式·重构·动态规划
AC赳赳老秦6 小时前
OpenClaw+Power Apps 实战:自动生成 Power Apps 应用、连接 Excel 数据源
大数据·开发语言·python·serverless·excel·deepseek·openclaw
keke.shengfengpolang7 小时前
数据科学与大数据技术和大数据管理与应用怎么抉择?
大数据
产业家8 小时前
AI长跑,来到了腾讯的主场
大数据·人工智能
小赖同学啊8 小时前
可信数据空间中异构数据处理与安全保障方案
大数据
HavenlonLabs8 小时前
重塑链上未来的隐形基石:长期主义下的生态演进
大数据·人工智能·安全·区块链
huangdong_8 小时前
京东商品图片视频批量下载与m3u8视频合并技术完整实现方案
大数据·前端·数据库
Java 码思客9 小时前
【ElasticSearch从入门到架构师】第9章:ES 读写底层流程深度拆解
大数据·elasticsearch·搜索引擎
ttt606_9 小时前
门店业绩上报系统功能拆解:门店业绩上报如何提高数据精确度与时效性?
大数据·人工智能
ACP广源盛139246256739 小时前
GSV2221@ACP#DP 1.4 MST 多屏转换芯片,物理 AI 多模态交互的视觉中枢
大数据·人工智能·嵌入式硬件·gpt·spark