Azure AI 服務

本節將引導您設定 AzureVectorStore,以使用 Azure AI 搜尋服務儲存文件嵌入向量並執行相似性搜尋。

Azure AI 搜尋是一個多功能的雲端託管資訊檢索系統,是 Microsoft 更大的 AI 平台的一部分。 除此之外,它還允許使用者使用基於向量的儲存和檢索來查詢資訊。

先決條件

  1. Azure 訂用帳戶:您需要一個 Azure 訂用帳戶才能使用任何 Azure 服務。

  2. Azure AI 搜尋服務:建立一個 AI 搜尋服務。 服務建立完成後,從設定下的金鑰區段取得管理員 apiKey,並從概觀區段下的Url欄位檢索端點。

  3. (選用)Azure OpenAI 服務:建立一個 Azure OpenAI 服務注意: 您可能需要填寫一份單獨的表格才能取得 Azure Open AI 服務的存取權限。 服務建立完成後,從資源管理下的金鑰和端點區段取得端點和 apiKey。

組態

在啟動時,如果您已選擇透過在建構函式中將相關的 initialize-schema boolean 屬性設定為 true,或者如果使用 Spring Boot,則在您的 application.properties 檔案中設定 …​initialize-schema=trueAzureVectorStore 可以嘗試在您的 AI 搜尋服務實例中建立新的索引。

這是一個重大變更! 在 Spring AI 的早期版本中,此結構描述初始化是預設發生的。

或者,您可以手動建立索引。

若要設定 AzureVectorStore,您將需要從上述先決條件中檢索的設定以及您的索引名稱

  • Azure AI 搜尋端點

  • Azure AI 搜尋金鑰

  • (選用)Azure OpenAI API 端點

  • (選用)Azure OpenAI API 金鑰

您可以將這些值作為作業系統環境變數提供。

export AZURE_AI_SEARCH_API_KEY=<My AI Search API Key>
export AZURE_AI_SEARCH_ENDPOINT=<My AI Search Index>
export OPENAI_API_KEY=<My Azure AI API Key> (Optional)

您可以將 Azure Open AI 實作替換為任何支援 Embeddings 介面的有效 OpenAI 實作。 例如,您可以使用 Spring AI 的 Open AI 或 TransformersEmbedding 實作來進行嵌入,而不是 Azure 實作。

相依性

將這些相依性新增至您的專案

1. 選擇一個 Embeddings 介面實作。 您可以從以下選項中選擇

  • OpenAI 嵌入

  • Azure AI 嵌入

  • 本地句子轉換器嵌入

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

2. Azure (AI 搜尋) 向量儲存區

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-azure-store</artifactId>
</dependency>
請參閱相依性管理章節,將 Spring AI BOM 新增至您的建置檔案。

組態屬性

您可以使用 Spring Boot 組態中的以下屬性來自訂 Azure 向量儲存區。

屬性 預設值

spring.ai.vectorstore.azure.url

spring.ai.vectorstore.azure.api-key

spring.ai.vectorstore.azure.initialize-schema

false

spring.ai.vectorstore.azure.index-name

spring_ai_azure_vector_store

spring.ai.vectorstore.azure.default-top-k

4

spring.ai.vectorstore.azure.default-similarity-threshold

0.0

spring.ai.vectorstore.azure.embedding-property

embedding

spring.ai.vectorstore.azure.index-name

spring-ai-document-index

範例程式碼

若要在您的應用程式中組態 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 程式庫提供的 EmbeddingModel,該程式庫實作了所需的 Embeddings 介面。

@Bean
public VectorStore vectorStore(SearchIndexClient searchIndexClient, EmbeddingModel embeddingModel) {
  return new AzureVectorStore(searchIndexClient, embeddingModel,
    // Define the metadata fields to be used
    // in the similarity search filters.
    List.of(MetadataField.text("country"),
            MetadataField.int64("year"),
            MetadataField.bool("active")));
}

您必須明確列出篩選運算式中使用的任何中繼資料金鑰的所有中繼資料欄位名稱和類型。 上面的列表註冊了可篩選的中繼資料欄位:類型為 TEXTcountry、類型為 INT64year 和類型為 BOOLEANactive

如果可篩選的中繼資料欄位已使用新條目擴充,您必須 (重新) 上傳/更新具有此中繼資料的文件。

在您的主要程式碼中,建立一些文件

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(documents);

最後,檢索與查詢相似的文件

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 ['UK', 'NL'] && 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", "UK", "NL"),
         b.gte("year", 2020)).build()));

可移植的篩選運算式會自動轉換為專有的 Azure 搜尋 OData 篩選器。 例如,以下可移植的篩選運算式

country in ['UK', 'NL'] && year >= 2020

會轉換為以下 Azure OData 篩選運算式

$filter search.in(meta_country, 'UK,NL', ',') and meta_year ge 2020