追踪 DSPy🧩
DSPy 是一个用于构建模块化 AI 系统的开源框架,并提供用于优化提示词 (prompts) 和权重 (weights) 的算法。
MLflow Tracing 为 DSPy 提供了自动追踪功能。你可以通过调用 mlflow.dspy.autolog() 函数来开启 DSPy 的追踪功能,嵌套追踪记录会在调用 DSPy 模块时自动记录到当前的 MLflow Experiment 中。
import mlflow
mlflow.dspy.autolog()
MLflow DSPy 集成不仅仅是追踪。MLflow 为 DSPy 提供了完整的跟踪体验,包括模型跟踪、索引管理和评估。请参阅 MLflow DSPy Flavor 以了解更多信息!
使用示例
import dspy
import mlflow
# Enabling tracing for DSPy
mlflow.dspy.autolog()
# Optional: Set a tracking URI and an experiment
mlflow.set_tracking_uri("https://:5000")
mlflow.set_experiment("DSPy")
# Define a simple ChainOfThought model and run it
lm = dspy.LM("openai/gpt-4o-mini")
dspy.configure(lm=lm)
# Define a simple summarizer model and run it
class SummarizeSignature(dspy.Signature):
"""Given a passage, generate a summary."""
passage: str = dspy.InputField(desc="a passage to summarize")
summary: str = dspy.OutputField(desc="a one-line summary of the passage")
class Summarize(dspy.Module):
def __init__(self):
self.summarize = dspy.ChainOfThought(SummarizeSignature)
def forward(self, passage: str):
return self.summarize(passage=passage)
summarizer = Summarize()
summarizer(
passage=(
"MLflow Tracing is a feature that enhances LLM observability in your LLM applications and AI agents "
"by capturing detailed information about the execution of your application's services. Tracing provides "
"a way to record the inputs, outputs, and metadata associated with each intermediate step of a request, "
"enabling you to easily pinpoint the source of bugs and unexpected behaviors."
)
)
评估过程中的追踪
评估 DSPy 模型是开发 AI 系统的重要一步。MLflow Tracing 可以通过提供有关程序针对每个输入执行的详细信息,帮助你在评估后跟踪程序的性能。
当为 DSPy 启用 MLflow 自动追踪时,执行 DSPy 的 内置评估套件 时将自动生成追踪记录。以下示例演示了如何在 MLflow 中运行评估并查看追踪记录。
import dspy
from dspy.evaluate.metrics import answer_exact_match
import mlflow
# Enabling tracing for DSPy evaluation
mlflow.dspy.autolog(log_traces_from_eval=True)
# Define a simple evaluation set
eval_set = [
dspy.Example(question="How many 'r's are in the word 'strawberry'?", answer="3").with_inputs(
"question"
),
dspy.Example(question="How many 'a's are in the word 'banana'?", answer="3").with_inputs(
"question"
),
dspy.Example(question="How many 'e's are in the word 'elephant'?", answer="2").with_inputs(
"question"
),
]
# Define a program
class Counter(dspy.Signature):
question: str = dspy.InputField()
answer: str = dspy.OutputField(desc="Should only contain a single number as an answer")
cot = dspy.ChainOfThought(Counter)
# Evaluate the programs
with mlflow.start_run(run_name="CoT Evaluation"):
evaluator = dspy.evaluate.Evaluate(
devset=eval_set,
return_all_scores=True,
return_outputs=True,
show_progress=True,
)
aggregated_score, outputs, all_scores = evaluator(cot, metric=answer_exact_match)
# Log the aggregated score
mlflow.log_metric("exact_match", aggregated_score)
# Log the detailed evaluation results as a table
mlflow.log_table(
{
"question": [example.question for example in eval_set],
"answer": [example.answer for example in eval_set],
"output": outputs,
"exact_match": all_scores,
},
artifact_file="eval_results.json",
)
如果你打开 MLflow UI 并转到“CoT Evaluation”运行,你将看到评估结果,以及在 Traces 选项卡上评估期间生成的追踪记录列表。
你可以通过调用 mlflow.dspy.autolog() 函数并将 log_traces_from_eval 参数设置为 False,来禁用这些步骤的追踪功能。
编译(优化)过程中的追踪
编译(优化)是 DSPy 的核心概念。通过编译,DSPy 会自动优化你的 DSPy 程序提示词和权重,以达到最佳性能。
默认情况下,MLflow 不会在编译期间生成追踪,因为编译可能会触发数百或数千次 DSPy 模块的调用。若要启用编译过程的追踪,你可以调用 mlflow.dspy.autolog() 函数并将 log_traces_from_compile 参数设置为 True。
import dspy
import mlflow
# Enable auto-tracing for compilation
mlflow.dspy.autolog(log_traces_from_compile=True)
# Optimize the DSPy program as usual
tp = dspy.MIPROv2(metric=metric, auto="medium", num_threads=24)
optimized = tp.compile(cot, trainset=trainset)
令牌使用情况
MLflow 会自动跟踪 DSPy 的 Token 使用情况。有关以编程方式访问此信息的详细信息,请参阅 Token 使用量和成本跟踪 文档。
目前 DSPy 不支持成本跟踪,因为在 span 属性中可能无法获取模型名称。
禁用自动追踪
可以通过调用 mlflow.dspy.autolog(disable=True) 或 mlflow.autolog(disable=True) 全局禁用 DSPy 的自动追踪。