LLM Judge
Bedrock-backed judge for subjective metrics, with versioned prompt templates and calibration against human scores.
Warning
Evaluated agent output is untrusted input in every judge prompt. See
SECURITY.md.
uaef.llm_judge.judge
LLM Judge interface for UAEF.
This module provides a high-level interface for LLM-based evaluation using AWS Bedrock with prompt templates and response validation.
LLMJudge
High-level interface for LLM-based evaluation.
Provides methods for: - Evaluating responses with criteria - Evaluating with reference answers - Evaluating tool calling - Batch evaluation - Response parsing and validation
Source code in uaef/llm_judge/judge.py
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
batch_evaluate(evaluations, evaluation_type='generic')
Perform batch evaluation for multiple requests.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
evaluations
|
List[Dict[str, Any]]
|
List of evaluation requests, each a dict with required fields |
required |
evaluation_type
|
str
|
Type of evaluation ("generic", "reference", "response_quality", etc.) |
'generic'
|
Returns:
| Type | Description |
|---|---|
List[Tuple[Optional[float], Optional[str], Optional[Exception]]]
|
List of tuples (score, reasoning, error) for each evaluation. |
List[Tuple[Optional[float], Optional[str], Optional[Exception]]]
|
If successful, error is None. If failed, score and reasoning are None. |
Source code in uaef/llm_judge/judge.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 | |
evaluate(question, response, criteria, custom_template=None)
Evaluate a response against criteria.
Generic evaluation method that can be used for any criteria.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user's question |
required |
response
|
str
|
The agent's response |
required |
criteria
|
str
|
Evaluation criteria description |
required |
custom_template
|
Optional[str]
|
Custom prompt template (optional) |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, str]
|
Tuple of (score, reasoning) |
Raises:
| Type | Description |
|---|---|
LLMJudgeError
|
If evaluation fails |
Source code in uaef/llm_judge/judge.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
evaluate_response_quality(question, response, metric_name, context=None, expected_output=None, template_version=None, custom_template=None)
Evaluate response quality (relevance, completeness, hallucination).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user's question |
required |
response
|
str
|
The agent's response |
required |
metric_name
|
str
|
Metric name ("answer_relevance", "completeness", "hallucination") |
required |
context
|
Optional[str]
|
Context documents (for hallucination detection) |
None
|
expected_output
|
Optional[str]
|
Expected output (for completeness) |
None
|
template_version
|
Optional[str]
|
Template version (optional) |
None
|
custom_template
|
Optional[str]
|
Custom prompt template (optional) |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, str]
|
Tuple of (score, reasoning) |
Raises:
| Type | Description |
|---|---|
LLMJudgeError
|
If evaluation fails |
Source code in uaef/llm_judge/judge.py
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 | |
evaluate_responsible_ai(response, metric_name, template_version=None, custom_template=None)
Evaluate responsible AI metrics (safety, bias, toxicity).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response
|
str
|
The agent's response |
required |
metric_name
|
str
|
Metric name ("safety", "bias", "toxicity") |
required |
template_version
|
Optional[str]
|
Template version (optional) |
None
|
custom_template
|
Optional[str]
|
Custom prompt template (optional) |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, str]
|
Tuple of (score, reasoning) |
Raises:
| Type | Description |
|---|---|
LLMJudgeError
|
If evaluation fails |
Source code in uaef/llm_judge/judge.py
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 | |
evaluate_tool_calling(question, called_tools, all_tools, expected_tools=None)
Evaluate tool calling quality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user's question |
required |
called_tools
|
List[Dict[str, Any]]
|
List of tools that were called |
required |
all_tools
|
List[Dict[str, Any]]
|
List of all available tools |
required |
expected_tools
|
Optional[List[Dict[str, Any]]]
|
Expected tools to be called (optional) |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, str]
|
Tuple of (score, reasoning) |
Raises:
| Type | Description |
|---|---|
LLMJudgeError
|
If evaluation fails |
Source code in uaef/llm_judge/judge.py
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 | |
evaluate_with_reference(question, reference, response, metric_name='accuracy', template_version=None, custom_template=None)
Evaluate a response against a reference answer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user's question |
required |
reference
|
str
|
Reference/ground truth answer |
required |
response
|
str
|
The agent's response |
required |
metric_name
|
str
|
Name of metric template to use (default: "accuracy") |
'accuracy'
|
template_version
|
Optional[str]
|
Template version (optional) |
None
|
custom_template
|
Optional[str]
|
Custom prompt template (optional) |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[float, str]
|
Tuple of (score, reasoning) |
Raises:
| Type | Description |
|---|---|
LLMJudgeError
|
If evaluation fails |
Source code in uaef/llm_judge/judge.py
187 188 189 190 191 192 193 194 195 196 197 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 | |
LLMJudgeError
Bases: Exception
Base exception for LLM Judge errors.
Source code in uaef/llm_judge/judge.py
30 31 32 | |
get_llm_judge()
Get the global LLM Judge instance.
Returns:
| Type | Description |
|---|---|
LLMJudge
|
LLMJudge instance |
Source code in uaef/llm_judge/judge.py
586 587 588 589 590 591 592 593 594 595 596 | |
reset_llm_judge()
Reset the global LLM Judge to None.
Source code in uaef/llm_judge/judge.py
610 611 612 613 | |
set_llm_judge(judge)
Set the global LLM Judge instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
judge
|
LLMJudge
|
LLMJudge instance to set as global |
required |
Source code in uaef/llm_judge/judge.py
599 600 601 602 603 604 605 606 607 | |
uaef.llm_judge.bedrock_client
Bedrock client wrapper for UAEF LLM Judge.
This module provides a centralized Bedrock client with retry logic, error handling, and batch processing capabilities.
BedrockClient
Wrapper for AWS Bedrock client with retry logic and error handling.
Provides: - Automatic retry with exponential backoff - Error handling for rate limits, timeouts, service errors - Support for multiple Bedrock models - Batch invocation for offline evaluation
Source code in uaef/llm_judge/bedrock_client.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 | |
batch_invoke(model_id, prompts, max_tokens=2048, temperature=0.0, top_p=0.1, top_k=50, stop_sequences=None)
Invoke Bedrock model for multiple prompts (batch processing).
This method processes prompts sequentially with retry logic for each. For true parallel batch processing, use AWS Bedrock Batch Inference API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Bedrock model ID |
required |
prompts
|
List[str]
|
List of prompts to process |
required |
max_tokens
|
int
|
Maximum tokens in response |
2048
|
temperature
|
float
|
Sampling temperature |
0.0
|
top_p
|
float
|
Nucleus sampling parameter |
0.1
|
top_k
|
int
|
Top-k sampling parameter |
50
|
stop_sequences
|
Optional[List[str]]
|
List of stop sequences |
None
|
Returns:
| Type | Description |
|---|---|
List[Tuple[Optional[str], Optional[Dict[str, Any]], Optional[Exception]]]
|
List of tuples (response_text, metadata, error) for each prompt. |
List[Tuple[Optional[str], Optional[Dict[str, Any]], Optional[Exception]]]
|
If successful, error is None. If failed, response_text and metadata are None. |
Source code in uaef/llm_judge/bedrock_client.py
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 | |
invoke_model(model_id, prompt, max_tokens=2048, temperature=0.0, top_p=0.1, top_k=50, stop_sequences=None, system=None)
Invoke a Bedrock model with retry logic.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_id
|
str
|
Bedrock model ID (e.g., "us.anthropic.claude-sonnet-4-6") |
required |
prompt
|
str
|
The prompt to send to the model |
required |
max_tokens
|
int
|
Maximum tokens in response |
2048
|
temperature
|
float
|
Sampling temperature (0.0-1.0) |
0.0
|
top_p
|
float
|
Nucleus sampling parameter |
0.1
|
top_k
|
int
|
Top-k sampling parameter |
50
|
stop_sequences
|
Optional[List[str]]
|
List of stop sequences |
None
|
system
|
Optional[str]
|
Optional system-level instruction, sent via the model's
dedicated system field (Anthropic Claude's top-level |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[str, Dict[str, Any]]
|
Tuple of (response_text, metadata) where metadata includes usage stats |
Raises:
| Type | Description |
|---|---|
BedrockRateLimitError
|
If rate limit exceeded after retries |
BedrockTimeoutError
|
If request times out after retries |
BedrockServiceError
|
If service error occurs after retries |
BedrockClientError
|
For other errors |
Source code in uaef/llm_judge/bedrock_client.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 | |
BedrockClientError
Bases: Exception
Base exception for Bedrock client errors.
Source code in uaef/llm_judge/bedrock_client.py
23 24 25 | |
BedrockRateLimitError
Bases: BedrockClientError
Exception raised when rate limit is exceeded.
Source code in uaef/llm_judge/bedrock_client.py
28 29 30 | |
BedrockServiceError
Bases: BedrockClientError
Exception raised for Bedrock service errors.
Source code in uaef/llm_judge/bedrock_client.py
38 39 40 | |
BedrockTimeoutError
Bases: BedrockClientError
Exception raised when request times out.
Source code in uaef/llm_judge/bedrock_client.py
33 34 35 | |
uaef.llm_judge.prompt_templates
Prompt template library for UAEF LLM Judge.
This module provides versioned prompt templates for LLM-based metrics with validation and custom template support.
PromptTemplateError
Bases: Exception
Base exception for prompt template errors.
Source code in uaef/llm_judge/prompt_templates.py
25 26 27 | |
PromptTemplateLoader
Loader for versioned prompt templates.
Supports: - Loading templates from versioned directories - Custom template overrides - Template validation - Variable substitution
Source code in uaef/llm_judge/prompt_templates.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 302 303 304 305 306 | |
format_template(template, variables)
Format a template with variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template
|
str
|
Template string with {variable} placeholders |
required |
variables
|
Dict[str, Any]
|
Dictionary of variable values |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted template string |
Raises:
| Type | Description |
|---|---|
PromptTemplateValidationError
|
If required variables are missing |
Source code in uaef/llm_judge/prompt_templates.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 | |
get_template_info(metric_name, version=None)
Get information about a template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric |
required |
version
|
Optional[str]
|
Template version (defaults to default_version) |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with template info (path, size, variables) |
Source code in uaef/llm_judge/prompt_templates.py
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 302 303 304 305 306 | |
list_templates(version=None)
List available templates for a version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
version
|
Optional[str]
|
Template version (defaults to default_version) |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of template names (without .txt extension) |
Source code in uaef/llm_judge/prompt_templates.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
list_versions()
List available template versions.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of version names |
Source code in uaef/llm_judge/prompt_templates.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 | |
load_template(metric_name, version=None, custom_template=None)
Load a prompt template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric (e.g., "answer_relevance") |
required |
version
|
Optional[str]
|
Template version (defaults to default_version) |
None
|
custom_template
|
Optional[str]
|
Custom template string to use instead of file |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Template string |
Raises:
| Type | Description |
|---|---|
PromptTemplateNotFoundError
|
If template file not found |
PromptTemplateError
|
For other errors |
Source code in uaef/llm_judge/prompt_templates.py
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | |
validate_response(response_text, expected_format='json', candidate_text=None)
Validate LLM response against expected format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
response_text
|
str
|
Response text from LLM |
required |
expected_format
|
str
|
Expected format ("json" or "text") |
'json'
|
candidate_text
|
Optional[Any]
|
Security review H-01: the untrusted candidate
content this response scored (exactly what was passed to
|
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Parsed response (dict for JSON, str for text) |
Raises:
| Type | Description |
|---|---|
PromptTemplateValidationError
|
If response doesn't match expected format |
Source code in uaef/llm_judge/prompt_templates.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 | |
PromptTemplateNotFoundError
Bases: PromptTemplateError
Exception raised when prompt template is not found.
Source code in uaef/llm_judge/prompt_templates.py
30 31 32 | |
PromptTemplateValidationError
Bases: PromptTemplateError
Exception raised when prompt template validation fails.
Source code in uaef/llm_judge/prompt_templates.py
35 36 37 | |
get_template_loader()
Get the global prompt template loader.
Returns:
| Type | Description |
|---|---|
PromptTemplateLoader
|
PromptTemplateLoader instance |
Source code in uaef/llm_judge/prompt_templates.py
313 314 315 316 317 318 319 320 321 322 323 | |
reset_template_loader()
Reset the global template loader to None.
Source code in uaef/llm_judge/prompt_templates.py
337 338 339 340 | |
set_template_loader(loader)
Set the global prompt template loader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
loader
|
PromptTemplateLoader
|
PromptTemplateLoader instance to set as global |
required |
Source code in uaef/llm_judge/prompt_templates.py
326 327 328 329 330 331 332 333 334 | |
uaef.llm_judge.calibration
Prompt calibration engine for UAEF LLM Judge.
This module provides tools for calibrating LLM judge prompts by comparing LLM scores with human judgments and suggesting improvements.
CalibrationEngine
Engine for calibrating LLM judge prompts.
Provides: - Comparison of LLM scores with human judgments - Correlation metrics (Pearson, Spearman) - Identification of disagreement patterns - Prompt improvement suggestions - Performance tracking over time
Source code in uaef/llm_judge/calibration.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
add_sample(sample_id, metric_name, prompt_version, llm_score, human_score, question, response, llm_reasoning, human_reasoning=None)
Add a calibration sample.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_id
|
str
|
Unique sample identifier |
required |
metric_name
|
str
|
Name of the metric |
required |
prompt_version
|
str
|
Version of the prompt used |
required |
llm_score
|
float
|
Score from LLM judge |
required |
human_score
|
float
|
Score from human reviewer |
required |
question
|
str
|
The question/input |
required |
response
|
str
|
The agent's response |
required |
llm_reasoning
|
str
|
LLM's reasoning |
required |
human_reasoning
|
Optional[str]
|
Human's reasoning (optional) |
None
|
Returns:
| Type | Description |
|---|---|
CalibrationSample
|
CalibrationSample instance |
Source code in uaef/llm_judge/calibration.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
analyze_disagreements(metric_name, prompt_version, disagreement_threshold=0.2)
Analyze disagreement patterns between LLM and human scores.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric |
required |
prompt_version
|
str
|
Version of the prompt |
required |
disagreement_threshold
|
float
|
Threshold for identifying disagreements |
0.2
|
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with disagreement analysis |
Source code in uaef/llm_judge/calibration.py
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
calculate_metrics(metric_name, prompt_version, disagreement_threshold=0.2)
Calculate calibration metrics for a specific metric and prompt version.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric |
required |
prompt_version
|
str
|
Version of the prompt |
required |
disagreement_threshold
|
float
|
Threshold for identifying disagreements |
0.2
|
Returns:
| Type | Description |
|---|---|
CalibrationMetrics
|
CalibrationMetrics instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If insufficient samples |
Source code in uaef/llm_judge/calibration.py
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 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 | |
export_samples(metric_name=None, prompt_version=None)
Export calibration samples as dictionaries.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
Optional[str]
|
Filter by metric name (optional) |
None
|
prompt_version
|
Optional[str]
|
Filter by prompt version (optional) |
None
|
Returns:
| Type | Description |
|---|---|
List[Dict[str, Any]]
|
List of sample dictionaries |
Source code in uaef/llm_judge/calibration.py
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 | |
get_samples(metric_name=None, prompt_version=None, min_disagreement=None)
Get calibration samples with optional filtering.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
Optional[str]
|
Filter by metric name (optional) |
None
|
prompt_version
|
Optional[str]
|
Filter by prompt version (optional) |
None
|
min_disagreement
|
Optional[float]
|
Minimum disagreement threshold (optional) |
None
|
Returns:
| Type | Description |
|---|---|
List[CalibrationSample]
|
List of CalibrationSample instances |
Source code in uaef/llm_judge/calibration.py
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 | |
suggest_improvements(metric_name, prompt_version, disagreement_threshold=0.2)
Suggest prompt improvements based on disagreement analysis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric |
required |
prompt_version
|
str
|
Version of the prompt |
required |
disagreement_threshold
|
float
|
Threshold for identifying disagreements |
0.2
|
Returns:
| Type | Description |
|---|---|
List[str]
|
List of improvement suggestions |
Source code in uaef/llm_judge/calibration.py
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 | |
track_performance(metric_name)
Track prompt performance over time across versions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_name
|
str
|
Name of the metric |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dictionary with performance tracking data |
Source code in uaef/llm_judge/calibration.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 | |
CalibrationMetrics
dataclass
Metrics for prompt calibration.
Source code in uaef/llm_judge/calibration.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
is_well_calibrated(min_correlation=0.7, max_mae=0.15)
Check if prompt is well calibrated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_correlation
|
float
|
Minimum acceptable correlation |
0.7
|
max_mae
|
float
|
Maximum acceptable mean absolute error |
0.15
|
Returns:
| Type | Description |
|---|---|
bool
|
True if well calibrated |
Source code in uaef/llm_judge/calibration.py
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
CalibrationSample
dataclass
A single calibration sample with LLM and human scores.
Source code in uaef/llm_judge/calibration.py
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | |
is_disagreement
property
Check if LLM and human scores disagree significantly.
score_difference
property
Calculate absolute difference between LLM and human scores.