MLflow 提示优化 (beta)

重要

此功能目前以 Beta 版提供。

MLflow 提供 mlflow.genai.optimize_prompts() API,使你能够使用评估指标和训练数据自动改进提示。 借助此功能,你可以通过应用提示优化算法、减少手动工作量并确保质量一致来提高任何代理框架的提示有效性。

MLflow 通过GepaPromptOptimizer的研究和验证支持 GEPA 优化算法。 GEPA 通过 LLM 驱动的反思和自动反馈,迭代地细化提示,最终实现了系统化和数据驱动的改进。

主要优势

  • 自动改进:在不手动优化的情况下根据评估指标优化提示。
  • 数据驱动优化:使用训练数据和自定义评分器来指导优化。
  • 框架不可知:适用于任何代理框架,提供广泛的兼容性。
  • 联合优化:支持同时优化多个提示,以实现最佳整体性能。
  • 灵活评估:提供对自定义评分器和聚合函数的支持。
  • 版本控制:在 MLflow 提示注册表中自动注册优化提示。
  • 可扩展:通过扩展基类插入自定义优化算法。

重要

API optimize_prompts 需要 MLflow >= 3.5.0

提示优化示例

有关提示优化的简单示例,请参阅 “优化提示”教程

API 会生成改进的提示,可在评估条件上更好地执行。

示例:简单提示→优化提示

在优化之前:

Answer this question: {{question}}

优化后:

Answer this question: {{question}}.
Focus on providing precise,
factual information without additional commentary or explanations.

1. **Identify the Subject**: Clearly determine the specific subject
of the question (e.g., geography, history)
and provide a concise answer.

2. **Clarity and Precision**: Your response should be a single,
clear statement that directly addresses the question.
Do not add extra details, context, or alternatives.

3. **Expected Format**: The expected output should be the exact answer
with minimal words where appropriate.
For instance, when asked about capitals, the answer should
simply state the name of the capital city,
e.g., "Tokyo" for Japan, "Rome" for Italy, and "Paris" for France.

4. **Handling Variations**: If the question contains multiple
parts or variations, focus on the primary query
 and answer it directly. Avoid over-complication.

5. **Niche Knowledge**: Ensure that the responses are based on
commonly accepted geographic and historical facts,
as this type of information is crucial for accuracy in your answers.

Adhere strictly to these guidelines to maintain consistency
and quality in your responses.

有关完整说明,请参阅 MLflow 文档

高级用法

有关高级用例,请参阅以下指南:

常见用例

以下部分提供了常见用例的示例代码。

提高准确性

优化提示以生成更准确的输出:

from mlflow.genai.scorers import Correctness

result = mlflow.genai.optimize_prompts(
    predict_fn=predict_fn,
    train_data=dataset,
    prompt_uris=[prompt.uri],
    optimizer=GepaPromptOptimizer(reflection_model="databricks:/databricks-gpt-5"),
    scorers=[Correctness(model="databricks:/databricks-claude-sonnet-4-5")],
)

针对安全性进行优化

确保输出安全:

from mlflow.genai.scorers import Safety

result = mlflow.genai.optimize_prompts(
    predict_fn=predict_fn,
    train_data=dataset,
    prompt_uris=[prompt.uri],
    optimizer=GepaPromptOptimizer(reflection_model="databricks:/databricks-claude-sonnet-4-5"),
    scorers=[Safety(model="databricks:/databricks-claude-sonnet-4-5")],
)

Troubleshooting

以下部分提供了常见错误的故障排除指南。

问题:优化时间过长

解决方案:减小数据集大小或减少优化器预算:

# Use fewer examples
small_dataset = dataset[:20]

# Use faster model for optimization
optimizer = GepaPromptOptimizer(
    reflection_model="databricks:/databricks-gpt-5-mini", max_metric_calls=100
)

问题:未观察到任何改进

解决方案:检查评估指标并增加数据集多样性,如下所示:

  • 确保评分人员准确衡量你关心的内容。
  • 增加训练数据大小和多样性。
  • 尝试修改优化器配置。
  • 验证输出形式是否与预期匹配。

问题:未使用提示

解决方案:确保 predict_fn 调用 mlflow.entities.model_registry.PromptVersion.format

# ✅ Correct - loads from registry
def predict_fn(question: str):
    prompt = mlflow.genai.load_prompt(f"prompts:/{prompt_location}@latest)
    return llm_call(prompt.format(question=question))

# ❌ Incorrect - hardcoded prompt
def predict_fn(question: str):
    return llm_call(f"Answer: {question}")

后续步骤

若要了解有关 API 的详细信息,请参阅“优化提示”(Beta)。

若要详细了解 GenAI 应用程序的跟踪和评估,请参阅以下文章: