在 Azure AI 搜尋服務中使用答案合成,產生具有引文支援的回應 (預覽版)

註

Azure AI 搜尋服務 可透過 Azure 入口網站、REST API 及 Azure SDK 取得。 它同時也是 Foundry IQ 的基礎,這是一個管理式知識層,能將企業內容轉化為可重複使用、權限感知的知識庫,供 Microsoft Foundry 入口網站中的代理使用。

重要

標記(預覽)的功能、能力或屬性不受服務等級協議涵蓋,也不建議用於生產工作負載,且在正式上架前可能會有所變動或受限。 Azure AI 搜尋服務 預覽條款適用於所有預覽功能,無論是獨立功能還是正式推出功能的一部分。

預設情況下,Azure AI 搜尋服務 中的知識庫會執行資料擷取,從你的知識來源回傳原始的基礎區塊。 資料擷取有助於擷取特定資訊,但缺乏複雜查詢所需的脈絡與推理。

您可以改為啟用答案合成 (預覽版),此功能會使用知識庫中指定的 LLM,以自然語言回答查詢。 每個答案都附有引用來源,並且遵循你提供的指示,例如使用項目符號列表。

你可以在知識庫或擷取請求中設定這個屬性。 知識庫設定會為所有查詢建立預設值,而 retrieve request 設定則會依查詢逐一覆蓋預設值。

使用支援

Azure portal Microsoft Foundry 入口網站 .NET SDK Python SDK Java 開發套件 JavaScript SDK REST API
✔️ ✔️ ✔️ ✔️ ✔️ ✔️ ✔️

先決條件

  • 一個具有指定LLM的知識庫的Azure AI 搜尋服務服務。

  • 更新知識庫的權限 設定無金鑰驗證,並將搜尋服務參與者角色指派給您的使用者帳戶(建議),或使用管理員 API 金鑰。

  • 若要對 LLM 進行傳出呼叫,搜尋服務必須有具備 Microsoft Foundry 資源上 Cognitive Services User 權限的受控識別。

  • 最新的 Azure.Search.Documents 預覽套件:dotnet add package Azure.Search.Documents --prerelease

  • 對於無鑰匙認證,套件如下 Azure.Identity : dotnet add package Azure.Identity

限制與考量

  • minimal檢索推理工作會停用 LLM 處理,因此在知識庫定義和檢索要求中,都與答案合成不相容。 欲了解更多資訊,請參閱 設定檢索推理努力。

  • 答案合成會根據輸入與輸出代幣數量,從 Azure OpenAI 收取按需付費的費用。 費用會顯示在指派給該知識庫的大型語言模型下。 欲了解更多資訊,請參閱 區域可用性、限額及計費。

在知識庫中啟用答案合成

本節示範如何在現有知識庫中實現答案綜合。 雖然你可以用這種配置來建立新的知識庫,但知識庫的建立超出了本文的範圍。

在 OutputMode 定義中,將 "answerSynthesis" 設為 KnowledgeBase。 可選擇性地設定 AnswerInstructions 自訂答案輸出。 以下範例指示知識庫為 Use concise bulleted lists。

var aoaiParams = new AzureOpenAIVectorizerParameters
{
    ResourceUri = new Uri("<aoai-endpoint>"),
    DeploymentName = "<aoai-gpt-deployment>",
    ModelName = "<aoai-gpt-model>",
};

var knowledgeBase = new KnowledgeBase(
    name: "<knowledge-base-name>",
    knowledgeSources: new[] { new KnowledgeSourceReference("<knowledge-source-name>") })
{
    Models = { new KnowledgeBaseAzureOpenAIModel(aoaiParams) },
    OutputMode = "answerSynthesis",
    AnswerInstructions = "Use concise bulleted lists",
};

await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);

參考資料:SearchIndexClient, 知識庫

在 output_mode 定義中,將 "answerSynthesis" 設為 KnowledgeBase。 可選擇性地設定 answer_instructions 自訂答案輸出。 以下範例指示知識庫為 Use concise bulleted lists。

from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
    AzureOpenAIVectorizerParameters,
    KnowledgeBase,
    KnowledgeBaseAzureOpenAIModel,
    KnowledgeSourceReference,
)

aoai_params = AzureOpenAIVectorizerParameters(
    resource_url="<aoai-endpoint>",
    deployment_name="<aoai-gpt-deployment>",
    model_name="<aoai-gpt-model>",
)

knowledge_base = KnowledgeBase(
    name="<knowledge-base-name>",
    models=[KnowledgeBaseAzureOpenAIModel(azure_open_ai_parameters=aoai_params)],
    knowledge_sources=[KnowledgeSourceReference(name="<knowledge-source-name>")],
    output_mode="answerSynthesis",
    answer_instructions="Use concise bulleted lists",
)

index_client = SearchIndexClient(endpoint=search_endpoint, credential=credential)
index_client.create_or_update_knowledge_base(knowledge_base)

參考資料:SearchIndexClient, 知識庫

在知識庫定義中,將 outputMode 設為 "answerSynthesis"。 可選擇性地設定 answerInstructions 自訂答案輸出。 以下範例指示知識庫為 Use concise bulleted lists。

### Enable answer synthesis in a knowledge base
PUT {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}?api-version=2026-08-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
    "name": "{{knowledge-base-name}}",
    "knowledgeSources": [ ... // OMITTED FOR BREVITY ],
    "models": [ ... // OMITTED FOR BREVITY ],
    "outputMode": "answerSynthesis",
    "answerInstructions": "Use concise bulleted lists"
}

參考資料:知識庫 - 建立或更新

在擷取請求中啟用答案合成

為了在每個查詢時控制回應格式,你可以在查詢時啟用答案綜合。 此方法覆蓋知識庫中指定的預設輸出模式。

在 OutputMode 上將 "answerSynthesis" 設為 KnowledgeBaseRetrievalRequest。

var client = new KnowledgeBaseRetrievalClient(
    endpoint: new Uri(searchEndpoint),
    knowledgeBaseName: knowledgeBaseName,
    credential: credential);

var request = new KnowledgeBaseRetrievalRequest();
request.Messages.Add(
    new KnowledgeBaseMessage(
        content: new[]
        {
            new KnowledgeBaseMessageTextContent("What is healthcare?")
        }) { Role = "user" });
request.OutputMode = "answerSynthesis";

var result = await client.RetrieveAsync(request);

參考資料:KnowledgeBaseRetrievalClient, KnowledgeBaseRetrievalRequest

在 output_mode 上將 "answerSynthesis" 設為 KnowledgeBaseRetrievalRequest。

from azure.search.documents.knowledgebases import KnowledgeBaseRetrievalClient
from azure.search.documents.knowledgebases.models import (
    KnowledgeBaseMessage,
    KnowledgeBaseMessageTextContent,
    KnowledgeBaseRetrievalRequest,
)

agent_client = KnowledgeBaseRetrievalClient(
    endpoint=search_endpoint,
    credential=credential,
    knowledge_base_name=knowledge_base_name,
)

request = KnowledgeBaseRetrievalRequest(
    messages=[
        KnowledgeBaseMessage(
            role="user",
            content=[KnowledgeBaseMessageTextContent(text="What is healthcare?")],
        )
    ],
    output_mode="answerSynthesis",
)

result = agent_client.retrieve(retrieval_request=request)

參考資料:KnowledgeBaseRetrievalClient, KnowledgeBaseRetrievalRequest

在擷取要求中,將 outputMode 設為 "answerSynthesis"。

### Enable answer synthesis in a retrieve request
POST {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}/retrieve?api-version=2026-08-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}

{
    "messages": [
        {
            "role": "user",
            "content": [
                {
                    "type": "text",
                    "text": "What is healthcare?"
                }
            ]
        }
    ],
    "outputMode": "answerSynthesis"
}

參考資料:知識檢索 - 檢索

獲得一個綜合答案

啟用答案綜合時,知識庫會根據你選擇性地指定的指令,回傳一個自然語言的答案。 你的知識來源引用格式為 [ref_id:<number>]。

例如,如果你的指示是 Use concise bulleted lists ,而你的查詢是 What is healthcare?,回應應該會類似以下範例。

{
  "response": [
    {
      "content": [
        {
          "type": "text",
          "text": "- Healthcare encompasses various services provided to patients and the general population ..."
        }
      ]
    }
  ]
}

完整 text 輸出如下:

"- Healthcare encompasses various services provided to patients and the general population, including primary health services, hospital care, dental care, mental health services, and alternative health services [ref_id:1].\n- It involves the delivery of safe, effective, patient-centered care through different modalities, such as in-person encounters, shared medical appointments, and group education sessions [ref_id:0].\n- Behavioral health is a significant aspect of healthcare, focusing on the connection between behavior and overall health, including mental health and substance use [ref_id:2].\n- The healthcare system aims to ensure quality of care, access to providers, and accountability for positive outcomes while managing costs effectively [ref_id:2].\n- The global health system is evolving to address complex health needs, emphasizing the importance of cross-sectoral collaboration and addressing social determinants of health [ref_id:4]."

根據你的知識庫設定,回應可能包含其他資訊,例如活動日誌和參考陣列。 欲了解更多資訊,請參閱 建立知識庫。