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

如何获取实验的性能指标

TIP

追踪项目与实验在后端使用同一套底层数据结构,我们称之为「session」。

你在文档中可能会看到这几个词混用,但它们指的都是同一个底层数据结构。

我们正在统一文档与 API 中的术语。

用 Python 或 TypeScript SDK 的 evaluate 运行实验后,可以通过 read_project/readProject 方法获取该实验的性能指标。

实验详情的载荷包含下列字段:

json
{
  "start_time": "2024-06-06T01:02:51.299960",
  "end_time": "2024-06-06T01:03:04.557530+00:00",
  "extra": {
    "metadata": {
      "git": {
        "tags": null,
        "dirty": true,
        "branch": "ankush/agent-eval",
        "commit": "...",
        "repo_name": "...",
        "remote_url": "...",
        "author_name": "Ankush Gola",
        "commit_time": "...",
        "author_email": "..."
      },
      "revision_id": null,
      "dataset_splits": ["base"],
      "dataset_version": "2024-06-05T04:57:01.535578+00:00",
      "num_repetitions": 3
    }
  },
  "name": "SQL Database Agent-ae9ad229",
  "description": null,
  "default_dataset_id": null,
  "reference_dataset_id": "...",
  "id": "...",
  "run_count": 9,
  "latency_p50": 7.896,
  "latency_p99": 13.09332,
  "first_token_p50": null,
  "first_token_p99": null,
  "total_tokens": 35573,
  "prompt_tokens": 32711,
  "completion_tokens": 2862,
  "total_cost": 0.206485,
  "prompt_cost": 0.163555,
  "completion_cost": 0.04293,
  "tenant_id": "...",
  "last_run_start_time": "2024-06-06T01:02:51.366397",
  "last_run_start_time_live": null,
  "feedback_stats": {
    "cot contextual accuracy": {
      "n": 9,
      "avg": 0.6666666666666666,
      "values": {
        "CORRECT": 6,
        "INCORRECT": 3
      }
    }
  },
  "session_feedback_stats": {},
  "run_facets": [],
  "error_rate": 0,
  "streaming_rate": 0,
  "test_run_number": 11
}

从中可以提取的性能指标包括:

  • latency_p50:第 50 百分位延迟(秒)。
  • latency_p99:第 99 百分位延迟(秒)。
  • total_tokens:使用的 token 总数。
  • prompt_tokens:使用的提示词 token 数。
  • completion_tokens:使用的补全 token 数。
  • total_cost:实验的总成本。
  • prompt_cost:提示词 token 的成本。
  • completion_cost:补全 token 的成本。
  • feedback_stats:实验的反馈统计。
  • error_rate:实验的错误率。
  • first_token_p50:生成首个 token 耗时的第 50 百分位延迟(使用流式输出时)。
  • first_token_p99:生成首个 token 耗时的第 99 百分位延迟(使用流式输出时)。

下面的示例展示如何用 Python 与 TypeScript SDK 获取实验的性能指标。

首先作为前置步骤,创建一个极简数据集。这里只在 Python 中演示,TypeScript 做法相同。评估的更多细节见操作指南。

python
from langsmith import Client

client = Client()

# Create a dataset
dataset_name = "HelloDataset"
dataset = client.create_dataset(dataset_name=dataset_name)

examples = [
    {
        "inputs": {"input": "Harrison"},
        "outputs": {"expected": "Hello Harrison"},
    },
    {
        "inputs": {"input": "Ankush"},
        "outputs": {"expected": "Hello Ankush"},
    },
]

client.create_examples(dataset_id=dataset.id, examples=examples)

接下来创建一个实验,从 evaluate 的结果中取到实验名,再获取该实验的性能指标。

python
from langsmith.schemas import Example, Run
dataset_name = "HelloDataset"

def foo_label(root_run: Run, example: Example) -> dict:
    return {"score": 1, "key": "foo"}

from langsmith import evaluate

results = evaluate(
    lambda inputs: "Hello " + inputs["input"],
    data=dataset_name,
    evaluators=[foo_label],
    experiment_prefix="Hello",
)

resp = client.read_project(project_name=results.experiment_name, include_stats=True)
print(resp.model_dump_json(indent=2))
typescript
import { Client } from "langsmith";
import { evaluate } from "langsmith/evaluation";
import type { EvaluationResult } from "langsmith/evaluation";
import type { Run, Example } from "langsmith/schemas";

// Row-level evaluator
function fooLabel(rootRun: Run, example: Example): EvaluationResult {
    return {score: 1, key: "foo"};
}

const client = new Client();

const results = await evaluate(
    (inputs) => {
        return { output: "Hello " + inputs.input };
    },
    {
        data: "HelloDataset",
        experimentPrefix: "Hello",
        evaluators: [fooLabel],
    }
);

const resp = await client.readProject({
    projectName: results.experimentName,
    includeStats: true
})
console.log(JSON.stringify(resp, null, 2))

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