Skip to content
LangSmith 文档版本 跟随上游 源快照 ad5e272c 更新于 2026-10-18

如何在本地运行评估(仅 Python)

有时在本地运行评估而不将任何结果上传到 LangSmith 会很有帮助。例如,如果你正在快速迭代提示词,想用少量示例做冒烟测试,或者在验证目标函数和评估器函数是否定义正确,你可能不希望记录这些评估。

你可以使用 LangSmith Python SDK,并向 evaluate() / aevaluate() 传入 upload_results=False 来实现。

这会像往常一样运行你的应用和评估器并返回相同的输出,但不会向 LangSmith 记录任何内容。这不仅包括实验结果,还包括应用与评估器的追踪。

NOTE

如果你想将结果上传到 LangSmith,同时还需要在脚本中处理它们(用于质量门禁、自定义聚合等),请参阅在本地读取实验结果。

示例

我们来看一个示例:

需要 langsmith>=0.2.0。示例还使用了 pandas。

python
from langsmith import Client

# 1. Create and/or select your dataset
ls_client = Client()
dataset = ls_client.clone_public_dataset(
    "https://smith.langchain.com/public/a63525f9-bdf2-4512-83e3-077dc9417f96/d"
)

# 2. Define an evaluator
def is_concise(outputs: dict, reference_outputs: dict) -> bool:
    return len(outputs["answer"]) < (3 * len(reference_outputs["answer"]))

# 3. Define the interface to your app
def chatbot(inputs: dict) -> dict:
    return {"answer": inputs["question"] + " is a good question. I don't know the answer."}

# 4. Run an evaluation
experiment = ls_client.evaluate(
    chatbot,
    data=dataset,
    evaluators=[is_concise],
    experiment_prefix="my-first-experiment",
    # 'upload_results' is the relevant arg.
    upload_results=False
)

# 5. Analyze results locally
results = list(experiment)

# Check if 'is_concise' returned False.
failed = [r for r in results if not r["evaluation_results"]["results"][0].score]

# Explore the failed inputs and outputs.
for r in failed:
    print(r["example"].inputs)
    print(r["run"].outputs)

# Explore the results as a Pandas DataFrame.
# Must have 'pandas' installed.
df = experiment.to_pandas()
df[["inputs.question", "outputs.answer", "reference.answer", "feedback.is_concise"]]
python
{'question': 'What is the largest mammal?'}
{'answer': "What is the largest mammal? is a good question. I don't know the answer."}
{'question': 'What do mammals and birds have in common?'}
{'answer': "What do mammals and birds have in common? is a good question. I don't know the answer."}
inputs.questionoutputs.answerreference.answerfeedback.is_concise
0What is the largest mammal?What is the largest mammal? is a good question. I don't know the answer.The blue whaleFalse
1What do mammals and birds have in common?What do mammals and birds have in common? is a good question. I don't know the answer.They are both warm-bloodedFalse

BytesRouter 是社区翻译站点,所有译文仅供参考,一切以官方英文原文为准。