跳到主要内容

如何定义要评估的目标函数

运行评估需要三个主要部分

  1. 测试输入和预期输出的 数据集
  2. 您要评估的目标函数。
  3. 对目标函数的输出进行评分的评估器

本指南向您展示如何根据您正在评估的应用程序部分定义目标函数。请参阅如何创建数据集如何定义评估器,以及运行端到端评估的示例

目标函数签名

为了在代码中评估应用程序,我们需要一种运行应用程序的方法。当使用 evaluate() (Python/TypeScript) 时,我们将通过传入目标函数参数来完成此操作。这是一个函数,它接受数据集 示例 的输入,并将应用程序输出作为字典返回。在此函数中,我们可以随意调用我们的应用程序。我们也可以随意格式化输出。关键是,我们定义的任何评估器函数都应与我们在目标函数中返回的输出格式一起使用。

from langsmith import Client

# 'inputs' will come from your dataset.
def dummy_target(inputs: dict) -> dict:
return {"foo": 1, "bar": "two"}

# 'inputs' will come from your dataset.
# 'outputs' will come from your target function.
def evaluator_one(inputs: dict, outputs: dict) -> bool:
return outputs["foo"] == 2

def evaluator_two(inputs: dict, outputs: dict) -> bool:
return len(outputs["bar"]) < 3

client = Client()
results = client.evaluate(
dummy_target, # <-- target function
data="your-dataset-name",
evaluators=[evaluator_one, evaluator_two],
...
)
自动追踪

evaluate() 将自动追踪您的目标函数。这意味着如果您在目标函数中运行任何可追踪的代码,这也将被追踪为目标追踪的子运行。

示例:单次 LLM 调用

当我们迭代 Prompt 或比较模型时,评估单次 LLM 调用可能很有用

设置环境变量 OPENAI_API_KEY 并安装依赖 pip install -U openai langsmith

from langsmith import wrappers
from openai import OpenAI

# Optionally wrap the OpenAI client to automatically
# trace all model calls.
oai_client = wrappers.wrap_openai(OpenAI())

def target(inputs: dict) -> dict:
# This assumes your dataset has inputs with a 'messages' key.
# You can update to match your dataset schema.
messages = inputs["messages"]
response = oai_client.chat.completions.create(
messages=messages,
model="gpt-4o-mini",
)
return {"answer": response.choices[0].message.content}

示例:非 LLM 组件

有时,您可能想要评估应用程序中不涉及 LLM 的步骤。这包括但不限于

  • RAG 应用中的检索步骤
  • 工具的执行

在此示例中,我们展示了如何测试简单的计算器工具。在实践中,评估对于具有更复杂且难以进行单元测试的行为的组件非常有用,例如检索器或在线研究工具。

from langsmith import traceable

# Optionally decorate with '@traceable' to trace all invocations of this function.
@traceable
def calculator_tool(operation: str, number1: float, number2: float) -> str:
if operation == "add":
return str(number1 + number2)
elif operation == "subtract":
return str(number1 - number2)
elif operation == "multiply":
return str(number1 * number2)
elif operation == "divide":
return str(number1 / number2)
else:
raise ValueError(f"Unrecognized operation: {operation}.")

# This is the function you will evaluate.
def target(inputs: dict) -> dict:
# This assumes your dataset has inputs with `operation`, `num1`, and `num2` keys.
operation = inputs["operation"]
number1 = inputs["num1"]
number2 = inputs["num2"]
result = calculator_tool(operation, number1, number2)
return {"result": result}

示例:应用程序或 Agent

评估您的 Agent 应用的完整输出可以捕捉多个组件之间的交互,从而提供更真实的端到端性能图景。端到端评估还可以发现测试隔离函数或单个 LLM 调用时可能遗漏的集成和错误处理问题。

from my_agent import agent

# This is the function you will evaluate.
def target(inputs: dict) -> dict:
# This assumes your dataset has inputs with a `messages` key
messages = inputs["messages"]
# Replace `invoke` with whatever you use to call your agent
response = agent.invoke({"messages": messages})
# This assumes your agent output is in the right format
return response
LangGraph / LangChain 目标

如果您有一个 LangGraph/LangChain Agent,它接受数据集中定义的输入,并返回您要在评估器中使用的输出格式,您可以将该对象直接作为目标传入

from my_agent import agent
from langsmith import Client

client = Client()
client.evaluate(agent, ...)

此页面是否对您有帮助?


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