SafetyBench
repository·main·Indexed 43 days ago
https://github.com/thu-coai/safetybenchA large-scale benchmark for evaluating the safety of Large Language Models (LLMs) using 11,435 multiple-choice questions across 7 safety categories in English and Chinese. It supports zero-shot and few-shot evaluation settings and provides datasets via Hugging Face.
What's inside SafetyBench
- SafetyBench is a comprehensive benchmark designed to evaluate the safety of Large Language Models (LLMs). It consists of 11,435 multiple-choice questions across 7 distinct safety categories. The benchmark supports both Chinese and English, providing datasets for both languages as well as a specific Chinese subset. It is designed for zero-shot and few-shot evaluation settings.
Understand SafetyBench data structure
mainThe dataset contains several components:
test_zh: Test questions in Chinese.test_en: Test questions in English.test_zh_subset: A downsampled Chinese subset (300 questions per category) with highly sensitive keywords removed.dev_zhanddev_en: Contain 5 examples for each safety category, intended for use as few-shot demonstrations.
Data Format Details:
- The
optionsfield contains up to four items, corresponding to options A, B, C, and D. - In the
answerfield (found in dev data), answers are mapped as integers:0-> A,1-> B,2-> C,3-> D.
Download SafetyBench data
mainYou can download the test questions and few-shot examples using two primary methods:
- Using a shell script: Run the provided
download_data.shscript to download data viawget. - Using Python: Use the
datasetslibrary by running the provideddownload_data.pyscript.
Data is hosted on Hugging Face.
./code/download_data.sh# Alternatively, use the provided python script ./code/download_data.py- Using a shell script: Run the provided
Evaluate models on SafetyBench
mainEvaluations are typically conducted in zero-shot or five-shot settings. The process involves extracting predicted answers from the model's responses.
While the benchmark is not reasoning-intensive (like MMLU), you can use Chain-of-Thought (CoT) prompting if desired. For specific models, minor prompt adjustments may be required to improve the accuracy of answer extraction.
Refer to
./code/evaluate_baichuan.pyfor a concrete implementation example.Prepare and submit results to SafetyBench
mainTo submit your model's performance to the SafetyBench website, you must prepare a UTF-8 encoded JSON file.
JSON Format Requirements:
- The key must be the
idfield from the original test questions. - The value must be the predicted answer as an integer:
0(A),1(B),2(C), or3(D).
Example structure:
{ "0": 0, "1": 1, "2": 3, "3": 2 }- The key must be the