Skip to content
A flat, minimalist 8-bit illustration at a size of 128x128 pixels displays a geometric file folder containing structured data sheets, each shown as rectangles with horizontal lines. These sheets emerge from a vivid, abstract text block and a simple image icon. Surrounding the folder are clean, minimal circuit or chip symbols indicating AI, along with lines or shapes representing data connections. The entire image is created using just five distinct corporate colors and lacks any background, shadows, or gradients.

Cast

The cast function in GenAIScript allows you to convert text or images to structured data. It provides a simple interface to leverage the power of LLMs for extracting data from unstructured text and images.

cast is defined in the GenAIScript runtime and needs to be imported. It takes the unstructure text (or files), a JSON schema and returns the extract data (or error).

import { cast } from "genaiscript/runtime"
const { data } = await cast(
"The quick brown fox jumps over the lazy dog.; jumps",
{
type: "object",
properties: {
partOfSpeech: { type: "string" },
},
},
{
instructions: `You will be presented with a sentence and a word contained
in that sentence. You have to determine the part of speech for a given word`,
}
)

You can pass a function that takes a prompt context and build the DATA variable programmatically. This allows you to select files, images and other GenAIScript options.

const res = await cast(_ => {
_.defImages('DATA', img)
}, ...)

The cast function uses the cast model alias by default. You can modify this alias or specify another model in the options.

const res = await cast("...", {
model: "large",
})

The options are passed internally to the inline prompt and can be used to modify the behavior of the LLM.

This function is inspired from Marvin.