vercel 5小时前 · 2026-11-24 07:24:06 · 2 阅读
AI 网关现已支持信息抽取模型
Extraction models(信息抽取模型)现已登陆 AI Gateway。它们可以从你提供的内容中找出特定信息,比如发票号或联系人姓名,并以独立字段的形式返回,方便代码直接使用。
ScaleDown Extract-1 是首个上线的抽取模型,可以从文本、图片和 PDF 中抽取文本值或文本列表,适合处理发票、合同及其他各类记录。
目前抽取功能仅支持通过 AI SDK 使用。安装 ai@7.0.176 或更高版本后,调用 experimental_extract 并传入模型 ID scaledown/extract-1 即可:
1import { experimental_extract as extract } from 'ai';2
3const result = await extract({4 model: 'scaledown/extract-1',5 state: 'Invoice INV-2041 from Acme Corp. Contacts: Ana, Bo.',6 fields: {7 invoiceNumber: { type: 'text', instructions: 'The invoice number' },8 contacts: { type: 'list', items: 'text', instructions: 'Contact names' },9 },10});11
12console.log(result.answers.invoiceNumber.value); // 'INV-2041'13console.log(result.answers.contacts.value); // ['Ana', 'Bo']从文本中抽取发票号和联系人姓名。
fields 中的每个键都会在 result.answers 里有对应条目。如果某个字段未找到,其 value 为 null。Extract-1 会为每个匹配结果(包括列表中的单个条目)返回 0 到 1 之间的置信度分数,方便你对不确定的结果标记复核。对于文本输入,它还能返回匹配内容所在的字符偏移位置。
要从 PDF 或图片中抽取信息,将其作为 file part 传入 state 即可:
1import { readFile } from 'node:fs/promises';2import { experimental_extract as extract } from 'ai';3
4const result = await extract({5 model: 'scaledown/extract-1',6 state: [7 {8 type: 'file',9 data: await readFile('./invoice.pdf'),10 mediaType: 'application/pdf',11 },12 ],13 fields: {14 vendor: { type: 'text', instructions: 'The company that issued the invoice' },15 total: { type: 'text', instructions: 'The total amount due' },16 lineItems: { type: 'list', items: 'text', instructions: 'The line items' },17 },18});19
20console.log(result.answers);从 PDF 中抽取供应商、总金额和明细条目。
提取请求会出现在AI Gateway 日志和费用追踪中。查看提取快速入门了解配置和认证方法。
原始来源: vercel