Skip to content

Your first GenAI script

GenAIScript use stylized JavaScript with minimal syntax. They are stored as files (genaisrc/*.genai.js) in your project. The execution of a genaiscript creates the prompt that will be sent to the LLM.

  1. Use the > GenAiScript: Create new script... command in the command palette (Ctrl+Shift+P on Windows/Linux, ⇧⌘P on Mac) to create a new script.

    The command palette and the create script command

  2. The resulting file will be placed in the genaisrc folder in your project.

    • Directorygenaisrc scripts are created here by default
      • genaiscript.d.ts (TypeScript type definitions)
      • jsconfig.json (TypeScript compiler configuration)
      • proofreader.genai.js

the Context

GenAIScript exposes the context through the env variable. The context is implicitly defined by the location you start executing the script.

  • you can right click on a folder and the env.files will contain all the files nested in that folder.
  • you can right click on or in a file and the env.files will contain only that file.
  • you can run the script using the command line interface and specify content of env.files in the CLI arguments.
proofreader.genai.js
// the context
def("FILES", env.files)

The def function inlines the content of env.files in the final prompt in a LLM friendly way. It also has a number of additional options to control precisely how the content should be inlined.

proofreader.genai.js
def("FILES", env.files, {
// line numbers help with generating diffs
lineNumber: true,
// filter .md files only
endsWith: ".md",
})

the Task

The $ function is used to build the prompt text, it renders and writes the text to the prompt ($ is a template literal).

proofreader.genai.js
// the task
$`You are an expert technical writer and proofreader.
Review the documents in FILE and report the 5 most important issues.`

$ supports placeholders, ${...}. You also can reference the LLM variable defined with def freely in the text as well.

proofreader.genai.js
const files = def("FILE", env.files)
const n = 5
$`Review the documents in ${files}
and report the ${n} most important issues.`

the Metadata

You can add a call to the script function to provides metadata about the script and the model. The metadata is used to display the script in the UI and configure the LLM model.

proofreader.genai.js
// the metadata
script({
// user interface
title: "Technical proofreading",
description: "Reviews the text as a tech writer.",
group: "documentation",
// model
temperature: 0
})

The title, description, and group properties are used to display the script in the UI and can be helpful when the user is searching for a script.

The quickpick entry for the snippet above

Next steps

Run your script from Visual Studio Code.