dspy-optimize-anything
General↓ 0 installsUpdated 94d ago
Curatedmajiayu000
Universal text artifact optimizer using GEPA's optimize_anything API for code, prompts, agent architectures, configs, and more
SKILL.md preview
---
name: dspy-optimize-anything
description: Universal text artifact optimizer using GEPA's optimize_anything API for code, prompts, agent architectures, configs, and more
allowed-tools:
- Read
- Write
- Glob
- Grep
---
# GEPA optimize_anything
## Goal
Optimize any artifact representable as text — code, prompts, agent architectures, vector graphics, configurations — using a single declarative API powered by GEPA's reflective evolutionary search.
## When to Use
- **Beyond prompt optimization** — optimizing code, configs, SVGs, scheduling policies, etc.
- **Single hard problems** — circle packing, kernel generation, algorithm discovery
- **Batch related problems** — CUDA kernels, code generation tasks with cross-transfer
- **Generalization** — agent skills, policies, or prompts that must transfer to unseen inputs
- When you can **express quality as a score** and provide **diagnostic feedback** (ASI)
## Inputs
| Input | Type | Description |
|-------|------|-------------|
| `seed_candidate` | `str \| dict[str, str] \| None` | Starting artifact text, or `None` for seedless mode |
| `evaluator` | `Callable` | Returns score (higher=better), optionally with ASI dict |
| `dataset` | `list \| None` | Training examples (for multi-task and generalization modes) |
| `valset` | `list \| None` | Validation set (for generalization mode) |
| `objective` | `str \| None` | Natural language description of what to optimize for |
| `background` | `str \| None` | Domain knowledge and constraints |
| `config` | `GEPAConfig \| None` | Engine, reflection, and tracking settings |
## Outputs
| Output | Type | Description |
|--------|------|-------------|
| `result.best_candidate` | `str \| dict` | Best optimized artifact |
## Workflow
### Phase 1: Install
```bash
pip install gepa
```
### Phase 2: Define Evaluator with ASI
The evaluator scores a candidate and returns Actionable Side Information (ASI) — diagnostic feedback that guides the LLM proposer during reflection.
**Simple evaluator (score only):**
```python
import gepa.optimize_anything as oa
def evaluate(candidate: str) -> float:
score, diagnostic = run_my_system(candidate)
oa.log(f"Error: {diagnostic}") # captured as ASI
return score
```
**Rich evaluator (score + structured ASI):**
```python
def evaluate(candidate: str) -> tuple[float, dict]:
result = execute_code(candidate)
return result.score, {
"Error": result.stderr,
"Output": result.stdout,
"Runtime": f"{result.time_ms:.1f}ms",
}
```
ASI can include open-ended text, structured data, multi-objectives (via `scores`), or images (via `gepa.Image`) for vision-capable LLMs.
### Phase 3: Choose Optimization Mode
**Mode 1 — Single-Task Search:** Solve one hard problem. No dataset needed.
```python
result = oa.optimize_anything(
seed_candidate="<your initial artifact>",
evaluator=evaluate,
)
```
**Mode 2 — Multi-Task Search:** Solve a batch of related problems with cross-transfer.
```python
result = oa.optimize_anything(
seed_candidate="<your initial artifact>",
evaluator=evaluate,
dataset=tasks,
)
```
**Mode 3 — Generalization:** Build a skill/prompt/policy that transfers to unseen problems.
```python
result = oa.optimize_anything(
seed_candidate="<your initial artifact>",
evaluator=evaluate,
dataset=train,
valset=val,
)
```
**Seedless mode:** Describe what you need instead of providing a seed.
```python
result = oa.optimize_anything(
evaluator=evaluate,
objective="Generate a Python function `reverse()` that reverses a string.",
)
```
### Phase 4: Use Results
```python
print(result.best_candidate)
```
## Production Example
```python
import gepa.optimize_anything as oa
from gepa import Image
import logging
logger = logging.getLogger(__name__)
# ---------- SVG optimization with VLM feedback ----------
GOAL = "a pelican riding a bicycle"
VLM = "vertex_ai/gemini-3-flash-preview"
VISUAL_ASPECTS = [
{"id": "overall"
…