聊聊Spring AI的ChromaVectorStore

本文主要研究一下Spring AI的ChromaVectorStore

示例

pom.xml

		<dependency><groupId>org.springframework.ai</groupId><artifactId>spring-ai-starter-vector-store-chroma</artifactId></dependency>

配置

spring:ai:vectorstore:type: chromachroma:initialize-schema: truecollectionName: "test1"client:host: http://localhostport: 8000

代码

    @Testpublic void testAddAndSearch() {List<Document> documents = List.of(new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),new Document("The World is Big and Salvation Lurks Around the Corner"),new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));// Add the documents to Milvus Vector StorechromaVectorStore.add(documents);// Retrieve documents similar to a queryList<Document> results = this.chromaVectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());log.info("results:{}", JSON.toJSONString(results));}

输出如下:

results:[{"contentFormatter":{"excludedEmbedMetadataKeys":[],"excludedInferenceMetadataKeys":[],"metadataSeparator":"\n","metadataTemplate":"{key}: {value}","textTemplate":"{metadata_string}\n\n{content}"},"formattedContent":"distance: 0.4350912\nmeta1: meta1\n\nSpring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!","id":"53ce7adb-07ba-429c-b443-40edffde2c89","metadata":{"distance":0.4350912,"meta1":"meta1"},"score":0.5649088025093079,"text":"Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!"},{"contentFormatter":{"$ref":"$[0].contentFormatter"},"formattedContent":"distance: 0.57093126\n\nThe World is Big and Salvation Lurks Around the Corner","id":"cd79cfe8-8503-4e2e-bb1e-ec0e294bc800","metadata":{"distance":0.57093126},"score":0.42906874418258667,"text":"The World is Big and Salvation Lurks Around the Corner"},{"contentFormatter":{"$ref":"$[0].contentFormatter"},"formattedContent":"distance: 0.59360236\nmeta2: meta2\n\nYou walk forward facing the past and you turn back toward the future.","id":"f266f142-3044-4b09-8178-55abe6ef84c5","metadata":{"distance":0.59360236,"meta2":"meta2"},"score":0.40639764070510864,"text":"You walk forward facing the past and you turn back toward the future."}]

源码

ChromaVectorStoreAutoConfiguration

org/springframework/ai/vectorstore/chroma/autoconfigure/ChromaVectorStoreAutoConfiguration.java

@AutoConfiguration
@ConditionalOnClass({ EmbeddingModel.class, RestClient.class, ChromaVectorStore.class, ObjectMapper.class })
@EnableConfigurationProperties({ ChromaApiProperties.class, ChromaVectorStoreProperties.class })
@ConditionalOnProperty(name = SpringAIVectorStoreTypes.TYPE, havingValue = SpringAIVectorStoreTypes.CHROMA,matchIfMissing = true)
public class ChromaVectorStoreAutoConfiguration {@Bean@ConditionalOnMissingBean(ChromaConnectionDetails.class)PropertiesChromaConnectionDetails chromaConnectionDetails(ChromaApiProperties properties) {return new PropertiesChromaConnectionDetails(properties);}@Bean@ConditionalOnMissingBeanpublic ChromaApi chromaApi(ChromaApiProperties apiProperties,ObjectProvider<RestClient.Builder> restClientBuilderProvider, ChromaConnectionDetails connectionDetails,ObjectMapper objectMapper) {String chromaUrl = String.format("%s:%s", connectionDetails.getHost(), connectionDetails.getPort());var chromaApi = new ChromaApi(chromaUrl, restClientBuilderProvider.getIfAvailable(RestClient::builder),objectMapper);if (StringUtils.hasText(connectionDetails.getKeyToken())) {chromaApi.withKeyToken(connectionDetails.getKeyToken());}else if (StringUtils.hasText(apiProperties.getUsername()) && StringUtils.hasText(apiProperties.getPassword())) {chromaApi.withBasicAuthCredentials(apiProperties.getUsername(), apiProperties.getPassword());}return chromaApi;}@Bean@ConditionalOnMissingBean(BatchingStrategy.class)BatchingStrategy chromaBatchingStrategy() {return new TokenCountBatchingStrategy();}@Bean@ConditionalOnMissingBeanpublic ChromaVectorStore vectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi,ChromaVectorStoreProperties storeProperties, ObjectProvider<ObservationRegistry> observationRegistry,ObjectProvider<VectorStoreObservationConvention> customObservationConvention,BatchingStrategy chromaBatchingStrategy) {return ChromaVectorStore.builder(chromaApi, embeddingModel).collectionName(storeProperties.getCollectionName()).initializeSchema(storeProperties.isInitializeSchema()).observationRegistry(observationRegistry.getIfUnique(() -> ObservationRegistry.NOOP)).customObservationConvention(customObservationConvention.getIfAvailable(() -> null)).batchingStrategy(chromaBatchingStrategy).build();}static class PropertiesChromaConnectionDetails implements ChromaConnectionDetails {private final ChromaApiProperties properties;PropertiesChromaConnectionDetails(ChromaApiProperties properties) {this.properties = properties;}@Overridepublic String getHost() {return this.properties.getHost();}@Overridepublic int getPort() {return this.properties.getPort();}@Overridepublic String getKeyToken() {return this.properties.getKeyToken();}}}

ChromaVectorStoreAutoConfiguration在spring.ai.vectorstore.typechroma时启用,它根据ChromaApiProperties创建ChromaApi,再根据ChromaVectorStoreProperties创建ChromaVectorStore

ChromaApiProperties

org/springframework/ai/vectorstore/chroma/autoconfigure/ChromaApiProperties.java

@ConfigurationProperties(ChromaApiProperties.CONFIG_PREFIX)
public class ChromaApiProperties {public static final String CONFIG_PREFIX = "spring.ai.vectorstore.chroma.client";private String host = "http://localhost";private int port = 8000;private String keyToken;private String username;private String password;//......
}	

ChromaApiProperties主要是配置spring.ai.vectorstore.chroma.client,它提供了host、port、keyToken、username、password这些属性

ChromaVectorStoreProperties

org/springframework/ai/vectorstore/chroma/autoconfigure/ChromaVectorStoreProperties.java

@ConfigurationProperties(ChromaVectorStoreProperties.CONFIG_PREFIX)
public class ChromaVectorStoreProperties extends CommonVectorStoreProperties {public static final String CONFIG_PREFIX = "spring.ai.vectorstore.chroma";private String collectionName = ChromaVectorStore.DEFAULT_COLLECTION_NAME;public String getCollectionName() {return this.collectionName;}public void setCollectionName(String collectionName) {this.collectionName = collectionName;}}

ChromaVectorStoreProperties提供了spring.ai.vectorstore.chroma,它从CommonVectorStoreProperties继承了initializeSchema属性,自己提供了collectionName属性

小结

Spring AI提供了spring-ai-starter-vector-store-chroma用于自动装配ChromaVectorStore。要注意的是embeddingDimension默认是1536,如果出现cannot unpack non-iterable coroutine object错误,记得把chroma版本降到0.6.2。

doc

  • vectordbs/chroma

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/76013.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

整数编码 - 华为OD统一考试(A卷、Java)

题目描述 实现一种整数编码方法,使得待编码的数字越小,编码后所占用的字节数越小。 编码规则如下: 编码时7位一组,每个字节的低7位用于存储待编码数字的补码。字节的最高位表示后续是否还有字节,置1表示后面还有更多的字节,置0表示当前字节为最后一个字节。采用小端序编…

Linux 递归查找并删除目录下的文件

在 Linux 中&#xff0c;可以使用 find 命令递归查找并删除目录下的文件 1、示例命令 find /path/to/directory -type f -name "filename_pattern" -exec rm -f {} 2、参数说明 /path/to/directory&#xff1a;要查找的目标目录type f&#xff1a;表示查找文件&am…

【笔记】VS中C#类库项目引用另一个类库项目的方法

VS中C#类库项目引用另一个类库项目的方法 在 C# 开发中&#xff0c;有时我们需要在一个类库项目中引用另一个类库项目&#xff0c;但另一个项目可能尚未编译成 DLL。在这种情况下&#xff0c;我们仍然可以通过 Visual Studio 提供的项目引用功能进行依赖管理。 &#x1f3af; …

第五讲(下)| string类的模拟实现

string类的模拟实现 一、Member constants&#xff08;成员常数&#xff09;npos 二、Member functions&#xff08;成员函数&#xff09;constructor&#xff08;构造&#xff09;、destructor&#xff08;析构&#xff09;、c_str遍历1 &#xff1a;Iterators遍历2&#xff1…

洛谷题单3-P4956 [COCI 2017 2018 #6] Davor-python-流程图重构

题目描述 在征服南极之后&#xff0c;Davor 开始了一项新的挑战。下一步是在西伯利亚、格林兰、挪威的北极圈远征。 他将在 2018 年 12 月 31 日开始出发&#xff0c;在这之前需要一共筹集 n 元钱。 他打算在每个星期一筹集 x 元&#xff0c;星期二筹集 xk 元&#xff0c;……

【正点原子】如何设置 ATK-DLMP135 开发板 eth0 的开机默认 IP 地址

开机就想让 eth0 乖乖用静态 IP&#xff1f;别再被 DHCP 抢走地址了&#xff01; 三步教你彻底掌控 ATK-DLMP135 的网络启动配置&#xff0c;简单粗暴&#xff0c;实测有效&#xff01; 正点原子STM32MP135开发板Linux核心板嵌入式ARM双千兆以太网CAN 1. 删除 dhcpcd 自动获取…

以UE5第三方插件库为基础,编写自己的第三方库插件,并且能够在运行时复制.dll

首先&#xff0c;创建一个空白的C 项目&#xff0c;创建第三方插件库。如下图所示 编译自己的.Dll 和.lib 库&#xff0c;打开.sln 如下图 ExampleLibrary.h 的代码如下 #if defined _WIN32 || defined _WIN64 #define EXAMPLELIBRARY_IMPORT __declspec(dllimport) #elif d…

正则表达式示例集合

目录&#xff1a; 1、精准匹配2、字符匹配3、参考示例3.1、一个合理的用户名正则表达式3.2、匹配 HTML 标签及内容3.3、其他示例3.4、微信号正则表达式3.5、QQ号正则表达式3.6、车牌号号正则表达式3.7、邮箱正则表达式 1、精准匹配 单字符模式&#xff0c;如 a&#xff0c;不论…

2025 年前端与后端开发方向的抉择与展望-优雅草卓伊凡

2025 年前端与后端开发方向的抉择与展望-优雅草卓伊凡 在 2025 年这个科技浪潮奔涌的时代&#xff0c;软件开发领域持续变革&#xff0c;前端与后端开发方向的抉择&#xff0c;成为众多从业者和爱好者亟待破解的关键命题。卓伊凡就频繁收到这样的疑问&#xff1a;“2025 年了&…

巧用数论与动态规划破解包子凑数问题

本文针对“包子凑数”问题&#xff0c;深入解析如何通过最大公约数&#xff08;GCD&#xff09;判断无法组成的数目是否无限&#xff0c;并结合动态规划高效求解有限情况下的具体数目。通过清晰的算法思路、代码实现及示例详解&#xff0c;揭秘数论与动态规划在组合问题中的巧妙…

什么是数据

一、数据的本质定义​​ ​​哲学视角​​ 亚里士多德《形而上学》中"未加工的观察记录"现代认知科学&#xff1a;人类感知系统接收的原始刺激信号&#xff08;如视网膜光信号、听觉神经电信号&#xff09;信息论奠基人香农&#xff1a;消除不确定性的度量载体 ​​…

FreeRTOS中互斥量实现数据共享优化

在 FreeRTOS 中&#xff0c;当读操作远多于写操作时&#xff0c;使用**互斥量&#xff08;Mutex&#xff09;会导致读任务频繁阻塞&#xff0c;降低系统性能。此时&#xff0c;可以通过实现读者-写者锁&#xff08;Reader-Writer Lock&#xff09;**优化&#xff0c;允许多个读…

国内虚拟电厂(VPP)管控平台供应商

以下是几家专注于虚拟电厂业务的供应商及其官网地址&#xff1a; 1. 华茂能联科技有限公司 官网地址&#xff1a;https://huamod.com/简介&#xff1a;华茂能联是分布式资源管理与虚拟电厂产品与服务提供商&#xff0c;团队汇聚了来自美国、欧洲和国内多个行业知名研究机构或…

协方差相关问题

为什么无偏估计用 ( n − 1 ) (n-1) (n−1) 而不是 n n n&#xff0c;区别是什么&#xff1f; 在统计学中&#xff0c;无偏估计是指估计量的期望值等于总体参数的真实值。当我们用样本数据估计总体方差或协方差时&#xff0c;分母使用 ( n − 1 ) (n-1) (n−1) 而不是 n n…

算法设计学习6

实验目的及要求&#xff1a; 目标是使学生学会分析数据对象的特点&#xff0c;掌握数据组织的方法和在计算机中的存储方式&#xff0c;能够对具体问题中所涉及的数据选择合适的逻辑结构、存储结构&#xff0c;进而在此基础上&#xff0c;对各种具体操作设计高效的算法&#xff…

Java 三大特性—多态

目录 1、多态的概念2、多态的条件3、向上转型3.1 概念3.2 使用场景 4、向下转型5、多态的优缺点 1、多态的概念 多态&#xff0c;通俗来讲就是多种形态&#xff0c;即对于同样的行为&#xff0c;不同的对象去完成会产生不同的状态。比如动物都会吃东西&#xff0c;小狗和小猫都…

Ubuntu 24.04 LTS系统安装RTX 4090显卡驱动和cuda并部署ollama下载DeepSeek模型【自用详细版】

自己捣鼓玩玩哈&#xff0c;正好有机子 1. 安装驱动前的系统配置工作 卸载原有驱动并禁用nouveau sudo apt remove --purge nvidia*sudo cp /etc/modprobe.d/blacklist.conf /etc/modprobe.d/blacklist.conf.backup //备份文件sudo vim /etc/modprobe.d/blacklist.conf //修…

【一篇搞定配置】一篇带你从配置到使用(PyCharm远程)完成服务器运行项目(配置、使用一条龙)【全网最详细版】

&#x1f308; 个人主页&#xff1a;十二月的猫-CSDN博客 &#x1f525; 系列专栏&#xff1a; &#x1f3c0;各种软件安装与配置_十二月的猫的博客-CSDN博客 &#x1f4aa;&#x1f3fb; 十二月的寒冬阻挡不了春天的脚步&#xff0c;十二点的黑夜遮蔽不住黎明的曙光 目录 1.…

Mamba模型

为什么要提出mamba模型&#xff1f; transformer特点&#xff1a;训练快&#xff0c;推理慢&#xff0c;计算成本O&#xff08;n*n&#xff09; Rnn的特点&#xff1a;训练慢&#xff0c;推理快&#xff0c;容易遗忘 其实很容易理解&#xff0c;因为RNN的输入只包含前一个隐…

如何在 Windows 11 上查找计算机的 IP 地址?

原文&#xff1a;如何在 Windows 11 上查找计算机的 IP 地址&#xff1f; | w3cschool笔记 在开始之前&#xff0c;我们先来了解一下什么是 IP 地址&#xff1a; 假设你住在一栋公寓楼里&#xff0c;快递员需要把包裹送到你家。为了确保快递能准确送到&#xff0c;你需要提供…