LLM 基准测试 Quickstart
benchmark_llm 模板在折扣计算任务上对多种 LLM 模型做基准测试与比较。
创建项目
ragas quickstart benchmark_llm
cd benchmark_llm
安装依赖
uv sync
设置 API Key
export OPENAI_API_KEY="your-openai-key"
# Or other provider keys as needed
运行评测
uv run python evals.py
要基准测试特定模型:
uv run python evals.py --model gpt-4o
uv run python evals.py --model gpt-3.5-turbo
项目结构
benchmark_llm/
├── README.md # Project documentation
├── pyproject.toml # Project configuration
├── prompt.py # Prompt implementation
├── evals.py # Evaluation workflow
├── __init__.py # Python package marker
└── evals/
├── datasets/
│ └── discount_benchmark.csv # Customer profiles and expected discounts
├── experiments/ # Evaluation results
└── logs/ # Execution logs
评测内容
该模板在结构化输出任务上基准测试 LLM 表现:
- 任务:根据客户画像计算折扣百分比
- 模型:比较 GPT-4、GPT-3.5、Claude、Gemini 等
- 输出格式:带折扣百分比的 JSON
- 指标:折扣准确率(correct/incorrect)
理解代码
Prompt(prompt.py)
根据客户画像计算折扣:
from prompt import run_prompt
profile = "Premium customer, 5 years tenure, $50k annual spend"
result = await run_prompt(profile, model="gpt-4o")
# Returns: {"discount_percentage": 15}
评测(evals.py)
基准测试模型准确率:
@discrete_metric(name="discount_accuracy", allowed_values=["correct", "incorrect"])
def discount_accuracy(prediction: str, expected_discount):
parsed_json = json.loads(prediction)
predicted_discount = parsed_json.get("discount_percentage")
if predicted_discount == int(expected_discount):
return MetricResult(value="correct", ...)
else:
return MetricResult(value="incorrect", ...)
测试数据
模板包含 evals/datasets/discount_benchmark.csv,其中包括:
- 客户画像(tenure、spend、tier 等)
- 期望折扣百分比
- 折扣计算的业务规则
基准测试多个模型
对相同评测在不同模型上运行:
# GPT-4
uv run python evals.py --model gpt-4o
# GPT-3.5
uv run python evals.py --model gpt-3.5-turbo
# Claude
uv run python evals.py --model claude-3-5-sonnet-20241022
# Compare results
定制
添加你自己的任务
修改 prompt 以基准测试不同能力:
# Code generation
prompt = "Generate Python code to {task}"
# Summarization
prompt = "Summarize this text in 50 words: {text}"
# Classification
prompt = "Classify this email as spam/not-spam: {email}"
比较成本与延迟
跟踪额外指标:
import time
start = time.time()
response = await run_prompt(profile, model=model_name)
latency = time.time() - start
# Log cost and latency alongside accuracy
分析结果
比较模型表现:
import pandas as pd
gpt4_results = pd.read_csv("evals/experiments/gpt4_benchmark.csv")
gpt35_results = pd.read_csv("evals/experiments/gpt35_benchmark.csv")
print(f"GPT-4 Accuracy: {(gpt4_results['discount_accuracy'] == 'correct').mean():.1%}")
print(f"GPT-3.5 Accuracy: {(gpt35_results['discount_accuracy'] == 'correct').mean():.1%}")
下一步
- Judge Alignment - 衡量 judge 对齐
- Prompt 评测 - 比较不同 prompt