聊聊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.pswp.cn/bicheng/76013.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/76013.shtml
英文地址,請注明出處:http://en.pswp.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;你需要提供…