Chroma

这一部分将指导您设置Chroma VectorStore以存储文档嵌入并执行相似性搜索。

Chroma是什么?

Chroma是开源的嵌入数据库。它为您提供了存储文档嵌入、内容和元数据以及通过这些嵌入进行搜索的工具,包括元数据过滤。

前提条件

  1. OpenAI 账户:在OpenAI 注册并在API 密钥生成令牌。

  2. 访问 ChromeDB。附录设置本地 ChromaDB显示如何使用 Docker 容器在本地设置数据库。

在启动时,如果尚未配置,ChromaVectorStore会创建所需的集合。

配置

要设置ChromaVectorStore,您需要提供您的 OpenAI API 密钥。将其设置为环境变量,如下所示:

export SPRING_AI_OPENAI_API_KEY='您的_OpenAI_API_密钥'

依赖关系

将以下依赖项添加到您的项目中:

  • OpenAI:用于计算嵌入。

<dependency>
 <groupId>org.springframework.ai</groupId>
 <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
  • Chroma VectorStore。

<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-chroma-store</artifactId>
</dependency>
参考依赖管理部分,将 Spring AI BOM 添加到您的构建文件。

示例代码

创建一个带有正确ChromaDB授权配置的RestTemplate实例,并使用它创建一个ChromaApi实例:

@Bean
public RestTemplate restTemplate() {
   return new RestTemplate();
}

@Bean
public ChromaApi chromaApi(RestTemplate restTemplate) {
   String chromaUrl = "http://localhost:8000";
   ChromaApi chromaApi = new ChromaApi(chromaUrl, restTemplate);
   return chromaApi;
}

对于使用静态API令牌身份验证的ChromaDB,请使用ChromaApi#withKeyToken(<您的令牌凭据>)方法设置您的凭据。查看ChromaWhereIT以获取示例。

对于使用基本身份验证的ChromaDB,请使用ChromaApi#withBasicAuth(<您的用户>, <您的密码>)方法设置您的凭据。查看BasicAuthChromaWhereIT以获取示例。

通过将Spring Boot OpenAI starter集成到您的项目中,与OpenAI的嵌入进行集成。这为您提供了一个嵌入客户端的实现:

@Bean
public VectorStore chromaVectorStore(EmbeddingClient embeddingClient, ChromaApi chromaApi) {
 return new ChromaVectorStore(embeddingClient, chromaApi, "TestCollection");
}

在您的主要代码中,创建一些文档:

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!!”的文档。

元数据过滤

您还可以在ChromaVector存储中使用通用、可移植的元数据过滤器

例如,您可以使用文本表达式语言:

vectorStore.similaritySearch(
                    SearchRequest.defaults()
                            .withQuery("The World")
                            .withTopK(TOP_K)
                            .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                            .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

或者通过 Filter.Expression DSL 进行编程:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.defaults()
                    .withQuery("The World")
                    .withTopK(TOP_K)
                    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
                    .withFilterExpression(b.and(
                            b.in("john", "jill"),
                            b.eq("article_type", "blog")).build()));
这些(可移植)过滤表达式会自动转换为专有的 Chroma where 过滤表达式

例如,这个可移植的过滤表达式:

author in ['john', 'jill'] && article_type == 'blog'

被转换为专有的 Chroma 格式

{"$and":[
	{"author": {"$in": ["john", "jill"]}},
	{"article_type":{"$eq":"blog"}}]
}

在本地运行 Chroma

docker run -it --rm --name chroma -p 8000:8000 ghcr.io/chroma-core/chroma:0.4.15

localhost:8000/api/v1 启动 Chroma 存储