重要
标记为“预览”的特性、功能或属性不受服务级别协议 (SLA) 保障,不建议用于生产工作负载,并且在正式发布之前可能会更改或受到限制。 Azure AI 搜索预览条款适用于所有预览功能,无论是独立功能还是正式版功能的一部分。
重要
这些特性和功能是 2026-08-01 预览版 REST API 的一部分。 2026-08-01-preview 已作为你的 Azure 订阅的一部分许可给你使用,并受 Microsoft Product Terms、Microsoft Products and Services Data Protection Addendum(“DPA”)和 Supplemental Terms of Use for Azure Previews 中适用于“预览版”的条款约束。
2026-08-01-preview 版本支持连接到其他 Microsoft 服务和第三方服务。 使用这些服务受其各自的条款的约束,可能会导致数据处理或存储超出Azure符合性边界,以及流入Azure符合性边界的数据。
您有责任管理您的数据是否会流出您组织的合规和地理边界之外及其任何相关影响,并确保已配置适当的权限、边界和审批。
你负责仔细查看和测试在特定用例上下文中生成的应用程序,并做出所有适当的决策和自定义。 这包括实施自己的负责任的 AI 缓解措施,例如元系统、内容筛选器或其他安全系统,并确保应用程序满足适当的质量、可靠性、安全性和可信度标准。
在 Azure AI 搜索中, 知识库 是协调 代理检索的顶级对象。 它定义要查询的知识源以及用于检索作的默认行为。 在查询时, 检索方法 以知识库为目标,以运行配置的检索管道。
知识库指定:
指向可搜索内容的一个或多个知识源。
用于查询规划、答案合成或 Web 内容摘要的可选 LLM。 支持的任务因 API 版本和知识源类型而异。
控制路由、源选择和对象加密的自定义属性。
使用支持
| Azure 门户 | .NET SDK | Python SDK | Java SDK | JavaScript SDK | REST API |
|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
所需
Azure.Search.Documents软件包:对于 SDK 定义管理示例,最新的预览包:
dotnet add package Azure.Search.Documents --prerelease对于
2026-10-01,最新的稳定包:dotnet add package Azure.Search.Documents
对于无密钥身份验证,可使用
Azure.Identity包:dotnet add package Azure.Identity
所需
azure-search-documents软件包:对于 SDK 定义管理示例,最新的预览包:
pip install --pre azure-search-documents对于
2026-10-01,最新的稳定包:pip install azure-search-documents
对于无密钥身份验证,可使用
azure-identity包:pip install azure-identity
所需的搜索服务 REST API 版本:
对于预览功能: 2026-11-01-preview
对于本文中所述的 GA 协定:
2026-10-01. 较旧的2026-04-01示例保留其较窄的协定。
对于无密钥身份验证,请在每个 HTTP 请求的标头中包含
AuthorizationMicrosoft Entra ID令牌。
配置访问权限
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-endpoint>", credential = DefaultAzureCredential())
在模型提供程序上,将 认知服务用户 分配到搜索服务的托管标识。 如果要在本地进行测试,请将相同的角色分配给用户帐户。
对于本地测试,请按照《快速入门:在没有密钥的情况下连接》的步骤操作,以获取特定订阅和租户的个人访问令牌。 在每个请求中指定访问令牌,该令牌应类似于以下示例。
# List indexes using roles GET {{search-endpoint}}/indexes?api-version=2026-10-01 Content-Type: application/json Authorization: Bearer {{search-access-token}}
SDK 定义管理示例使用管理 API 密钥。 有关建议的无密钥设置,请参阅 “配置访问权限”。
检查现有知识库
知识库是顶级可重用对象。 了解现有知识库有助于重复使用或命名新对象。
使用所选 API 版本的定义客户端列出知识库。 Python示例循环访问原生分页列表。 C# 示例使用前缀搜索和页面大小为 10。
using System;
using System.Collections.Generic;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
var endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
var indexClient = new SearchIndexClient(
endpoint, new AzureKeyCredential(key));
var knowledgeBases = new List<KnowledgeBase>();
await foreach (var kb in indexClient.GetKnowledgeBasesAsync(
search: "sdk-", pageSize: 10, searchType: ListingSearchType.Prefix))
{
knowledgeBases.Add(kb);
}
Reference:SearchIndexClient
import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(
endpoint=os.environ["SEARCH_ENDPOINT"],
credential=AzureKeyCredential(os.environ["SEARCH_API_KEY"]),
)
knowledge_bases = list(index_client.list_knowledge_bases())
Reference:SearchIndexClient
# List knowledge bases
GET {{search-endpoint}}/knowledgebases?api-version={{api-version}}&$select=name
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
参考:知识库 - 列表
按名称读取现有知识库并检查所选定义属性。 使用 SDK 创建示例中的名称,或替换为你可以读取的知识库。
using System;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
var endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
var indexClient = new SearchIndexClient(
endpoint, new AzureKeyCredential(key));
var kb = (await indexClient.GetKnowledgeBaseAsync("<knowledge-base-name>")).Value;
Reference:SearchIndexClient
import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(
endpoint=os.environ["SEARCH_ENDPOINT"],
credential=AzureKeyCredential(os.environ["SEARCH_API_KEY"]),
)
kb = index_client.get_knowledge_base("<knowledge-base-name>")
Reference:SearchIndexClient
# Get knowledge base
GET {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
参考:知识库 - 获取
以下 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-10-01 架构定义的一部分。
创建知识库
重要
较旧的 2026-04-01 API 版本支持最少的提取检索,但不支持模型支持的规划或答案合成。 这些功能在 GA 2026-10-01 REST 协定中可用。 不要通过仅更改其 API 版本来迁移预览定义。
在 Foundry 模型中,知识库将一个或多个知识源(可搜索内容)与 Azure OpenAI 的可选 LLM 连接起来。 省略 models 创建无模型知识库。 设置的属性为查询执行和检索响应建立默认值。
创建知识库后,可以更新其属性,包括 将本机模型连接切换到 APIM 或自定义域终结点。 如果知识库正在使用中,更新将对下一次检索生效。
使用现有的、已填充的搜索索引知识源和经过授权可创建知识库的凭据。 将 {{knowledge-source-name}}、{{search-token}} 和 {{search-endpoint}} 替换为你的输入。 此示例创建一个用于提取检索的无模型知识库。
PUT {{search-endpoint}}/knowledgebases('core-minimal-kb')?api-version=2026-10-01
Content-Type: application/json
Authorization: Bearer {{search-token}}
{
"name": "core-minimal-kb",
"knowledgeSources": [
{ "name": "{{knowledge-source-name}}" }
],
"retrievalReasoningEffort": {
"kind": "minimal"
},
"outputMode": "extractiveData"
}
参考:知识库 - 创建或更新
在此协定中,每个 knowledgeSources 条目仅包含 name。 在知识源上存储源配置,例如 searchIndexParameters.baseFilter。 设置特定于请求的选项,例如 alwaysQuerySource 和 failOnError,在检索时的 knowledgeSourceParams 中,而不是在 KB 成员身份条目中。
auto、retrieveDefaults 和 neverQuerySource 不属于 GA 承诺。
using System;
using Azure.Identity;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.Models;
string searchEndpoint = "<search-endpoint>";
var indexClient = new SearchIndexClient(
new Uri(searchEndpoint),
new DefaultAzureCredential());
var knowledgeBase = new KnowledgeBase(
"your-knowledge-base",
new[] { new KnowledgeSourceReference("your-knowledge-source") })
{
Description = "A knowledge base for product support content.",
RetrieveDefaults = new KnowledgeBaseRetrieveDefaults
{
MaxRuntimeInSeconds = 45,
MaxOutputDocuments = 8,
MaxOutputSizeInTokens = 12000
}
};
await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);
Reference:SearchIndexClient、 SearchClientOptions.ServiceVersion、 KnowledgeBase
区域结束
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
KnowledgeBase,
KnowledgeBaseRetrieveDefaults,
KnowledgeSourceReference,
)
search_endpoint = "<search-endpoint>"
index_client = SearchIndexClient(
endpoint=search_endpoint,
credential=DefaultAzureCredential(),
)
knowledge_base = KnowledgeBase(
name="your-knowledge-base",
description="A knowledge base for product support content.",
knowledge_sources=[
KnowledgeSourceReference(name="your-knowledge-source"),
],
retrieve_defaults=KnowledgeBaseRetrieveDefaults(
max_runtime_in_seconds=45,
max_output_documents=8,
max_output_size_in_tokens=12000,
),
)
index_client.create_or_update_knowledge_base(knowledge_base)
Reference:SearchIndexClient, KnowledgeBase
PUT {{search-endpoint}}/knowledgebases/your-knowledge-base?api-version=2026-11-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
{
"name": "your-knowledge-base",
"description": "A knowledge base for product support content.",
"knowledgeSources": [
{
"name": "your-knowledge-source"
}
],
"retrieveDefaults": {
"maxRuntimeInSeconds": 45,
"maxOutputDocuments": 8,
"maxOutputSizeInTokens": 12000
}
}
参考:知识库 - 创建或更新
若要覆盖一个请求的存储值,请在预览检索请求中设置相应的maxRuntimeInSecondsmaxOutputDocuments字段和maxOutputSize字段。
为基于浏览器的检索调用配置 CORS
重要
跨域资源共享(CORS)允许基于浏览器的应用程序直接从服务请求数据。 根据 CORS 配置,外部网页可以使用用户的浏览器上下文访问或调用服务及其数据。 此访问权限可能会造成安全威胁。 启用 CORS 有你自己的风险。
从 2026-05-01-preview API 版本开始,知识库可为基于浏览器的应用程序定义 corsOptions,这些应用程序可直接通过 JavaScript 调用 retrieve 操作。 CORS 策略标识哪些浏览器源可以向知识库发送检索请求。
CORS 也受 2026-10-01支持。 如果省略 corsOptions,默认门户源将保持允许状态,但不允许使用未配置的自定义源。 CORS 控制浏览器访问,而不是授权:调用方仍需要检索内容的权限。
使用受信任的源列表或单个 "*" 值。 不要与其他来源混合 "*" 。
maxAgeInSeconds 控制预检缓存、接受非负值,默认值为 5 分钟。
对于 GA REST,请使用名为 product-docs-ks 的现有、已填充的搜索索引知识源和有权创建知识库的凭据。
PUT {{search-endpoint}}/knowledgebases('browser-chat-kb')?api-version=2026-10-01
Content-Type: application/json
Authorization: Bearer {{search-token}}
{
"name": "browser-chat-kb",
"knowledgeSources": [
{ "name": "product-docs-ks" }
],
"retrievalReasoningEffort": {
"kind": "minimal"
},
"outputMode": "extractiveData",
"corsOptions": {
"allowedOrigins": ["https://myapp.example.com"],
"maxAgeInSeconds": 300
}
}
参考:知识库 - 创建或更新
若要单独检查 CORS 策略,请执行以下操作:
- 使用
api-version=2026-10-01.. 将 OPTIONS 预检发送到此知识库的检索路由。 将https://myapp.example.com设置为Origin,请求使用 POST 方法,并标识检索请求使用的标头。 - 检查响应的允许源和预检缓存标头。 对于此配置的源,预期策略允许源并使用配置的 300 秒预检缓存期限。
- 使用未配置的自定义源重复此操作。 预期该源没有 CORS 权限;不要从特定的拒绝状态代码推断结果。
- 单独发送授权检索请求。 策略创建或预检不会授予内容访问权限。 HTTP 预检检查不会决定浏览器行为或调用方文档的权限。
SDK 示例更新现有知识库。 仅使用要更改的知识库。 每个示例读取现有模型、更改存储的配置、保存并读回。 这些定义操作不会验证 HTTP 或浏览器 CORS 强制实施。
保留现有的允许的来源,并将说明和预检缓存期限更改为 120 秒。 知识库必须已定义 CorsOptions。
using System;
using Azure;
using Azure.Search.Documents;
using Azure.Search.Documents.Indexes;
var endpoint = new Uri(Environment.GetEnvironmentVariable("SEARCH_ENDPOINT"));
var key = Environment.GetEnvironmentVariable("SEARCH_API_KEY");
var indexClient = new SearchIndexClient(
endpoint, new AzureKeyCredential(key));
var kb = (await indexClient.GetKnowledgeBaseAsync("<knowledge-base-name>")).Value;
kb.Description = "Updated CORS configuration.";
kb.CorsOptions.MaxAgeInSeconds = 120;
var updated = (await indexClient.CreateOrUpdateKnowledgeBaseAsync(kb)).Value;
var readback = (await indexClient.GetKnowledgeBaseAsync(updated.Name)).Value;
设置预期的允许的源和 60 秒预检缓存期限。
import os
from azure.core.credentials import AzureKeyCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import CorsOptions
index_client = SearchIndexClient(
endpoint=os.environ["SEARCH_ENDPOINT"],
credential=AzureKeyCredential(os.environ["SEARCH_API_KEY"]),
)
kb = index_client.get_knowledge_base("<knowledge-base-name>")
kb.cors_options = CorsOptions(
allowed_origins=["https://myapp.example.com"],
max_age_in_seconds=60,
)
updated = index_client.create_or_update_knowledge_base(kb)
readback = index_client.get_knowledge_base(updated.name)
PUT {{search-endpoint}}/knowledgebases/browser-chat-kb?api-version=2026-11-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
{
"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
}
}
查询知识库
若要验证配置的模型连接,请使用相同的 API 版本和知识库配置调用检索操作。 使用 LLM 支持的请求并检查非secret 模型活动和网关路由(如果适用)。 仅成功的创建或 GET 请求并不能验证模型调用。 对于普通查询,请使用 检索操作或 MCP 终结点。
删除知识库
如果不再需要知识库或需要在搜索服务上重新生成该知识库,请运行以下代码以删除该对象。
// Delete a knowledge base
using Azure.Search.Documents.Indexes;
var indexClient = new SearchIndexClient(
new Uri(searchEndpoint), credential);
await indexClient.DeleteKnowledgeBaseAsync(knowledgeBaseName);
Reference:SearchIndexClient
# Delete a knowledge base
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(
endpoint="<search-endpoint>",
credential=DefaultAzureCredential(),
)
index_client.delete_knowledge_base("<knowledge-base-name>")
Reference:SearchIndexClient
# Delete a knowledge base
DELETE {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
Authorization: Bearer {{search-access-token}}
参考:知识库 - 删除