Skip to content

Extraction

Extraction models return typed values from text and files, such as an invoice number or a list of names. Extract.ExtractAsync runs them. It is experimental.

These examples use scaledown/extract-1 through the Vercel AI Gateway. It supports text fields and list fields of text items.

var result = await Extract.ExtractAsync(
new ExtractRequest
{
Model = GatewayProvider.Create().ExtractionModel("scaledown/extract-1"),
State = "Invoice INV-2041 from Acme Corp. Contacts: Ana, Bo.",
Fields = new Dictionary<string, ExtractionField>
{
["invoiceNumber"] = new("text", DecisionInput.From("The invoice number")),
["vendor"] = new("text", DecisionInput.From("Company that issued the invoice")),
["contacts"] = new("list", DecisionInput.From("Contact names")) { Items = "text" },
},
}
);

snippet source | anchor

Pass the document as a file part:

var result = await Extract.ExtractAsync(
new ExtractRequest
{
Model = GatewayProvider.Create().ExtractionModel("scaledown/extract-1"),
State = DecisionState.From([
new DecisionFilePart(
"application/pdf",
FilePartInput.FromBytes(File.ReadAllBytes(path))
),
]),
Fields = new Dictionary<string, ExtractionField>
{
["vendor"] = new("text", DecisionInput.From("Company that issued the invoice")),
["total"] = new("text", DecisionInput.From("The invoice total")),
["lineItems"] = new("list", DecisionInput.From("Names of the line items"))
{
Items = "text",
},
},
}
);

snippet source | anchor

Set AI_GATEWAY_API_KEY, then run these from the repository root. The Gateway account needs a positive credit balance.

Terminal window
dotnet run --project examples/ExtractGatewayBasic
dotnet run --project examples/ExtractGatewayDocument

OpenAI, Anthropic, Google, and ScaleDown also create extraction models with ExtractionModel(id). The Extraction guide lists the field types, answers, and errors.

Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product