Azure AI Service

本节将指导您设置 AzureVectorStore 以存储文档嵌入并使用 Azure AI Search 服务执行相似性搜索。

Azure AI Search 是一个多功能的云托管云信息检索系统,是微软更大的 AI 平台的一部分。除了其他功能外,它允许用户使用基于向量的存储和检索查询信息。

前提条件

  1. Azure 订阅:您需要一个 Azure 订阅 来使用任何 Azure 服务。

  2. Azure AI Search 服务:创建一个 AI Search 服务。一旦服务创建完成,从 设置 下的 密钥 部分获取管理员 apiKey,并从 概述 部分的 URL 字段检索端点。

  3. (可选) Azure OpenAI 服务:创建一个 Azure OpenAI 服务注意: 您可能需要填写单独的表格以获取 Azure Open AI 服务的访问权限。一旦服务创建完成,从 资源管理 下的 密钥和端点 部分获取端点和 apiKey。

配置

在启动时,AzureVectorStore 将尝试在 AI Search 服务实例中创建新的索引。或者,您也可以手动创建索引。

要设置 AzureVectorStore,您将需要从上述前提条件中获取的设置以及您的索引名称:

  • Azure AI Search 端点

  • Azure AI Search 密钥

  • (可选) Azure OpenAI API 端点

  • (可选) Azure OpenAI API 密钥

您可以将这些值提供为操作系统环境变量。

export AZURE_AI_SEARCH_API_KEY=<我的 AI 搜索 API 密钥>
export AZURE_AI_SEARCH_ENDPOINT=<我的 AI 搜索索引>
export OPENAI_API_KEY=<我的 Azure AI API 密钥> (可选)

您可以使用任何支持嵌入接口的有效 OpenAI 实现替换 Azure Open AI 实现。例如,您可以使用 Spring AI 的 Open AI 或 TransformersEmbedding 实现来替代 Azure 实现。

依赖项

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

1. 选择嵌入接口的实现。您可以在以下选项之间进行选择:

  • OpenAI Embedding:

<dependency>
   <groupId>org.springframework.ai</groupId>
   <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
  • 或者 Azure AI Embedding:

<dependency>
 <groupId>org.springframework.ai</groupId>
 <artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
</dependency>
  • 或者 本地 Sentence Transformers 嵌入:

<dependency>
 <groupId>org.springframework.ai</groupId>
 <artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
</dependency>

2. Azure (AI Search) 向量存储

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

示例代码

要在应用程序中配置 Azure SearchIndexClient,您可以使用以下代码:

@Bean
public SearchIndexClient searchIndexClient() {
  return new SearchIndexClientBuilder().endpoint(System.getenv("AZURE_AI_SEARCH_ENDPOINT"))
    .credential(new AzureKeyCredential(System.getenv("AZURE_AI_SEARCH_API_KEY")))
    .buildClient();
}

要创建一个向量存储,您可以使用以下代码,将上面示例中创建的 SearchIndexClient bean 和 Spring AI 库提供的实现所需嵌入接口的 EmbeddingClient 注入:

@Bean
public VectorStore vectorStore(SearchIndexClient searchIndexClient, EmbeddingClient embeddingClient) {
  return new AzureVectorStore(searchIndexClient, embeddingClient,
    // 定义用于相似性搜索过滤器中的元数据字段。
    List.of(MetadataField.text("country"),
            MetadataField.int64("year"),
            MetadataField.bool("active")));
}

您必须明确列出过滤器表达式中使用的任何元数据键的所有元数据字段名称和类型。上面的列表注册了可过滤的元数据字段:country 类型为 TEXTyear 类型为 INT64active 类型为 BOOLEAN

如果可过滤的元数据字段扩展了新条目,则必须(重新)上传/更新带有此元数据的文档。

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

List<Document> documents = List.of(
	new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "BG", "year", 2020)),
	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("country", "NL", "year", 2023)));

将文档添加到您的向量存储中:

vectorStore.add(List.of(document));

最后,检索与查询相似的文档:

List<Document> results = vectorStore.similaritySearch(
    SearchRequest
      .query("Spring")
      .withTopK(5));

如果一切顺利,您应该检索到包含文本“Spring AI rocks!!”的文档。

元数据过滤

您也可以在 AzureVectorStore 中利用通用的便携式 元数据过滤器

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

vectorStore.similaritySearch(
   SearchRequest
      .query("The World")
      .withTopK(TOP_K)
      .withSimilarityThreshold(SIMILARITY_THRESHOLD)
      .withFilterExpression("country in ['英国', '荷兰'] && year >= 2020"));

或者以编程方式使用表达式 DSL:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(
    SearchRequest
      .query("The World")
      .withTopK(TOP_K)
      .withSimilarityThreshold(SIMILARITY_THRESHOLD)
      .withFilterExpression(b.and(
         b.in("country", "英国", "荷兰"),
         b.gte("year", 2020)).build()));

便携式的过滤表达式会自动转换为专有的 Azure Search OData 过滤器。例如,以下便携式过滤表达式:

country in ['英国', '荷兰'] && year >= 2020

会转换为以下 Azure OData 过滤器表达式

$filter search.in(meta_country, '英国,荷兰', ',') and meta_year ge 2020