注释
此功能目前处于公开预览状态。 此预览版未随附服务级别协议,建议不要用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Azure 预览版的使用条款。
默认情况下,Azure AI Search中的 知识库 执行 数据提取,这将从您的知识源返回原始基础块。 数据提取可用于检索特定信息,但缺少复杂查询所需的上下文和推理。
可以改为启用答案合成,该功能使用在你的知识库中指定的LLM,以自然语言回答查询。 每个答案包括检索到的源的引文,并遵循提供的任何说明,例如使用项目符号列表。
可以通过两种方式启用答案合成:
- 在知识库中(成为所有查询的默认选项)
- 在单个检索请求上(替代默认值)
重要
-
minimal检索推理工作会禁用 LLM 处理,因此它与knowledge base定义和检索请求中的应答合成不兼容。 有关详细信息,请参阅 设置检索推理工作。
先决条件
具有指定 LLM 的知识库的 Azure AI 搜索服务。
更新知识库的权限。 使用分配给用户帐户的 Search Service 参与者角色配置 无密钥身份验证或使用 API 密钥。
对于对 LLM 的出站调用,搜索服务必须具有在 Azure AI 服务资源上具有托管标识和认知服务用户权限。
2025-11-01-preview REST API 或等效的 Azure SDK 预览包:.NET Java JavaScript Python 。
在knowledge base中启用答案合成
本部分介绍如何在现有knowledge base中启用答案合成。 尽管可以将此配置用于新的知识库,但knowledge base创建超出了本文的范围。
若要在知识库中启用答案合成,请:
请使用 2025-11-01-preview 的 Knowledge Base - 创建或更新(REST API)来制定请求。
在请求正文中,将
outputMode设置为answerSynthesis.(可选)使用
answerInstructions自定义答案输出。 本示例指示知识库Use concise bulleted lists。
@search-url = <YOUR SEARCH SERVICE URL>
@api-key = <YOUR API KEY>
@knowledge-base-name = <YOUR KNOWLEDGE BASE NAME>
### Enable answer synthesis in a knowledge base
PUT {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version=2025-11-01-preview HTTP/1.1
Content-Type: application/json
api-key: {{api-key}}
{
"name": "{{knowledge-base-name}}",
"knowledgeSources": [ ... // OMITTED FOR BREVITY ],
"models": [ ... // OMITTED FOR BREVITY ],
"outputMode": "answerSynthesis",
"answerInstructions": "Use concise bulleted lists"
}
注释
此示例假定你使用基于密钥的身份验证进行本地概念证明测试。 我们建议在生产工作负载中使用基于角色的访问控制。 有关详细信息,请参阅 使用角色连接到 Azure AI Search。
在检索请求中启用应答合成
对于对响应格式的按查询控制,可以在查询时启用答案合成。 此方法替代在knowledge base中指定的默认输出模式。
若要在检索请求中启用应答合成,请执行以下项:
使用 2025-11-01 预览版 知识检索 - 检索(REST API) 来生成请求。
在请求正文中,将
outputMode设置为answerSynthesis.
@search-url = <YOUR SEARCH SERVICE URL>
@api-key = <YOUR API KEY>
@knowledge-base-name = <YOUR KNOWLEDGE BASE NAME>
### Enable answer synthesis in a retrieve request
POST {{search-url}}/knowledgebases/{{knowledge-base-name}}/retrieve?api-version=2025-11-01-preview HTTP/1.1
Content-Type: application/json
api-key: {{api-key}}
{
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is healthcare?"
}
]
}
],
"outputMode": "answerSynthesis"
}
注释
此示例假定你使用基于密钥的身份验证进行本地概念证明测试。 我们建议在生产工作负载中使用基于角色的访问控制。 有关详细信息,请参阅 使用角色连接到 Azure AI Search。
获取合成答案
启用答案合成后,Knowledge 检索 - 检索(REST API)会根据knowledge base中指定的说明返回自然语言答案。 对知识源的引文的格式设置为 [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 ... // TRIMMED FOR BREVITY"
}
]
}
]
}
完整 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]."
根据knowledge base的配置,响应可能包含其他信息,例如活动日志和引用数组。 有关详细信息,请参阅 创建 knowledge base。