CoolFace
Apppublic

abdeljalilELmajjodi/prompt-refinement-studio

sourceHugging Faceapache-2.0updated 7mo agoView on Hugging Face
3likes
App README

<p align="center"> <img src="assets/logo.png" alt="Prompt Refinement Logo" width="250"/> </p> <div align="center"> <h1>Prompt Refinement </h1> </div> <div align="center">

![Python 3.12+](https://www.python.org/downloads/) ![License: MIT](https://opensource.org/licenses/MIT) ![Code Style: Ruff](https://github.com/astral-sh/ruff) ![Mistral AI](https://mistral.ai) </div>

Iterative prompt engineering, solved.

Prompt Refinement is an automated framework that evolves your LLM prompts using a genetic-style optimization loop. It systematically improves prompt quality by minimizing cost and maximizing accuracy against your ground-truth dataset.

Instead of guessing what works, let Prompt Refinement treat your prompt as a hyperparameter to be optimized.


⚡️ Why Prompt Refinement?

Manual prompt engineering is tedious, unscientific, and hard to reproduce. Prompt Refinement automates the process:

  • —🔄 Iterative Evolution: Uses a "Generator" LLM to strictly improve prompts based on specific performance bottlenecks.
  • —🎯 Multi-Objective Scoring: Balances Accuracy, Novelty, and Cost to find the global optimum.
  • —🤖 Mistral Native: Optimized for Mistral's model family (Medium/Large) for high-reasoning prompt generation.
  • —📊 Traceable History: Tracks every prompt version, score, and latency for full observability.
  • —🖥️ Streamlit UI: Includes a premium Mistral-branded web interface with live progress tracking.

🚀 Quick Start

1. Install

Prompt Refinement enables modern Python development with uv.

bash
# Clone the repository
git clone https://github.com/yourusername/prompt-refinement.git
cd prompt-refinement

# Sync dependencies
uv sync

2. Configure Environment

Create a .env file with your Mistral API key:

bash
echo "MISTRALAI_API_KEY=your_key_here" > .env

3. Run the Demo

Option A: Command Line

We include a baked-in example using the HaluEval dataset (hallucination detection):

bash
uv run python prompt_refinement/cli/main.py

Output:

text
============================================================
OPTIMIZATION COMPLETE
============================================================
  Initial prompt : You are a helpful assistant.
  Final prompt   : Check: [Document] | [Summary] → y/n
  Initial score  : 0.7606
  Final score    : 0.8347
  Iterations     : 3
============================================================
Option B: Streamlit UI

Launch the interactive web interface:

bash
uv run streamlit run prompt_refinement/ui/app.py

The UI provides:

  • —Live progress tracking with real-time charts and iteration cards
  • —Full sidebar configuration for models, weights, and dataset
  • —Results dashboard with prompt comparison, score trajectory, and iteration details

🏗 Architecture

Prompt Refinement implements a feedback loop inspired by evolutionary algorithms. It doesn't just "rewrite" prompts; it measures them.

mermaid
graph LR
    A[Initial Prompt] --> B(Executor)
    B --> C{Evaluator}
    C -->|Score & Bottleneck| D[Generator]
    D -->|New Variations| B

Components

ModuleComponentRole
`Executor`PromptExecutorThe Runtime. Executes the current prompt against your test cases (dataset).
`Evaluator`EvaluatorThe Judge. Scores outputs on Accuracy, Novelty, and Cost. Identifies the "Bottleneck".
`Generator`PromptGeneratorThe Architect. Uses meta-prompting to propose improved variations targeting the specific bottleneck.
`Optimizer`PromptOptimizerThe Orchestrator. Manages the loop, early stopping, and tournament selection.

Scoring System

The composite score is a weighted sum of three metrics, each normalized to [0, 1]:

MetricDefault WeightDescription
Accuracy70%Exact-match comparison between model responses and expected answers.
Novelty20%Word-level overlap between the current prompt and the previous iteration. Encourages exploration.
Cost10%Token efficiency relative to a baseline budget. Uses baseline / (baseline + avg_tokens) for smooth scoring.

⚖️ Optimizer Decision

The OptimizerDecision logic governs how the loop progresses and when it decides to finish.

1. Stop-Criteria (Early Stopping)

The orchestrator checks several conditions before proceeding to a new iteration:

  • —Max Iterations: Loop terminates once max_iter_numb is reached.
  • —Improvement Plateau: If the relative improvement of the score is less than the improvement_threshold (e.g., < 1%), the optimizer stops to save costs.
  • —Convergence: If a variation reaches a "perfect" score threshold, the optimization is considered successful.

2. Best-Variant Selection

In each iteration, the PromptGenerator proposes multiple variations targeting the current Bottleneck. The optimizer then:

  1. 1.Executes each variation against the full evaluation dataset.
  2. 2.Calculates the composite score for every candidate.
  3. 3.Selects the variation with the highest absolute score to become the "parent" for the next generation.

💻 Programmatic Usage

Integrate Prompt Refinement into your own evaluation pipeline:

python
from mistralai import Mistral
from datasets import load_dataset
from prompt_refinement import (
    EvaluatorConfig,
    OptimizerConfig,
    PromptOptimizer,
)

# 1. Setup Client
client = Mistral(api_key="...")

# 2. Load Data (Must have 'questions' and ground truth 'answers')
dataset = load_dataset("your_dataset", split="train[:50]")

# 3. Configure Optimization
config = OptimizerConfig(
    executor_model_name="mistral-medium-latest",  # Fast runner
    executor_client=client,
    generator_model_name="mistral-large-latest",  # Smart improver
    generator_client=client,
    max_iter_numb=5,
    improvement_threshold=0.01,
    questions="question_column",
    answers="answer_column",
    num_variations=3,
    task_description="Extract entities from financial reports...",
)

# 4. Run
optimizer = PromptOptimizer(config, EvaluatorConfig(), dataset)
result = optimizer.optimize("Extract info.")

print(f"Winner: {result.final_prompt}")

⚙️ Advanced Configuration

EvaluatorConfig

Fine-tune the evaluation logic:

ParameterDefaultDescription
weights{"accuracy": 0.7, "novelty": 0.2, "cost": 0.1}Relative importance of each scoring metric.
baseline_tokens500Reference token budget for cost scoring. Responses matching this budget score 0.5; cheaper scores higher, costlier scores lower.
model"mistral-medium-latest"Model identifier for evaluation context.

OptimizerConfig

ParameterDescription
executor_model_nameModel used to execute prompts against the dataset.
generator_model_nameModel used to generate improved prompt variations.
max_iter_numbMaximum number of optimization iterations.
improvement_thresholdMinimum score improvement required to continue iterating.
num_variationsNumber of prompt variants generated per iteration.
task_descriptionNatural language description of the task (helps the generator).

📁 Project Structure

prompt-refinement/
├── assets/                       # Branding & image assets
│   └── logo.png
├── notebooks/                    # Research & exploration notebooks
├── prompt_refinement/            # Main package
│   ├── core/                     # Internal logic (evaluators, optimizer, etc.)
│   ├── ui/                       # Streamlit web interface
│   │   └── app.py
│   ├── cli/                      # CLI entry points
│   │   └── main.py
│   └── __init__.py               # Public API exports
├── pyproject.toml                # Project metadata & dependencies
├── .streamlit/config.toml        # Streamlit theme configuration
└── tests/                        # Unit & integration tests

🤝 Contributing

Contributions are welcome! Please run the test suite before submitting a PR:

bash
uv run pytest tests/