Skip to content
GenAIScript logo A yellow square with genai text

Generative AI Scripting

Prompting is Coding

Programmatically assemble prompts for LLMs using JavaScript. Orchestrate LLMs, tools, and data in a single script.

  • JavaScript toolbox to work with prompts
  • Abstraction to make it easy and productive
  • Seamless Visual Studio Code integration

Hello world

Say to you want to create an LLM script that generates a ‘hello world’ poem. You can write the following script:

$`Write a 'hello world' poem.`

The $ function is a template tag that creates a prompt. The prompt is then sent to the LLM (you configured), which generates the poem.

Let’s make it more interesting by adding files, data, and structured output. Say you want to include a file in the prompt, and then save the output in a file. You can write the following script:

// read files
const file = await workspace.readText("data.txt")
// include the file content in the prompt in a context-friendly way
def("DATA", file)
// the task
$`Analyze DATA and extract data in JSON in data.json.`

The def function includes the content of the file, and optimizes it if necessary for the target LLM. GenAIScript script also parses the LLM output and will extract the data.json file automatically.

Next steps

Listen to the podcast

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

Minimal syntax to build prompts using JavaScript or TypeScript.

$`Summarize ${env.files}. Today is ${new Date()}.`

LLM Tools

Register JavaScript functions as LLM tools (with fallback for models that don’t support tools).

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

or use @agentic tools

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

Model Context Provider Client

Use tools exposed in MCP Servers

defTool({ "memory": {
command: "npx",
args: ["-y", "@modelcontextprotocol/server-memory"]
}})

LLM Agents

Combine tools and inline prompts into an agent.

defAgent(
"git",
"Agent that answer git questions for the current repo",
"You are a helpful expert in using git.",
{ tools: ["git"] }
)
script({ tools: "agent" })
$`Do a statistical analysis of the last commits`

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]+/, { globs: "*.md" })

Web search

Web search using Bing or Tavily.

const pages = await retreival.webSearch("what are the latest news about AI?")

Browser automation

Browse and scrape the web with Playwright.

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

RAG built-in

Vector search.

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

Safety First!

GenAIScript provides built-in Responsible AI system prompts and Azure Content Safety supports to validate content safety.

script({ ...,
system: ["system.safety_harmful_content", ...],
contentSafety: "azure" // use azure content safety
})
const safety = await host.contentSafety()
const res = await safety.detectPromptInjection(env.vars.input)

GitHub Models and GitHub Copilot

Run models through GitHub using GitHub Models or GitHub Copilot.

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

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
---
system:
Write a short poem about
user:
{{something}}.
importTemplate("poem.prompty", { something: "code " })

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.

Case Studies

Tales from the real world using GenAIScript.

Samples

Fully fledged scripts ready to use.

Guides

A cookbook full of recipes to make you a genius scripter.

Agents

Builtin Agents

LLM friendly docs

If you are an LLM crawler, fetch https://microsoft.github.io/genaiscript/.well-known/llms.txt for an documentation map or add the .md suffix to any documentation URLs to get a raw markdown content.

For example, https://microsoft.github.io/genaiscript/guides/prompt-as-code.md (note the .md extension)