主题
如何在实验中重试失败的运行(仅 Python)
运行评估时,在大型数据集上可能有一小部分示例因速率限制、网络问题或其他瞬时错误而失败。你无需重跑整个评估,只需在实验上定位并重试那些失败的示例。
本指南展示如何在评估流程中加入重试逻辑,只重试失败的示例:用 error_handling='ignore' 参数跳过记录出错的运行,再自动找出未成功的示例并在 Python 中重跑。
第 1 步:运行初始评估
先运行初始评估,并忽略错误,以免出错的运行被记录:
python
from langsmith import Client
client = Client()
# Run initial evaluation, ignoring errors
# error_handling='ignore' prevents errored runs from being logged
results = await client.aevaluate(
target,
data="dataset",
evaluators=[your_evaluators],
error_handling='ignore'
)第 2 步:重试失败的示例并记录到同一个实验
先取出所有未成功的示例:
python
# Identify unsuccessful examples
runs = client.list_runs(project_name=results.experiment_name)
successful_example_ids = [r.reference_example_id for r in runs]
unsuccessful_examples = (e for e in client.list_examples(dataset_name="dataset") if e.id not in successful_examples)再重跑所有失败的示例,并把它们记录到同一个实验中:
python
# Retry only the failed examples, log
results_retry = await client.aevaluate(
target,
unsuccessful_examples,
evaluators=[your_evaluators],
experiment=results.experiment_name,
error_handling='ignore'
)