Metadata-Version: 2.5
Name: judgemeter
Version: 0.2.0
Summary: Measure how far an LLM judge can be trusted: human agreement, position bias and self-consistency.
Project-URL: Homepage, https://github.com/aman-singh01/judgemeter
Project-URL: Issues, https://github.com/aman-singh01/judgemeter/issues
Author: Aman Singh Rajpoot
License: MIT
License-File: LICENSE
Keywords: cohen-kappa,evaluation,llm,llm-as-a-judge,llm-evals,position-bias
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.10
Provides-Extra: dev
Requires-Dist: build>=1.0; extra == 'dev'
Requires-Dist: pytest-cov>=4; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff>=0.5; extra == 'dev'
Requires-Dist: scikit-learn>=1.3; extra == 'dev'
Description-Content-Type: text/markdown

# judgemeter

[![CI](https://github.com/aman-singh01/judgemeter/actions/workflows/ci.yml/badge.svg)](https://github.com/aman-singh01/judgemeter/actions/workflows/ci.yml)
[![PyPI](https://img.shields.io/pypi/v/judgemeter)](https://pypi.org/project/judgemeter/)
![Python](https://img.shields.io/badge/python-3.10%2B-blue)
![Dependencies](https://img.shields.io/badge/dependencies-none-brightgreen)
![License](https://img.shields.io/badge/license-MIT-green)

Measure how far an LLM judge can be trusted before you rely on its scores.

LLM-as-a-judge is everywhere in evaluation pipelines, but the judge itself is rarely checked. judgemeter answers three questions about any judge, whatever framework or model it uses:

| Check | Question it answers |
| --- | --- |
| **Human agreement** | Does the judge label things the way people do? (Cohen's kappa, with a confidence interval) |
| **Position bias** | Does the judge's pick change when only the order of the answers changes? |
| **Self-consistency** | Does the judge give the same answer when asked the same thing again? |

It has no dependencies and works with any judge you can wrap in a Python function.

## Install

```bash
pip install judgemeter
```

## Quick start

### Agreement with human labels

```python
from judgemeter import agreement

judge = ["pass", "fail", "pass", "pass", "fail", "pass"]
human = ["pass", "fail", "fail", "pass", "fail", "pass"]

result = agreement(judge, human, bootstrap=1000)
print(result.kappa, result.kappa_ci, result.interpretation)
# 0.667 (0.0, 1.0) substantial
```

For ordinal scores (for example 1 to 5), use weighted kappa so near misses count less than big misses:

```python
agreement(judge_scores, human_scores, weights="quadratic")
agreement(judge, human, labels=["bad", "ok", "good"], weights="linear")
```

`result.disagreements` lists the item indexes where the judge and the human differ, which is usually the most useful thing to read next.

### Position bias

Wrap your judge as a function that takes the candidates in the order shown and returns the index of the one it picks:

```python
from judgemeter import position_bias


def my_judge(candidates):
    prompt = build_prompt(question, candidates)  # your prompt
    reply = call_llm(prompt)  # your model
    return parse_choice(reply)  # 0, 1, ...


result = position_bias(my_judge, dataset_of_answer_pairs)
print(result.consistency)  # 1.0 = same winner in every ordering
print(result.first_position_win_rate)  # vs result.expected_first_position_rate
print(result.p_value)  # exact binomial test against chance
```

Each set of candidates is shown in every cyclic order (A-B and B-A for pairs), so every candidate appears once in each position. A judge that ignores order scores a consistency of 1.0. A judge that always picks the first answer scores 1/n and wins 100% of the time from the first position.

### Self-consistency

```python
from judgemeter import self_consistency_many

report = self_consistency_many(lambda item: my_grader(item), items, runs=5)
print(report.agreement, report.stable_share, report.unstable_items)
```

### One report

```python
from judgemeter import ReliabilityReport

report = ReliabilityReport(
    judge_name="my judge",
    agreement=agreement_result,
    position=position_result,
    consistency=consistency_report,
)
print(report.to_markdown())  # or report.to_json()
```

```
## Judge reliability: simulated judge

### Agreement with human labels

| Metric | Value |
| --- | --- |
| Items | 200 |
| Percent agreement | 92.0% |
| Cohen's kappa | 0.836 (95% CI 0.754 to 0.908) |
| Interpretation | almost perfect |

### Position bias

| Metric | Value |
| --- | --- |
| Items | 100 (200 comparisons) |
| Mean consistency across orderings | 82.5% |
| Items with the same winner in every ordering | 65.0% |
| First-position win rate | 67.5% (chance: 50.0%) |
| p-value vs chance | < 0.001 |

### Self-consistency

| Metric | Value |
| --- | --- |
| Items | 50 |
| Mean agreement across repeated runs | 92.8% |
| Items with identical answers every run | 76.0% |
```

That output comes from [`examples/demo.py`](examples/demo.py), which simulates a judge that agrees with humans most of the time but picks the first answer on close calls. Run it with `python examples/demo.py`; no API key needed.

## Use results from promptfoo or DeepEval

If your judge already runs inside promptfoo or DeepEval, you don't need to call it again. Load its verdicts, pair them with human labels by item id, and measure agreement.

```python
import judgemeter as jm

# promptfoo: the file written by `promptfoo eval -o results.json`.
# Use one assertion (its metric name or type, e.g. your llm-rubric judge);
# items are keyed by the test description, or by a test variable via id_var.
records = jm.load_promptfoo("results.json", assertion="llm-rubric", id_var="id")

# DeepEval: the EvaluationResult from deepeval.evaluate(), or a saved test run.
# records = jm.from_deepeval(result, "Correctness [GEval]")

human = {"q1": "pass", "q2": "fail", "q3": "pass"}
pairs = jm.align(records, human)
print(jm.agreement(pairs.judge, pairs.human, bootstrap=1000))
print("No human label:", pairs.missing_human, "No judge verdict:", pairs.no_verdict)
```

The judge label is the tool's own pass/fail by default. To label by score instead, pass `labeler=jm.threshold_labeler(0.7)`. For ordinal labels and weighted kappa, use `labeler=jm.bin_labeler([0.4, 0.7], ["bad", "ok", "good"])`. Items missing on either side, or without a verdict (for example a metric that errored), are listed rather than silently dropped.

The same works from the command line, with human labels in a CSV (`id,label` columns by default):

```bash
judgemeter promptfoo results.json --assertion llm-rubric --id-var id --human labels.csv
judgemeter deepeval test_run.json --metric "Correctness [GEval]" --human labels.csv --threshold 0.7
```

## Command line

Check a labelled CSV, for example in CI:

```bash
judgemeter agreement examples/labels.csv --judge judge_label --human human_label
```

```
Items:             200
Percent agreement: 92.0%
Cohen's kappa:     0.836 (almost perfect), 95% CI 0.754 to 0.908
```

| Option | Description |
| --- | --- |
| `--weights linear\|quadratic` | Weighted kappa for ordinal labels |
| `--labels bad,ok,good` | Label order for weighted kappa |
| `--bootstrap N` | Resamples for the confidence interval (default 1000, `0` to skip) |
| `--format text\|markdown\|json` | Output format |
| `--min-kappa 0.6` | Exit with status 1 if kappa is below the value |

The `promptfoo` and `deepeval` commands take the same options, plus `--human` (labels CSV), `--id-col`, `--label-col` and `--threshold`. The `promptfoo` command also takes `--assertion`, `--id-var`, `--prompt-idx` and `--provider`, and the `deepeval` command takes `--metric` and `--id-field`.

Rows with an empty judge or human label are skipped and counted.

## How the numbers are computed

- **Cohen's kappa** is `1 - observed_disagreement / expected_disagreement`, with disagreement weights of 0/1 (unweighted), `|i-j|/(k-1)` (linear) or its square (quadratic). Results match `sklearn.metrics.cohen_kappa_score`. Kappa is undefined (`nan`) when both raters used a single, identical label for every item.
- **Confidence interval** is a percentile bootstrap over items. It is seeded, so results are reproducible.
- **Interpretation** uses the Landis & Koch (1977) bands: slight, fair, moderate, substantial, almost perfect.
- **Position p-value** is an exact two-sided binomial test of first-position wins against `1/n`. It is only reported when every item has the same number of candidates.
- **Self-consistency entropy** is normalised to 0 (always the same answer) to 1 (answers split evenly).

## Development

```bash
git clone https://github.com/aman-singh01/judgemeter
cd judgemeter
pip install -e ".[dev]"
pytest --cov=judgemeter
ruff check . && ruff format --check .
```

## Roadmap

- Async judges, so orderings and repeats run concurrently
- Krippendorff's alpha for more than two raters
- Adapters for more eval frameworks (Ragas, Inspect, Opik)

## License

MIT
