Python: Evaluating LLMs in Under 150 Lines

A Python framework for evaluating LLMs is presented with a goal of fewer than 150 lines of code. It must incorporate exact matching, judgment by an LLM, and comparison of prompt versions. Its customer support example simulates a stricter instruction that degrades an open response.
A strict instruction degrades a simulated response
In response to a request to switch from a monthly subscription to an annual plan during the current period, the simulated version v2_strict replies only with "BILLING." The v1_baseline version, on the other hand, provided a billing menu and mentioned an automatic prorated calculation based on the remaining days. This is a manufactured degradation in the code, attributed to a strict labeling instruction applied to a request that called for an explanation.
The same version change alters routing requests differently. For the double charge of invoice 4021, the output changes from "Category: BILLING." to "BILLING," the expected label. A crash report during a PDF export retains "BUG_REPORT," in line with the test expectations.
The cancellation request also receives different formulations. The first version offers apologies and refers to the settings; the next acknowledges the service interruption, proposes to confirm an email address, or indicates the cancellation path in the subscription settings. These responses come from a simulation dictionary, not from a performance measurement of Gemini.
The checks distinguish labels and written responses
The reference set includes routing requests and support exchanges where both content and tone matter. Each entry has an identifier, a task category, a request, and an expected outcome. The approach favors a small informative set, without necessarily requiring hundreds or thousands of examples.
For deterministic categories, the planned method is an exact comparison after normalization. Routing, classification, and entity extraction are among the intended applications. For a response whose formulation may vary, the project instead envisions an evaluator LLM guided by a rubric, with a structured outcome using Pydantic.
The AI brief the pros read every evening
The 7 best AI stories, the essentials in 5 min. Free.
Included when you join: our pick of the best AI guides & comparisons.
Choose your rhythm
Free · No spam · Unsubscribe in 1 click
The test tc_03_angry_cancel thus requires an empathetic acknowledgment of the frustration caused by the service unavailability, followed by precise instructions for cancellation, without pressure or a defensive attitude. The test tc_04_plan_switch demands a direct response in complete sentences. It must specify where to modify the subscription or explain the billing adjustment, particularly the prorated amount.
Gemini remains optional during trials
Execution is planned in a Python notebook, such as Colab, with simulation enabled by default via USE_MOCK_LLM. No access identifier is required in this mode. The installation command cites google-genai and pydantic; imports also include json, os, re, datetime, timezone, and pandas, with display settings adjusted to have no limit on column width, as well as BaseModel and Field.
To make real calls, the simulation must be disabled, and a key obtained from Google AI Studio must be provided, possibly stored in the Colab secret GEMINI_API_KEY. The program checks Colab secrets and, if this read causes an exception, looks for an environment variable. Without a key, it halts execution, asking to add one or revert to simulation. The configuration targets gemini-2.5-flash and initializes its client only in real mode; compatibility with the free offering of the Gemini API is announced.
The function call_llm isolates the call from the rest of the system and receives the system instruction, user message, and version_tag, defaulting to v1. In simulation mode, an unlisted version or request returns "UNKNOWN." In real mode, generate_content receives the model, message, and system instruction; the returned text is stripped of its surrounding whitespace.
The parameter temperature=0.0 aims to contain variations between executions. The goal is to better distinguish changes associated with instructions from those arising from sampling, without presenting this setting as a guarantee of absolute determinism.
Tracking must compare each case individually
A limited modification of an instruction's wording can cause a previously successful example to fail. The proposed tracking must therefore retain the results associated with each version of the prompt and then compare the cases separately. It must notably identify a success that has turned into a failure, even when the aggregated success rate does not reveal the regression.
The project combines this versioned comparison and scoring methods into a reusable framework, without imposing a heavy MLOps infrastructure or an observability service.
Brief AI — AI news in French
The essentials of artificial intelligence news, decoded and explained every day.