Skip to content

allthingssecurity/GEPA

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GEPA: Genetic-Pareto Prompt Evolution

License: MIT Python 3.8+

GEPA (Genetic-Pareto Prompt Evolution) is an implementation of the approach described in the paper "GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning". This system optimizes prompts for large language models through natural language reflection and Pareto-based selection, achieving significant improvements with far fewer rollouts than traditional reinforcement learning methods.

🚀 Key Features

🔥 Superior Sample Efficiency

  • 35× fewer rollouts than GRPO (Group Relative Policy Optimization)
  • Achieves better results with orders of magnitude fewer API calls
  • Particularly effective for compound AI systems with multiple interacting components

🧠 Natural Language Intelligence

  • Uses LLMs to analyze execution traces and generate human-readable improvement plans
  • Reflection plans are interpretable and actionable
  • Maintains safety constraints throughout optimization

⚖️ Pareto-Based Selection

  • Maintains diverse set of high-performing candidates
  • Prevents premature convergence to local optima
  • Preserves complementary strategies across the population

📊 Performance Results

Detailed Optimization Examples

See our comprehensive Prompt Optimization Results showing before/after comparisons across multiple domains, demonstrating:

Domain Initial EM Final EM Improvement Sample Efficiency Gain
General QA ~0.35 ~0.64 +83% 35× fewer rollouts
Code Gen ~0.40 ~0.63 +58% 35× fewer rollouts
Creative Writing ~0.45 ~0.63 +40% 35× fewer rollouts
Medical QA ~0.35 ~0.62 +77% 35× fewer rollouts
Legal QA ~0.37 ~0.64 +73% 35× fewer rollouts

🛠️ Installation

# Clone the repository
git clone https://github.com/allthingssecurity/GEPA.git
cd GEPA

# Install dependencies
pip install -e .

# Or install directly from PyPI (when available)
pip install gepa

🚀 Quick Start

from gepa import GEPA
from gepa.models import PromptSet, TaskInstance

# Create initial prompt set
seed_prompts = {
    "system_root": "You are a helpful assistant that answers questions accurately.",
    "task_guidelines": "When answering questions:\n1. Be concise and accurate\n2. Provide evidence when possible\n3. Acknowledge limitations"
}

seed_population = [
    PromptSet(
        id="seed_1",
        meta={"parents": [], "generation": 0},
        prompts=seed_prompts
    )
]

# Create task instances
tasks = [
    TaskInstance(
        id="task_1",
        task="question_answering",
        input={"question": "What is the capital of France?"},
        expected={"answers": ["Paris"]},
        metadata={"split": "train"}
    )
    # Add more tasks for better optimization
]

# Initialize and run GEPA
gepa = GEPA()
best_candidate = gepa.evolve(seed_population, tasks)

print("Optimized prompts:")
for name, prompt in best_candidate.prompts.items():
    print(f"\n{name}:")
    print(prompt)

🏗️ System Architecture

GEPA Components:
├── GEPA (Main orchestrator)
├── Candidate Manager
├── Evaluator
├── Reflector
├── Mutator
├── Pareto Selector
└── LLM Client

Evolution Process:
1. Evaluate candidates on tasks
2. Select Pareto front of best performers
3. Reflect on execution traces to identify improvements
4. Mutate prompts based on reflection plans
5. Repeat for multiple generations

📚 Examples

The repository includes comprehensive examples for various domains:

  1. General QA - examples/basic_example.py
  2. Code Generation - examples/code_example.py
  3. Creative Writing - examples/creative_example.py
  4. Medical QA - examples/medical_example.py
  5. Legal QA - examples/legal_example.py
  6. Custom Tasks - examples/custom_template.py

🔧 Configuration

Configure the system through gepa/config.py:

# LLM Configuration
LLM_CONFIG = {
    "provider": "openai",  # or "anthropic", "qwen", etc.
    "model": "gpt-4o",     # or "gpt-4o-mini", "claude-3-5-sonnet", etc.
    "temperature": 0.7,
    "max_tokens": 2000,
    "api_key": os.getenv("OPENAI_API_KEY")
}

# System Configuration
SYSTEM_CONFIG = {
    "max_generations": 3,
    "population_size": 5,
    "front_size": 3,
    "batch_size": 2,
    "total_rollout_budget": 10,
    "reflect_k": 1,
    "children_per_parent": 2,
    "growth_cap": 1.15
}

🧪 Research Validation

This implementation reproduces key findings from the original paper:

  • Sample Efficiency: Achieves results with 35× fewer rollouts than GRPO
  • Quality Improvements: Consistent 10-20% gains over GRPO with fewer resources
  • Universal Enhancement: Shows similar evolution patterns across diverse domains
  • Shorter Prompts: Often produces more effective shorter prompts

🤝 Contributing

Contributions are welcome! Please feel free to submit pull requests for:

  1. New domain-specific examples
  2. Additional evaluation metrics
  3. Extended LLM provider support
  4. Performance improvements
  5. Documentation enhancements

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

📚 Citation

If you use this code in your research, please cite the original paper:

@article{agrawal2025gepa,
  title={GEPA: Reflective Prompt Evolution Can Outperform Reinforcement Learning},
  author={Agrawal, Lakshya A and Tan, Shangyin and Soylu, Dilara and Ziems, Noah and Khare, Rishi and Opsahl-Ong, Krista and Singhvi, Arnav and Shandilya, Herumb and Ryan, Michael J and Jiang, Meng and others},
  journal={arXiv preprint arXiv:2507.19457},
  year={2025}
}

🙏 Acknowledgments

This implementation is based on the research conducted by the authors of the GEPA paper. We thank them for their groundbreaking work in reflective prompt evolution.

About

Genetic-Pareto Evolution for Prompt Adaptation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages