Skip to content
GenAIScript logo A yellow square with genai text

Generative AI Scripting

Prompting is Coding

Programmatically assemble prompts for LLMs using JavaScript.

extract-data.genai.mjs
// define the context
def("FILE", env.files, { endsWith: ".pdf" })
// structure the data
const schema = defSchema("DATA", { type: "array", items: { type: "string" } })
// assign the task
$`Analyze FILE and extract data to JSON.`
// save results to file
defFileOutput("*.pdf.txt", "Extracted data", { schema })
// tools
defTool("weather", "live weahter", { city: "Paris" }, /* schema */
async ({ city }) => { ... "sunny" }) /* callback */
...

Next steps

Configure your LLMs

Configure the secrets to access your LLMs.

Write your first script

Follow Getting Started to write your first script.

A screenshot of VSCode with a genaiscript opened

Features

GenAIScript brings essential LLM prompt tooling into a cohesive scripting environment.

Stylized JavaScript

Build prompts programmatically using JavaScript or TypeScript.

def("FILE", env.files, { endsWith: ".pdf" })
$`Summarize FILE. Today is ${new Date()}.`

Reuse and Share Scripts

Scripts are files! They can be versioned, shared, forked, …

  • Directorygenaisrc
    • my-script.genai.mjs
    • another-great-script.genai.mjs

Data Schemas

Define, validate, repair data using schemas.

const data = defSchema("MY_DATA",
{ type: "array", items: { ... }, })
$`Extract data from files using ${data} schema.`

Ingest text from PDFs, DOCX, ...

Manipulate PDFs, DOCX, …

// automatically convert to text
def("PDF", env.files, { endsWith: ".pdf" })
// or parse and process
const { pages } = await parsers.PDF(env.files[0])

Ingest tables from CSV, XLSX, ..

Manipulate tabular data from CSV, XLSX, …

// automatically convert to text
def("DATA", env.files, {
endsWith: ".csv",
// take top 100 rows
sliceHead: 100,
})
// or parse to JavaScript object array
const rows = await parsers.CSV(env.files[0])
// render as markdown table
defData("ROWS", rows, { sliceHead: 100 })

Generate Files

Extract files and diff from the LLM output. Preview changes in Refactoring UI.

$`Save the result in poem.txt.`
FILE ./poem.txt
```txt
The quick brown fox jumps over the lazy dog.
```
  • poem.txt extracted by genaiscript

File search

Grep or fuzz search files

const { files } = await workspace.grep(/[a-z][a-z0-9]+/, "**/*.md")

Browser automation

Browse and scrape the web with Playwright.

const page = await host.browse("https://...")
const table = await page.locator("table[...]").innerHTML()
def("TABLE", HTML.convertToMarkdown(table))

RAG built-in

Vector search.

const { files } = await retrieval.vectorSearch("cats", "**/*.md")

GitHub Models and GitHub Copilot

Run models through GitHub using GitHub Models or GitHub Copilot.

script({ ..., model: "github:gpt-4o" })

LLM Tools

Register JavaScript functions as LLM tools

defTool("weather", "live weahter",
{ city: "Paris" }, // schema
async ({ city }) => // callback
{ ... "sunny" }
)

or use built-in @agentic tools

import { WeatherClient } from "@agentic/weather"
defTool(new WeatherClient())

Code Interpreter

Let the LLM run code in a sandboxed execution environment.

script({ tools: ["python_code_interpreter"] })

Containers

Run code in Docker containers.

const c = await host.container({
image: "python:alpine",
})
const res = await c.exec("python", ["--version"])

LLM Composition

Run LLMs to build your LLM prompts.

// summarize each files individually
for (const file of env.files) {
const { text } = await runPrompt((_) => {
_.def("FILE", file)
_.$`Summarize the FILE.`
})
// use result in main prompt
_.def("SUMMARY", text)
}
// use summary
$`Summarize all the summaries.`

Prompty

Run or convert Prompty files using GenAIScript.

poem.prompty
---
name: poem
---
Write a short poem.

Automate with CLI

Automate using the CLI, integrate reports in your CI/CD pipeline.

npx genaiscript run tlaplus-linter "*.tla"

Pull Request Reviews

Integrate into your Pull Requests checks through comments, reviews or description updates. Supports GitHub Actions and Azure DevOps pipelines.

npx genaiscript ... --pull-request-reviews

Tests and Evals

Build reliable prompts using tests and evals powered by promptfoo.

script({ ..., tests: {
files: "penguins.csv",
rubric: "is a data analysis report",
facts: "The data refers about penguin population in Antartica.",
}})
Visual Studio Test Explorer opened with a few genaiscript tests.