Azure.AI.DocumentIntelligence(.NET)是用于从文档中提取文本、表格和结构化数据的 Azure AI 文档智能 SDK,支持预构建模型与自定义模型。
【安装与配置】 通过以下命令安装: dotnet add package Azure.AI.DocumentIntelligence dotnet add package Azure.Identity 当前版本为 v1.0.0(GA)。 所需环境变量: DOCUMENT_INTELLIGENCE_ENDPOINT=https://<resource-name>.cognitiveservices.azure.com/ DOCUMENT_INTELLIGENCE_API_KEY=<your-api-key> BLOB_CONTAINER_SAS_URL=https://<storage>.blob.core.windows.net/<container>?<sas-token>
【认证方式】
- Microsoft Entra ID(推荐):使用 DefaultAzureCredential 构造 DocumentIntelligenceClient。注意 Entra ID 需要自定义子域名(如 https://<resource-name>.cognitiveservices.azure.com/),不能使用区域端点。
- API Key:使用 AzureKeyCredential 传入 API Key 进行认证。
【客户端类型】 DocumentIntelligenceClient:用于分析文档、分类文档。 DocumentIntelligenceAdministrationClient:用于构建和管理自定义模型与分类器。
【预构建模型】 prebuilt-read:提取文本、语言、手写内容。 prebuilt-layout:提取文本、表格、选择标记与结构。 prebuilt-invoice:提取发票字段(供应商、条目、总额)。 prebuilt-receipt:提取收据字段(商户、条目、总额)。 prebuilt-idDocument:提取身份证件字段(姓名、出生日期、地址)。 prebuilt-businessCard:提取名片字段。 prebuilt-tax.us.w2:提取 W-2 税表字段。 prebuilt-healthInsuranceCard.us:提取健康保险卡字段。
【核心工作流】
- 分析发票:调用 AnalyzeDocumentAsync 并传入 prebuilt-invoice 模型 ID 和文档 URI,通过 result.Documents 遍历字段,可读取 VendorName(字符串类型)和 InvoiceTotal(货币类型),并可通过 Items 列表字段提取行项目。每个字段可访问 Confidence 置信度。
- 提取布局:使用 prebuilt-layout 模型,可遍历 result.Pages 获取每页的行与词,遍历 result.Tables 获取表格的行列数及各单元格内容。
- 分析收据:使用 prebuilt-receipt 模型,可提取 MerchantName、Total(货币金额)和 TransactionDate 等字段。
- 构建自定义模型:使用 DocumentIntelligenceAdministrationClient 的 BuildDocumentModelAsync,传入模型 ID、构建模式和 BlobContentSource,返回 DocumentModelDetails,包含模型 ID、创建时间及各文档类型对应的字段架构与置信度。
- 构建文档分类器:通过 BuildClassifierAsync 传入分类器 ID 和各文档类型的 BlobContentSource(可设置 Prefix),返回 DocumentClassifierDetails。
- 分类文档:使用 ClassifyDocumentOptions 传入分类器 ID 和文档 URI,调用 ClassifyDocumentAsync,从 result.Documents 读取文档类型与置信度。
- 管理模型:支持获取资源配置详情、获取指定模型、列出模型和删除模型。
【关键类型】 DocumentIntelligenceClient、DocumentIntelligenceAdministrationClient、AnalyzeResult、AnalyzedDocument、DocumentField、DocumentFieldType(String、Date、Number、Currency 等)、DocumentPage、DocumentTable、DocumentModelDetails、BlobContentSource。
【构建模式】 DocumentBuildMode.Template:适用于固定版式文档(表单)。 DocumentBuildMode.Neural:适用于可变版式文档。
【最佳实践】 生产环境使用 DefaultAzureCredential。 复用客户端实例——客户端是线程安全的。 处理长时间运行的操作——为简单起见可使用 WaitUntil.Completed。 检查字段置信度——始终验证 Confidence 属性。 使用合适的模型——常见文档用预构建模型,专用文档用自定义模型。 使用自定义子域名——Entra ID 认证所必需。
【错误处理】 使用 try/catch 捕获 RequestFailedException,可读取 ex.Status 和 ex.Message。
【相关 SDK】 Azure.AI.DocumentIntelligence:文档分析(本 SDK)。 Azure.AI.FormRecognizer:旧版 SDK(已弃用),建议改用 DocumentIntelligence。
【参考链接】 NuGet 包:https://www.nuget.org/packages/Azure.AI.DocumentIntelligence API 参考:https://learn.microsoft.com/dotnet/api/azure.ai.documentintelligence GitHub 示例:https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/documentintelligence/Azure.AI.DocumentIntelligence/samples Document Intelligence Studio:https://documentintelligence.ai.azure.com/ 预构建模型:https://aka.ms/azsdk/formrecognizer/models
【限制】 仅当任务明确匹配上述范围时使用。不要将输出视为环境特定验证、测试或专家审查的替代品。如果缺少所需输入、权限、安全边界或成功标准,应停止并请求澄清。