
关于
使用预构建和自定义模型从文档中提取文本、表格和结构化数据。
name: azure-ai-document-intelligence-ts description: "使用预构建和自定义模型从文档中提取文本、表格和结构化数据。" risk: unknown source: community date_added: "2026-02-27"
Azure 文档智能 REST SDK(TypeScript)
使用预构建和自定义模型从文档中提取文本、表格和结构化数据。
安装
npm install @azure-rest/ai-document-intelligence @azure/identity
环境变量
DOCUMENT_INTELLIGENCE_ENDPOINT=https://<resource>.cognitiveservices.azure.com
DOCUMENT_INTELLIGENCE_API_KEY=<api-key>
认证
重要:这是REST客户端。DocumentIntelligence 是函数,不是类。
DefaultAzureCredential
import DocumentIntelligence from "@azure-rest/ai-document-intelligence";
import { DefaultAzureCredential } from "@azure/identity";
const client = DocumentIntelligence(
process.env.DOCUMENT_INTELLIGENCE_ENDPOINT!,
new DefaultAzureCredential()
);
API Key
import DocumentIntelligence from "@azure-rest/ai-document-intelligence";
const client = DocumentIntelligence(
process.env.DOCUMENT_INTELLIGENCE_ENDPOINT!,
{ key: process.env.DOCUMENT_INTELLIGENCE_API_KEY! }
);
分析文档(URL)
import DocumentIntelligence, {
isUnexpected,
getLongRunningPoller,
AnalyzeOperationOutput
} from "@azure-rest/ai-document-intelligence";
const initialResponse = await client
.path("/documentModels/{modelId}:analyze", "prebuilt-layout")
.post({
contentType: "application/json",
body: { urlSource: "https://example.com/document.pdf" },
queryParameters: { locale: "en-US" }
});
if (isUnexpected(initialResponse)) {
throw initialResponse.body.error;
}
const poller = getLongRunningPoller(client, initialResponse);
const result = (await poller.pollUntilDone()).body as AnalyzeOperationOutput;
console.log("Pages:", result.analyzeResult?.pages?.length);
分析文档(本地文件)
import { readFile } from "node:fs/promises";
const fileBuffer = await readFile("./document.pdf");
const base64Source = fileBuffer.toString("base64");
const initialResponse = await client
.path("/documentModels/{modelId}:analyze", "prebuilt-invoice")
.post({
contentType: "application/json",
body: { base64Source }
});
预构建模型
| 模型ID | 用途 |
|--------|------|
| prebuilt-layout | 文本、表格、选择标记、图形 |
| prebuilt-read | 纯文本提取(OCR) |
| prebuilt-invoice | 发票字段提取 |
| prebuilt-receipt | 收据字段提取 |
| prebuilt-idDocument | 身份证件 |
| prebuilt-businessCard | 名片 |
| prebuilt-tax.us.w2 | 美国W-2税表 |
处理结果
// 遍历页面
for (const page of result.analyzeResult?.pages ?? []) {
console.log(`第 ${page.pageNumber} 页: ${page.words?.length} 个词`);
}
// 遍历表格
for (const table of result.analyzeResult?.tables ?? []) {
console.log(`表格: ${table.rowCount} 行 x ${table.columnCount} 列`);
for (const cell of table.cells) {
console.log(`[${cell.rowIndex},${cell.columnIndex}]: ${cell.content}`);
}
}
常见问题
- 认证失败:确保端点URL不含尾部斜杠
- 模型不支持:检查API版本是否支持所选模型
- 文件过大:最大文件大小为500MB,最多2000页
兼容工具
Claude CodeCursor
标签
后端开发
