资讯动态

Spring AI + bge-large + Milvus 构建私有化语义内容检索方案

发布时间:2026/10/27 1:17:42 来源:尧图企业网站定制
一、Spring AI 构建私有化语义内容检索Spring AI是Spring官方社区项目旨在简化Java AI应用程序开发让Java开发者像使用Spring开发普通应用一样开发AI应用。上篇文章我们基于Spring AI MCPDeepSeek R1快速实现了数据库ChatBI能力。在AIGC领域向量语义能力同样也有着十分重要的地位。因此本篇文章探索Spring AI bge-large-zh-v1.5 Milvus搭建一套私有化的内容语义检索能力方案。其中bge-large-zh-v1.5本地私有化部署采用vLLM进行部署和优化。Spring AI采用1.0.0-SNAPSHOT版本。处理过程大致如下图所示实验前需要提前部署好Milvus向量库如果不清楚可参考下面文章的介绍。Milvus 向量数据库介绍及使用二、vLLM 部署 bge-large-zh-v1.5 模型如果不了解vLLM可参考下面这篇文章中的介绍和使用过程利用 vLLM 优化部署私有化大模型让推理速度飞起首先使用modelscope下载模型modelscope download--modelBAAI/bge-large-zh-v1.5--local_dirbge-large-zh-v1.5加载模型启动服务vllm serve bge-large-zh-v1.5\--port8070\--dtypeauto\--taskembed\--trust-remote-code\--api-key token-abc123使用curl测试服务接口是否正常curlhttp://localhost:8070/v1/embeddings\-HContent-Type: application/json\-HAuthorization: Bearer token-abc123\-d{ model: bge-large-zh-v1.5, input: [ 你好小毕超 ] }三、Spring AI 集成 OpenAI 格式向量模型Spring AI关于集成OpenAI格式向量模型的文档介绍如下https://docs.spring.io/spring-ai/reference/api/embeddings/openai-embeddings.html新建SpringBoot项目在pom中修改如下依赖?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsdgroupIdcom.example/groupIdmodelVersion4.0.0/modelVersionartifactIdembedding/artifactIdversion0.0.1-SNAPSHOT/versionnameembedding/namedescriptionembedding/descriptionpropertiesjava.version17/java.versionproject.build.sourceEncodingUTF-8/project.build.sourceEncodingproject.reporting.outputEncodingUTF-8/project.reporting.outputEncodingspring-boot.version3.3.0/spring-boot.versionspring-ai.version1.0.0-SNAPSHOT/spring-ai.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-model-openai/artifactId/dependency/dependenciesdependencyManagementdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-dependencies/artifactIdversion${spring-boot.version}/versiontypepom/typescopeimport/scope/dependencydependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-bom/artifactIdversion${spring-ai.version}/versiontypepom/typescopeimport/scope/dependency/dependencies/dependencyManagementrepositoriesrepositorynameCentral Portal Snapshots/nameidcentral-portal-snapshots/idurlhttps://central.sonatype.com/repository/maven-snapshots//urlreleasesenabledfalse/enabled/releasessnapshotsenabledtrue/enabled/snapshots/repositoryrepositoryidspring-milestones/idnameSpring Milestones/nameurlhttps://repo.spring.io/milestone/urlsnapshotsenabledfalse/enabled/snapshots/repositoryrepositoryidspring-snapshots/idnameSpring Snapshots/nameurlhttps://repo.spring.io/snapshot/urlreleasesenabledfalse/enabled/releases/repository/repositoriesbuildpluginsplugingroupIdorg.apache.maven.plugins/groupIdartifactIdmaven-compiler-plugin/artifactIdversion3.8.1/versionconfigurationsource17/sourcetarget17/targetencodingUTF-8/encoding/configuration/pluginplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactIdversion${spring-boot.version}/versionconfigurationmainClasscom.example.embedding.EmbeddingApplication/mainClassskiptrue/skip/configurationexecutionsexecutionidrepackage/idgoalsgoalrepackage/goal/goals/execution/executions/plugin/plugins/build/project修改application.yml添加vLLM服务的链接信息:spring:ai:openai:base-url:http://127.0.0.1:8070api-key:token-abc123embedding:options:model:bge-large-zh-v1.5使用向量能力SpringBootTestclassEmbeddingApplicationTests{ResourceprivateEmbeddingModelembeddingModel;Testvoidembed(){EmbeddingResponseembeddingResponseembeddingModel.call(newEmbeddingRequest(List.of(你好小毕超,你叫什么名字),OpenAiEmbeddingOptions.builder().build()));embeddingResponse.getResults().forEach(e-{System.out.println(向量维度e.getOutput().length);for(floatf:e.getOutput())System.out.print(f);System.out.println();});}}运行后可以看到向量模型的维度是1024维已经向量化后的信息。四、Spring AI 集成 Milvus 向量数据库实现持久化和语义检索能力Spring AI关于Milvus的文档如下https://docs.spring.io/spring-ai/reference/api/vectordbs/milvus.html#milvus-properties在pom中增加Milvus依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-starter-vector-store-milvus/artifactId/dependencyapplication.yml中添加Milvus的链接信息spring:ai:openai:base-url:http://127.0.0.1:8070api-key:token-abc123embedding:options:model:bge-large-zh-v1.5vectorstore:milvus:client:host:127.0.0.1port:19530username:rootpassword:milvusdatabaseName:defaultcollectionName:vector_testembeddingDimension:1024indexType:IVF_FLATmetricType:COSINEinitialize-schema:true3.1 内容持久化至向量库中SpringBootTestclassMilvusTests{ResourceVectorStorevectorStore;TestvoidtoMinvus(){ListDocumentdocumentsList.of(newDocument(我的爱好是打篮球,Map.of(name,张三,age,18)),newDocument(我的爱好的是学习,Map.of(name,李四,age,30)),newDocument(今天的天气是多云,Map.of(name,王五,age,50)),newDocument(我的心情非常愉悦,Map.of(name,赵六,age,25)),newDocument(我叫小毕超,Map.of(name,小毕超,age,28)));vectorStore.add(documents);}}运行后可在Milvus中看到自动创建的collection3.2 语义内容检索SpringBootTestclassMilvusTests{ResourceVectorStorevectorStore;Testvoidsimilarity(){ListDocumentresultsvectorStore.similaritySearch(SearchRequest.builder().query(你叫什么名字).topK(3).similarityThreshold(0.2).build());Optional.ofNullable(results).ifPresent(res-{res.forEach(d-System.out.println(d.getText() d.getMetadata()));});}}根据meta进行过滤检索过滤表达可以使用文本表达式方式Testvoidsimilarity2(){ListDocumentresultsvectorStore.similaritySearch(SearchRequest.builder().query(你叫什么名字).topK(5).similarityThreshold(0.2).filterExpression(age 15 age 30).build());Optional.ofNullable(results).ifPresent(res-{res.forEach(d-System.out.println(d.getText() d.getMetadata()));});}其中表达式也可使用api的方式Testvoidsimilarity3(){FilterExpressionBuilderfilternewFilterExpressionBuilder();ListDocumentresultsvectorStore.similaritySearch(SearchRequest.builder().query(你叫什么名字).topK(5).similarityThreshold(0.2).filterExpression(filter.and(filter.gt(age,15),filter.lt(age,30)).build()).build());Optional.ofNullable(results).ifPresent(res-{res.forEach(d-System.out.println(d.getText() d.getMetadata()));});}可以得到同样的结果

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

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

免费获取报价 →
↑