Chroma
本节将指导您如何设置 Chroma VectorStore 来存储文档嵌入并执行相似性搜索。
Chroma 是开源嵌入数据库。它为您提供了存储文档嵌入、内容和元数据的工具,以及搜索这些嵌入(包括元数据过滤)的功能。
先决条件
-
访问 ChromeDB。设置本地 ChromaDB 附录显示了如何使用 Docker 容器在本地设置数据库。
-
EmbeddingModel
实例用于计算文档嵌入。有多个选项可用:-
如果需要,EmbeddingModel 的 API 密钥用于生成由
ChromaVectorStore
存储的嵌入。
-
在启动时,如果尚未配置,ChromaVectorStore
会创建所需的集合。
自动配置
Spring AI 自动配置、starter 模块的构件名称发生了重大变化。 请参阅 升级说明 了解更多信息。 |
Spring AI 为 Chroma 向量存储提供 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 仓库添加到您的构建文件中。
向量存储实现可以为您初始化所需的模式,但您必须通过在适当的构造函数中指定 initializeSchema
布尔值或在 application.properties
文件中设置 …initialize-schema=true
来选择加入。
注意:这是一个重大变更!在 Spring AI 的早期版本中,这种模式初始化是默认发生的。
此外,您还需要一个配置好的 EmbeddingModel
bean。有关更多信息,请参阅 EmbeddingModel 部分。
以下是所需 bean 的示例:
@Bean
public EmbeddingModel embeddingModel() {
// 可以是任何其他 EmbeddingModel 实现。
return new OpenAiEmbeddingModel(OpenAiApi.builder().apiKey(System.getenv("OPENAI_API_KEY")).build());
}
要连接到 Chroma,您需要提供实例的访问详情。 可以通过 Spring Boot 的 application.properties 提供简单配置,
# Chroma 向量存储连接属性
spring.ai.vectorstore.chroma.client.host=<您的 Chroma 实例主机>
spring.ai.vectorstore.chroma.client.port=<您的 Chroma 实例端口>
spring.ai.vectorstore.chroma.client.key-token=<您的访问令牌(如果配置)>
spring.ai.vectorstore.chroma.client.username=<您的用户名(如果配置)>
spring.ai.vectorstore.chroma.client.password=<您的密码(如果配置)>
# Chroma 向量存储集合属性
spring.ai.vectorstore.chroma.initialize-schema=<true 或 false>
spring.ai.vectorstore.chroma.collection-name=<您的集合名称>
# Chroma 向量存储配置属性
# 如果使用 OpenAI 自动配置,则为 OpenAI API 密钥。
spring.ai.openai.api.key=<OpenAI Api-key>
请查看向量存储的 配置参数 列表,了解默认值和配置选项。
现在您可以在应用程序中自动装配 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")));
// 添加文档
vectorStore.add(documents);
// 检索与查询相似的文档
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
您可以在 Spring Boot 配置中使用以下属性来自定义向量存储。
属性 | 描述 | 默认值 |
---|---|---|
|
服务器连接主机 |
|
|
服务器连接端口 |
|
|
访问令牌(如果配置) |
- |
|
访问用户名(如果配置) |
- |
|
访问密码(如果配置) |
- |
|
集合名称 |
|
|
是否初始化所需的模式 |
|
对于使用 静态 API 令牌身份验证 保护的 ChromaDB,使用 对于使用 基本身份验证 保护的 ChromaDB,使用 |
元数据过滤
您也可以利用 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 where
过滤器表达式。
例如,这个可移植过滤器表达式:
author in ['john', 'jill'] && article_type == 'blog'
被转换为专有的 Chroma 格式
{"$and":[
{"author": {"$in": ["john", "jill"]}},
{"article_type":{"$eq":"blog"}}]
}
手动配置
如果您更喜欢手动配置 Chroma 向量存储,可以通过在 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 = "http://localhost: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:1.0.0
在 localhost:8000/api/v1 启动一个 chroma 存储