Introduction to GenAIScript
main<scriptname>.genai.mjs and are stored in a genaisrc directory within your repository.repository·main·Indexed 25 days ago
https://github.com/microsoft/genaiscriptA JavaScript/TypeScript toolbox for programmatically assembling, orchestrating, and executing LLM prompts. GenAIScript allows developers to integrate tools, data schemas, file ingestion (PDF, DOCX, CSV, XLSX), and agents into a structured workflow. Key features include the $ template tag for prompt creation, defSchema for data validation, built-in vector search for RAG, and support for various model providers including GitHub Models, Copilot, Ollama, and LocalAI.
<scriptname>.genai.mjs and are stored in a genaisrc directory within your repository.GenAIScript is a GLUE language designed to connect existing tools and LLM capabilities, rather than being a standalone agent framework.
Key characteristics include:
GenAIScript supports a wide range of Large Language Model (LLM) providers. This includes major cloud providers, local model runners, and any model that implements an OpenAI-compatible API.
Cloud & Managed Providers:
Local & Other Providers:
GenAIScript is a framework for creating AI-enhanced scripts using stylized JavaScript. It allows users to define LLM context, execute arbitrary JavaScript, package prompts, call LLMs, and unpack structured outputs (like JSON or file edits).
Key terms:
tokenizers helper module provides functions to count, truncate, and chunk text into tokens. By default, it uses the large tokenizer, but you can specify a different tokenizer by passing a model identifier in the options object.GenAIScript annotations integrate with several environments:
--pull-request-reviews flag allows annotations to appear as review comments.GenAIScript uses parsers to transform raw text responses from AI models into actionable outputs. This process follows the formula: Response x Parsers = Files + Data.
Key capabilities include:
GenAIScript allows you to programmatically assemble prompts for LLMs using JavaScript or TypeScript. You can install the Visual Studio Code Extension or use the command line to get started quickly.
To create a basic prompt, use the $ template tag. To include files or data and extract structured output, use workspace.readText, def, and specify a target file for the LLM to generate.
// 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.`You can connect to an Azure Blob Storage container and download blobs as Node.js Buffer objects. This is useful because the defImages function supports the Buffer type.
To access the storage account and container names via the GenAIScript CLI, deconstruct account and container from env.vars.
import { BlobServiceClient } from "@azure/storage-blob"
import { DefaultAzureCredential } from "@azure/identity"
import { buffer } from "node:stream/consumers"
// Access variables set via GenAIScript CLI
const { account = "myblobs", container = "myimages" } = env.vars
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
new DefaultAzureCredential()
)
const containerClient = blobServiceClient.getContainerClient(container)
// Iterate and download blobs into buffers
for await (const blob of containerClient.listBlobsFlat()) {
const blockBlobClient = containerClient.getBlockBlobClient(blob.name)
const downloadBlockBlobResponse = await blockBlobClient.download(0)
const body = await downloadBlockBlobResponse.readableStreamBody
const image = await buffer(body)
// 'image' is now a Buffer ready for defImages
}interrupt function and the Command primitive. This allows workflows to pause and wait for user input or manual reviews.logprobs mode allows you to see the probability of each token returned by the LLM, which is useful for diagnosing model behavior and performance. Note that this feature is not available in all models or providers.
You can enable logprobs using one of two methods:
--logprobs flag to your run command.logprobs: true to your script's metadata object.