本仓库包含论文 引导审查:揭示 LLM “思维”控制的表征向量(作者:Hannah Cyberey 和 David Evans)的代码实现。
我们提出了一种从 LLM 内部找到“引导向量”的方法,用于检测和控制模型输出中的审查程度。欢迎查看这篇 博客文章 以快速了解我们的工作。
试试我们的演示:
注意: 两个演示都需要 Huggingface 账户。该演示托管在 Huggingface 的 ZeroGPU 上,所有用户均可免费使用,但每日使用配额有限。
下载仓库:
git clone https://github.com/hannahxchen/llm-censorship-steering.git
cd llm-censorship-steering
使用 Python 3.11+ 创建虚拟环境并激活它:
conda create -y -n censorship-steering python=3.11
conda activate censorship-steering
安装依赖:
pip install -r requirements.txt
查找引导向量
要为指令模型找到审查引导向量,请运行:
python -m llm_steering.run \
--run_train \
--model_name meta-llama/Llama-2-7b-chat-hf \
--censor_type refusal \
--n_train 1000 --n_val 500 \
--threshold 0.1 \
--filter_layer_pct 0.2
配置文件将保存到指定目录。或者,您也可以使用 python -m llm_steering.run --config_file CONFIG_FILE,通过传入符合 llm_steering/config.py 中定义格式的 YAML 配置文件来运行。
对于推理模型,我们使用以下配置:
python -m llm_steering.run \
--run_train \
--model_name deepseek-ai/DeepSeek-R1-Distill-Qwen-7B \
--censor_type thought_suppress \
--n_train -1 --n_val 1000 \
--threshold 0.1 \
--filter_layer_pct 0.05 \
--save_dir SAVE_DIR
应用引导向量
使用以下命令来应用引导向量:
python -m llm_steering.run
--run_steering \
--config_file SAVE_DIR/config.yaml \
--generation_batch_size 8 \
--coeff -1 \
--datasets jailbreakbench ccp_sensitive
您可以通过 --coeff 设置单个系数,或使用 --min_coeff、--max_coeff 和 --increment 设置系数范围。默认情况下,它应用从 -1 到 1、增量为 0.2 的值。所有模型输出将保存到 SAVE_DIR/evaluation/。
llm_steering/run.py 的所有参数:
(用于训练和验证)
model_name:使用 Huggingface 上模型仓库的名称。censor_type:指令模型使用 "refusal",推理模型使用 "thought_suppress"。method:计算候选向量的方法。可用选项:WMD(加权平均差)、MD(均值差)。默认方法是 WMD。n_train、n_valid:训练和验证样本数量。若为 -1,则使用全部样本。threshold:用于标记审查/未审查样本的阈值分数。filter_layer_pct:过滤最后 N 百分比层的比例。save_dir:保存结果的目录路径。(用于应用引导向量)
run_steering:应用找到的引导向量。compute_projection:计算标量投影。datasets:要应用引导的数据集。(参见下方可用的数据集)layer_ids:要干预的层 ID。默认仅使用向量验证期间识别出的顶层。coeff:运行单个系数值。min_coeff:最小系数。max_coeff:最大系数。increment:系数的增量。max_new_tokens:生成的最大 token 数量。num_return_sequences:每个输入生成的序列数量。top_p:采样所用的 top p 值。temperature:采样所用的温度。(通用参数)
config_file:YAML 配置文件的路径。use_cache:复用已存储的缓存结果。如果您需要恢复进程但不想重新运行整个流程,这会很有用。脚本将复用/跳过已保存的工件(例如,预处理的训练/验证数据、使用某个系数生成的输出)。batch_size:提取激活值时的批大小。generation_batch_size:运行生成时的批大小。seed:随机种子。可用的数据集:
jailbreakbench:来自 JailbreakBench 的 harmful 子集。sorrybench:来自 SorryBench 的完整提示集。alpaca_test_sampled:从 Alpaca-Cleaned 中采样的 300 条提示。xstest_safe、xstest_unsafe:来自 XSTest 的完整提示集。ccp_sensitive:CCP Sensitive 提示,覆盖 68 个不同的敏感主题。每个主题有 20 条提示。ccp_sensitive_sampled:较小的 CCP Sensitive 子集,每个主题包含 5 条提示。deccp_censored:来自 deccp 的 censored 子集。要使用 WildGuard 评估生成的输出,请运行:
python -m llm_steering.run_eval \
--config_file CONFIG_FILE_PATH \
--batch_size BATCH_SIZE \
--run_wildguard
该脚本将处理保存在 SAVE_DIR/evaluation/ 下的所有模型输出文件。添加 --use_cache 可跳过已经处理过的文件。
WildGuard 提供三种类型的检测,并生成如下格式的输出:
Harmful request: yes
Response refusal: yes
Harmful response: no
我们提取每种检测类型的“yes”或“no”token 的概率。结果将添加到与生成输出相同的文件中。
如果您觉得这项工作有用,请考虑引用我们的论文:
@inproceedings{cyberey2025steering,
title={Steering the CensorShip: Uncovering Representation Vectors for {LLM} ''Thought'' Control},
author={Hannah Cyberey and David Evans},
booktitle={Second Conference on Language Modeling},
year={2025}
}