|
最新快照版本请使用Spring AI 1.1.0! |
Chroma
本节将带你了解如何设置Chroma VectorStore,以存储文档嵌入并进行相似性搜索。
Chroma 是开源的嵌入数据库。它为你提供了存储文档嵌入、内容和元数据的工具,并能搜索这些嵌入,包括元数据过滤。
前提条件
-
访问ChromaDB。兼容Chroma Cloud,或者在附录中设置本地ChromaDB,说明如何用Docker容器本地搭建数据库。
-
对于Chroma Cloud:你需要从Chroma Cloud仪表盘获取API密钥、租户名和数据库名。
-
对于本地 ChromaDB:除了启动容器外,无需额外配置。
-
-
嵌入模型实例用于计算文档嵌入。有几种选择:-
如有需要,需为 EmbeddingModel 提供 API 密钥,以生成存储于
色矢量存储.
-
启动时,色矢量存储如果尚未配置,则创建所需的集合。
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 Chroma 向量商店提供 Spring Boot 自动配置。
要启用它,请在项目的 Maven 中添加以下依赖pom.xml文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-chroma</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-chroma'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
| 请参阅“遗物仓库”部分,将Maven Central和/或快照仓库添加到你的构建文件中。 |
向量存储实现可以帮你初始化所需的模式,但你必须通过指定初始化模式在适当的构造函数中进行布尔值,或通过设置…initialize-schema=true在application.properties文件。
| 这是一个颠覆性的变革!在早期版本的 Spring AI 中,这种模式初始化是默认的。 |
此外,你还需要一个配置嵌入模型豆。更多信息请参阅嵌入模型部分。
这里是所需豆子的一个例子:
@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>
请查看矢量存储的配置参数列表,了解默认值和配置选项。
现在你可以在应用中自动接线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 配置中使用以下属性来自定义向量存储。
| 属性 | 描述 | 默认值 |
|---|---|---|
|
服务器连接主机 |
|
|
服务器连接端口 |
|
|
访问Tokens(如果配置过) |
- |
|
访问用户名(如果已配置) |
- |
|
访问密码(如果已配置) |
- |
|
租户(Chroma Cloud必备) |
|
|
数据库名称(Chroma Cloud 需要) |
|
|
收藏名称 |
|
|
是否初始化所需的模式(如果不存在,创建租户/数据库/集合) |
|
|
对于采用静态APITokens认证保护的ChromaDB,请使用 对于使用 Basic 认证保护的 ChromaDB,可以使用 |
Chroma Cloud 配置
对于Chroma Cloud,你需要提供Chroma Cloud实例中的租户名和数据库名称。这里有一个示例配置:
# 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
|
关于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());
或者程序化地使用滤波。表达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哪里 过滤表达。 |
例如,这个可移植的Filter表达式:
author in ['john', 'jill'] && article_type == 'blog'
转换为专有的Chroma格式
{"$and":[
{"author": {"$in": ["john", "jill"]}},
{"article_type":{"$eq":"blog"}}]
}
手动配置
如果你更喜欢手动配置Chroma矢量存储,可以通过创建一个色矢量存储在你的Spring Boot应用中加入豆子。
将以下依赖项添加到你的项目中: * Chroma矢量商店。
<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的物料清单添加到你的构建文件中。 |
示例代码
创建一个RestClient.Builder实例中带有适当的ChromaDB授权配置,并用它来创建色素API实例:
@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;
}
通过将 Spring Boot OpenAI 启动程序添加到你的项目中,集成 OpenAI 的嵌入。这为你提供了 Embeddings 客户端的实现:
@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");
如果一切顺利,你应该取回包含“春季AI超棒!!”文字的文档。
本地运行Chroma
docker run -it --rm --name chroma -p 8000:8000 ghcr.io/chroma-core/chroma:1.0.0
在localhost:8000/api/v1启动chroma存储