发布时间:2026/8/12 14:22:15
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/8/12 2:34:13

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

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

2026/8/13 8:23:23

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

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

2026/8/12 10:20:56

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

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

2026/8/13 9:38:06

MySQL数据库基础(一)库操作|表操作|数据类型|表约束详解

1. 数据库基础 1.1 什么是数据库 文件保存数据的缺点: 1) 文件的安全性问题 2) 文件不利于数据查询和管理 3) 文件不利于存储海量数据 4) 文件在程序中控制不方便 数据库:解决文件存储的缺陷,更加高效管理数据;数据库水平是…

2026/8/13 9:38:06

云克隆Luminex液相悬浮芯片(DAO/GH/IL10/IL1b/IL6/LPS六因子Panel)实现肠道屏障功能、细菌易位与系统性炎症的同步定量分析

肠道屏障功能完整性的评估,长期以来缺乏一个“从门户到全身”的高效一体化检测方案。DAO(二胺氧化酶)是肠黏膜屏障完整性的“直接度量尺”,LPS是细菌产物易位的“分子证据”,GH是肠黏膜修复能力的“风向标”&#xff0…

2026/8/13 9:38:06

3分钟学会Android应用级虚拟定位:FakeLocation完整使用教程

3分钟学会Android应用级虚拟定位:FakeLocation完整使用教程 【免费下载链接】FakeLocation Xposed module to mock locations per app. 项目地址: https://gitcode.com/gh_mirrors/fak/FakeLocation 你是否希望在不暴露真实位置的情况下使用某些应用&#xf…

2026/8/13 9:33:06

ROS 2 Lyrical Luth 第1章 系统入门教程

ROS 2 Lyrical Luth 第一章 系统入门教程 ROS2 Lyrical正式版替换测试版记录前言 本文对标原文档《ROS Noetic 入门》结构,科学严谨完整讲解ROS 2 Lyrical Luth(代号 lyrical),覆盖版本背景、PC 原生 Ubuntu 安装、虚拟机部署、D…

2026/8/12 10:37:12

如何快速生成中国车牌图片:Python开源工具完整指南

如何快速生成中国车牌图片:Python开源工具完整指南 【免费下载链接】chinese_license_plate_generator 中国车牌生成器 项目地址: https://gitcode.com/gh_mirrors/ch/chinese_license_plate_generator 中国车牌生成器是一个基于Python的开源项目&#xff0c…

2026/8/12 5:35:25

当 LLM 遇见大文档:主流开源项目如何处理上下文超限

从 Agentic Loop 到 Repo Map,七种策略与六类陷阱引言:128K vs 10MB 的硬冲突 2026 年的 LLM 上下文窗口已达到 128K ~ 1M token(≈ 0.5MB ~ 4MB 文本),但 LLM 想要处理的真实数据规模远远超过这个量级:真实…

2026/8/13 0:02:21

Prefix Cache

Prefix Cache(前缀缓存) 是大模型推理引擎(如 vLLM、SGLang、TensorRT-LLM)中用于跨请求复用已计算 KV Cache 的核心内存与计算优化技术。 它的核心目的在于:彻底消除重复 Prompt 的 Prefill 阶段计算,将首…

2026/8/13 0:02:21

VSCode插件精选:从AI补全到代码规范,打造高效开发环境

1. 项目概述:为什么说插件是VSCode的灵魂?如果你和我一样,每天有超过8小时的时间是在VSCode里度过的,那你肯定明白,一个顺手的开发环境有多重要。VSCode本身已经足够优秀了,但真正让它从“好用的编辑器”蜕…

2026/8/13 0:02:21

如何快速完成文件批量重命名:FreeReNamer终极指南

如何快速完成文件批量重命名:FreeReNamer终极指南 【免费下载链接】FreeReNamer 功能强大又易用的文件批量重命名软件 项目地址: https://gitcode.com/gh_mirrors/fr/FreeReNamer 你是否曾经面对成百上千个杂乱无章的文件感到头疼?传统的手动重命…

2026/8/10 11:20:30

实测才敢推 AI论文网站 2026最新测评与推荐

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。一、综…

2026/8/11 17:06:59

2026必备!AI论文网站测评:最新推荐与深度对比

2026年真正好用的AI论文网站,核心看生成的论文质量、低AI味、格式正确、学术适配四大指标。综合实测,千笔AI、ThouPen、豆包、DeepSeek、Grammarly 是当前最值得推荐的梯队,覆盖从免费到付费、从中文到英文、从文科到理工的全场景需求。 一、…

2026/8/11 3:05:11

摆脱论文困扰!盘点2026年全网爆红的的AI论文写作工具

一天写完毕业论文在2026年已不再是天方夜谭。2026年最炸裂、实测能大幅提速的AI论文写作工具,覆盖选题构思、文献整理、内容生成、格式排版等核心场景,真正帮你高效搞定论文难题。 一、全流程王者:一站式搞定论文全链路(一天定稿首…