Skip to main content

Use LLMs to get classification risk scores on tabular tasks.

Project description

Tests status PyPI status Documentation status PyPI version PyPI - License Python compatibility Huggingface dataset

A toolbox for evaluating statistical properties of LLMs

Folktexts provides a suite of Q&A datasets for evaluating uncertainty, calibration, accuracy and fairness of LLMs on individual outcome prediction tasks. It provides a flexible framework to derive prediction tasks from survey data, translates them into natural text prompts, extracts LLM-generated risk scores, and computes statistical properties of these risk scores by comparing them to the ground truth outcomes.

Note: using reasoning models / chain-of-thought (click to expand)

Support for reasoning-style benchmarking via the ReasoningQA interface lives on the reasoning-models branch (tree/reasoning-models). If you’re on main / using the PyPI release, switch to that branch and install from source:

Disclaimer: This reasoning-model support is experimental. If you run into any issues/bugs, please open a GitHub issue at socialfoundations/folktexts/issues.

git clone https://github.com/socialfoundations/folktexts.git
cd folktexts
git checkout reasoning-models

pip install -e .        # or: pip install -e ".[apis]" for web-hosted models
mkdir -p results models data

# Example: Qwen3-4B in thinking mode (ReasoningQA)
download_models --model "Qwen/Qwen3-4B" --save-dir models
run_acs_benchmark \
  --model models/Qwen--Qwen3-4B \
  --task ACSIncome \
  --results-dir results \
  --data-dir data \
  --subsampling 0.01 \
  --batch-size 1 \
  --reasoning-prompting \
  --enable-thinking

Use --reasoning-prompting to enable ReasoningQA prompts; add --enable-thinking for models that support it (e.g., Qwen3).

Use folktexts to benchmark your LLM:

  • Pre-defined Q&A benchmark tasks are provided based on data from the American Community Survey (ACS). Each tabular prediction task from the popular folktables package is made available as a natural-language Q&A task.
  • Parsed and ready-to-use versions of each folktexts dataset can be found on Huggingface.
  • The package can be used to customize your tasks. Select a feature to define your prediciton target. Specify subsets of input features to vary outcome uncertainty. Modify prompting templates to evaluate mappings from tabular data to natural text prompts. Compare different methods to extract uncertainty values from LLM responses. Extract raw risk scores and outcomes to perform custom statistical evaluations. Package documentation can be found here.

folktexts-diagram

Table of contents

Getting started

Installing

Install package from PyPI:

pip install folktexts

Basic setup

Go through the following steps to run the benchmark tasks. Alternatively, if you only want ready-to-use datasets, see this section.

  1. Create conda environment
conda create -n folktexts python=3.11
conda activate folktexts
  1. Install folktexts package
pip install folktexts
  1. Create models dataset and results folder
mkdir results
mkdir models
mkdir data
  1. Download transformers model and tokenizer
download_models --model 'google/gemma-2b' --save-dir models
  1. Run benchmark on a given task
run_acs_benchmark --results-dir results --data-dir data --task 'ACSIncome' --model models/google--gemma-2b

Run run_acs_benchmark --help to get a list of all available benchmark flags.

Ready-to-use datasets

Ready-to-use Q&A datasets generated from the 2018 American Community Survey are available via Logo datasets.

import datasets
acs_task_qa = datasets.load_dataset(
    path="acruz/folktexts",
    name="ACSIncome",   # Choose which task you want to load
    split="test")       # Choose split according to your intended use case

Example usage

Example code snippet that loads a pre-trained model, collects and parses Q&A data for the income-prediction task, and computes risk scores on the test split.

# Load transformers model
from folktexts.llm_utils import load_model_tokenizer
model, tokenizer = load_model_tokenizer("gpt2")   # using tiny model as an example

from folktexts.acs import ACSDataset
acs_task_name = "ACSIncome"     # Name of the benchmark ACS task to use

# Create an object that classifies data using an LLM
from folktexts import TransformersLLMClassifier
clf = TransformersLLMClassifier(
    model=model,
    tokenizer=tokenizer,
    task=acs_task_name,
)
# NOTE: You can also use a web-hosted model like GPT4 using the `WebAPILLMClassifier` class

# Use a dataset or feed in your own data
dataset = ACSDataset.make_from_task(acs_task_name)   # use `.subsample(0.01)` to get faster approximate results

# You can compute risk score predictions using an sklearn-style interface
X_test, y_test = dataset.get_test()
test_scores = clf.predict_proba(X_test)

If you only care about the overall benchmark results and not individual predictions, you can simply run the following code instead of using .predict_proba() directly:

from folktexts.benchmark import Benchmark, BenchmarkConfig
bench = Benchmark.make_benchmark(
    task=acs_task_name, dataset=dataset,  # These vars are defined in the snippet above
    model=model, tokenizer=tokenizer,
    numeric_risk_prompting=True,    # See the full list of configs below in the README
)
bench_results = bench.run(results_root_dir="results")

Example snippet showcasing how to fit the binarization threshold on a few training samples (note that this is not fine-tuning), and obtaining discretized predictions using .predict().

# Optionally, you can fit the threshold based on a few samples
clf.fit(*dataset[0:100])    # (`dataset[...]` will access training data)

# ...in order to get more accurate binary predictions with `.predict`
test_preds = clf.predict(X_test)

Benchmark features and options

Here's a summary list of the most important benchmark options/flags used in conjunction with the run_acs_benchmark command line script, or with the Benchmark class.

Option Description Examples
--model Name of the model on huggingface transformers, or local path to folder with pretrained model and tokenizer. Can also use web-hosted models with "[provider]/[model-name]". meta-llama/Meta-Llama-3-8B, openai/gpt-4o-mini
--task Name of the ACS task to run benchmark on. ACSIncome, ACSEmployment
--results-dir Path to directory under which benchmark results will be saved. results
--data-dir Root folder to find datasets in (or download ACS data to). ~/data
--numeric-risk-prompting Whether to use verbalized numeric risk prompting, i.e., directly query model for a probability estimate. By default will use standard multiple-choice Q&A, and extract risk scores from internal token probabilities. Boolean flag (True if present, False otherwise)
--use-chat-template Format prompts using the tokenizer's chat template (recommended for instruct/chat models). Pair with --system-prompt and/or --chat-prompt to override the defaults. By default uses zero-shot prompting without a chat template. Boolean flag (True if present, False otherwise)
--use-web-api-model Whether the given --model name corresponds to a web-hosted model or not. By default this is False (assumes a huggingface transformers model). If this flag is provided, --model must contain a litellm model identifier (examples here). Boolean flag (True if present, False otherwise)
--subsampling Which fraction of the dataset to use for the benchmark. By default will use the whole test set. 0.01
--fit-threshold Whether to use the given number of samples to fit the binarization threshold. By default will use a fixed $t=0.5$ threshold instead of fitting on data. 100
--batch-size The number of samples to process in each inference batch. Choose according to your available VRAM. 10, 32

Full list of options:

usage: run_acs_benchmark [-h] --model MODEL --results-dir RESULTS_DIR --data-dir DATA_DIR [--task TASK] [--few-shot FEW_SHOT] [--batch-size BATCH_SIZE] [--context-size CONTEXT_SIZE] [--fit-threshold FIT_THRESHOLD] [--subsampling SUBSAMPLING] [--seed SEED] [--use-web-api-model] [--dont-correct-order-bias] [--numeric-risk-prompting] [--reuse-few-shot-examples] [--balance-few-shot-examples] [--use-chat-template] [--chat-prompt CHAT_PROMPT] [--system-prompt SYSTEM_PROMPT]
                         [--use-feature-subset USE_FEATURE_SUBSET] [--use-population-filter USE_POPULATION_FILTER] [--max-api-rpm MAX_API_RPM] [--logger-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]

Benchmark risk scores produced by a language model on ACS data.

options:
  -h, --help            show this help message and exit
  --model MODEL         [str] Model name or path to model saved on disk
  --results-dir RESULTS_DIR
                        [str] Directory under which this experiment's results will be saved
  --data-dir DATA_DIR   [str] Root folder to find datasets on
  --task TASK           [str] Name of the ACS task to run the experiment on
  --few-shot FEW_SHOT   [int] Use few-shot prompting with the given number of shots
  --batch-size BATCH_SIZE
                        [int] The batch size to use for inference
  --context-size CONTEXT_SIZE
                        [int] The maximum context size when prompting the LLM
  --fit-threshold FIT_THRESHOLD
                        [int] Whether to fit the prediction threshold, and on how many samples
  --subsampling SUBSAMPLING
                        [float] Which fraction of the dataset to use (if omitted will use all data)
  --seed SEED           [int] Random seed -- to set for reproducibility
  --use-web-api-model   [bool] Whether use a model hosted on a web API (instead of a local model)
  --dont-correct-order-bias
                        [bool] Whether to avoid correcting ordering bias, by default will correct it
  --numeric-risk-prompting
                        [bool] Whether to prompt for numeric risk-estimates instead of multiple-choice Q&A
  --reuse-few-shot-examples
                        [bool] Whether to reuse the same samples for few-shot prompting (or sample new ones every time)
  --balance-few-shot-examples
                        [bool] Whether to sample evenly from all classes in few-shot prompting
  --use-chat-template   [bool] Whether to format prompts using the tokenizer's chat template (for instruct/chat models)
  --chat-prompt CHAT_PROMPT
                        [str] Custom assistant prefill text to use with chat templates
  --system-prompt SYSTEM_PROMPT
                        [str] Custom system prompt text to use with chat templates
  --use-feature-subset USE_FEATURE_SUBSET
                        [str] Optional subset of features to use for prediction, comma separated
  --use-population-filter USE_POPULATION_FILTER
                        [str] Optional population filter for this benchmark; must follow the format 'column_name=value' to filter the dataset by a specific value.
  --max-api-rpm MAX_API_RPM
                        [int] Maximum number of API requests per minute (if using a web-hosted model)
  --logger-level {DEBUG,INFO,WARNING,ERROR,CRITICAL}
                        [str] The logging level to use for the experiment

Evaluating feature importance

By evaluating LLMs on tabular classification tasks, we can use standard feature importance methods to assess which features the model uses to compute risk scores.

You can do so yourself by calling folktexts.cli.eval_feature_importance (add --help for a full list of options).

Here's an example for the Llama3-70B-Instruct model on the ACSIncome task (warning: takes 24h on an Nvidia H100):

python -m folktexts.cli.eval_feature_importance --model 'meta-llama/Meta-Llama-3-70B-Instruct' --task ACSIncome --subsampling 0.1
feature importance on llama3 70b it

This script uses sklearn's permutation_importance to assess which features contribute the most for the ROC AUC metric (other metrics can be assessed using the --scorer [scorer] parameter).

FAQ

  1. Q: Can I use folktexts with a different dataset?

    A: Yes! Folktexts provides the whole ML pipeline needed to produce risk scores using LLMs, together with a few example ACS datasets. You can easily apply these same utilities to a different dataset following the example jupyter notebook.

  2. Q: How do I create a custom prediction task based on American Community Survey data?

    A: Simply create a new TaskMetadata object with the parameters you want. Follow the example jupyter notebook for more details.

  3. Q: Can I use folktexts with closed-source models?

    A: Yes! We provide compatibility with local LLMs via 🤗 transformers and compatibility with web-hosted LLMs via litellm. For example, you can use --model='gpt-4o' --use-web-api-model to use GPT-4o when calling the run_acs_benchmark script. Here's a complete list of compatible OpenAI models. Note that some models are not compatible as they don't enable access to log-probabilities. Using models through a web API requires installing extra optional dependencies with pip install 'folktexts[apis]'.

  4. Q: Can I use folktexts to fine-tune LLMs on survey prediction tasks?

    A: The package does not feature specific fine-tuning functionality, but you can use the data and Q&A prompts generated by folktexts to fine-tune an LLM for a specific prediction task.

Citation

@inproceedings{cruz2024evaluating,
    title={Evaluating language models as risk scores},
    author={Andr\'{e} F. Cruz and Moritz Hardt and Celestine Mendler-D\"{u}nner},
    booktitle={The Thirty-eight Conference on Neural Information Processing Systems Datasets and Benchmarks Track},
    year={2024},
    url={https://openreview.net/forum?id=qrZxL3Bto9}
}

License and terms of use

Code licensed under the MIT license.

The American Community Survey (ACS) Public Use Microdata Sample (PUMS) is governed by the U.S. Census Bureau terms of service.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

folktexts-0.2.0.tar.gz (95.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

folktexts-0.2.0-py3-none-any.whl (95.3 kB view details)

Uploaded Python 3

File details

Details for the file folktexts-0.2.0.tar.gz.

File metadata

  • Download URL: folktexts-0.2.0.tar.gz
  • Upload date:
  • Size: 95.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for folktexts-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a727fcd7fec16c932a65be89bc393b88b731245b1abdb48feaa2bf17fcb09175
MD5 654d2a4465cb8a3b1aef17f34de0dec0
BLAKE2b-256 f91ee7b7a2bca54cf3af186f4edf6128cc4fe1fdd317ba7863abf9f5e8a9df5b

See more details on using hashes here.

File details

Details for the file folktexts-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: folktexts-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 95.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for folktexts-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80b5e052df7465816139634b2420b45f10f2d4bcd3b7e0411b604c0cee7486f3
MD5 f19631f940e550b417e8396fc2d6dab8
BLAKE2b-256 47aeae18455179c1a60b9093ba185bc8e53d27163e54e51670e27d7ee0d2862d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page