# microsoft/promptwizard architecture

> Architecture diagram of the GitHub repository microsoft/promptwizard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/microsoft/promptwizard
- Repository: https://github.com/microsoft/promptwizard

## Overview

##### Project Type & Purpose
- PromptWizard is a framework focused on prompt optimization for large language models (LLMs). Its main goal is to iteratively generate, critique, and refine prompts (and in-context examples) to improve performance on various tasks.
- The project supports multiple datasets and optimization scenarios (with or without examples, synthetic data generation, etc.), and it involves both configuration management and dynamic API interaction with external LLMs.

##### Key Components
a. Core Prompt Optimization Engine:
- The “promptopt” module (located under promptwizard/glue/promptopt) is central. It includes components for instantiation, running prompt optimization, and applying various prompt optimization techniques.
- Within promptopt, the "techniques" directory (notably the "critique_n_refine" submodule) encapsulates the feedback loop where the LLM is asked to generate variants of prompts, critique those variants, and then refine them. This cycle is fundamental to the self-evolving mechanism.

b. LLM Interaction & API Handling:
- The “llm” module (in promptwizard/glue/common/llm) abstracts API interactions. This component manages the communication with external LLM endpoints (either Azure OpenAI or OpenAI via credentials specified in .env files).

c. Configuration & Environment Management:
- Configuration files (like the promptopt_config.yaml found within each dataset demo folder) drive the hyperparameters for prompt mutation, number of iterations, prompt variations, etc.
- .env files are used to set up environmental variables for API keys and endpoints. These configurations are consumed by various modules and need to be shown as foundational inputs.

d. Utility & Logging Modules:
- The “common/utils” modules under promptwizard/glue/common provide file operations, logging, and runtime task management to support the optimization loop.
- The “paramlogger” (in promptwizard/glue/paramlogger) records and logs parameters – an important aspect to track prompt mutations and their performance.

e. Dataset and Demo Layers:
- In the demos directory, several subfolders (e.g., gsm8k, svamp, aquarat, bbh, scenarios) provide a hands-on structure for different datasets and scenarios. They include Jupyter notebooks to demonstrate how prompt optimization is initiated using scenario-specific configuration.
- As “usage examples” or “demo entry points” that initiate the optimization process by setting up configurations and environments.

##### Relationships & Data Flow
- The configuration module (YAML + .env) feeds parameters into the optimization engine.
- The optimization engine sends the initial prompt (with base instruction and in-context examples) to the LLM API module.
- The LLM API module responds with outputs that the "critique_n_refine" technique receives to generate refined prompt variations.
- Metric/score logging is handled by the paramlogger utility, which influences the selection of the “best” prompt for further iterations.
- Finally, the refined prompt that optimizes performance (as validated against a dataset or task metric) is output by the system.

##### Architectural Patterns & Design Principles
- The design follows a feedback-driven, iterative loop cycle, emphasizing modularity (each aspect – configuration, API, utility) is encapsulated.
- Separation of concerns: The design clearly differentiates the prompt generation and refinement (engine), the means to interact with external systems (LLM API), and the configuration/parameter logging.
- Extensibility: The architecture allows for new techniques to be added under the "techniques" module without disturbing core functionalities.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Runner
    Runner("Dataset/Demo Runner"):::runner

    %% Configuration & Environment Management
    subgraph "Configuration & Environment Management"
        ConfigYAML("Config YAML"):::config
        EnvVar("Env Variables"):::config
    end

    %% Prompt Optimization Engine
    subgraph "Prompt Optimization Engine"
        Popt("Core Prompt Optimization Engine"):::engine
        Pgen("Initial Prompt Generation"):::engine
        Crit("Critique & Refine Technique"):::engine
        Popt -.-> Pgen
    end

    %% LLM API Interaction Module
    LLM_API("LLM API Interaction Module"):::api

    %% Utility & Logging
    subgraph "Utility & Logging"
        Utils("Common Utils"):::util
        ParamLogger("ParamLogger"):::util
    end

    %% Data Flow Connections
    Runner --> ConfigYAML
    Runner --> EnvVar
    ConfigYAML --> Pgen
    EnvVar --> Pgen
    Pgen -->|"send_prompt"| LLM_API
    LLM_API -->|"evaluate"| Crit
    Crit -->|"refine"| Pgen
    Crit -->|"log_metrics"| ParamLogger
    Pgen -- "support" --> Utils

    %% Click Events
    click Popt "https://github.com/microsoft/promptwizard/tree/main/promptwizard/glue/promptopt"
    click Crit "https://github.com/microsoft/promptwizard/tree/main/promptwizard/glue/promptopt/techniques/critique_n_refine"
    click LLM_API "https://github.com/microsoft/promptwizard/tree/main/promptwizard/glue/common/llm"
    click ConfigYAML "https://github.com/microsoft/promptwizard/blob/main/demos/aquarat/configs/promptopt_config.yaml"
    click EnvVar "https://github.com/microsoft/promptwizard/blob/main/demos/gsm8k/.env"
    click Utils "https://github.com/microsoft/promptwizard/tree/main/promptwizard/glue/common/utils"
    click ParamLogger "https://github.com/microsoft/promptwizard/tree/main/promptwizard/glue/paramlogger"
    click Runner "https://github.com/microsoft/promptwizard/tree/main/demos"

    %% Styles
    classDef config fill:#FAD7A0,stroke:#C0392B,stroke-width:2px;
    classDef engine fill:#AED6F1,stroke:#2874A6,stroke-width:2px;
    classDef api fill:#FCF3CF,stroke:#F39C12,stroke-width:2px;
    classDef util fill:#D5F5E3,stroke:#27AE60,stroke-width:2px;
    classDef runner fill:#E8DAEF,stroke:#8E44AD,stroke-width:2px;
```
