发布时间:2026/7/5 3:00:16
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/7/5 1:58:26

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

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

2026/7/5 2:05:55

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

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

2026/7/5 1:04:52

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

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

2026/7/5 5:34:36

抖音下载器:企业级分布式架构与高性能内容采集系统

抖音下载器:企业级分布式架构与高性能内容采集系统 【免费下载链接】douyin-downloader A practical Douyin downloader for both single-item and profile batch downloads, with progress display, retries, SQLite deduplication, and browser fallback support.…

2026/7/5 5:34:36

如何让你的游戏机变身B站追番神器?wiliwili完整指南

如何让你的游戏机变身B站追番神器?wiliwili完整指南 【免费下载链接】wiliwili 第三方B站客户端,目前可以运行在PC全平台、PSVita、PS4 、Xbox 和 Nintendo Switch上 项目地址: https://gitcode.com/GitHub_Trending/wi/wiliwili 还在为游戏机无法…

2026/7/5 5:34:36

三步掌握GBFR-Logs:从新手到高手的游戏数据分析指南

三步掌握GBFR-Logs:从新手到高手的游戏数据分析指南 【免费下载链接】gbfr-logs GBFR Logs lets you track damage statistics with a nice overlay DPS meter for Granblue Fantasy: Relink. 项目地址: https://gitcode.com/gh_mirrors/gb/gbfr-logs 在《碧…

2026/7/5 0:34:33

国内大模型选型与企业级落地实战指南

我不能提供任何关于访问境外网络信息的技术方案或变通方法。根据中国法律法规和网络管理要求,所有互联网服务必须遵守国家关于网络安全、数据安全和内容安全的规定。ChatGPT及其后续版本(如所谓“GPT-5”)是由境外机构研发的大语言模型&#…

2026/7/5 0:34:33

三步实战方案:高效获取智慧教育平台电子课本PDF的完整流程

三步实战方案:高效获取智慧教育平台电子课本PDF的完整流程 【免费下载链接】tchMaterial-parser 国家中小学智慧教育平台 电子课本下载工具,帮助您从智慧教育平台中获取电子课本的 PDF 文件网址并进行下载,让您更方便地获取课本内容。 项目…

2026/7/5 0:34:33

国内大模型选型与企业级落地实战指南

我不能提供任何关于访问境外网络信息的技术方案或变通方法。根据中国法律法规和网络管理要求,所有互联网服务必须遵守国家关于网络安全、数据安全和内容安全的规定。ChatGPT及其后续版本(如所谓“GPT-5”)是由境外机构研发的大语言模型&#…

2026/7/5 0:34:33

三步实战方案:高效获取智慧教育平台电子课本PDF的完整流程

三步实战方案:高效获取智慧教育平台电子课本PDF的完整流程 【免费下载链接】tchMaterial-parser 国家中小学智慧教育平台 电子课本下载工具,帮助您从智慧教育平台中获取电子课本的 PDF 文件网址并进行下载,让您更方便地获取课本内容。 项目…

2026/7/5 2:48:20

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的英文界面感…