mirror of
https://github.com/magnus919/agent-skills.git
synced 2026-09-18 15:06:28 +03:00
Greenfield SkillOpt: 3 epochs for a Stanford DSPy compiler skill. DSPy is a fundamentally different paradigm from chain/RAG frameworks. Epoch 1 — Prominence: - Hard-gate blockquote: 'DSPy is NOT a chain framework' - Core Paradigm section with runnable code example early Epoch 2 — Decision Guidance: - Framework Routing Guide (DSPy vs LlamaIndex vs LangChain vs LangGraph) - Where to Start table mapping entry points - Troubleshooting table with reference links Epoch 3 — Pattern Expansion: - Optimizer selection cheat sheet from official docs - Caching, compilation cost management, save/load - FAQ covering paradigm confusion, errors, deployment 12 files: SKILL.md, 7 references, 3 templates, 1 script. v1.0.0 -> v1.0.3 across 3 epochs. All API surfaces validated against dspy.ai official docs — optimizer selection guide, caching, core modules, FAQ. Signed-off-by: Jasper <jasper@montcastle.bitches>
80 lines
2.0 KiB
Markdown
80 lines
2.0 KiB
Markdown
# DSPy Compilation Guide
|
|
|
|
## Caching
|
|
|
|
DSPy caches all LM calls by default. Two environment variables control cache directories:
|
|
|
|
| Variable | Scope |
|
|
|----------|-------|
|
|
| `DSP_CACHEDIR` | Legacy clients (deprecated `dspy.OpenAI`, `dspy.ColBERTv2`) |
|
|
| `DSPY_CACHEDIR` | Current `dspy.LM` client |
|
|
|
|
```python
|
|
import os
|
|
os.environ["DSPY_CACHEDIR"] = os.path.join(os.getcwd(), "cache")
|
|
|
|
# Disable cache per model
|
|
lm = dspy.LM("openai/gpt-4o-mini", cache=False)
|
|
```
|
|
|
|
## Compilation Cost Management
|
|
|
|
Compilation is expensive. A typical MIPROv2 run with `auto="medium"` on gpt-4o-mini:
|
|
- ~3200 API calls
|
|
- ~2.7M input tokens, ~156K output tokens
|
|
- ~$3 USD (gpt-3.5 pricing, current models may vary)
|
|
|
|
**Cost-saving strategies:**
|
|
- Start with `BootstrapFewShot` (cheapest)
|
|
- Use `auto="light"` before `auto="medium"` or `auto="heavy"`
|
|
- Cache aggressively — re-running with cache is free
|
|
- Use a cheaper prompt_model at compile time than your task_model
|
|
|
|
## Save and Load
|
|
|
|
```python
|
|
# After compilation
|
|
compiled_program.save("my_program.json")
|
|
|
|
# Later, to use
|
|
program = MyProgram()
|
|
program.load("my_program.json")
|
|
```
|
|
|
|
## _compiled Flag
|
|
|
|
When a sub-module has `_compiled = True`, it is skipped during re-optimization. This enables the "optimize inner -> embed in outer -> optimize outer" pattern.
|
|
|
|
```python
|
|
# Force recompile a sub-module
|
|
module._compiled = False
|
|
```
|
|
|
|
## Sharing Between Programs
|
|
|
|
```python
|
|
# Export for deployment
|
|
compiled_program.save("deploy/qa_program.json")
|
|
|
|
# In deployment code
|
|
import dspy
|
|
lm = dspy.LM("openai/gpt-4o-mini")
|
|
dspy.configure(lm=lm)
|
|
|
|
class QAModule(dspy.Module):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.qa = dspy.ChainOfThought("question -> answer")
|
|
|
|
qa = QAModule()
|
|
qa.load("deploy/qa_program.json")
|
|
result = qa(question="What is DSPy?")
|
|
```
|
|
|
|
## Key Guidelines
|
|
|
|
- Compile on a representative trainset (50-500 examples typical)
|
|
- Track cost and latency from day one
|
|
- One compile per program version; serve the saved artifact many times
|
|
- Provider-side prompt caching (Anthropic, OpenAI) reduces latency for repeated ReAct calls
|