注释
此智能检索功能在 2026-04-01 REST API 版本中通过编程接口普遍可用。 Azure门户继续提供对所有代理检索功能的仅限预览的访问权限。
如果选择使用预览版 REST API 版本,则可以访问尚未正式发布此功能的功能。 预览版功能在没有服务级别协议的情况下提供,不建议用于生产工作负荷。
重要
这些特性和功能是 2026-05-01 预览版 REST API 的一部分。 2026-05-01-preview 作为Azure订阅的一部分获得许可,并受Microsoft产品条款、Microsoft产品和服务数据保护附录(“DPA”)和Azure预览版补充使用条款的约束。
2026-05-01-preview 支持连接到其他 Microsoft 服务和第三方服务。 使用这些服务受其各自的条款的约束,可能会导致数据处理或存储超出Azure符合性边界,以及流入Azure符合性边界的数据。
您有责任管理您的数据是否会流出您组织的合规和地理边界之外及其任何相关影响,并确保已配置适当的权限、边界和审批。
你负责仔细查看和测试在特定用例上下文中生成的应用程序,并做出所有适当的决策和自定义。 这包括实施自己的负责任的 AI 缓解措施,例如元系统、内容筛选器或其他安全系统,并确保应用程序满足适当的质量、可靠性、安全性和可信度标准。
重要
这些特性和功能是 2026-05-01 预览版 REST API 的一部分。 2026-05-01-preview 作为Azure订阅的一部分获得许可,并受Microsoft产品条款、Microsoft产品和服务数据保护附录(“DPA”)和Azure预览版补充使用条款的约束。
2026-05-01-preview 支持连接到其他 Microsoft 服务和第三方服务。 使用这些服务受其各自的条款的约束,可能会导致数据处理或存储超出Azure符合性边界,以及流入Azure符合性边界的数据。
您有责任管理您的数据是否会流出您组织的合规和地理边界之外及其任何相关影响,并确保已配置适当的权限、边界和审批。
在 Azure AI 搜索中, 知识库 是协调 代理检索的顶级对象。 它定义要查询的知识源以及用于检索作的默认行为。 在查询时, 检索方法 以知识库为目标,以运行配置的检索管道。
知识库指定:
- 指向可搜索内容的一个或多个知识源。
- 用于查询规划、答案合成或 Web 内容摘要的可选 LLM。 支持的任务因 API 版本和知识源类型而异。
- 控制路由、源选择和对象加密的自定义属性。
使用支持
| Azure 门户 | .NET SDK | Python SDK | Java SDK | JavaScript SDK | REST API |
|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
所需
Azure.Search.Documents软件包:对于 2026-05-01-preview 功能,最新的预览包为:
dotnet add package Azure.Search.Documents --prerelease关于2026-04-01的功能,最新的稳定包是:
dotnet add package Azure.Search.Documents
所需
azure-search-documents软件包:对于 2026-05-01-preview 功能,最新的预览包为:
pip install --pre azure-search-documents关于2026-04-01的功能,最新的稳定包是:
pip install azure-search-documents
所需的 REST API 版本:
一般可用的功能: 搜索服务 2026-04-01
配置访问权限
Azure AI 搜索需要从 Azure AI 服务访问 LLM。 我们建议使用 Microsoft Entra ID 进行身份验证,并使用基于角色的访问控制进行授权。 若要分配角色,你必须是 所有者或用户访问管理员。 如果无法使用角色,请改用基于密钥的身份验证。
在模型提供程序上,将 认知服务用户 分配到搜索服务的托管标识。 如果要在本地进行测试,请将相同的角色分配给用户帐户。
对于本地测试,请按照快速入门:在没有密钥的情况下连接中的步骤登录到特定订阅和租户。 应在每个请求中使用
DefaultAzureCredential而不是AzureKeyCredential,其格式应类似于以下示例:// Authenticate using roles using Azure.Search.Documents.Indexes; using Azure.Identity; var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new DefaultAzureCredential());
在模型提供程序上,将 认知服务用户 分配到搜索服务的托管标识。 如果要在本地进行测试,请将相同的角色分配给用户帐户。
对于本地测试,请按照快速入门:在没有密钥的情况下连接中的步骤登录到特定订阅和租户。 应在每个请求中使用
DefaultAzureCredential而不是AzureKeyCredential,其格式应类似于以下示例:# Authenticate using roles from azure.identity import DefaultAzureCredential index_client = SearchIndexClient(endpoint = "search_url", credential = DefaultAzureCredential())
在模型提供程序上,将 认知服务用户 分配到搜索服务的托管标识。 如果要在本地进行测试,请将相同的角色分配给用户帐户。
对于本地测试,请按照《快速入门:在没有密钥的情况下连接》的步骤操作,以获取特定订阅和租户的个人访问令牌。 在每个请求中指定访问令牌,该令牌应类似于以下示例:
# List indexes using roles GET https://{{search-url}}/indexes?api-version=2026-04-01 Content-Type: application/json Authorization: Bearer {{access-token}}
重要
本文中的代码片段使用 API 密钥。 如果使用基于角色的身份验证,请相应地更新每个请求。 在指定这两种方法的请求中,API 密钥优先。
检查现有知识库
知识库是顶级可重用对象。 了解现有知识库有助于重复使用或命名新对象。
运行以下代码,按名称列出现有知识库。 该列表包括你的搜索服务上的所有知识库,而不考虑用于创建它们的 API 版本。
// List knowledge bases by name
using Azure.Search.Documents.Indexes;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);
var knowledgeBases = indexClient.GetKnowledgeBasesAsync();
Console.WriteLine("Knowledge Bases:");
await foreach (var kb in knowledgeBases)
{
Console.WriteLine($" - {kb.Name}");
}
Reference:SearchIndexClient
# List knowledge bases by name
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
for kb in index_client.list_knowledge_bases():
print(f" - {kb.name}")
Reference:SearchIndexClient
# List knowledge bases
GET {{search-url}}/knowledgebases?api-version={{api-version}}&$select=name
Content-Type: application/json
api-key: {{search-api-key}}
参考:知识库 - 列表
还可以按名称返回单个知识库来查看其 JSON 定义。
using Azure.Search.Documents.Indexes;
using System.Text.Json;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);
// Specify the knowledge base name to retrieve
string kbNameToGet = "earth-knowledge-base";
// Get a specific knowledge base definition
var knowledgeBaseResponse = await indexClient.GetKnowledgeBaseAsync(kbNameToGet);
var kb = knowledgeBaseResponse.Value;
// Serialize to JSON for display
string json = JsonSerializer.Serialize(kb, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);
Reference:SearchIndexClient
# Get a knowledge base definition
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
import json
index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
kb = index_client.get_knowledge_base("knowledge_base_name")
print(json.dumps(kb.as_dict(), indent = 2))
Reference:SearchIndexClient
# Get knowledge base
GET {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
Content-Type: application/json
api-key: {{search-api-key}}
参考:知识库 - 获取
以下 JSON 是知识库的示例响应。
{
"name": "my-kb",
"description": "A sample knowledge base.",
"retrievalInstructions": null,
"answerInstructions": null,
"outputMode": null,
"knowledgeSources": [
{
"name": "my-blob-ks"
}
],
"models": [],
"encryptionKey": null,
"retrievalReasoningEffort": {
"kind": "low"
}
}
注释
响应架构反映用于创建知识库的 API 版本。 使用正式版 2026-04-01 API 版本创建的知识库返回比 2026-05-01-preview 更窄的定义。 有关每个版本支持哪些属性的详细信息,请参阅下一部分。
创建知识库
重要
2026-04-01 API 版本仅接受正式发布的知识源类型,并支持最少的提取检索。 不支持仅预览功能,例如查询规划、答案合成和可配置推理工作。 要使用全部功能,请使用 2026-05-01-preview。
创建知识库后,可以随时更新其属性。 如果知识库正在使用中,更新将对下一次检索生效。
// Create a knowledge base
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.KnowledgeBases.Models;
using Azure;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(apiKey));
var aoaiParams = new AzureAIservicesVectorizerParameters
{
ResourceUri = new Uri(aoaiEndpoint),
DeploymentName = aoaiGptDeployment,
ModelName = aoaiGptModel,
ApiKey = aoaiApiKey
};
var knowledgeBase = new KnowledgeBase(
name: "my-kb",
knowledgeSources: new KnowledgeSourceReference[]
{
new KnowledgeSourceReference("hotels-ks"),
new KnowledgeSourceReference("earth-at-night-ks")
}
)
{
Description = "This knowledge base handles questions directed at two unrelated sample indexes.",
RetrievalInstructions = "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
AnswerInstructions = "Provide a two-sentence, concise, and informative answer based on the retrieved documents.",
OutputMode = KnowledgeRetrievalOutputMode.AnswerSynthesis,
Models = { new KnowledgeBaseAzureAIservicesModel(AzureAIservicesParameters: aoaiParams) },
RetrievalReasoningEffort = new KnowledgeRetrievalLowReasoningEffort()
};
await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);
Console.WriteLine($"Knowledge base '{knowledgeBase.Name}' created or updated successfully.");
Reference:SearchIndexClient、 KnowledgeBase
# Create a knowledge base
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
KnowledgeBase,
KnowledgeSourceReference,
)
from azure.search.documents.knowledgebases.models import KnowledgeRetrievalLowReasoningEffort
index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
aoai_params = AzureAIservicesVectorizerParameters(
resource_url = "aoai_endpoint",
api_key="aoai_api_key",
deployment_name = "aoai_gpt_deployment",
model_name = "aoai_gpt_model",
)
knowledge_base = KnowledgeBase(
name = "my-kb",
description = "This knowledge base handles questions directed at two unrelated sample indexes.",
retrieval_instructions = "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
answer_instructions = "Provide a two-sentence, concise, and informative answer based on the retrieved documents.",
output_mode = "answerSynthesis",
knowledge_sources = [
KnowledgeSourceReference(name = "hotels-ks"),
KnowledgeSourceReference(name = "earth-at-night-ks"),
],
models = [KnowledgeBaseAzureAIservicesModel(azure_open_ai_parameters = aoai_params)],
encryption_key = None,
retrieval_reasoning_effort = KnowledgeRetrievalLowReasoningEffort(),
)
index_client.create_or_update_knowledge_base(knowledge_base)
print(f"Knowledge base '{knowledge_base.name}' created or updated successfully.")
Reference:SearchIndexClient、 KnowledgeBase
# Create a knowledge base
PUT {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version=2026-05-01-preview
Content-Type: application/json
api-key: {{search-api-key}}
{
"name" : "my-kb",
"description": "This knowledge base handles questions directed at two unrelated sample indexes.",
"retrievalInstructions": "Use the hotels knowledge source for queries about where to stay, otherwise use the earth at night knowledge source.",
"answerInstructions": null,
"outputMode": "answerSynthesis",
"knowledgeSources": [
{
"name": "hotels-ks"
},
{
"name": "earth-at-night-ks"
}
],
"models" : [
{
"kind": "AzureAIservices",
"AzureAIservicesParameters": {
"resourceUri": "{{model-provider-url}}",
"apiKey": "{{model-api-key}}",
"deploymentId": "gpt-5.4-mini",
"modelName": "gpt-5.4-mini"
}
}
],
"encryptionKey": null,
"retrievalReasoningEffort": {
"kind": "low"
}
}
参考:知识库 - 创建或更新
知识库属性
传递以下属性以创建知识库。
| Name | Description | 类型 | 必选 |
|---|---|---|---|
Name |
知识库的名称。 它在知识库集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 。 | String | 是的 |
KnowledgeSources |
一个或多个 受支持的知识源。 | Array | 是的 |
Description |
知识库的说明。 LLM 使用该说明来指导查询规划。 | String | 否 |
RetrievalInstructions |
提示 LLM 确定某个知识源是否应在查询范围内。 如果有多个知识源,请包含此提示。 此字段同时影响知识来源选择和查询表述。 例如,说明可以追加信息或确定知识来源的优先级。 指令直接传递到 LLM,这意味着可以提供破坏查询规划的说明,例如导致绕过关键知识源的说明。 | String | 否 |
AnswerInstructions |
自定义指令以定制合成答案。 默认值为 NULL。 有关详细信息,请参阅 如何使用答案合成生成有引文支持的响应。 | String | 否 |
OutputMode |
有效值是 AnswerSynthesis(用于 LLM 制定的答案)或 ExtractedData(用于完整的搜索结果),你可以将其作为下游步骤传递给 LLM。 |
String | 否 |
RetrievalReasoningEffort |
确定与 LLM 相关的查询处理级别。 有效值为 minimal、 low (默认值)和 medium。 有关详细信息,请参阅 设置检索推理工作。 |
物体 | 否 |
| Name | Description | 类型 | 必选 |
|---|---|---|---|
name |
知识库的名称。 它在知识库集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 。 | String | 是的 |
description |
知识库的说明。 LLM 使用该说明来指导查询规划。 | String | 否 |
retrieval_instructions |
提示 LLM 确定某个知识源是否应在查询范围内。 如果有多个知识源,请包含此提示。 此字段同时影响知识来源选择和查询表述。 例如,说明可以追加信息或确定知识来源的优先级。 将指令直接传递给 LLM。 可以提供中断查询规划的说明,例如导致绕过基本知识源的说明。 | String | 否 |
answer_instructions |
自定义指令以定制合成答案。 默认值为 NULL。 有关详细信息,请参阅 如何使用答案合成生成有引文支持的响应。 | String | 否 |
output_mode |
有效值是 answerSynthesis(用于 LLM 制定的答案)或 extractedData(用于完整的搜索结果),你可以将其作为下游步骤传递给 LLM。 |
String | 否 |
knowledge_sources |
一个或多个 受支持的知识源。 | Array | 是的 |
encryption_key |
客户 管理的密钥 ,用于加密知识库和生成的对象中的敏感信息。 | 物体 | 否 |
retrieval_reasoning_effort |
确定与 LLM 相关的查询处理级别。 有效值为 minimal、 low (默认值)和 medium。 有关详细信息,请参阅 设置检索推理工作。 |
物体 | 否 |
| Name | Description | 类型 | 必选 |
|---|---|---|---|
name |
知识库的名称。 它在知识库集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 。 | String | 是的 |
description |
知识库的说明。 LLM 使用该说明来指导查询规划。 | String | 否 |
retrievalInstructions |
提示 LLM 确定某个知识源是否应在查询范围内。 如果有多个知识源,请包含此提示。 此字段同时影响知识来源选择和查询表述。 例如,说明可以追加信息或确定知识来源的优先级。 将指令直接传递给 LLM,这意味着可以提供破坏查询计划的指令,例如会导致绕过关键知识源的指令。 | String | 否 |
answerInstructions |
自定义指令以定制合成答案。 默认值为 NULL。 有关详细信息,请参阅 如何使用答案合成生成有引文支持的响应。 | String | 否 |
outputMode |
有效值是 answerSynthesis(用于 LLM 制定的答案)或 extractedData(用于完整的搜索结果),你可以将其作为下游步骤传递给 LLM。 |
String | 否 |
knowledgeSources |
一个或多个 受支持的知识源。 | Array | 是的 |
encryptionKey |
客户 管理的密钥 ,用于加密知识库和生成的对象中的敏感信息。 | 物体 | 否 |
retrievalReasoningEffort.kind |
确定与 LLM 相关的查询处理级别。 有效值为 minimal、 low (默认值)和 medium。 有关详细信息,请参阅 设置检索推理工作。 |
物体 | 否 |
为基于浏览器的检索调用配置 CORS (预览版)
重要
可以使用 2026-05-01-preview 启用跨域资源共享(CORS),这样基于浏览器的应用程序就可以直接从服务请求数据。 根据 CORS 配置,外部网页可能能够使用用户的浏览器上下文访问或调用服务及其数据,并创建其他安全威胁。 启用 CORS 有你自己的风险。
在 2026-05-01-preview API 版本中,知识库可以定义 corsOptions,供直接通过 JavaScript 调用 retrieve 操作的浏览器应用程序使用。 CORS 策略标识哪些浏览器源可以向知识库发送检索请求。
省略 corsOptions时,知识库没有 CORS 策略,浏览器会阻止跨源检索请求。
以下示例创建一个知识库,该知识库允许从一个浏览器源检索请求。
using Azure;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(apiKey));
var knowledgeBase = new KnowledgeBase(
name: "browser-chat-kb",
knowledgeSources: new[] { new KnowledgeSourceReference("product-docs-ks") }
)
{
Description = "A knowledge base that allows one browser app origin.",
CorsOptions = new CorsOptions(new[] { "https://myapp.example.com" })
{
MaxAgeInSeconds = 300
}
};
await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
CorsOptions,
KnowledgeBase,
KnowledgeSourceReference,
)
index_client = SearchIndexClient(endpoint="search_url", credential=AzureKeyCredential("api_key"))
knowledge_base = KnowledgeBase(
name="browser-chat-kb",
description="A knowledge base that allows one browser app origin.",
knowledge_sources=[KnowledgeSourceReference(name="product-docs-ks")],
cors_options=CorsOptions(
allowed_origins=["https://myapp.example.com"],
max_age_in_seconds=300,
),
)
index_client.create_or_update_knowledge_base(knowledge_base)
PUT {{search-url}}/knowledgebases/browser-chat-kb?api-version=2026-05-01-preview
Content-Type: application/json
api-key: {{search-api-key}}
{
"name": "browser-chat-kb",
"description": "A knowledge base that allows one browser app origin.",
"knowledgeSources": [
{
"name": "product-docs-ks"
}
],
"corsOptions": {
"allowedOrigins": [
"https://myapp.example.com"
],
"maxAgeInSeconds": 300
}
}
corsOptions 接受以下属性。
| Name | Description | 类型 | 必选 |
|---|---|---|---|
allowedOrigins |
列出可从浏览器调用知识库的源。 对于生产应用程序,请使用明确的来源列表。 可以使用 "*" 允许所有来源,但不建议在生产环境中使用此设置。 |
Array | 是的 |
maxAgeInSeconds |
控制浏览器可以缓存预检响应的时长。 如果省略该值,预检缓存时长默认为 300 秒。 | Integer | 否 |
查询知识库
创建知识库后,调用 检索操作或 MCP 终结点 对其进行查询。
删除知识库
如果不再需要知识库或需要在搜索服务上重新生成该知识库,请运行以下代码以删除该对象。
// Delete a knowledge base
using Azure.Search.Documents.Indexes;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);
await indexClient.DeleteKnowledgeBaseAsync(knowledgeBaseName);
System.Console.WriteLine($"Knowledge base '{knowledgeBaseName}' deleted successfully.");
Reference:SearchIndexClient
# Delete a knowledge base
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
index_client.delete_knowledge_base("knowledge_base_name")
print(f"Knowledge base deleted successfully.")
Reference:SearchIndexClient
# Delete a knowledge base
DELETE {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
api-key: {{search-api-key}}
参考:知识库 - 删除