Basic Inspect AI Example
Inspect AI is an open source framework from the UK AI Security Institute for
evaluating large language models. The Workflow below defines a small inline evaluation task, runs it against
an OpenAI model, applies a quality gate that fails the Workflow if accuracy drops below a threshold, and exports
the scores as metrics.
You can paste this directly into the YAML of an existing or new Workflow, just make sure to update the
name and namespace for your environment if needed.
- The
spec.configproperty defines two configuration parameters:model, the OpenAI model to evaluate, andminAccuracy, the accuracy below which the quality gate fails. Override them when you run the Workflow to evaluate another model or tighten the gate. - The
spec.content.filesproperty defines three inline files:requirements.txt- the Python packages the evaluation needs.task.py- an Inspect AI task with three samples that are scored with theincludes()scorer.gate.py- a quality gate script that reads the Inspect log and fails the run if accuracy is belowminAccuracy.
- The
spec.containerproperty defines the working directory, Python image, and environment variables for the Workflow; theOPENAI_API_KEYis resolved using the credential expression so the key is never stored in the Workflow definition itself. - The Run evaluation and quality gate step installs Inspect AI, runs the evaluation, and applies the quality gate -
turning a model evaluation into a pass/fail test result. Its
cacheblock keeps the pip download cache between executions, so later runs install the packages from the cache instead of downloading them again; see Dependency Caching. - The Export metrics step runs the
testkube-ai-metrics-parserimage, which converts the Inspect log into aneval.lpfile with the score of each scorer metric, tagged with the model. - The Save logs and metrics step uploads the Inspect JSON logs and the metrics file as artifacts; the
alwayscondition makes sure they are uploaded even if the evaluation or quality gate fails.
kind: TestWorkflow
apiVersion: testworkflows.testkube.io/v1
metadata:
name: inspect-ai-getting-started
namespace: testkube
labels:
docs: example
type: ai
spec:
config:
model:
type: string
default: gpt-5.4-mini
minAccuracy:
type: string
default: "0.5"
content:
files:
- path: /data/requirements.txt
content: |
inspect-ai
openai
- path: /data/task.py
content: |
from inspect_ai import Task, task
from inspect_ai.dataset import Sample
from inspect_ai.scorer import includes
from inspect_ai.solver import generate
@task
def capitals():
return Task(
dataset=[
Sample(input="What is the capital of France? Reply with only the city name.", target="Paris"),
Sample(input="What is the capital of Japan? Reply with only the city name.", target="Tokyo"),
Sample(input="What is the capital of Sweden? Reply with only the city name.", target="Stockholm"),
],
solver=generate(),
scorer=includes(),
)
- path: /data/gate.py
content: |
import glob, json, os, sys
THRESHOLD = float(os.environ["MIN_ACCURACY"])
files = sorted(glob.glob("logs/*.json"))
if not files:
print("No Inspect log found"); sys.exit(1)
data = json.load(open(files[-1]))
def find_accuracy(o):
out = []
if isinstance(o, dict):
for k, v in o.items():
if k == "accuracy" and isinstance(v, dict) and "value" in v:
out.append(v["value"])
else:
out += find_accuracy(v)
elif isinstance(o, list):
for x in o:
out += find_accuracy(x)
return out
accs = find_accuracy(data)
acc = accs[0] if accs else None
print(f"accuracy={acc} threshold={THRESHOLD}")
if acc is None or acc < THRESHOLD:
print("Quality gate FAILED"); sys.exit(1)
print("Quality gate PASSED")
container:
workingDir: /data
image: python:3.12-slim
env:
- name: HOME
value: /data
- name: PIP_CACHE_DIR
value: /root/.cache/pip
- name: MIN_ACCURACY
value: "{{config.minAccuracy}}"
- name: OPENAI_API_KEY
value: '{{credential("OPENAI_API_KEY")}}'
resources:
requests:
cpu: 500m
memory: 1Gi
steps:
- name: Run evaluation and quality gate
cache:
key: 'pip-{{ hash_files("requirements.txt") }}'
restoreKeys:
- "pip-"
paths:
- /root/.cache/pip
shell: |
set -e
pip install -r requirements.txt
inspect eval task.py --model openai/{{config.model}} --log-dir ./logs --log-format json
python gate.py
- name: Export metrics
condition: always
container:
image: docker.io/kubeshop/testkube-ai-metrics-parser:latest
shell: testkube-ai-metrics-parser inspect-ai -i 'logs/*.json' -o metrics/eval.lp --tag model={{config.model}} --stdout
- name: Save logs and metrics
condition: always
artifacts:
paths:
- logs/*.json
- metrics/*.lp
After execution, the evaluation output and quality-gate result are shown in the Log Output tab, and the
Inspect JSON logs are available under the Artifacts tab of the execution. Testkube ingests the eval.lp file as
custom metrics under the inspect_eval measurement, so you can follow the
accuracy of each model across executions in Insights.