概念:配对评估
LangSmith 支持以比较方式评估现有实验。您可以对来自多个实验的输出进行相互评分,而不是一次评估一个输出。在本指南中,您将使用 evaluate() 与两个现有实验来定义评估器运行配对评估。最后,您将使用 LangSmith UI 来查看配对实验

前提条件

  • 如果您尚未创建要比较的实验,请查看快速入门操作指南以开始评估。
  • 本指南需要 langsmith Python 版本 >=0.2.0 或 JS 版本 >=0.2.9
您还可以使用 evaluate_comparative() 与两个以上的现有实验。

evaluate() 比较参数

最简单的情况下,evaluate / aevaluate 函数采用以下参数:
参数描述
target您想要相互评估的两个现有实验的列表。这些可以是 uuid 或实验名称。
evaluators您想要附加到此评估的配对评估器列表。请参阅下面的部分了解如何定义这些。
除此之外,您还可以传入以下可选参数:
参数描述
randomize_order / randomizeOrder可选的布尔值,指示每次评估是否应随机化输出的顺序。这是一种最小化提示中位置偏差的策略:通常,LLM 会根据顺序偏向其中一个响应。这应该主要通过提示工程来解决,但这是另一个可选的缓解措施。默认为 False。
experiment_prefix / experimentPrefix附加到配对实验名称开头的前缀。默认为 None。
description配对实验的描述。默认为 None。
max_concurrency / maxConcurrency要运行的最大并发评估数。默认为 5。
client要使用的 LangSmith 客户端。默认为 None。
metadata要附加到配对实验的元数据。默认为 None。
load_nested / loadNested是否加载实验的所有子运行。当为 False 时,只有根跟踪将传递给您的评估器。默认为 False。

定义配对评估器

配对评估器只是具有预期签名的函数。

评估器参数

自定义评估器函数必须具有特定的参数名称。它们可以采用以下参数的任何子集:
  • inputs: dict:与数据集中的单个示例对应的输入字典。
  • outputs: list[dict]:每个实验在给定输入上产生的字典输出的两项列表。
  • reference_outputs / referenceOutputs: dict:与示例关联的参考输出字典(如果可用)。
  • runs: list[Run]:两个实验在给定示例上生成的完整 Run 对象的两项列表。如果您需要访问中间步骤或每次运行的元数据,请使用此选项。
  • example: Example:完整的数据集 Example,包括示例输入、输出(如果可用)和元数据(如果可用)。
对于大多数用例,您只需要 inputsoutputsreference_outputs / referenceOutputsrunsexample 仅当您需要应用程序的实际输入和输出之外的一些额外跟踪或示例元数据时才有用。

评估器输出

自定义评估器应返回以下类型之一: Python 和 JS/TS
  • dict:具有以下键的字典:
    • key,表示将记录的反馈键
    • scores,从运行 ID 到该运行分数的映射。
    • comment,这是一个字符串。最常用于模型推理。
目前仅 Python
  • list[int | float | bool]:两项分数列表。假定该列表与 runs / outputs 评估器参数具有相同的顺序。评估器函数名称用于反馈键。
请注意,您应该选择与运行上的标准反馈不同的反馈键。我们建议为配对反馈键添加前缀 pairwise_ranked_

运行配对评估

以下示例使用一个提示,该提示要求 LLM 在两个 AI 助手响应之间决定哪个更好。它使用结构化输出来解析 AI 的响应:0、1 或 2。
In the Python example below, we are pulling this structured prompt from the LangChain Hub and using it with a LangChain chat model wrapper.Usage of LangChain is totally optional. To illustrate this point, the TypeScript example uses the OpenAI SDK directly.
  • Python: Requires langsmith>=0.2.0
  • TypeScript: Requires langsmith>=0.2.9
from langchain_classic import hub
from langchain.chat_models import init_chat_model
from langsmith import evaluate

# See the prompt: https://smith.langchain.com/hub/langchain-ai/pairwise-evaluation-2
prompt = hub.pull("langchain-ai/pairwise-evaluation-2")
model = init_chat_model("gpt-4o")
chain = prompt | model

def ranked_preference(inputs: dict, outputs: list[dict]) -> list:
    # Assumes example inputs have a 'question' key and experiment
    # outputs have an 'answer' key.
    response = chain.invoke({
        "question": inputs["question"],
        "answer_a": outputs[0].get("answer", "N/A"),
        "answer_b": outputs[1].get("answer", "N/A"),
    })
    if response["Preference"] == 1:
        scores = [1, 0]
    elif response["Preference"] == 2:
        scores = [0, 1]
    else:
        scores = [0, 0]
    return scores

evaluate(
    ("experiment-1", "experiment-2"),  # Replace with the names/IDs of your experiments
    evaluators=[ranked_preference],
    randomize_order=True,
    max_concurrency=4,
)

View pairwise experiments

Navigate to the “Pairwise Experiments” tab from the dataset page: Pairwise Experiments Tab Click on a pairwise experiment that you would like to inspect, and you will be brought to the Comparison View: Pairwise Comparison View You may filter to runs where the first experiment was better or vice versa by clicking the thumbs up/thumbs down buttons in the table header: Pairwise Filtering
Connect these docs programmatically to Claude, VSCode, and more via MCP for real-time answers.