Skip to content

amirkiarafiei/dotprompt-example-tutorial

Repository files navigation

dotprompt-mvc-demo

A "Zero to Hero" resource for Google's dotprompt applying the Model-View-Controller (MVC) pattern to AI Engineering.

Philosophy

"Prompts are Code." This repository demonstrates how to decouple prompt logic (View) from application logic (Controller) while enforcing strict data contracts (Model).

MVC Diagram

The Problem: Prompt Spaghetti

Prompt Spaghetti

Getting Started

  1. Install Dependencies

    npm install
  2. Set up Environment Copy .env.example to .env and add your Google API Key (Gemini).

    cp .env.example .env
  3. Run Examples

    • Example 0: The Legacy Nightmare (How NOT to do it)

      npm run legacy
    • Example 1: The MVC Basic (Separation of Concerns)

      npm run basic
    • Example 2: The Contract (Type Safety with Picoschema)

      npm run schema
    • Example 3: The Component (Reusability with Partials)

      npm run partials
    • Example 4: The Eyes (Multi-modal Vision)

      npm run vision
    • Example 5: The Memory (Chat History)

      npm run chat

Architecture

/dotprompt-mvc-demo
  ├── prompts/                # THE VIEW LAYER (Templates)
  │    ├── _system.prompt     # (Partial) Shared System Persona
  │    ├── 01_basic.prompt
  │    ├── 02_schema.prompt
  │    ├── 03_partials.prompt
  │    ├── 04_vision.prompt
  │    └── 05_chat.prompt
  └── src/                    # THE CONTROLLER LAYER (Logic)
       ├── 00_legacy.ts       # The "Before" (Spaghetti)
       ├── 01_basic.ts        # The "View" (Separation)
       ├── 02_schema.ts       # The "Model" (Type Safety)
       ├── 03_partials.ts     # The "Component" (Reusability)
       ├── 04_vision.ts       # The "Eyes" (Multi-modal)
       └── 05_chat.ts         # The "Memory" (History/Context)

Documentation

Read the full guide in docs/medium.md.

Testing

Run the test suite to verify all examples are correctly structured:

npm test

The tests verify:

  • All prompt files exist and have correct structure
  • Prompts use the correct model (gemini-2.5-flash-lite)
  • Picoschema syntax is valid
  • TypeScript files compile correctly

For live LLM tests (which call the actual Gemini API), set your GOOGLE_API_KEY environment variable before running tests.

About

A collection of usage examples for Google's Dotprompt library, demonstrating the MVC pattern as described in the accompanying Medium article.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published