Chroma
本節將引導您設定 Chroma VectorStore 來儲存文件嵌入並執行相似性搜尋。
Chroma 是一個開源的嵌入式資料庫。它為您提供了儲存文件嵌入、內容和元資料的工具,以及搜尋這些嵌入(包括元資料過濾)的功能。
前提條件
-
訪問 ChromaDB。附錄 本地設定 ChromaDB 展示瞭如何使用 Docker 容器在本地設定資料庫。
-
EmbeddingModel
例項用於計算文件嵌入。有多種選項可用-
如果需要,為 EmbeddingModel 提供一個 API 金鑰,用於生成由
ChromaVectorStore
儲存的嵌入。
-
啟動時,如果 ChromaVectorStore
所需的集合尚未提供,它將建立該集合。
自動配置
Spring AI 自動配置、starter 模組的 artifact 名稱發生了重大變化。更多資訊請參考升級說明。 |
Spring AI 為 Chroma Vector Store 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到專案的 Maven pom.xml
檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-chroma</artifactId>
</dependency>
或者新增到您的 Gradle build.gradle
構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-chroma'
}
請參閱依賴管理部分,瞭解如何將 Spring AI BOM 新增到您的構建檔案中。 |
請參閱倉庫部分,瞭解如何將 Maven Central 和/或 Snapshot Repositories 新增到您的構建檔案中。 |
向量儲存實現可以為您初始化所需的 schema,但您必須透過在相應的建構函式中指定 initializeSchema
布林值或在 application.properties
檔案中設定 …initialize-schema=true
來選擇啟用此功能。
這是破壞性變更!在早期版本的 Spring AI 中,此 schema 初始化是預設發生的。 |
此外,您還需要一個已配置的 EmbeddingModel
bean。更多資訊請參考EmbeddingModel部分。
這裡是所需 bean 的示例
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other EmbeddingModel implementation.
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
}
要連線到 Chroma,您需要提供例項的訪問詳細資訊。可以透過 Spring Boot 的 application.properties 提供簡單的配置,
# Chroma Vector Store connection properties
spring.ai.vectorstore.chroma.client.host=<your Chroma instance host>
spring.ai.vectorstore.chroma.client.port=<your Chroma instance port>
spring.ai.vectorstore.chroma.client.key-token=<your access token (if configure)>
spring.ai.vectorstore.chroma.client.username=<your username (if configure)>
spring.ai.vectorstore.chroma.client.password=<your password (if configure)>
# Chroma Vector Store collection properties
spring.ai.vectorstore.chroma.initialize-schema=<true or false>
spring.ai.vectorstore.chroma.collection-name=<your collection name>
# Chroma Vector Store configuration properties
# OpenAI API key if the OpenAI auto-configuration is used.
spring.ai.openai.api.key=<OpenAI Api-key>
請查看向量儲存的配置引數列表,瞭解預設值和配置選項。
現在您可以在應用程式中自動裝配(auto-wire)Chroma Vector Store 並使用它
@Autowired VectorStore vectorStore;
// ...
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
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置屬性
您可以在 Spring Boot 配置中使用以下屬性來自定義向量儲存。
屬性 | 描述 | 預設值 |
---|---|---|
|
伺服器連線主機 |
|
|
伺服器連線埠 |
|
|
訪問令牌(如果已配置) |
- |
|
訪問使用者名稱(如果已配置) |
- |
|
訪問密碼(如果已配置) |
- |
|
集合名稱 |
|
|
是否初始化所需的 schema |
|
對於使用靜態 API Token 認證進行安全加固的 ChromaDB,使用 對於使用基本認證進行安全加固的 ChromaDB,使用 |
元資料過濾
您也可以在 ChromaVector store 中利用通用、可移植的元資料過濾器。
例如,您可以使用文字表示式語言
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或透過程式設計方式使用 Filter.Expression
DSL
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression(b.and(
b.in("john", "jill"),
b.eq("article_type", "blog")).build()).build());
這些(可移植的)過濾器表示式會自動轉換為專有的 Chroma where 過濾表示式。 |
例如,這個可移植的過濾表示式
author in ['john', 'jill'] && article_type == 'blog'
被轉換為專有的 Chroma 格式
{"$and":[
{"author": {"$in": ["john", "jill"]}},
{"article_type":{"$eq":"blog"}}]
}
手動配置
如果您更喜歡手動配置 Chroma Vector Store,可以在您的 Spring Boot 應用程式中建立一個 ChromaVectorStore
bean。
將這些依賴項新增到您的專案中:* Chroma VectorStore。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-chroma-store</artifactId>
</dependency>
-
OpenAI:計算嵌入所需。您可以使用任何其他嵌入模型實現。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
請參閱依賴管理部分,瞭解如何將 Spring AI BOM 新增到您的構建檔案中。 |
示例程式碼
建立一個帶有適當 ChromaDB 授權配置的 RestClient.Builder
例項,並使用它來建立一個 ChromaApi
例項
@Bean
public RestClient.Builder builder() {
return RestClient.builder().requestFactory(new SimpleClientHttpRequestFactory());
}
@Bean
public ChromaApi chromaApi(RestClient.Builder restClientBuilder) {
String chromaUrl = "https://:8000";
ChromaApi chromaApi = new ChromaApi(chromaUrl, restClientBuilder);
return chromaApi;
}
透過將 Spring Boot OpenAI starter 新增到您的專案中,與 OpenAI 的嵌入整合。這為您提供了一個 Embeddings 客戶端的實現。
@Bean
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
return ChromaVectorStore.builder(chromaApi, embeddingModel)
.collectionName("TestCollection")
.initializeSchema(true)
.build();
}
在您的主程式碼中,建立一些文件
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")));
將文件新增到您的向量儲存中
vectorStore.add(documents);
最後,檢索與查詢相似的文件
List<Document> results = vectorStore.similaritySearch("Spring");
如果一切順利,您應該會檢索到包含文字 "Spring AI rocks!!" 的文件。
在本地執行 Chroma
docker run -it --rm --name chroma -p 8000:8000 ghcr.io/chroma-core/chroma:0.5.20
在 localhost:8000/api/v1 啟動 Chroma store