Skip to main content

如何运行成对评估

关键概念

LangSmith 支持以比较的方式评估现有实验。 这允许您将多个实验的输出相互对比评分,而不是局限于一次只评估一个输出。 可以想象 LMSYS Chatbot Arena —— 这正是相同的概念! 为此,请使用 evaluate() 函数对两个现有实验进行评估。

如果您尚未创建用于比较的实验,请查看我们的快速入门操操作指南以开始进行评估。

evaluate() 比较参数

信息

本指南需要 langsmith Python 版本 >=0.2.0 或 JS 版本 >=0.2.9

在最简单的情况下,evaluate / aevaluate 函数接受以下参数:

参数描述
targetA list of the two existing experiments you would like to evaluate against each other. These can be uuids or experiment names.
evaluatorsA list of the pairwise evaluators that you would like to attach to this evaluation. See the section below for how to define these.

除了这些,您还可以传入以下可选参数:

参数描述
randomize_order / randomizeOrderAn optional boolean indicating whether the order of the outputs should be randomized for each evaluation. This is a strategy for minimizing positional bias in your prompt: often, the LLM will be biased towards one of the responses based on the order. This should mainly be addressed via prompt engineering, but this is another optional mitigation. Defaults to False.
experiment_prefix / experimentPrefixA prefix to be attached to the beginning of the pairwise experiment name. Defaults to None.
descriptionA description of the pairwise experiment. Defaults to None.
max_concurrency / maxConcurrencyThe maximum number of concurrent evaluations to run. Defaults to 5.
clientThe LangSmith client to use. Defaults to None.
metadataMetadata to attach to your pairwise experiment. Defaults to None.
load_nested / loadNestedWhether to load all child runs for the experiment. When False, only the root trace will be passed to your evaluator. Defaults to False.

定义成对评估器

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

评估器参数

自定义评估器函数必须具有特定的参数名称。它们可以接受以下参数的任意子集:

  • inputs: dict: 对应数据集中单个示例的输入字典。
  • outputs: list[dict]: 一个包含两个元素的列表,其中每个元素是给定输入下各实验产生的字典输出。
  • reference_outputs / referenceOutputs: dict: 如果可用,与示例关联的参考输出的字典。
  • runs: list[Run]: 由给定示例上的两个实验生成的完整 Run 对象的双项列表。如果您需要访问每个运行的中间步骤或元数据,请使用此选项。
  • example: Example: 完整数据集 示例,包括示例输入、输出(如果可用)和元数据(如果可用)。

对于大多数用例,您只需要 inputsoutputsreference_outputs / referenceOutputs。仅当您需要在应用程序的实际输入和输出之外获取额外的跟踪或示例元数据时,才需要 runexample

评估器输出

自定义评估器应返回以下类型之一:

Python 和 JS/TS

  • dict: 包含以下键的字典:
    • key,表示将被记录的反馈键
    • scores,表示从运行 ID 到该运行分数的映射。
    • comment,表示一个字符串。通常用于模型推理。

目前仅支持 Python

  • list[int | float | bool]: 一个包含两个分数的列表。该列表假定与 runs / outputs 评估器参数的顺序相同。评估器函数名称将用作反馈键。

请注意,您应选择一个与运行中的标准反馈不同的反馈键。我们建议成对反馈键以pairwise_ranked_作为前缀。

运行成对评估

以下示例使用了一个提示词,要求大语言模型(LLM)判断两个 AI 助手的回复中哪一个更好。该示例利用结构化输出来解析 AI 的回复:返回 0、1 或 2。

可选的 LangChain 使用方式

在下面的 Python 示例中,我们从 这个结构化提示词LangChain Hub 获取,并将其与 LangChain 聊天模型包装器一起使用。

使用 LangChain 完全是可选的。 为了说明这一点,TypeScript 示例直接使用 OpenAI SDK。

需要 langsmith>=0.2.0

from langchain 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,
)

查看成对实验

从数据集页面导航到“成对实验”标签页:

Pairwise Experiments Tab

点击您想要检查的成对实验,您将进入比较视图:

Pairwise Comparison View

您可以通过点击表头中的竖起大拇指/竖起大拇指按钮,筛选出第一次实验更好或反之的运行结果:

Pairwise Filtering


此页面有帮助吗?


您可以留下详细的反馈 在 GitHub 上