How microsoft/promptwizard works
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.