发布时间:2026/7/1 0:21:59
springai使用chroma向量数据库 文章目录使用maven依赖创建实体类KnowledgeService接口类KnowledgeServiceImpl接口实现类controller报错报错 Error creating bean with name vectorStore defined in class path resource [org/springframework/ai/autoconfigure/vectorstore/chroma/ChromaVectorStoreAutoConfiguration.class]: The v1 API is deprecated. Please use /v2 apis开发环境用chromadb。版本springai # 1.0.0-M6chromadb # 0.5.23要和springai匹配所以不能选高版本有两种方案1、python安装的也能用 # 这里用的这种安装 pip install chromadb0.5.23# 版本要和springai版本兼容启动 chroma run--host0.0.0.0--port8000验证 http://localhost:8000/docs# 这个地址可以看到chromadb的接口即可2、docker安装windows本地版略使用maven依赖dependencygroupIdorg.springframework.ai/groupIdartifactIdspring-ai-chroma-store-spring-boot-starter/artifactIdversion1.0.0-M6/version/dependency创建实体类springai的Document已经支持chromadb直接继承就行。importorg.springframework.ai.document.Document;importjava.util.Map;publicclassKnowledgeDocumentextendsDocument{/** * 业务创建时的构造方法自动生成 ID */publicKnowledgeDocument(Stringcontent,StringsourceFile){super(content);this.getMetadata().put(source,sourceFile);this.getMetadata().put(created_at,System.currentTimeMillis());}/** * 内部全参构造方法支持传入指定的 ID用于从数据库/向量库还原对象 */privateKnowledgeDocument(Stringid,Stringcontent,MapString,Objectmetadata){super(id,content,metadata);}/** * 将 Spring AI 的 Document 转为我们的业务实体 */publicstaticKnowledgeDocumentfrom(Documentdoc){// 直接使用包含 ID 的构造方法避免调用不存在的 setId 方法returnnewKnowledgeDocument(doc.getId(),doc.getText(),doc.getMetadata());}}KnowledgeService接口类publicinterfaceKnowledgeService{/** * 上传并解析文档入库 */voiduploadAndIndex(MultipartFilefile)throwsException;/** * 根据内容检索相关文档 */ListKnowledgeDocumentsearch(Stringquery,inttopK);}KnowledgeServiceImpl接口实现类importcom.example.demo.entity.KnowledgeDocument;importcom.example.demo.service.KnowledgeService;importlombok.RequiredArgsConstructor;importorg.springframework.ai.document.Document;importorg.springframework.ai.transformer.splitter.TokenTextSplitter;importorg.springframework.ai.vectorstore.SearchRequest;importorg.springframework.ai.vectorstore.VectorStore;importorg.springframework.stereotype.Service;importorg.springframework.web.multipart.MultipartFile;importjava.nio.charset.StandardCharsets;importjava.util.List;importjava.util.stream.Collectors;ServiceRequiredArgsConstructorpublicclassKnowledgeServiceImplimplementsKnowledgeService{privatefinalVectorStorevectorStore;privatefinalTokenTextSplittertokenTextSplitternewTokenTextSplitter();OverridepublicvoiduploadAndIndex(MultipartFilefile)throwsException{// 1. 读取文件内容StringcontentnewString(file.getBytes(),StandardCharsets.UTF_8);// 2. 创建原始 DocumentDocumentrawDocnewKnowledgeDocument(content,file.getOriginalFilename());// 3. 文本分块 (Chunking)ListDocumentchunkstokenTextSplitter.apply(List.of(rawDoc));// 4. 存入 ChromaDBvectorStore.add(chunks);}OverridepublicListKnowledgeDocumentsearch(Stringquery,inttopK){SearchRequestrequestSearchRequest.builder().query(query).topK(topK).similarityThreshold(0.7).build();ListDocumentresultsvectorStore.similaritySearch(request);// 将底层 Document 转换回我们的业务实体returnresults.stream().map(KnowledgeDocument::from).collect(Collectors.toList());}}controllerimportcom.example.demo.entity.JsonResult;importcom.example.demo.entity.KnowledgeDocument;importcom.example.demo.service.KnowledgeService;importlombok.extern.slf4j.Slf4j;importorg.apache.commons.lang3.StringUtils;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.*;importorg.springframework.web.multipart.MultipartFile;importjava.util.List;/* * 知识库管理表 */RestController()RequestMapping(/api)Slf4jpublicclassKnowledgeController{AutowiredprivateKnowledgeServiceknowledgeService;/** * 上传并解析文档入库 */PostMapping(/knowledge/upload)publicJsonResultupload(RequestParam(file)MultipartFilefile){StringmethodName知识库文档上传;JsonResultresultJsonResult.ok();try{log.info(methodName_操作开始, fileName{},file.getOriginalFilename());// 参数校验if(file.isEmpty()){thrownewIllegalArgumentException(上传的文件不能为空);}knowledgeService.uploadAndIndex(file);resultJsonResult.ok(文档上传并解析成功);log.info(methodName_操作完成, fileName{},file.getOriginalFilename());returnresult;}catch(IllegalArgumentExceptione){log.error(methodName_操作失败, error,e);returnJsonResult.fail(-1,操作失败,e.getMessage());}catch(Exceptione){log.error(methodName_操作异常, error,e);returnJsonResult.fail(-1,操作异常,请联系系统管理员);}}/** * 根据内容检索相关文档 */GetMapping(/knowledge/search)publicJsonResultsearch(RequestParam(query)Stringquery,RequestParam(valuetopK,defaultValue3)IntegertopK){StringmethodName知识库内容检索;JsonResultresultJsonResult.ok();try{log.info(methodName_查询操作开始, query{}, topK{},query,topK);// 参数校验if(StringUtils.isEmpty(query)){thrownewIllegalArgumentException(检索关键词不能为空);}ListKnowledgeDocumentdocumentsknowledgeService.search(query,topK);resultJsonResult.ok(documents);log.info(methodName_操作完成, 检索到 {} 条结果,documents.size());returnresult;}catch(IllegalArgumentExceptione){log.error(methodName_操作失败, error,e);returnJsonResult.fail(-1,操作失败,e.getMessage());}catch(Exceptione){log.error(methodName_操作异常, error,e);returnJsonResult.fail(-1,操作异常,请联系系统管理员);}}}报错报错 Error creating bean with name ‘vectorStore’ defined in class path resource [org/springframework/ai/autoconfigure/vectorstore/chroma/ChromaVectorStoreAutoConfiguration.class]: The v1 API is deprecated. Please use /v2 apischromadb和springai的版本问题。一开始chromadb用的是1.5.9和springai不匹配改为0.5.23版本是兼容v1版本的问题解决。

相关新闻

2026/6/30 23:26:29

生产级多维聚合:pandas groupby的五大工程化陷阱与实战

1. 项目概述:为什么多维聚合不是“加个groupby”就完事了?我在银行数据平台组干了八年,从最早用SQL写几十行嵌套子查询做客户分层,到现在每天在Jupyter里调试pandas的agg链式调用,最深的体会是:真正的业务分…

2026/6/30 23:17:26

2026最新英语教学APP挑选指南 3个实用方法帮你避开选购误区

说实话我当初19年帮合作校选第一批英语数字化教学工具的时候踩过巨坑,当时贪功能全,选了个号称覆盖全学科的平台,结果英语口语批改不准,学生读错的重音识别不出来,后台学情数据还导不出来,老师改作业反而要…

2026/6/30 23:36:22

vCenter SSO密码忘记完整重置教程:网页+命令行兜底实操

运维工作中经常遗忘vCenter SSO管理员密码(administratorvsphere.local),导致无法登录vSphere Web Client管理虚拟化集群,影响日常运维、备份、集群配置等操作。很多人遇到网页解析报错、找不到重置入口的问题,本文基于…

2026/7/1 16:31:24

5种神奇效果!TranslucentTB让你的Windows任务栏焕然一新

5种神奇效果!TranslucentTB让你的Windows任务栏焕然一新 【免费下载链接】TranslucentTB A lightweight utility that makes the Windows taskbar translucent/transparent. 项目地址: https://gitcode.com/gh_mirrors/tr/TranslucentTB 想让你的Windows桌面…

2026/7/1 16:31:24

本地部署开源分布式追踪后端系统 Tempo 并实现外部访问

Tempo 是一款开源的分布式追踪后端系统,它是一个现代化、高性能、可扩展的解决方案,由 Grafana Labs 开发并维护,专门用于存储和查询大规模的分布式追踪数据。本文将详细介绍如何利用 Docker 在局域网内部署 Tempo 并结合路由侠实现外网访问局…

2026/7/1 15:31:23

WiFi热图绘制终极指南:3分钟学会免费网络优化神器

WiFi热图绘制终极指南:3分钟学会免费网络优化神器 【免费下载链接】wifi-heat-mapper whm also known as wifi-heat-mapper is a Python library for benchmarking Wi-Fi networks and gather useful metrics that can be converted into meaningful easy-to-unders…

2026/7/1 0:31:06

3个高效策略:快速掌握Axure中文界面配置

3个高效策略:快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…

2026/7/1 0:31:06

3个高效策略:快速掌握Axure中文界面配置

3个高效策略:快速掌握Axure中文界面配置 【免费下载链接】axure-cn Chinese language file for Axure RP. Axure RP 简体中文语言包。支持 Axure 11、10、9。不定期更新。 项目地址: https://gitcode.com/gh_mirrors/ax/axure-cn 还在为Axure RP的英文界面感…