註
Azure AI 搜尋服務 可透過 Azure 入口網站、REST API 及 Azure SDK 取得。 它同時也是 Foundry IQ 的基礎,這是一個管理式知識層,能將企業內容轉化為可重複使用、權限感知的知識庫,供 Microsoft Foundry 入口網站中的代理使用。
重要
標記(預覽)的功能、能力或屬性不受服務等級協議涵蓋,也不建議用於生產工作負載,且在正式上架前可能會有所變動或受限。 Azure AI 搜尋服務 預覽條款適用於所有預覽功能,無論是獨立功能還是正式推出功能的一部分。
在 Azure AI 搜尋服務 中,知識庫 是一個頂層物件,負責協調 智能檢索。 它定義要查詢哪些知識來源,以及檢索操作的預設行為。 在查詢時, 檢索方法 會針對知識庫執行已設定的檢索管線。
知識庫規定:
一個或多個指向可搜尋內容的知識來源。
用於查詢規劃、答案合成或 Web 內容摘要的選用 LLM。 支援的任務依 API 版本及知識來源類型而異。
自訂屬性控制路由、來源選擇與物件加密。
使用支援
| Azure portal | Microsoft Foundry 入口 | .NET SDK | Python SDK | Java 開發套件 | JavaScript SDK | REST API |
|---|---|---|---|---|---|---|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
先決條件
任何提供 Agent 擷取的區域中的 Azure AI 搜尋服務。 如果你使用 受管理身份 來進行基於角色的部署模型存取,你的搜尋服務必須是 Basic 等級或更高等級。
一個或多個 知識來源。 使用
2026-08-01-previewAPI 版本,以存取預覽知識來源,或將 LLM 與非 Web 知識來源搭配使用。 使用2026-04-01API 版本以獲得一般可用的知識來源及最小化的擷取。(條件)Azure OpenAI 採用 支援的 LLM 部署。 如果你的知識庫包含網路知識來源,則必須具備 LLM。 對於其他知識來源,在
2026-08-01-previewAPI 版本中,LLM 為選用項目;在2026-04-01API 版本中則不受支援。允許建立知識庫。 設定無金鑰驗證,並將搜尋服務參與者角色指派給您的使用者帳戶(建議),或使用管理員 API 金鑰。
若知識庫指定 LLM,搜尋服務必須擁有 受管理身分,並且在 Microsoft Foundry 資源上具備 Cognitive Services User 權限。
必備的
Azure.Search.Documents套件:關於
2026-08-01-preview功能,最新預覽包:dotnet add package Azure.Search.Documents --prerelease若要使用
2026-04-01功能,請採用最新的穩定套件:dotnet add package Azure.Search.Documents
對於無鑰匙認證,套件如下
Azure.Identity:dotnet add package Azure.Identity
必備的
azure-search-documents套件:關於
2026-08-01-preview功能,最新預覽包:pip install --pre azure-search-documents若要使用
2026-04-01功能,請採用最新的穩定套件:pip install azure-search-documents
對於無鑰匙認證,套件如下
azure-identity:pip install azure-identity
必要的搜尋服務 REST API 版本:
預覽功能: 2026-08-01-preview
適用於正式運作的功能:2026-04-01
對於無金鑰認證,請在
Authorization每個 HTTP 請求的標頭中加入 Microsoft Entra ID 令牌。
支援的模型
在 Foundry 模型中使用 Azure OpenAI 中的以下 LLM 之一。 Azure OpenAI 會決定你所選擇部署的區域可用性。 部署說明請參見 Foundry 入口網站 中的
GPT-4 家族已被棄用。 關於模型生命週期指引、退役日期及現況,請參見「模型退役與淘汰」及「模型退役時程-Microsoft Foundry」。
| 模型 | 支援的 API 版本 |
|---|---|
gpt-4o (已取代) |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-4o-mini (已取代) |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-4.1 (已取代) |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-4.1-mini (已取代) |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-4.1-nano (已取代) |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-5 |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-5-mini |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-5-nano |
2025-11-01-預覽,2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.1 |
2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.2 |
2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.4 |
2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.4-mini |
2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.4-nano |
2026-05-01-預覽,2026-08-01-預覽 |
gpt-5.5 |
2026-08-01-預覽 |
gpt-5.6-sol |
2026-08-01-預覽 |
gpt-5.6-terra |
2026-08-01-預覽 |
gpt-5.6-luna |
2026-08-01-預覽 |
設定存取權限
Azure AI 搜尋服務 需要在 Foundry Models 中存取 Azure OpenAI 的 LLM。 我們建議使用 Microsoft Entra ID 進行認證,並以角色為基礎存取授權。 要指派角色,您必須是 擁有者或使用者存取管理員。 如果不能用角色,改用基於金鑰的認證。
在模型提供者上,將 Cognitive Services User 指派給搜尋服務的受控識別。 如果你是在本地測試,請把同樣的角色分配給你的使用者帳號。
本地測試時,請依照 快速入門中的步驟操作:無需金鑰連接 以登入特定訂閱和租戶。 在
DefaultAzureCredential每個請求中使用 代替AzureKeyCredential,這應該與以下範例相似。// Authenticate using roles using Azure.Search.Documents.Indexes; using Azure.Identity; var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new DefaultAzureCredential());
在模型提供者上,將 Cognitive Services User 指派給搜尋服務的受控識別。 如果你是在本地測試,請把同樣的角色分配給你的使用者帳號。
本地測試時,請依照 快速入門中的步驟操作:無需金鑰連接 以登入特定訂閱和租戶。 在
DefaultAzureCredential每個請求中使用 代替AzureKeyCredential,這應該與以下範例相似。# Authenticate using roles from azure.identity import DefaultAzureCredential index_client = SearchIndexClient(endpoint = "<search-endpoint>", credential = DefaultAzureCredential())
在模型提供者上,將 Cognitive Services User 指派給搜尋服務的受控識別。 如果你是在本地測試,請把同樣的角色分配給你的使用者帳號。
本地測試時,請依照 快速啟動的步驟操作:無鑰匙連線 以獲得特定訂閱和租戶的個人存取權杖。 在每個請求中指定你的存取權杖,應該與以下範例相似。
# List indexes using roles GET {{search-endpoint}}/indexes?api-version=2026-04-01 Content-Type: application/json Authorization: Bearer {{search-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}");
}
參考資料:SearchIndexClient
# List knowledge bases by name
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
index_client = SearchIndexClient(endpoint = "<search-endpoint>", credential = DefaultAzureCredential())
for kb in index_client.list_knowledge_bases():
print(f" - {kb.name}")
參考資料:SearchIndexClient
# List knowledge bases
GET {{search-endpoint}}/knowledgebases?api-version={{api-version}}&$select=name
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
參考資料:知識庫列表
你也可以以名稱回傳單一知識庫,來檢視其 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);
參考資料:SearchIndexClient
# Get a knowledge base definition
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
import json
index_client = SearchIndexClient(endpoint = "<search-endpoint>", credential = DefaultAzureCredential())
kb = index_client.get_knowledge_base("<knowledge-base-name>")
print(json.dumps(kb.as_dict(), indent = 2))
參考資料: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-04-01 API 版本建立的知識庫,回傳的定義會比 2026-08-01-preview 更狹窄。 欲了解更多版本支援的屬性,請參閱 建立知識庫。
建立知識庫
重要
2026-04-01 API 版本僅接受一般可用的知識來源類型,並支援最小化的擷取性檢索。 它不支援僅預覽的功能,例如查詢規劃、答案綜合以及可設定的推理工作。 若要完整功能,請使用 2026-08-01-preview.
知識庫會將一或多個知識來源 (可搜尋內容) 連線到 Foundry Models 中來自 Azure OpenAI 的選用 LLM。 你設定的屬性會建立查詢執行和檢索回應的預設值。
建立知識庫後,你可以隨時更新其屬性。 如果知識庫正在使用中,更新會在下一次檢索時生效。
// Create a knowledge base
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
using Azure.Search.Documents.KnowledgeBases.Models;
using Azure.Identity;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new DefaultAzureCredential());
var aoaiParams = new AzureOpenAIVectorizerParameters
{
ResourceUri = new Uri(aoaiEndpoint),
DeploymentName = aoaiGptDeployment,
ModelName = aoaiGptModel,
};
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 = "Answer in two concise sentences.",
OutputMode = KnowledgeRetrievalOutputMode.AnswerSynthesis,
Models = { new KnowledgeBaseAzureOpenAIModel(azureOpenAIParameters: aoaiParams) },
RetrievalReasoningEffort = new KnowledgeRetrievalAutoReasoningEffort()
};
await indexClient.CreateOrUpdateKnowledgeBaseAsync(knowledgeBase);
Console.WriteLine($"Knowledge base '{knowledgeBase.Name}' created or updated successfully.");
參考資料:SearchIndexClient, 知識庫
# Create a knowledge base
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
AzureOpenAIVectorizerParameters,
KnowledgeBase,
KnowledgeBaseAzureOpenAIModel,
KnowledgeSourceReference,
)
from azure.search.documents.knowledgebases.models import (
KnowledgeRetrievalAutoReasoningEffort,
KnowledgeRetrievalOutputMode,
)
index_client = SearchIndexClient(endpoint = "<search-endpoint>", credential = DefaultAzureCredential())
aoai_params = AzureOpenAIVectorizerParameters(
resource_url = "<aoai-endpoint>",
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 = "Answer in two concise sentences.",
output_mode = KnowledgeRetrievalOutputMode.ANSWER_SYNTHESIS,
knowledge_sources = [
KnowledgeSourceReference(name = "hotels-ks"),
KnowledgeSourceReference(name = "earth-at-night-ks"),
],
models = [KnowledgeBaseAzureOpenAIModel(azure_open_ai_parameters = aoai_params)],
encryption_key = None,
retrieval_reasoning_effort = KnowledgeRetrievalAutoReasoningEffort(),
)
index_client.create_or_update_knowledge_base(knowledge_base)
print(f"Knowledge base '{knowledge_base.name}' created or updated successfully.")
參考資料:SearchIndexClient, 知識庫
# Create a knowledge base
PUT {{search-endpoint}}/knowledgebases/my-kb?api-version=2026-08-01-preview
Content-Type: application/json
Authorization: Bearer {{search-access-token}}
{
"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": "Answer in two concise sentences.",
"outputMode": "answerSynthesis",
"knowledgeSources": [
{
"name": "hotels-ks"
},
{
"name": "earth-at-night-ks"
}
],
"models" : [
{
"kind": "azureOpenAI",
"azureOpenAIParameters": {
"resourceUri": "{{aoai-endpoint}}",
"deploymentId": "gpt-5.4-mini",
"modelName": "gpt-5.4-mini"
}
}
],
"encryptionKey": null,
"retrievalReasoningEffort": {
"kind": "auto"
}
}
參考資料:知識庫 - 建立或更新
設定預設取回限制(預覽)
從 API 版本開始 2026-08-01-preview ,你可以使用可選 retrieveDefaults 物件將請求範圍的預設值儲存在知識庫中。 每個儲存的屬性僅在擷取請求省略對應請求欄位時適用:
| 存放財產 | 擷取請求欄位 |
|---|---|
maxRuntimeInSeconds |
maxRuntimeInSeconds |
maxOutputDocuments |
maxOutputDocuments |
maxOutputSizeInTokens |
maxOutputSize |
輸出權杖預算在儲存及覆寫時會使用不同的屬性名稱。 在 retrieveDefaults 中設定 maxOutputSizeInTokens,並在擷取要求中使用 maxOutputSize。
每個物業的有效價值依以下順序獨立確定:
- 擷取要求中的對應值。
- 知識庫
retrieveDefaults物件的價值。 - 當兩個層級都沒有此屬性時所採用的服務預設值。
以下範例使用一個名為 your-knowledge-source的現有搜尋索引知識來源。 它儲存 45 秒的執行預算,最多可儲存八個輸出文件,以及 12,000 個代幣的輸出預算。
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 options = new SearchClientOptions(
SearchClientOptions.ServiceVersion.V2026_08_01_Preview);
var indexClient = new SearchIndexClient(
new Uri(searchEndpoint),
new DefaultAzureCredential(),
options);
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);
參考資料:SearchIndexClient、 SearchClientOptions.ServiceVersion、 知識庫
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(),
api_version="2026-08-01-preview",
)
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)
參考資料:SearchIndexClient, 知識庫
PUT {{search-endpoint}}/knowledgebases/your-knowledge-base?api-version=2026-08-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
}
}
參考資料:知識庫 - 建立或更新
若要用 20 秒、一份文件和 5,000 個標記來覆蓋這些儲存的值,請參見 「驗證知識庫擷取預設值」。
設定 CORS 以用於瀏覽器檢索呼叫(預覽)
重要
跨來源資源共享(CORS)允許瀏覽器應用程式直接向服務請求資料。 根據您的 CORS 設定,外部網頁可能會利用使用者的瀏覽器上下文存取或調用服務及其資料。 這種存取可能造成安全威脅。 啟用 CORS 風險自負。
自 2026-05-01-preview API 版本起,知識庫可為以瀏覽器為基礎、直接從 JavaScript 呼叫 retrieve 動作的應用程式定義 corsOptions。 CORS 政策會識別哪些瀏覽器來源可以向知識庫發送檢索請求。
當你省略 corsOptions時,知識庫就沒有 CORS 政策,瀏覽器會封鎖跨來源檢索請求。
以下範例建立一個知識庫,允許從單一瀏覽器來源檢索請求。
using Azure.Identity;
using Azure.Search.Documents.Indexes;
using Azure.Search.Documents.Indexes.Models;
var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new DefaultAzureCredential());
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);
參考資料:CorsOptions, 知識庫
from azure.identity import DefaultAzureCredential
from azure.search.documents.indexes import SearchIndexClient
from azure.search.documents.indexes.models import (
CorsOptions,
KnowledgeBase,
KnowledgeSourceReference,
)
index_client = SearchIndexClient(endpoint="<search-endpoint>", credential=DefaultAzureCredential())
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)
參考資料:CorsOptions, 知識庫
PUT {{search-endpoint}}/knowledgebases/browser-chat-kb?api-version=2026-08-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
}
}
查詢知識庫
建立知識庫後,呼叫 擷取動作或 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.");
參考資料: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>")
print(f"Knowledge base deleted successfully.")
參考資料:SearchIndexClient
# Delete a knowledge base
DELETE {{search-endpoint}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
Authorization: Bearer {{search-access-token}}
參考資料:知識庫 - 刪除