Backend · LLM evaluation pipeline

Grading AI answers with an AI judge

A Python pipeline that scores pastoral answers for biblical fidelity on a 0–3 scale, in English and French, then rewrites strong answers into subtly compromised ones to build paired training data.

PythonOpenAI APIgpt-4o-minigpt-4.1pandasRegex parsingJSON checkpoints
5doctrinal categories
2languages, EN + FR
2LLM passes per row
0–3fidelity scale
01 · Case study

The problem and the approach

Why this pipeline exists, and how its design follows from that.

The problem

A model meant to answer pastoral questions has to be faithful to Scripture across sensitive topics: marriage and gender, the sanctity of life, salvation, justice and biblical authority. Building and testing that model needs large amounts of labelled data: answers graded for fidelity, plus clear examples of what a subtly wrong answer looks like.

Labelling thousands of answers by hand, in two languages, is slow and inconsistent. Subtly compromised answers are especially hard to write on purpose.

The solution

A two-stage, LLM-as-judge pipeline. A low-temperature judge grades every answer against a fixed 0–3 rubric and justifies each score. A second, stronger model then rewrites faithful answers into plausible but compromised ones. The judge scores each rewrite again, so every pair in the dataset is verified, not assumed.

01

Judge

A fixed rubric in a pastor-theologian prompt, run at temperature 0.3 so the same answer gets the same score. Every score comes with a 1–2 sentence justification citing Scripture.

02

Rewrite

Only answers rated 2 or 3 are rewritten, by gpt-4.1 at temperature 0.7, using at least two of five named distortion techniques. The result should sound pastoral but fail a strict grader.

03

Verify

The rewrite goes back through the same judge. Original and rewrite are stored side by side with both ratings and justifications, giving a contrastive dataset.

02 · Showcase

Screens

What the backend produces at each stage, from the live run to the final training data. Click any screen to view it full size.

03 · How it works

Inside the pipeline

Follow one row of data through both scripts. It plays automatically. Click any step to inspect it.

Stage 1

Evaluate

evaluate_reponse.py
rated CSV → stage 2
Stage 2

Regenerate

re-analysis_response.py


        
0Strongly rejectedContradicts Scripture
1Mildly rejectedSoftens or distorts
2NeutralAmbiguous stance
3AcceptableClear and faithful
04 · Challenges

What was hard, and how I solved it

Long batch jobs against a paid, non-deterministic API turn small problems into expensive ones.

Challenge

LLM output is free-form text

The judge returns markdown, sometimes bold, sometimes not, and in French it writes “Note” instead of “Rating”.

Solution

Tolerant regex parsing

Patterns accept optional **, several separators and both labels, then pull out the score and the full justification.

re.search(r"\*{0,2}(?:Note|Rating)…")
Challenge

Runs crash halfway

Hundreds of rows at about 3 seconds each. A network error or rate limit would mean paying to redo everything.

Solution

Per-row checkpoints

Each result is written to CSV and to a JSON checkpoint right after its API call. Restarting resumes at last_index + 1.

load_checkpoint() · save_checkpoint()
Challenge

Rate limits and transient errors

The API rejects bursts and occasionally fails for no clear reason.

Solution

Retries, pacing and cost logging

Up to 3 retries with a delay, 2.5s between rows, and tokens and estimated cost printed for every call. Runs are capped at a chosen row count.

max_retries=3 · time.sleep(2.5)
Challenge

The rewriter echoes the prompt

Rewrites came back wrapped in scaffolding like “Techniques employed: 1, 3” and “Your Output:”, which would pollute the dataset.

Solution

Language-aware cleaning

A cleaning pass strips the EN and FR markers and horizontal rules, and collapses extra blank lines, before the answer is judged again.

clean_regenerated_answer(text, lang)
Challenge

Is a rewrite really worse?

A model asked to “compromise” an answer can overshoot into absurdity, or barely change it.

Solution

Closed-loop verification

The same judge scores every rewrite. Each pair stores both ratings, so weak rewrites are easy to spot and filter out.

Original Rating → Regenerated Rating
05 · Outcome

Results and what I'd do next

What it delivers

  • A repeatable way to grade answers for doctrinal fidelity, in English and French, with a written reason for every score.
  • Paired “faithful vs compromised” examples for training and evaluating models, each pair verified by the judge.
  • Unattended batch runs that survive crashes and rate limits without repeating paid calls.
  • Plain CSV and JSON outputs that reviewers can open in Excel and that slot straight into training pipelines.

Next iteration

  • Structured outputs. Ask the model for JSON with a schema instead of parsing text with regex.
  • Exact token counts. Use usage.prompt_tokens rather than a word-count estimate for cost.
  • Concurrency. Async requests with a rate limiter instead of sequential calls with a fixed sleep.
  • Judge calibration. Compare a human-labelled sample with the judge to measure agreement.
  • Modern SDK. Move to the current OpenAI Python client and add CLI flags for non-interactive runs.