
提取模型现已登陆 AI Gateway。它们能从你提供的内容中找出具体细节,例如发票号码或联系人姓名,并将其作为独立字段返回,供你的代码使用。
ScaleDown Extract-1 是首个可用的提取模型。它可以从文本、图像和 PDF 中提取文本值或文本值列表。你可以用它来处理发票、合同和其他记录。
提取功能目前仅通过 AI SDK 提供。使用 [email protected] 或更高版本,可通过 experimental_extract 和模型 ID scaledown/extract-1 使用:
import { experimental_extract as extract } from 'ai';
const result = await extract({ model: 'scaledown/extract-1', state: 'Invoice INV-2041 from Acme Corp. Contacts: Ana, Bo.', fields: { invoiceNumber: { type: 'text', instructions: 'The invoice number' }, contacts: { type: 'list', items: 'text', instructions: 'Contact names' }, },});
console.log(result.answers.invoiceNumber.value); // 'INV-2041'console.log(result.answers.contacts.value); // ['Ana', 'Bo']
从文本中提取发票号码和联系人姓名。
fields 中的每个键在 result.answers 中都有对应条目。如果某个字段未找到,其值为 null。Extract-1 会为匹配项返回 0 到 1 的置信度分数,包括列表中的各个条目,因此你可以标记不确定的结果以供审核。对于文本输入,它还能返回字符偏移量,显示匹配出现的位置。
要从 PDF 或图像中提取,请将其作为文件部分传入 state:
import { readFile } from 'node:fs/promises';import { experimental_extract as extract } from 'ai';
const result = await extract({ model: 'scaledown/extract-1', state: [ { type: 'file', data: await readFile('./invoice.pdf'), mediaType: 'application/pdf', }, ], fields: { vendor: { type: 'text', instructions: 'The company that issued the invoice' }, total: { type: 'text', instructions: 'The total amount due' }, lineItems: { type: 'list', items: 'text', instructions: 'The line items' }, },});
console.log(result.answers);
从 PDF 中提取供应商、总额和明细项。
提取请求会出现在 AI Gateway 日志和支出跟踪中。请参阅提取快速入门了解设置和身份验证。