第十章 Spring AI API中文版 - Chroma Vector Databases

Spring AI > Spring AI API > Vector Databases

Chroma

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

Chroma容器

Chroma是什么?

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

先决条件

OpenAI账户:在OpenAI注册并生成API密钥

访问ChromeDB。附录部分展示了如何使用Docker容器在本地设置DB。

启动时,ChromaVectorStore会创建所需的集合,如果尚未提供。

配置

要设置ChromaVectorStore,您需要提供您的OpenAI API密钥。像这样将其设置为环境变量:

export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key'

依赖项

将这些依赖项添加到您的项目中:

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 store一起使用。

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

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

http://localhost:8000/api/v1启动一个chroma store

  • 22
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明爷们儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值