你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
注意
Foundry IQ (Azure AI 搜索) 提供直接索引和查询 API 以及用于托管检索的高级知识库和知识库 API。 使用 Azure 门户、REST API 或 Azure SDK 配置这些功能,或将代理连接到 Microsoft Foundry 门户中的知识库。
默认情况下,Foundry IQ(Azure AI 搜索)中的知识库执行数据提取,无需生成答案即可从知识源返回地面内容。
你可以改为启用答案合成,它使用知识库中配置的 LLM 生成自然语言答案。 你可以提供答案指令来指定响应的形式,例如要求以项目符号列表作答。 参考资料提供用于引用标注的来源 ID。
可以在知识库或检索请求中设置输出模式。 知识库设置为所有查询建立默认值,而检索请求设置会基于查询替代默认值。
使用支持
| Azure 门户 | Microsoft Foundry 门户 | .NET SDK | Python SDK | Java SDK | JavaScript SDK | REST API |
|---|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
先决条件
一个包含知识库的 Foundry IQ 服务,且该知识库已指定 LLM。
更新知识库的权限。 使用分配给用户帐户的搜索服务参与者角色(建议)或使用管理员 API 密钥配置无密钥身份验证。
对于向 LLM 发出的出站调用,Foundry IQ 服务必须具有一个托管标识,并且该标识在 Microsoft Foundry 资源上具有认知服务用户权限。
最新的
Azure.Search.Documents预览包:dotnet add package Azure.Search.Documents --prerelease对于无密钥身份验证,请使用
Azure.Identity软件包:dotnet add package Azure.Identity
最新的
azure-search-documents预览包:pip install --pre azure-search-documents对于无密钥身份验证,请使用
azure-identity软件包:pip install azure-identity
2026-10-01或更高版本的搜索 REST API。 本示例使用2026-11-01-preview。对于无密钥身份验证,请在每个 HTTP 请求的标头中包含
AuthorizationMicrosoft Entra ID令牌。
限制和注意事项
使用常规的
minimal推理强度会跳过基于模型的查询规划,但不会跳过答案合成。 答案合成本身就需要配置模型,即使使用minimal也是如此。 有关详细信息,请参阅 设置检索推理工作。答案合成会产生Azure OpenAI 的即用即付费用,这些费用基于输入和输出令牌的数量。 费用显示在分配给知识库的 LLM 下。 有关详细信息,请参阅 区域可用性、限制和计费。
在知识库中启用答案合成
若要通过 Azure API 管理(APIM)路由知识库检索模型,请使用 2026-11-01-preview,并配合 REST API 使用。
在现有知识库中启用答案合成。 有关知识库创建,请参阅 “创建知识库”。
在OutputMode定义中,将"answerSynthesis"设置为KnowledgeBase。 (可选)设置为 AnswerInstructions 自定义答案输出。 以下示例指示知识库 Use concise bulleted lists。
var indexClient = new SearchIndexClient(
new Uri(searchEndpoint), credential);
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);
Reference:SearchIndexClient, KnowledgeBase
在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)
Reference:SearchIndexClient, KnowledgeBase
在知识库定义中,将 outputMode 设置为 "answerSynthesis"。 (可选)设置为 answerInstructions 自定义答案输出。 以下示例指示知识库 Use concise bulleted lists。
如果知识库定义了命名工具,请在更新中包含其现有的 tools 数组。
### Enable answer synthesis in a knowledge base
PUT {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}?api-version=2026-11-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"
}
参考:知识库 - 创建或更新
在检索请求中启用应答合成
对于对响应格式的按查询控制,可以在查询时启用答案合成。 此方法替代知识库中指定的默认输出模式。
将答案合成与 REST 配合使用
使用一个现有知识库,其 models 数组包含 受支持的模型、已填充的搜索索引知识源和有权检索内容的凭据。 如果知识库使用 Azure OpenAI,请配置访问权限。
POST {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}/retrieve?api-version=2026-11-01-preview
Content-Type: application/json
Authorization: Bearer {{search-token}}
{
"intents": [
{
"type": "semantic",
"search": "What is the return policy?"
}
],
"retrievalReasoningEffort": {
"kind": "minimal"
},
"outputMode": "answerSynthesis",
"includeActivity": true
}
参考:知识检索 - 检索
检查响应消息、引用和独立的 modelAnswerSynthesis 活动。 若要使用基于模型的查询规划,请改为选择low或medium。 请提供消息或意图中的一种,不要同时提供两者。
其他示例
以下示例展示了通过 SDK 发出的请求和基于消息的 REST 请求之间的对应关系。
在 OutputMode 上将 "answerSynthesis" 设置为 KnowledgeBaseRetrievalRequest。
var client = new KnowledgeBaseRetrievalClient(
endpoint: new Uri(searchEndpoint),
knowledgeBaseName: knowledgeBaseName,
credential: credential);
var request = new KnowledgeBaseRetrievalRequest
{
RetrievalReasoningEffort = new KnowledgeRetrievalLowReasoningEffort()
};
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,
KnowledgeRetrievalLowReasoningEffort,
)
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?")],
)
],
retrieval_reasoning_effort=KnowledgeRetrievalLowReasoningEffort(),
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-11-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
{
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is healthcare?"
}
]
}
],
"retrievalReasoningEffort": {
"kind": "low"
},
"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]."
根据知识库的配置,响应可能包含其他信息,例如活动日志和引用数组。 有关详细信息,请参阅 创建知识库。
相关内容
- 创建知识库
- 查询知识库
- 快速入门:智能体检索 (使用答案合成)
- Python示例:Foundry IQ blob 知识源(使用答案合成)