|
此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3! |
Chroma
本节将指导您设置Chroma VectorStore,用于存储文档嵌入并进行相似性搜索。
Chroma是开源的嵌入式数据库。它让你具备存储文档嵌入、内容和元数据,并通过这些嵌入式内容进行搜索,包括元数据过滤。
前提条件
-
访问ChromaDB。与Chroma云兼容,或在附录中显示如何在Docker容器中本地设置DB。
-
对于Chroma云:您需要从Chroma云管理界面获取您的API密钥、租户名称和数据库名称。
-
在本地ChromaDB环境中:启动容器即可,无需额外配置。
-
-
0个实例用于计算文档嵌入。几种选项都可用:
-
如果需要,获取用于在存储于
ChromaVectorStore中的嵌入模型的API密钥。
-
在启动时,ChromaVectorStore会创建所需的集合,如果未预先配置,则会自动创建。
自动配置
|
There has been a significant change in the Spring AI auto-configuration, starter modules' artifact names. Please refer to the 升级说明以获取更多信息。 |
Spring AI提供Spring Boot自动生成Chroma向量存储。
To启用它,请在您的项目Maven pom.xml文件中添加以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-chroma</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-chroma'
}
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
| 引用 Artifact 仓库 部分,以在构建文件中添加 Maven Central 和/或 Snapshot 仓库。 |
The vector store implementation can initialize the requisite schema for you, but you must opt-in by specifying the initializeSchema boolean in the appropriate constructor or by setting …initialize-schema=true in the application.properties file.
| 这是一个破坏性变更!在早期版本的 Spring AI 中,此模式初始化是默认进行的。 |
此外,您还需要配置一个EmbeddingModel的豆。有关详细信息,请参阅
以下是所需Bean的一个示例:
@Bean
public EmbeddingModel embeddingModel() {
// Can be any other EmbeddingModel implementation.
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(System.getenv("OPENAI_API_KEY")).build());
}
连接到Chroma,你需要提供对您的实例的访问详细信息。 一个简单的配置可以通过Spring Boot的application.properties来完成,
# Chroma Vector Store connection properties
spring.ai.vectorstore.chroma.client.host=<your Chroma instance host> // for Chroma Cloud: api.trychroma.com
spring.ai.vectorstore.chroma.client.port=<your Chroma instance port> // for Chroma Cloud: 443
spring.ai.vectorstore.chroma.client.key-token=<your access token (if configure)> // for Chroma Cloud: use the API key
spring.ai.vectorstore.chroma.client.username=<your username (if configure)>
spring.ai.vectorstore.chroma.client.password=<your password (if configure)>
# Chroma Vector Store tenant and database properties (required for Chroma Cloud)
spring.ai.vectorstore.chroma.tenant-name=<your tenant name> // default: SpringAiTenant
spring.ai.vectorstore.chroma.database-name=<your database name> // default: SpringAiDatabase
# 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>
请查看以下向量存储的配置参数列表,了解默认值和配置选项。这些配置参数的类型为C0。
现在,您可以在应用中自动注入Chroma向量存储并使用它。
@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配置中来自定义向量存储。
| 属性 | 描述 | 默认值 |
|---|---|---|
|
服务器连接地址 |
|
|
服务器连接端口 |
|
|
如果配置了的话 |
- |
|
如果已配置,请获取用户的用户名。 |
- |
|
访问密码(如果已配置) |
- |
|
租户(适用于Chroma Cloud) |
|
|
数据库名称(用于 Chroma Cloud 必要) |
|
|
集合名 |
|
|
是否需要初始化所需的schema(如果它们不存在,则创建Tenant/Database/Collection) |
|
|
For ChromaDB, secured with Static API Token Authentication, use the 对于通过 |
Chroma云配置
对于Chroma Cloud,你需要从Chroma Cloud实例中提供Tenant和Database的名称。以下是示例配置:
# Chroma Cloud connection
spring.ai.vectorstore.chroma.client.host=api.trychroma.com
spring.ai.vectorstore.chroma.client.port=443
spring.ai.vectorstore.chroma.client.key-token=<your-chroma-cloud-api-key>
# Chroma Cloud tenant and database (required)
spring.ai.vectorstore.chroma.tenant-name=<your-tenant-id>
spring.ai.vectorstore.chroma.database-name=<your-database-name>
# Collection configuration
spring.ai.vectorstore.chroma.collection-name=my-collection
spring.ai.vectorstore.chroma.initialize-schema=true
|
"For Chroma Cloud:\
- 主机应设置为 |
元数据过滤
你可以利用ChromaVector存储的通用且可移植的元数据过滤器。
例如,你可以使用文本表达式语言:
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 0号过滤表达式。 |
例如,这个可移植的过滤表达式:
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>
-
用于计算嵌入所需。你可以使用任何其他嵌入模型实现。
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency>
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
示例代码
Create a RestClient.Builder instance with proper ChromaDB authorization configurations and Use it to create a ChromaApi instance:
@Bean
public RestClient.Builder builder() {
return RestClient.builder().requestFactory(new SimpleClientHttpRequestFactory());
}
@Bean
public ChromaApi chromaApi(RestClient.Builder restClientBuilder) {
String chromaUrl = "http://localhost:8000";
ChromaApi chromaApi = new ChromaApi(chromaUrl, restClientBuilder);
return chromaApi;
}
集成到 OpenAI 的嵌入式中,通过在项目中添加 Spring Boot OpenAI 开发板。这将为您提供一个实现嵌入式客户端的实现:
@Bean
public VectorStore chromaVectorStore(EmbeddingModel embeddingModel, ChromaApi chromaApi) {
return ChromaVectorStore.builder(chromaApi, embeddingModel)
.tenantName("your-tenant-name") // default: SpringAiTenant
.databaseName("your-database-name") // default: SpringAiDatabase
.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:1.0.0
starts a chroma store at localhost:8000/api/v1