资讯动态

Spring AI向量数据库配置简单总结

发布时间:2026/10/20 20:04:18 来源:尧图企业网站定制
版本Spring 3.4.5 Spring AI 1.0.9Redis作为向量库遇到的一些问题按照官方教程配置了对应的pomVectorStore总是显示没有注册怎么改都不行因此选择降级spring3.x,spring ai 1.x对话模型与编码模型配置后框架会自动装配但是配置中如果按照chat/embedding这样的配置方式会触发其它的功能缺少配置问题需要过滤这些没用到的在配置对话模型与编码模型时官方提供的OpenAi格式需要去除v1否则会路径拼接错误导致404Redispom.xmlparent groupIdorg.springframework.boot/groupId artifactIdspring-boot-starter-parent/artifactId version3.4.5/version relativePath/ /parent dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-bom/artifactId version1.0.9/version /dependency dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-model-openai/artifactId /dependency dependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-vector-store-redis/artifactId /dependencyapplication.ymlspring: application: name: balance-service autoconfigure: exclude: - org.springframework.ai.model.openai.autoconfigure.OpenAiAudioSpeechAutoConfiguration - org.springframework.ai.model.openai.autoconfigure.OpenAiAudioTranscriptionAutoConfiguration - org.springframework.ai.model.openai.autoconfigure.OpenAiAudioTranslationAutoConfiguration - org.springframework.ai.model.openai.autoconfigure.OpenAiImageAutoConfiguration - org.springframework.ai.model.openai.autoconfigure.OpenAiModerationAutoConfiguration ai: openai: chat: api-key: base-url: https://maas.qianwenaiapi.com/compatible-mode/v1 completions-path: /chat/completions #删掉默认的 /v1只保留后面一截 options: model: qwen3.8-max temperature: 1 embedding: base-url: https://api.siliconflow.cn/v1 api-key: embeddings-path: /embeddings options: model: BAAI/bge-large-zh-v1.5 vectorstore: redis: initialize-schema: true index-name: pest_control_idx prefix: ai_doc: data: redis: host: port: 6379 password: database:基本使用package com.message.demo.balancemodule.config; import lombok.extern.slf4j.Slf4j; import org.springframework.ai.document.Document; import org.springframework.ai.vectorstore.VectorStore; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; Slf4j Component public class PestControlKnowledgeLoader { Autowired private VectorStore vectorStore; EventListener(ApplicationReadyEvent.class) public void loadPestControlKnowledge() { ListDocument documents List.of( new Document( 储粮害虫是指危害储藏期间粮食及其产品的昆虫和螨类。常见种类包括玉米象、谷蠹、赤拟谷盗、锯谷盗、麦蛾、印度谷螟等。 防治原则以防为主综合防治包括清洁卫生防治、物理机械防治、化学防治和生物防治。 , Map.of(title, 储粮害虫概述与防治原则)), new Document( 玉米象Sitophilus zeamais成虫体长3-5mm圆筒形赤褐色至黑褐色头部前伸成长吻。 主要危害小麦、玉米、水稻和多种谷物。幼虫在粮粒内蛀食成虫在粮粒外危害是储粮的头号害虫。 防治方法低氧储藏充氮气或二氧化碳、低温储藏15℃以下、磷化铝熏蒸。 , Map.of(title, 玉米象的识别与防治)) ); try { vectorStore.add(documents); log.info([RAG] 储粮害虫清除知识库加载完成共 {} 条, documents.size()); } catch (Exception e) { System.out.println(vectorStore); log.error([RAG] 知识库加载失败: {}, e.getMessage(), e); } } }但是这里还有一个坑,也就是会发现如果这样操作的话无论你配置中的database怎么改都是默认的db0这是因为redis对应的RedisVectorStore会配置一个jedis,但是这个jedis默认就是0而且底层使用的初始化构造器应该是hostpassword,因此如果想要指定位置存储的话可以通过自定义操作使用URI形式构建连接对象。其次还要注意如果对未要求的话就不需要这样做如果有的话还要忽视框架的检查不然会有冲突上面的yml的exclude中添加这个org.springframework.ai.vectorstore.redis.autoconfigure.RedisVectorStoreAutoConfigurationimport org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import redis.clients.jedis.JedisPooled; import java.net.URI; Configuration public class RedisVectorStoreConfig { Bean public VectorStore vectorStore(EmbeddingModel embeddingModel) { JedisPooled jedisPooled new JedisPooled( URI.create(redis://:passwordip:6379/2)); return RedisVectorStore.builder(jedisPooled, embeddingModel) .indexName(pest_control_idx) .prefix(ai_doc:) .initializeSchema(true) .build(); } }Milivus搭建mkdir milvuscat docker-compose.yml EOF version: 3.5 services: etcd: image: quay.m.daocloud.io/coreos/etcd:v3.5.5 environment: - ETCD_AUTO_COMPACTION_MODErevision - ETCD_AUTO_COMPACTION_RETENTION1000 volumes: - ./volumes/etcd:/etcd command: etcd -advertise-client-urlshttp://etcd:2379 -listen-client-urls http://0.0.0.0:2379 --data-dir /etcd minio: image: minio/minio:latest environment: MINIO_ROOT_USER: minioadmin MINIO_ROOT_PASSWORD: minioadmin volumes: - ./volumes/minio:/minio_data command: minio server /minio_data --console-address :9001 ports: - 9001:9001 standalone: image: milvusdb/milvus:v2.4.17 command: [milvus, run, standalone] environment: ETCD_ENDPOINTS: etcd:2379 MINIO_ADDRESS: minio:9000 COMMON_SECURITY_AUTHORIZATION_ENABLED: true COMMON_SECURITY_ROOT_USER: root COMMON_SECURITY_ROOT_PASSWORD: Milvus123 volumes: - ./volumes/milvus:/var/lib/milvus ports: - 19530:19530 - 9091:9091 depends_on: - etcd - minio attu: image: zilliz/attu:v2.4 environment: MILVUS_URL: 172.17.0.1:19530 ports: - 3000:3000 depends_on: - standalone EOF防火墙开启195303000即可默认用户名密码为rootMilvusapplication.ymlspring: application: name: balance-service vectorstore: milvus: client: host: port: 19530 username: root password: database-name: default collection-name: pest_control_collection embedding-dimension: 1024 index-type: IVF_FLAT metric-type: COSINE # 是否自动建表、创建schema开发环境true生产建议手动建集合 false initialize-schema: truepom.xmldependency groupIdorg.springframework.ai/groupId artifactIdspring-ai-starter-vector-store-milvus/artifactId /dependency其余用法都一样可能有细微差别框架都封装好了

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑