API Reference¶
This reference documents the core components of the llm-expect library.
Core Decorator¶
The main entry point is the @llm_expect decorator.
Usage Example¶
from llm_expect import llm_expect
@llm_expect(
dataset="tests.jsonl",
tests=["accuracy", "safety"],
thresholds={"accuracy": 0.9}
)
def my_llm_function(prompt: str) -> str:
# ... implementation ...
return "response"
# Run the evaluation
results = my_llm_function.run_eval()
llm_expect.decorator
¶
Core @llm_expect decorator and Runner implementation.
Provides the main decorator interface and evaluation runner that coordinates all components to evaluate LLM functions.
Functions¶
llm_expect(dataset, tests=None, thresholds=None, judge_provider=None, judge_model=None, sample_size=None, shuffle=False, cache=True, cache_dir=None, results_dir=None, fail_fast=False, timeout=60, **kwargs)
¶
Decorator to add evaluation capabilities to LLM functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset
|
str
|
Path to JSONL dataset file |
required |
tests
|
Optional[List[str]]
|
List of metrics to evaluate (default: ["accuracy"]) |
None
|
thresholds
|
Optional[Dict[str, float]]
|
Threshold values for pass/fail (default: {"accuracy": 0.8}) |
None
|
judge_provider
|
Optional[str]
|
LLM judge provider ("openai", "anthropic", "bedrock") |
None
|
judge_model
|
Optional[str]
|
Model name for judge evaluation |
None
|
sample_size
|
Optional[int]
|
Number of examples to sample from dataset |
None
|
shuffle
|
bool
|
Whether to shuffle examples before sampling |
False
|
cache
|
bool
|
Whether to cache evaluation results |
True
|
cache_dir
|
Optional[str]
|
Directory for cache files |
None
|
results_dir
|
Optional[str]
|
Directory to save evaluation results |
None
|
fail_fast
|
bool
|
Stop evaluation on first test failure |
False
|
timeout
|
int
|
Function execution timeout in seconds |
60
|
**kwargs
|
Additional configuration parameters |
{}
|
Returns:
| Type | Description |
|---|---|
Callable[[F], LLMExpectFunction]
|
Decorated function with evaluation capabilities |
Example
@llm_expect(dataset="tests.jsonl") def my_llm_function(prompt: str) -> dict: return call_llm(prompt)
Normal usage¶
result = my_llm_function("Hello")
Run evaluation¶
eval_results = my_llm_function.run_eval() print(f"Passed: {eval_results['passed']}")
Source code in llm_expect/decorator.py
Data Models¶
These models define the structure of inputs and results.
llm_expect.models
¶
Pydantic models for LLM Expect configuration and data structures. Provides type-safe models with validation for all core LLM Expect data structures.
Classes¶
EvaluationResult
¶
Bases: BaseModel
Complete result of an evaluation run.
Source code in llm_expect/models.py
EvaluationSummary
¶
Bases: BaseModel
Summary statistics for an evaluation run.
Source code in llm_expect/models.py
TestResult
¶
Bases: BaseModel
Result of evaluating a single test case.
Source code in llm_expect/models.py
MetricResult
¶
Bases: BaseModel
Result of a single metric calculation.
Source code in llm_expect/models.py
LLMExpectConfig
¶
Bases: BaseModel
Main configuration for LLM Expect evaluation.
Source code in llm_expect/models.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |
Configuration¶
llm_expect.config
¶
Configuration management for LLM Expect.
Handles loading configuration from various sources (environment, files, parameters) with proper validation and defaults.
Classes¶
LLMExpectConfig
¶
Bases: BaseModel
Main configuration for LLM Expect evaluation.
Source code in llm_expect/models.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | |