TruthfulQA Benchmark

repository·main·Indexed 21 days ago

https://github.com/sylinrl/truthfulqa

A benchmark designed to measure whether language models mimic human falsehoods and misconceptions. It provides datasets and evaluation scripts for generative and multiple-choice tasks, supporting models such as GPT-3, GPT-Neo/J, GPT-2, and UnifiedQA. The toolkit includes the `evaluate.py` script for computing metrics like BLEURT, ROUGE, BLEU, and fine-tuned GPT-judge/GPT-info scores.

Tokens
2K
Snippets
8
Records
9
Agent score
26%

What's inside TruthfulQA

  1. How TruthfulQA tasks work: Generation vs Multiple-choice

    main

    TruthfulQA consists of two primary tasks:

    1. Generation (Main Task)

    Objective: Measure overall truthfulness (percentage of true answers) and informativeness (percentage of answers that are not evasive). Metrics:

    • GPT-judge / GPT-info: Fine-tuned GPT-3 metrics that predict human evaluations of truthfulness and informativeness.
    • Similarity Metrics: BLEURT (recommended), ROUGE, and BLEU. These are calculated as: [max similarity to a true reference answer] - [max similarity to a false reference answer].

    2. Multiple-choice

    Tests a model's ability to identify true statements.

    • MC1 (Single-true): Given 4-5 choices, select the only correct answer based on the highest log-probability of completion.
    • MC2 (Multi-true): The normalized total probability assigned to the set of all true reference answers.
    • New Binary Setting (Jan 2025): A recommended version where each question has exactly two options: a [Best Answer] and a [Best Incorrect Answer] from the dataset. Options (A) and (B) should be randomized.
  2. Install TruthfulQA locally

    main

    To run TruthfulQA on your local machine, clone the repository and install the dependencies. If you intend to run models on a GPU, ensure you have PyTorch installed with CUDA support, as the default installation from requirements.txt is CPU-only.

    To use GPT-J, you must manually download the HuggingFace-compatible model checkpoint provided by EleutherAI.

    git clone https://github.com/sylinrl/TruthfulQA
    cd TruthfulQA
    pip install -r requirements.txt
    pip install -e .
  3. Fine-tune GPT-3 for TruthfulQA evaluation

    main

    To achieve high accuracy in predicting human judgments of truthfulness and informativeness, you can fine-tune GPT-3 using the provided datasets: data/finetune_truth.jsonl (for GPT-judge) and data/finetune_info.jsonl (for GPT-info).

    It is recommended to use the following hyperparameters via the OpenAI CLI. Note that these fine-tuned models are specialized for TruthfulQA and are not expected to generalize to other tasks.

    openai api fine_tunes.create -t finetune_truth.jsonl -m curie --n_epochs 5 --batch_size 21 --learning_rate_multiplier 0.1 --no_packing
  4. Evaluate models using truthfulqa/evaluate.py

    main

    For supported models, you can generate answers and scores using the truthfulqa/evaluate.py script.

    To test a new model on the generation task, add its answers as an additional column to the input file. You can then pass the name of this column to the --models flag to compute the corresponding metrics.

    Supported Models:

    • GPT-3: ada, babbage, curie, davinci
    • GPT-Neo/J: neo-small, neo-med, neo-large, gptj
    • GPT-2: gpt2, gpt2-xl
    • UnifiedQA: uqa-small, uqa-base, uqa-large, uqa-3b

    Note: When using GPT-3 models or GPT-3 metrics (judge, info), you will be prompted for your OpenAI API key and the names of your fine-tuned models.

    python truthfulqa/evaluate.py --models your_model_column --metrics bleu rouge bleurt --input_path path/to/input.csv
  5. Run model evaluation with evaluate.py

    main

    Use the truthfulqa.evaluate module to generate model answers and compute metrics. You can test new models by adding their answers as an additional column in your input CSV file and including that column name in the --models flag.

    To persist results and cache models on Google Drive, mount your drive before running the command.

    # Example: Evaluate gpt2, neo-small, and uqa-small using MC, bleu, and bleurt metrics
    !python -m truthfulqa.evaluate \
        --models gpt2 neo-small uqa-small \
        --metrics mc bleu bleurt \
        --input_path TruthfulQA_demo.csv \
        --output_path TruthfulQA_answers.csv \
        --device 0
  6. Install TruthfulQA requirements

    main

    To set up the TruthfulQA environment, clone the repository, install the dependencies from requirements.txt, and perform a specific re-installation of protobuf to ensure compatibility. Finally, install the package in editable mode.

    Note for Colab users: Ensure your runtime is set to GPU. For models with 3B+ parameters, also enable High-RAM in the runtime shape settings.

    # Clone the repository
    !git clone https://github.com/sylinrl/TruthfulQA.git
    %cd TruthfulQA
    
    # Install dependencies
    !pip install -r requirements.txt
    
    # Fix protobuf installation
    !pip uninstall -y protobuf
    !pip install --no-binary protobuf protobuf
    
    # Install the package in editable mode
    !pip install -e .
  7. Reference: evaluate.py CLI flags

    main

    The truthfulqa/evaluate.py script accepts the following flags for model evaluation:

    | Flag        | Description |
    | ----------- | ----------------------------------------------------------------- |
    | `--models` | List of models to run (see below) |
    | `--metrics` | List of metrics to run. Valid: `mc`, `bleu`, `rouge`, `bleurt`, `judge`, `info` |
    | `--preset` | Prompt before each question. Valid: `qa`, `null`, `chat`, `long`, `help`, `harm` |
    | `--device`  | Device index if running on GPU (torch must be compiled with CUDA) |
    | `--input_path` | Location of question file |
    | `--output_path` | Location of results file |
    | `--cache_dir`  | Location of cached HuggingFace models |
    | `--gptj_path` | Location of GPT-J checkpoint |
  8. Load and view evaluation summaries

    main

    After running evaluate.py, a summary.csv file is generated containing high-level average metrics for each model. You can load this file using pandas to view the results.

    import pandas as pd
    summary = pd.read_csv('summary.csv')  # load the saved summary file from evaluate.py
    print(summary.to_string(index=False))
  9. Reference: Supported models in TruthfulQA

    main

    The evaluation script supports several model classes. Use the specific model identifiers listed below with the --models flag.

    | Model class       | Models |
    | ---------------- | ------------------ |
    | `GPT-3`          | ada, babbage, curie, davinci |
    | `GPT-Neo/J`      | neo-small, neo-med, neo-large, gptj |
    | `GPT-2`          | gpt2, gpt2-xl |
    | `UnifiedQA`      | uqa-small, uqa-base, uqa-large, uqa-3b |