GEPA框架如何优化AI提示词和代码?基于LLM反思与帕累托进化搜索
GEPA is a framework that uses LLM-based reflection and Pareto-efficient evolutionary search to optimize text parameters like prompts, code, and agent architectures, achieving significant performance improvements with minimal evaluations. 原文翻译: GEPA是一个利用基于LLM的反思和帕累托高效进化搜索来优化提示、代码和智能体架构等文本参数的框架,能以最少的评估实现显著的性能提升。
GEPAGenetic-Pareto框架,使用基于LLM的反思和帕累托高效进化搜索来优化文本参数的系统 是一个利用基于LLM的反思和帕累托高效进化搜索,来优化任何文本参数(如提示词、代码、智能体架构、配置)的框架。
GEPA 是一个利用基于LLM的反思和帕累托高效进化搜索,来优化任何文本参数(如提示词、代码、智能体架构、配置)的框架。
什么是 GEPA?
GEPA (Genetic-Pareto) 是一个用于针对任何评估指标优化任何具有文本参数系统的框架。与将执行轨迹压缩为单一标量奖励的强化学习(RL)或基于梯度的方法不同,GEPA 使用大语言模型(LLM)来阅读完整的执行轨迹——包括错误信息、性能分析数据、推理日志——以诊断候选方案为何失败,并提出针对性的修复方案。通过迭代式的反思、变异和帕累托感知选择,GEPA 能以最少的评估次数进化出高性能的变体。
GEPA (Genetic-Pareto) 是一个用于针对任何评估指标优化任何具有文本参数系统的框架。与将执行轨迹压缩为单一标量奖励的强化学习(RL)或基于梯度的方法不同,GEPA 使用大语言模型(LLM)来阅读完整的执行轨迹——包括错误信息、性能分析数据、推理日志——以诊断候选方案为何失败,并提出针对性的修复方案。通过迭代式的反思、变异和帕累托感知选择,GEPA 能以最少的评估次数进化出高性能的变体。
只要你能衡量它,你就能优化它:提示词、代码、智能体架构、调度策略、矢量图形等等。
只要你能衡量它,你就能优化它:提示词、代码、智能体架构、调度策略、矢量图形等等。
关键成果
| 成果 | 说明 |
|---|---|
| 成果 | 说明 |
|---|---|
“在AI上下文工程领域,DSPy一个来自斯坦福和MIT的LLM优化框架,被批评为采用'货物崇拜科学'方法,通过随机提示变异和噪声评估进行所谓的'优化'。和(尤其是)GEPA目前被严重低估了” — Tobi Lutke, Shopify CEO
"Both DSPy and (especially) GEPA are currently severely under hyped in the AI context engineering world" — Tobi Lutke, CEO, Shopify
安装
pip install gepa
pip install gepa
要安装 main 分支的最新版本:
pip install git+https://github.com/gepa-ai/gepa.git
pip install git+https://github.com/gepa-ai/gepa.git
快速开始
简单的提示词优化
用几行代码优化AIME基准测试中数学问题的系统提示词(完整教程):
import gepa
trainset, valset, _ = gepa.examples.aime.init_dataset()
seed_prompt = {
"system_prompt": "You are a helpful assistant. Answer the question. "
"Put your final answer in the format '### <answer>'"
}
result = gepa.optimize(
seed_candidate=seed_prompt,
trainset=trainset,
valset=valset,
task_lm="openai/gpt-4.1-mini",
max_metric_calls=150,
reflection_lm="openai/gpt-5",
)
print("Optimized prompt:", result.best_candidate['system_prompt'])
import gepa
>
trainset, valset, _ = gepa.examples.aime.init_dataset()
>
seed_prompt = {
"system_prompt": "You are a helpful assistant. Answer the question. "
"Put your final answer in the format '### <answer>'"
}
>
result = gepa.optimize(
seed_candidate=seed_prompt,
trainset=trainset,
valset=valset,
task_lm="openai/gpt-4.1-mini",
max_metric_calls=150,
reflection_lm="openai/gpt-5",
)
>
print("Optimized prompt:", result.best_candidate['system_prompt'])
结果:GPT-4.1 Mini 在 AIME 2025 上的准确率从 46.6% 提升至 56.6%(+10个百分点)。
Result: GPT-4.1 Mini goes from 46.6% → 56.6% on AIME 2025 (+10 percentage points).
与 DSPy 结合使用(推荐用于AI流水线)
在 DSPy 中使用 GEPA 进行提示词优化是最强大的方式,它以 dspy.GEPA 的形式提供。参见 dspy.GEPA 教程 获取可执行的笔记本。
The most powerful way to use GEPA for prompt optimization is within DSPy, where it's available as dspy.GEPA. See dspy.GEPA tutorials for executable notebooks.
import dspy
optimizer = dspy.GEPA(
metric=your_metric,
max_metric_calls=150,
reflection_lm="openai/gpt-5",
)
optimized_program = optimizer.compile(student=MyProgram(), trainset=trainset, valset=valset)
import dspy
>
optimizer = dspy.GEPA(
metric=your_metric,
max_metric_calls=150,
reflection_lm="openai/gpt-5",
)
optimized_program = optimizer.compile(student=MyProgram(), trainset=trainset, valset=valset)
optimize_anything:超越提示词
optimize_anything API 可以优化任何文本工件——代码、智能体架构、配置、SVG——而不仅仅是提示词。你提供一个评估器;系统负责处理搜索。
The optimize_anything API optimizes any text artifact — code, agent architectures, configurations, SVGs — not just prompts. You provide an evaluator; the system handles the search.
import gepa.optimize_anything as oa
from gepa.optimize_anything import optimize_anything, GEPAConfig, EngineConfig
def evaluate(candidate: str) -> float:
result = run_my_system(candidate)
oa.log(f"Output: {result.output}") # Actionable Side Information
oa.log(f"Error: {result.error}") # feeds back into reflection
return result.score
result = optimize_anything(
seed_candidate="<your initial artifact>",
evaluator=evaluate,
objective="Describe what you want to optimize for.",
config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)
import gepa.optimize_anything as oa
from gepa.optimize_anything import optimize_anything, GEPAConfig, EngineConfig
>
def evaluate(candidate: str) -> float:
result = run_my_system(candidate)
oa.log(f"Output: {result.output}") # Actionable Side Information
oa.log(f"Error: {result.error}") # feeds back into reflection
return result.score
>
result = optimize_anything(
seed_candidate="<your initial artifact>",
evaluator=evaluate,
objective="Describe what you want to optimize for.",
config=GEPAConfig(engine=EngineConfig(max_metric_calls=100)),
)
工作原理
传统的优化器只知道候选方案是否失败,但不知道为什么失败。GEPA 采取了不同的方法:
Traditional optimizers know that a candidate failed but not why. GEPA takes a different approach:
选择:从帕累托前沿在优化问题中,表示在不同任务子集上表现优异的候选解集合(在不同任务子集上表现优异的候选方案集合)中选择一个候选方案。
执行:在一个小批量数据集上执行,捕获完整的执行轨迹。
反思:一个LLM读取轨迹(错误信息、性能分析器输出、推理日志)并诊断失败原因。
变异:根据从所有祖先那里积累的经验教训,生成一个改进的候选方案。
接受:如果有所改进,则将其加入候选池,并更新帕累托前沿。
Select a candidate from the Pareto frontier (candidates excelling on different task subsets)
Execute on a minibatch, capturing full execution traces
Reflect — an LLM reads the traces (error messages, profiler output, reasoning logs) and diagnoses failures
Mutate — generate an improved candidate informed by accumulated lessons from all ancestors
Accept — add to the pool if improved, update the Pareto front
GEPA 还支持系统感知合并——结合两个在不同任务上表现出色的帕累托最优候选方案的优点。其核心概念是可操作的辅助信息:由评估器返回的诊断性反馈,它相当于文本优化领域的“梯度”。
GEPA also supports system-aware merge — combining strengths of two Pareto-optimal candidates excelling on different tasks. The key concept is Actionable Side Information (ASI): diagnostic feedback returned by evaluators that serves as the text-optimization analogue of a gradient.
For details, see the paper and the documentation.
适配器:将 GEPA 接入任何系统
GEPA connects to your system via the GEPAAdapter interface — implement evaluate and make_reflective_dataset, and GEPA handles the rest.
内置适配器:
Built-in adapters:
常见问题(FAQ)
GEPA框架具体能优化哪些类型的文本参数?
GEPA可以优化任何文本参数,包括提示词、代码、智能体架构、配置、调度策略等。其核心原则是“只要你能衡量它,你就能优化它”。
GEPA相比传统强化学习(RL)方法有什么优势?
GEPA利用LLM阅读完整执行轨迹进行反思诊断,结合帕累托进化搜索,通常仅需100-500次评估即可实现优化,比GRPO等RL方法快35倍,且评估成本更低。
GEPA在实际应用中有哪些关键成果?
关键成果包括:成本降低90倍击败Claude Opus;智能体准确率从32%提升至89%;云调度策略节省40.2%成本;已在Shopify、Databricks等50多个生产环境中应用。
| 适配器 | 描述 |
|---|---|
| 用于单轮LLM任务的系统提示词优化 | |
基于对数概率的分类优化——惩罚侥幸猜中,并将置信度诊断信息反馈给反思过程。需安装 pip install "gepa[confidence]" |
|
| 进化整个DSPy程序(签名、模块、控制流)。在MATH数据集上达到67% → 93%。 | |
版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。
文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容可能基于公开资料整理,亦可能使用 AI 辅助生成或润色;我们尽力确保准确与合规,但不保证完整性、时效性与适用性,请读者自行甄别并以官方信息为准。
若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。



