Prompt evaluation
The smallest system you can score: one prompt that labels a movie review positive or negative. Change the wording, re-run the same rows, and read the pass/fail column.
The system One prompt
Only the review text is sent to the model. The label stays out of the prompt and is compared with whatever the model returns.
A smoke test lives at python -m ragas_examples.prompt_evals.prompt. The bundled input “The movie was fantastic…” should print positive before you bother scoring a dataset.
The dataset text + label
Four reviews with the label you expect the prompt to emit. Written to datasets/test_dataset.csv.
samples = [
{"text": "I loved the movie! It was fantastic.", "label": "positive"},
{"text": "The movie was terrible and boring.", "label": "negative"},
{"text": "It was an average film, nothing special.", "label": "positive"},
{"text": "Absolutely amazing! Best movie of the year.", "label": "positive"},
]positive. An exact-match metric will fail a prompt that reasonably says negative. When a score looks wrong, read the row before you rewrite the prompt.The metric Exact match
A code metric. No judge model. The strings either match or they do not.
@discrete_metric(name="accuracy", allowed_values=["pass", "fail"])
def my_metric(prediction: str, actual: str):
if prediction == actual:
return MetricResult(value="pass", reason="")
return MetricResult(value="fail", reason="")allowed_values constrains the score to pass and fail. MetricResult carries the value and an optional reason. Here the reason is empty because equality is the whole explanation.
The experiment Re-run on change
One function per row. The decorator runs it across the dataset and writes the CSV.
@experiment()
async def run_experiment(row):
response = run_prompt(row["text"])
score = my_metric.score(prediction=response, actual=row["label"])
return {**row, "response": response, "score": score.value}Comparing two models
Add a model argument to the experiment and pass it through run_experiment.arun(dataset, model="gpt-4o"). Same rows, different model, two CSVs you can diff.
Key takeaways
- End to end:
python -m ragas_examples.prompt_evals.evalsbuilds the dataset, runs the prompt, scores accuracy, and writesexperiments/experiment_name.csv. - The CSV columns are the original row, the prompt’s
response, andscore. - Exact match is the right metric only when the label set is closed and unambiguous.