Core concept

Experimentation

An experiment is a deliberate change made to test a hypothesis. Swap the embedding model in a RAG system, keep the questions the same, and read whether the answers got better. Ragas turns that loop into an @experiment function and a CSV.

The loop One change at a time

Four principles: a measurable metric, stored results, an isolated change, and another hypothesis.

Make a changeRun evaluationsObserve resultsHypothesizenext change
Changing the prompt and the retriever in the same run hides which one moved the score.

What an experiment is made of Dataset, system, metrics

Setup loads the dataset. Run calls the system on every row. Evaluate applies metrics. Store writes the CSV.

Test dataset

The fixed inputs, and any expected outputs or grading notes, that every variant sees.

Application endpoint

The application, component, or model under test. One row in, one response out.

Metrics

The numbers that say whether this change beat the last one. Accuracy, precision, a custom judge.

The @experiment decorator One function per row

The decorated function receives a dataset row and returns that row plus the response and any metadata you want to keep.

@experiment()
async def my_experiment(row):
    response = await asyncio.to_thread(my_system_function, row["input"])
    return {
        **row,
        "response": response,
        "experiment_name": "baseline_v1",
        "model_version": "gpt-4o",
    }
dataset = Dataset.load(name="test_data", backend="local/csv", root_dir="./data")
results = await my_experiment.arun(dataset)

Extra arguments after the row become parameters of arun. The same function can score gpt-4o at temperature 0.1 and then gpt-3.5-turboat the same temperature, writing a separate result set each time.

@experiment()
async def model_comparison_experiment(row, model_name: str, temperature: float):
    response = await my_system_function(
        row["input"], model=model_name, temperature=temperature
    )
    return {
        **row,
        "response": response,
        "experiment_name": f"{model_name}_temp_{temperature}",
        "model_name": model_name,
        "temperature": temperature,
    }

results_gpt4 = await model_comparison_experiment.arun(
    dataset, model_name="gpt-4o", temperature=0.1
)

Keeping runs comparable Name, file, metadata

Ragas writes timestamped CSVs under experiments/. The name and the columns are what make two files comparable later.

PracticeWhat to record
NameWhat changed, a version, and a date: gpt4o_v2_prompt_temperature_0.1_20241201
Fileexperiments/20241201-143022-baseline_v1.csv
MetadataGit commit, environment, model version, token count, response time

Patterns that still fit one function

  • A/B. Pass variant and branch inside the experiment. Run arun twice.
  • Multi-stage. Return the retrieved docs and the final response, plus num_docs_retrieved, so a bad answer can be traced to retrieval.
  • Errors. Catch failures, store response, error, and success, and keep the rest of the rows.

Key takeaways

  • Score the response inside the experiment and store both value and reason next to the row.
  • A metric such as FactualCorrectness takes response and reference and returns a score you can write into the same CSV.
  • The CSV is the record of the hypothesis: input, output, score, and enough metadata to rerun it.