GenAIScript Documentation

repository·main·Indexed 25 days ago

https://github.com/microsoft/genaiscript

A 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.

Tokens
187.6K
Snippets
698
Records
1.2K
Agent score
74%

What's inside GenAIScript

  1. Understand the GenAIScript vs. Agent Framework distinction

    main

    GenAIScript is a GLUE language designed to connect existing tools and LLM capabilities, rather than being a standalone agent framework.

    Key characteristics include:

    • Adapter Focus: It focuses on creating adapters for moving data into and out of LLMs.
    • Static Orchestration: Unlike autonomous agents, GenAIScript uses a static orchestration graph. This means the execution flow is predictable: you know exactly when an LLM is called and with what specific arguments.
    • Targeted LLM Application: LLMs are applied in a targeted manner to embed results into existing automation workflows.
    • Bounded Execution: LLM usage is bounded to a specific, single request rather than an open-ended loop.
  2. Supported LLM Providers in GenAIScript

    main

    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:

    • OpenAI
    • Azure OpenAI (supports API Key or Entra ID authentication)
    • Azure AI Foundry (supports Azure AI Inference, OpenAI deployments, and non-OpenAI model deployments)
    • GitHub Copilot Chat (via Visual Studio Code)
    • GitHub Marketplace Models
    • Anthropic (including via AWS Bedrock)
    • Google Gemini
    • Mistral
    • Hugging Face Inference

    Local & Other Providers:

    • Ollama
    • LM Studio
    • Any OpenAI-compatible model
  3. Understand GenAIScript core concepts

    main

    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:

    • GenAIScript: A stylized JavaScript program that defines context, executes code, calls the LLM, and parses the output.
    • GPVM: The runtime system that executes a GenAIScript by integrating context into a prompt, calling the LLM, and extracting content from the result.
    • VS Code GenAIScript extension: An add-in for creating, editing, running, and debugging GenAIScripts.
    • Foundation models and LLMs: The underlying models used by the scripts (currently supporting various LLMs).
  4. Integration of annotations with VS Code and GitHub Actions

    main

    GenAIScript annotations integrate with several environments:

    • Visual Studio Code: Annotations are converted into Diagnostics, appearing in the Problems panel and as squiggly lines in the editor.
    • GitHub Actions: By default, annotations use GitHub Action Commands syntax, allowing them to be automatically extracted by GitHub when running in a workflow.
    • GitHub Pull Requests: Using the --pull-request-reviews flag allows annotations to appear as review comments.
  5. Process AI responses into files and structured data

    main

    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:

    • File Edits: Parsing responses to generate workspace edits, which can be viewed as refactoring previews in VSCode.
    • Diagnostics: Parsing responses to extract annotations such as errors, warnings, and notes (e.g., for GitHub Actions, VSCode diagnostics, or Azure DevOps).
    • Structured Data: Parsing responses into formats like JSON, YAML, or CSV, combined with schema validation and error repair capabilities.
  6. Quickstart with GenAIScript

    main

    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.`
  7. Read blobs from Azure Blob Storage into Buffers

    main

    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
    }
  8. Enable logprobs for script debugging

    main

    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:

    1. CLI Flag: Add the --logprobs flag to your run command.
    2. Script Metadata: Add logprobs: true to your script's metadata object.