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()
        )
相关推荐
2601_962218476 小时前
万象生鲜系统多仓协同同步技术支撑生鲜企业多仓模式数字化转型
大数据·运维·微服务·云原生·架构
隔窗听雨眠7 小时前
TDengine在AIOps中的硬核实战:从时序数据存储到智能运维的完整落地路径
大数据·运维·tdengine
Elastic 中国社区官方博客7 小时前
从建议到修复的 4 个阶段:使用 Elastic Workflows 实现人在回路中的自动化
运维·数据库·人工智能·后端·elasticsearch·ai·自动化
企鹅的企7 小时前
2027北京机器人展聚焦机器人出海合规,助力国产装备走向全球
大数据·机器人
峥嵘life7 小时前
Android16 系统 APEX 模块说明
android·大数据·开发语言
阿宁学科技术库8 小时前
关于“库库AI”在股票投研场景实用性的技术观察
大数据·人工智能·职场和发展
whcyhhh8 小时前
头歌实践教学平台:大数据存储2023(十三3)
大数据·开发语言·python
Easy_API8 小时前
OpenAI三周内第二次降价,GPT-5.6 Sol砍了20%到33
大数据·前端·人工智能·gpt·深度学习
小K讲AI营销8 小时前
智谱配售314亿港元:融资通道切换
大数据·人工智能
用户3610588626128 小时前
SparkStreaming 之 DStream 底层结构剖析
大数据·spark