Azure Cosmos DB

本节将指导您如何设置 CosmosDBVectorStore 来存储文档嵌入并执行相似性搜索。

什么是 Azure Cosmos DB?

Azure Cosmos DB 是微软的全球分布式云原生数据库服务,专为关键任务应用程序设计。 它提供高可用性、低延迟,并且能够水平扩展以满足现代应用程序的需求。 它是从零开始构建的,以全球分布、细粒度多租户和水平可扩展性为核心。 它是 Azure 中的基础服务,被微软大多数关键任务应用程序在全球范围内使用,包括 Teams、Skype、Xbox Live、Office 365、Bing、Azure Active Directory、Azure Portal、Microsoft Store 等。 它还被数千个外部客户使用,包括 OpenAI 的 ChatGPT 和其他需要弹性扩展、即用型全球分布以及跨地球的低延迟和高可用性的关键任务 AI 应用程序。

什么是 DiskANN?

DiskANN(基于磁盘的近似最近邻搜索)是 Azure Cosmos DB 中用于增强向量搜索性能的创新技术。 它通过索引存储在 Cosmos DB 中的嵌入,实现对高维数据的高效和可扩展的相似性搜索。

DiskANN 提供以下优势:

  • 效率:通过利用基于磁盘的结构,DiskANN 显著减少了与传统方法相比查找最近邻所需的时间。

  • 可扩展性:它可以处理超出内存容量的大型数据集,使其适用于各种应用程序,包括机器学习和 AI 驱动的解决方案。

  • 低延迟:DiskANN 最小化搜索操作期间的延迟,确保应用程序即使在处理大量数据时也能快速检索结果。

在 Spring AI 的 Azure Cosmos DB 上下文中,向量搜索将创建并利用 DiskANN 索引,以确保相似性查询的最佳性能。

使用自动配置设置 Azure Cosmos DB 向量存储

以下代码演示如何使用自动配置设置 CosmosDBVectorStore

import io.micrometer.observation.ObservationRegistry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.document.Document;
import org.springframework.ai.vectorstore.SearchRequest;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@SpringBootApplication
@EnableAutoConfiguration
public class DemoApplication implements CommandLineRunner {

    private static final Logger log = LoggerFactory.getLogger(DemoApplication.class);

    @Lazy
    @Autowired
    private VectorStore vectorStore;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        Document document1 = new Document(UUID.randomUUID().toString(), "Sample content1", Map.of("key1", "value1"));
        Document document2 = new Document(UUID.randomUUID().toString(), "Sample content2", Map.of("key2", "value2"));
		this.vectorStore.add(List.of(document1, document2));
        List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Sample content").topK(1).build());

        log.info("Search results: {}", results);

        // 从向量存储中删除文档
		this.vectorStore.delete(List.of(document1.getId(), document2.getId()));
    }

    @Bean
    public ObservationRegistry observationRegistry() {
        return ObservationRegistry.create();
    }
}

自动配置

Spring AI 自动配置、starter 模块的构件名称发生了重大变化。 请参阅 升级说明 了解更多信息。

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

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-vector-store-azure-cosmos-db</artifactId>
</dependency>

配置属性

以下配置属性可用于 Cosmos DB 向量存储:

属性 描述

spring.ai.vectorstore.cosmosdb.databaseName

要使用的 Cosmos DB 数据库名称。

spring.ai.vectorstore.cosmosdb.containerName

要使用的 Cosmos DB 容器名称。

spring.ai.vectorstore.cosmosdb.partitionKeyPath

分区键的路径。

spring.ai.vectorstore.cosmosdb.metadataFields

元数据字段的逗号分隔列表。

spring.ai.vectorstore.cosmosdb.vectorStoreThroughput

向量存储的吞吐量。

spring.ai.vectorstore.cosmosdb.vectorDimensions

向量的维度数。

spring.ai.vectorstore.cosmosdb.endpoint

Cosmos DB 的端点。

spring.ai.vectorstore.cosmosdb.key

Cosmos DB 的密钥(如果未提供密钥,将使用 [DefaultAzureCredential](learn.microsoft.com/azure/developer/java/sdk/authentication/credential-chains#defaultazurecredential-overview))。

使用过滤器的复杂搜索

您可以在 Cosmos DB 向量存储中使用过滤器执行更复杂的搜索。 以下是演示如何在搜索查询中使用过滤器的示例。

Map<String, Object> metadata1 = new HashMap<>();
metadata1.put("country", "UK");
metadata1.put("year", 2021);
metadata1.put("city", "London");

Map<String, Object> metadata2 = new HashMap<>();
metadata2.put("country", "NL");
metadata2.put("year", 2022);
metadata2.put("city", "Amsterdam");

Document document1 = new Document("1", "A document about the UK", this.metadata1);
Document document2 = new Document("2", "A document about the Netherlands", this.metadata2);

vectorStore.add(List.of(document1, document2));

FilterExpressionBuilder builder = new FilterExpressionBuilder();
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("The World")
    .topK(10)
    .filterExpression((this.builder.in("country", "UK", "NL")).build()).build());

不使用自动配置设置 Azure Cosmos DB 向量存储

以下代码演示如何在不依赖自动配置的情况下设置 CosmosDBVectorStore。建议使用 [DefaultAzureCredential](learn.microsoft.com/azure/developer/java/sdk/authentication/credential-chains#defaultazurecredential-overview) 进行 Azure Cosmos DB 的身份验证。

@Bean
public VectorStore vectorStore(ObservationRegistry observationRegistry) {
    // 创建 Cosmos DB 客户端
    CosmosAsyncClient cosmosClient = new CosmosClientBuilder()
            .endpoint(System.getenv("COSMOSDB_AI_ENDPOINT"))
            .credential(new DefaultAzureCredentialBuilder().build())
            .userAgentSuffix("SpringAI-CDBNoSQL-VectorStore")
            .gatewayMode()
            .buildAsyncClient();

    // 创建并配置向量存储
    return CosmosDBVectorStore.builder(cosmosClient, embeddingModel)
            .databaseName("test-database")
            .containerName("test-container")
            // 配置用于过滤的元数据字段
            .metadataFields(List.of("country", "year", "city"))
            // 设置分区键路径(可选)
            .partitionKeyPath("/id")
            // 配置性能设置
            .vectorStoreThroughput(1000)
            .vectorDimensions(1536)  // 匹配您的嵌入模型的维度
            // 添加自定义批处理策略(可选)
            .batchingStrategy(new TokenCountBatchingStrategy())
            // 添加用于指标的观察注册表
            .observationRegistry(observationRegistry)
            .build();
}

@Bean
public EmbeddingModel embeddingModel() {
    return new TransformersEmbeddingModel();
}

此配置显示了所有可用的构建器选项:

  • databaseName:您的 Cosmos DB 数据库名称

  • containerName:数据库中的容器名称

  • partitionKeyPath:分区键的路径(例如,"/id")

  • metadataFields:将用于过滤的元数据字段列表

  • vectorStoreThroughput:向量存储容器的吞吐量(RU/s)

  • vectorDimensions:向量的维度数(应与您的嵌入模型匹配)

  • batchingStrategy:文档操作的批处理策略(可选)

手动依赖设置

在您的 Maven 项目中添加以下依赖项:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-cosmos-db-store</artifactId>
</dependency>

访问原生客户端

Azure Cosmos DB 向量存储实现通过 getNativeClient() 方法提供对底层原生 Azure Cosmos DB 客户端(CosmosClient)的访问:

CosmosDBVectorStore vectorStore = context.getBean(CosmosDBVectorStore.class);
Optional<CosmosClient> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    CosmosClient client = nativeClient.get();
    // 使用原生客户端进行 Azure Cosmos DB 特定操作
}

原生客户端让您可以访问可能未通过 VectorStore 接口公开的 Azure Cosmos DB 特定功能和操作。