API Evaluation
Guide to evaluating OpenAI, Anthropic, and other API-based language models.
Overview
The lm-evaluation-harness supports evaluating API-based models through a unified TemplateAPI interface. This allows benchmarking of:
- - OpenAI models (GPT-4, GPT-3.5, etc.)
- - Anthropic models (Claude 3, Claude 2, etc.)
- - Local OpenAI-compatible APIs
- - Custom API endpoints
- - Benchmark closed-source models
- - Compare API models to open models
- - Validate API performance
- - Track model updates over time
- -
generate_until: ✅ - -
loglikelihood: ✅ - -
loglikelihood_rolling: ✅ - -
generate_until: ✅ - -
loglikelihood: ❌ (no logprobs) - -
loglikelihood_rolling: ❌ - -
model: Model identifier (required) - -
base_url: API endpoint (default: OpenAI) - -
num_concurrent: Concurrent requests (default: 5) - -
max_retries: Retry failed requests (default: 3) - -
timeout: Request timeout in seconds (default: 60) - -
tokenizer: Tokenizer to use (default: matches model) - -
tokenizer_backend:"tiktoken"or"huggingface" - - Use
--limit Nfor testing - - Start with
gpt-3.5-turbobeforegpt-4 - - Set
max_gen_toksto minimum needed - - Use
num_fewshot=0for zero-shot when possible - - Claude 3.5 Sonnet: $3.00 / 1M input, $15.00 / 1M output
- - Claude 3 Opus: $15.00 / 1M input, $75.00 / 1M output
- - Claude 3 Haiku: $0.25 / 1M input, $1.25 / 1M output
- - OpenAI API: https://platform.openai.com/docs/api-reference
- - Anthropic API: https://docs.anthropic.com/claude/reference
- - TemplateAPI:
lm_eval/models/api_models.py - - OpenAI models:
lm_eval/models/openai_completions.py - - Anthropic models:
lm_eval/models/anthropic_llms.py
Why evaluate API models:
Supported API Models
| Provider | Model Type | Request Types | Logprobs | |
| ---------- | ------------ | --------------- | ---------- | |
| OpenAI (completions) | openai-completions | All | ✅ Yes | |
| OpenAI (chat) | openai-chat-completions | generate_until only | ❌ No | |
| Anthropic (completions) | anthropic-completions | All | ❌ No | |
| Anthropic (chat) | anthropic-chat | generate_until only | ❌ No | |
| Local (OpenAI-compatible) | local-completions | Depends on server | Varies | |
| Model | MMLU | GSM8K | HumanEval | Cost |
| ------- | ------ | ------- | ----------- | ------ |
| GPT-4 Turbo | 86.4% | 92.0% | 67.0% | $$$$ |
| Claude 3 Opus | 86.8% | 95.0% | 84.9% | $$$$ |
| GPT-3.5 Turbo | 70.0% | 57.1% | 48.1% | $$ |
| Llama 2 70B | 68.9% | 56.8% | 29.9% | Free (self-host) |
| Mixtral 8x7B | 70.6% | 58.4% | 40.2% | Free (self-host) |
Best Practices
Rate Limiting
Respect API rate limits:
`bash
lm_eval --model openai-chat-completions \
--model_args \
model=gpt-4-turbo,\
num_concurrent=3,\ # Lower concurrency
timeout=120 \ # Longer timeout
--tasks mmlu
`
Reproducibility
Set temperature to 0 for deterministic results:
`bash
lm_eval --model openai-chat-completions \
--model_args model=gpt-4-turbo \
--tasks mmlu \
--gen_kwargs temperature=0.0
`
Or use seed for sampling:
`bash
lm_eval --model anthropic-chat \
--model_args model=claude-3-5-sonnet-20241022 \
--tasks gsm8k \
--gen_kwargs temperature=0.7,seed=42
`
Caching
API models automatically cache responses to avoid redundant calls:
`bash
First run: makes API calls
lm_eval --model openai-chat-completions \
--model_args model=gpt-4-turbo \
--tasks mmlu \
--limit 100
Second run: uses cache (instant, free)
lm_eval --model openai-chat-completions \
--model_args model=gpt-4-turbo \
--tasks mmlu \
--limit 100
`
Cache location: ~/.cache/lm_eval/
Error Handling
APIs can fail. Use retries:
`bash
lm_eval --model openai-chat-completions \
--model_args \
model=gpt-4-turbo,\
max_retries=5,\
timeout=120 \
--tasks mmlu
`
Troubleshooting
"Authentication failed"
Check API key:
`bash
echo $OPENAI_API_KEY # Should print sk-...
echo $ANTHROPIC_API_KEY # Should print sk-ant-...
`
"Rate limit exceeded"
Reduce concurrency:
`bash
--model_args num_concurrent=1
`
Or add delays between requests.
"Timeout error"
Increase timeout:
`bash
--model_args timeout=180
`
"Model not found"
For local APIs, verify server is running:
`bash
curl http://localhost:8000/v1/models
`
Cost Runaway
Use --limit for testing:
`bash
lm_eval --model openai-chat-completions \
--model_args model=gpt-4-turbo \
--tasks mmlu \
--limit 50 # Only 50 samples
`
Advanced Features
Custom Headers
`bash
lm_eval --model local-completions \
--model_args \
base_url=http://api.example.com/v1,\
header="Authorization: Bearer token,X-Custom: value"
`
Disable SSL Verification (Development Only)
`bash
lm_eval --model local-completions \
--model_args \
base_url=https://localhost:8000/v1,\
verify_certificate=false
`
Custom Tokenizer
`bash
lm_eval --model openai-chat-completions \
--model_args \
model=gpt-4-turbo,\
tokenizer=gpt2,\
tokenizer_backend=huggingface
`