ScaleDown
Vercel.AI.ScaleDown ports @ai-sdk/scaledown. It gives Decide.DecideAsync a decision model for ScaleDown’s Decisions API, and Extract.ExtractAsync an extraction model for its Extract API. Both capabilities are experimental, like Decide and Extract themselves.
var provider = ScaleDownProvider.Create(new ScaleDownProviderSettings { ApiKey = "..." });var result = await Decide.DecideAsync(new DecideRequest{ Model = provider.DecisionModel("classify-1"), State = "The export button crashes in Safari. It works in Chrome.", Questions = new Dictionary<string, DecisionQuestion> { ["hasWorkaround"] = new DecisionQuestion("boolean", "Can the user work around this issue?"), },});Provider
Section titled “Provider”ScaleDownProvider.Default reads SCALEDOWN_API_KEY when a request is made, not when the provider is created. It maps to scaledown and its alias scaleDown. ScaleDownProvider.Create(settings, handler) takes these ScaleDownProviderSettings:
| Setting | Meaning |
|---|---|
ApiKey |
API key, sent as x-api-key. Defaults to SCALEDOWN_API_KEY. |
ApiKeyProvider |
Resolves the API key on every request, as an async apiKey does upstream. Takes precedence over ApiKey. Models built with it cannot be serialized. |
BaseUrl |
Defaults to https://api.scaledown.xyz. A trailing slash is removed. |
Headers |
Sent with every request. Headers on the call win over them. |
AddScaleDown(configure) registers the provider with dependency injection. DecisionModel(id) and ExtractionModel(id) accept any model id. LanguageModel, EmbeddingModel, and ImageModel throw NoSuchModelError. The provider id of the decision model is scaledown.decision and the provider id of the extraction model is scaledown.extraction.
Decision model
Section titled “Decision model”DecisionScaleDownModel sends the model id, the state, and every question to POST {BaseUrl}/v1/scaledown in one request. classify selects the latest version and classify-1 pins version 1. Other ids are forwarded to ScaleDown for validation. It supports choice, boolean, and score questions. Any other type throws DecisionUnsupportedQuestionTypeError before the request.
- A
choicequestion returns the chosen option and its full probability distribution. A null option description sends the option name. - A
booleanquestion is sent as ScaleDown’snoultype, and thenoulanswer comes back as abooleananswer with aProbability. ScaleDown ignoresnoulcriteria, so they are added to the question instructions asAnswer yes when: ...andAnswer no when: ...lines. - A
scorequestion needs 2 to 10 non-null levels, otherwiseInvalidArgumentErroris thrown before the request. Scores are zero-based and can be fractional. - Text parts are joined with newlines. JSON state parts and structured instructions or criteria are sent as JSON text.
- A state can hold one JPEG, PNG, TIFF, or PDF file part with inline data. It is sent as a base64 document next to the text. Any other file, or a second one, throws
UnsupportedFunctionalityException. MaxOutputTokens,Reasoning, and everyproviderOptions.scaledownoption are not supported. Each is reported as anunsupportedwarning and not sent.
result.ProviderMetadata holds scaledown.confidence and scaledown.legend, keyed by question id, and scaledown.cost (USD) when ScaleDown supplies it. ScaleDownDecisionProviderMetadata describes that shape.
DecisionScaleDownModel.Serialize() and DecisionScaleDownModel.Deserialize(options) carry the model across a workflow boundary. A model restored without headers reads SCALEDOWN_API_KEY at call time.
A failed response throws ApiCallError with the detail text of the response. A response that does not fit the schema throws ApiCallError with the message Invalid JSON response.
Extraction model
Section titled “Extraction model”var result = await Extract.ExtractAsync(new ExtractRequest{ Model = ScaleDownProvider.Default.ExtractionModel("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" }, }, ProviderOptions = JsonDocument.Parse("""{"scaledown":{"threshold":0.5}}""").RootElement.Clone(),});ScaleDownExtractionModel calls POST {BaseUrl}/v1/chat/completions with a JSON schema for the fields. extract selects the latest model and extract-1 pins version 1. Both support text fields and list fields of text items, with the same text, JSON, and single-document inputs as the decision model. For document-only input, the model sends the non-empty user message that ScaleDown requires (Extract the requested fields from the document.).
- A
textfield returns the most confident verbatim match, or a nullTextwhen there is none. - A
listfield returns up to 30 distinct matches in source order, or nullTextItemswhen there are none. Each entry ofItemskeeps its confidence and source when ScaleDown reports them. - A text source converts ScaleDown’s UTF-8 byte offsets to UTF-16 offsets in the original state part. JSON parts have no source location. A document match cites the file when its location can be established. The OCR text is in
result.ProviderMetadataunderscaledown.ocrText. - ScaleDown extracts spans and does not normalize values.
number,date,time,datetime,duration, and lists of numbers or dates throwExtractionUnsupportedFieldTypeErrorbefore the request. Usetextfields to read amounts or dates verbatim. providerOptions.scaledown.thresholdsets the minimum confidence from 0 to 1 (default 0).ScaleDownExtractionModelOptionsdescribes it. A value outside that range throwsInvalidArgumentException.ReferenceDate,Reasoning, andMaxOutputTokensare not supported. Each is reported as anunsupportedwarning and not sent. ScaleDown does not returnPNullor guesses, so the model leaves them unset.
ScaleDownExtractionModel.Serialize() and ScaleDownExtractionModel.Deserialize(options) carry the model across a workflow boundary. Errors are the same as for the decision model: a failed response throws ApiCallError with the detail text, and content that is not valid extraction JSON throws InvalidResponseDataException.
Apache-2.0 · Copyright 2023 Vercel, Inc. · unofficial community port, not an official Vercel product