从 Azure Blob 存储和 ADLS Gen2 创建 Blob 知识源

注释

此智能检索功能在 2026-04-01 REST API 版本中通过编程接口普遍可用。 Azure门户继续提供对所有代理检索功能的仅限预览的访问权限。

使用 Blob 知识来源在代理检索管道中为 Azure Blob 内容编制索引并对其进行查询。 知识源 是独立创建的,在 知识库中引用,并在代理或聊天机器人在查询时调用 检索作 时用作基础数据。

创建 Blob 知识源时,请指定外部数据源、模型和属性以自动生成以下Azure AI 搜索对象:

  • 表示 Blob 容器的数据源。
  • 一个技能包,用于对来自容器内的多模态内容进行分块处理和可选矢量化。
  • 存储扩充内容并满足代理检索条件的索引。
  • 使用先前的对象来驱动索引和扩展管道的索引器。

注释

如果在 Azure 存储中的文档(blob)级别指定了用户访问,知识源可以将权限元数据转发到 Azure AI 搜索中的索引内容。 有关详细信息,请参阅 ADLS Gen2 权限元数据Blob RBAC 范围

使用支持

Azure 门户 .NET SDK Python SDK Java SDK JavaScript SDK REST API
✔️ ✔️ ✔️ ✔️ ✔️ ✔️

先决条件

检查现有知识来源

知识来源是顶级可重用对象。 了解现有知识源有助于重复使用或命名新对象。

运行以下代码,按名称和类型列出知识源。

// List knowledge sources by name and type
using Azure.Search.Documents.Indexes;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);
var knowledgeSources = indexClient.GetKnowledgeSourcesAsync();

Console.WriteLine("Knowledge Sources:");

await foreach (var ks in knowledgeSources)
{
    Console.WriteLine($"  Name: {ks.Name}, Type: {ks.GetType().Name}");
}

还可以按名称返回单个知识来源以查看其 JSON 定义。

using Azure.Search.Documents.Indexes;
using System.Text.Json;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), credential);

// Specify the knowledge source name to retrieve
string ksNameToGet = "earth-knowledge-source";

// Get its definition
var knowledgeSourceResponse = await indexClient.GetKnowledgeSourceAsync(ksNameToGet);
var ks = knowledgeSourceResponse.Value;

// Serialize to JSON for display
var jsonOptions = new JsonSerializerOptions 
{ 
    WriteIndented = true,
    DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.Never
};
Console.WriteLine(JsonSerializer.Serialize(ks, ks.GetType(), jsonOptions));

知识来源是顶级可重用对象。 了解现有知识源有助于重复使用或命名新对象。

运行以下代码,按名称和类型列出知识源。

# List knowledge sources by name and type
import requests
import json

endpoint = "{search_url}/knowledgesources"
params = {"api-version": "2025-11-01-preview", "$select": "name, kind"}
headers = {"api-key": "{api_key}"}

response = requests.get(endpoint, params = params, headers = headers)
print(json.dumps(response.json(), indent = 2))

还可以按名称返回单个知识来源以查看其 JSON 定义。

# Get a knowledge source definition
import requests
import json

endpoint = "{search_url}/knowledgesources/{knowledge_source_name}"
params = {"api-version": "2025-11-01-preview"}
headers = {"api-key": "{api_key}"}

response = requests.get(endpoint, params = params, headers = headers)
print(json.dumps(response.json(), indent = 2))

知识来源是顶级可重用对象。 了解现有知识源有助于重复使用或命名新对象。

使用 知识源 - 获取(REST API) 按名称和类型列出知识源。

### List knowledge sources by name and type
GET {{search-url}}/knowledgesources?api-version=2025-11-01-preview&$select=name,kind
api-key: {{api-key}}

还可以按名称返回单个知识来源以查看其 JSON 定义。

### Get a knowledge source definition
GET {{search-url}}/knowledgesources/{{knowledge-source-name}}?api-version=2025-11-01-preview
api-key: {{api-key}}

注释

要使用 ingestionPermissionOptions 实施文档级权限强制,需 2025-11-01-preview API 版本。 2026-04-01 不支持此功能。

特定于源的属性

对于 2025-11-01-preview 和 2026-04-01 API 版本,可以传递以下属性来创建 Blob 知识源。

Name Description 类型 可编辑 必需
Name 知识源的名称,该名称在知识源集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 String
Description 知识源的说明。 String
EncryptionKey 客户 管理的密钥 ,用于加密知识源和生成的对象中的敏感信息。 物体
AzureBlobParameters 特定于 blob 知识源的参数:ConnectionStringContainerNameFolderPathIsADLSGen2 物体
ConnectionString 基于密钥的 连接字符串 ,或者,如果使用托管标识,则为资源 ID。 String
ContainerName Blob 存储容器的名称。 String
FolderPath 容器中的文件夹。 String
IsADLSGen2 默认值为 False。 如果使用的是 ADLS Gen2 存储帐户,则设置为 True 布尔
Name Description 类型 可编辑 必需
name 知识源的名称,该名称在知识源集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 String
description 知识源的说明。 String
encryption_key 客户 管理的密钥 ,用于加密知识源和生成的对象中的敏感信息。 物体
azure_blob_parameters 特定于 blob 知识源的参数:connection_stringcontainer_namefolder_pathis_adls_gen2 物体
connection_string 基于密钥的 连接字符串 ,或者,如果使用托管标识,则为资源 ID。 String
container_name Blob 存储容器的名称。 String
folder_path 容器中的文件夹。 String
is_adls_gen2 默认值为 False。 如果使用的是 ADLS Gen2 存储帐户,则设置为 True 布尔
Name Description 类型 可编辑 必需
name 知识源的名称,该名称在知识源集合中必须是唯一的,并遵循 Azure AI 搜索中对象的 命名准则 String
kind 知识源的类型,其为azureBlob在本例中。 String
description 知识源的说明。 String
encryptionKey 客户 管理的密钥 ,用于加密知识源和生成的对象中的敏感信息。 物体
azureBlobParameters 特定于 blob 知识源的参数:connectionStringcontainerNamefolderPathisADLSGen2 物体
connectionString 基于密钥的 连接字符串 ,或者,如果使用托管标识,则为资源 ID。 String
containerName Blob 存储容器的名称。 String
folderPath 容器中的文件夹。 String
isADLSGen2 默认值为 false。 如果使用的是 ADLS Gen2 存储帐户,则设置为 true 布尔

检查引入状态

运行以下代码来监视引入进度和运行状况,包括知识源类型以及生成索引器管道并填充搜索索引的知识源的详细索引错误。

using Azure.Search.Documents.Indexes;
using System.Text.Json;

var indexClient = new SearchIndexClient(new Uri(searchEndpoint), new AzureKeyCredential(apiKey));

// Get knowledge source ingestion status
var statusResponse = await indexClient.GetKnowledgeSourceStatusAsync(knowledgeSourceName);
var status = statusResponse.Value;

// Serialize to JSON for display
var json = JsonSerializer.Serialize(status, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(json);

Reference:SearchIndexClient

# Check knowledge source ingestion status
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"))

status = index_client.get_knowledge_source_status("knowledge_source_name")
print(json.dumps(status.as_dict(), indent=2))

Reference:SearchIndexClient

### Check knowledge source ingestion status
GET {{search-url}}/knowledgesources/{{knowledge-source-name}}/status?api-version={{api-version}}
api-key: {{api-key}}
Content-Type: application/json 

参考:知识源 - 获取状态

包含引入参数且正在主动引入内容的请求的响应可能如以下示例所示。

{
  "kind": "azureBlob",
  "synchronizationStatus": "active",
  "synchronizationInterval": "1d",
  "currentSynchronizationState": {
    "startTime": "2026-04-10T19:30:00Z",
    "itemUpdatesProcessed": 1100,
    "itemsUpdatesFailed": 100,
    "itemsSkipped": 1100,
    "errors": [
      {
        "key": "Item id 1",
        "docURL": "https://contoso.blob.core.chinacloudapi.cn/contracts/2024/Q4/doc-00023.csv",
        "statusCode": 400,
        "componentName": "DocumentExtraction.AzureBlob.MyDataSource",
        "errorMessage": "Could not read the value of column 'foo' at index '0'.",
        "details": "The file could not be parsed.",
        "documentationLink": "https://go.microsoft.com/fwlink/?linkid=2049388"
      }
    ]
  },
  "lastSynchronizationState": {
    "status": "partialSuccess",
    "startTime": "2026-04-09T19:30:00Z",
    "endTime": "2026-04-09T19:40:01Z",
    "itemUpdatesProcessed": 1100,
    "itemsUpdatesFailed": 100,
    "itemsSkipped": 1100,
    "errors": null
  },
  "statistics": {
    "totalSynchronizations": 25,
    "averageSynchronizationDuration": "00:15:20",
    "averageItemsProcessedPerSynchronization": 500
  }
}

注释

从 2026-04-01 API 版本开始,文档级错误详细信息的 kind 属性和 currentSynchronizationState.errors[] 数组即可使用。 对于早期 API 版本,不会返回这些字段。 该 lastSynchronizationState.status 字段也是 2026-04-01 中的新增字段。

查看创建的对象

创建 Blob 知识源时,搜索服务还会创建索引器、索引、技能集和数据源。 不建议编辑这些对象,因为引入错误或不兼容可能会中断管道。

创建知识源后,响应会列出已创建的对象。 这些对象是根据固定模板创建的,其名称基于知识源的名称。 无法更改对象名称。

建议使用 Azure 门户验证输出创建。 工作流为:

  1. 检查索引器是否有成功或失败的消息。 此处会显示连接或配额错误。
  2. 检查可搜索内容的索引。 使用搜索资源管理器运行查询。
  3. 检查技能集,了解如何对内容进行分块和选择性地矢量化。
  4. 检查数据源以获取连接详细信息。 我们的示例使用 API 密钥来简单起见,但可以使用 Microsoft Entra ID 进行身份验证,并使用基于角色的访问控制进行授权。

分配给知识库

如果对知识源感到满意,请继续执行下一步:在 知识库中指定知识库中的知识源。

配置知识库后,使用 检索作 查询知识源。

Tip

若要强制实施文档级权限,请在创建此知识源时设置 ingestionPermissionOptions ,然后在检索请求中包含用户的访问令牌。 有关详细信息,请参阅 在查询时强制实施权限

删除知识来源

在删除知识库之前,必须删除引用它的任何知识库或更新知识库定义以删除引用。 对于生成索引和索引器管道的知识源,所有 生成的对象 都会被删除。 但是,如果使用现有索引创建知识源,则不会删除索引。

如果尝试删除正在使用的知识源,该作将失败并返回受影响的知识库列表。

要删除知识源,请执行以下步骤:

  1. 获取搜索服务上所有知识库的列表。

    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

    示例响应可能如下所示:

     Knowledge Bases:
       - earth-knowledge-base
       - hotels-sample-knowledge-base
       - my-demo-knowledge-base
    
  2. 获取单个知识库定义以检查知识源引用。

    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

    示例响应可能如下所示:

     {
       "Name": "earth-knowledge-base",
       "KnowledgeSources": [
         {
           "Name": "earth-knowledge-source"
         }
       ],
       "Models": [
         {}
       ],
       "RetrievalReasoningEffort": {},
       "OutputMode": {},
       "ETag": "\u00220x8DE278629D782B3\u0022",
       "EncryptionKey": null,
       "Description": null,
       "RetrievalInstructions": null,
       "AnswerInstructions": null
     }
    
  3. 删除知识库,或者如果有多个知识库,请更新知识库以删除源。 此示例显示删除。

    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

  4. 删除知识来源。

    await indexClient.DeleteKnowledgeSourceAsync(knowledgeSourceName);
    System.Console.WriteLine($"Knowledge source '{knowledgeSourceName}' deleted successfully.");
    

    Reference:SearchIndexClient

  1. 获取搜索服务上所有知识库的列表。

    # Get knowledge bases
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.indexes import SearchIndexClient
    
    index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
    
    print("Knowledge Bases:")
    for kb in index_client.list_knowledge_bases():
        print(f"  - {kb.name}")
    

    Reference:SearchIndexClient

    示例响应可能如下所示:

     {
         "@odata.context": "https://my-search-service.search.chinacloudapi.cn/$metadata#knowledgebases(name)",
         "value": [
         {
             "name": "my-kb"
         },
         {
             "name": "my-kb-2"
         }
         ]
     }
    
  2. 获取单个知识库定义以检查知识源引用。

    # Get a knowledge base definition
    from azure.core.credentials import AzureKeyCredential
    from azure.search.documents.indexes import SearchIndexClient
    
    index_client = SearchIndexClient(endpoint = "search_url", credential = AzureKeyCredential("api_key"))
    kb = index_client.get_knowledge_base("knowledge_base_name")
    print(kb)
    

    Reference:SearchIndexClient

    示例响应可能如下所示:

     {
       "name": "my-kb",
       "description": null,
       "retrievalInstructions": null,
       "answerInstructions": null,
       "outputMode": null,
       "knowledgeSources": [
         {
           "name": "my-blob-ks",
         }
       ],
       "models": [],
       "encryptionKey": null,
       "retrievalReasoningEffort": {
         "kind": "low"
       }
     }
    
  3. 删除知识库,或者如果有多个知识库,请更新知识库以删除源。 此示例显示删除。

    # 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

  4. 删除知识来源。

    # Delete a knowledge source
    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_source("knowledge_source_name")
    print(f"Knowledge source deleted successfully.")
    

    Reference:SearchIndexClient

  1. 获取搜索服务上所有知识库的列表。

    ### Get knowledge bases
    GET {{search-url}}/knowledgebases?api-version={{api-version}}&$select=name
    api-key: {{api-key}}
    

    参考:知识库 - 列表

    示例响应可能如下所示:

     {
         "@odata.context": "https://my-search-service.search.chinacloudapi.cn/$metadata#knowledgebases(name)",
         "value": [
         {
             "name": "my-kb"
         },
         {
             "name": "my-kb-2"
         }
         ]
     }
    
  2. 获取单个知识库定义以检查知识源引用。

    ### Get a knowledge base definition
    GET {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
    api-key: {{api-key}}
    

    参考:知识库 - 获取

    示例响应可能如下所示:

     {
       "name": "my-kb",
       "description": null,
       "retrievalInstructions": null,
       "answerInstructions": null,
       "outputMode": null,
       "knowledgeSources": [
         {
           "name": "my-blob-ks",
         }
       ],
       "models": [],
       "encryptionKey": null,
       "retrievalReasoningEffort": {
         "kind": "low"
       }
     }
    
  3. 删除知识库,或者如果有多个知识库,请更新知识库以删除源。 此示例显示删除。

    ### Delete a knowledge base
    DELETE {{search-url}}/knowledgebases/{{knowledge-base-name}}?api-version={{api-version}}
    api-key: {{api-key}}
    

    参考:知识库 - 删除

  4. 删除知识来源。

    ### Delete a knowledge source
    DELETE {{search-url}}/knowledgesources/{{knowledge-source-name}}?api-version={{api-version}}
    api-key: {{api-key}}
    

    参考:知识源 - 删除