Hello! This repository contains the code and data used to benchmark redteaming prompts against various models as seen in our Huggingface Leaderboard. This project is aimed to reveal weaknesses in both open-sourced and blackbox language models through redteaming attacks covering a diverse range of behaviors and topics.
The following datasets are used in this benchmark, with more on the way:
For benchmarking performance on content, we've separated OpenAI's usage policies into 15 different categories as done in Yi Zeng et al.:
We've provided the classification results for all our prompts in data/prompt_content_classification.csv.
/classes: Base classes for models, evaluations, and judges./configs: Config objects for loading models./data: Houses data stored in .json files from each of our collected datasets./evaluations: Different classes for each dataset for readability./judges: Houses code for interfacing with Llamaguard./models: Model classes for OpenAI, Anthropic, Cohere, and Huggingface models.We've provided both a requirements.txt and environment.yml file for creating environments. Note that we use vllm for inference, meaning that installation will require being on a machine with a GPU.
If you want to benchmark OpenAI, Anthropic, or Cohere models, create an .env file and fill in the following keys:
OPENAI_API_KEY=...
COHERE_API_KEY=...
CLAUDE_API_KEY=...
We've provided an example script, run_eval_benchmarks.py, which allows the user to specify a list of models with a list of datasets to benchmark against. By default, results are written to a /generations folder.
While Llamaguard was specifically fine-tuned to classify between safe and unsafe prompts, we found in our experiments that the model outputs a significant number of false-negatives, which in this case means biasing towards "safe" even when the output is clearly "unsafe."
We found that recent versions of gpt-4 are highly accurate in their classification, and thus we recommend using those models for better results.
We use the system prompt specificed in the Llamaguard paper, defined in judges/system_prompts.py. We also provide a version of the system prompt based on OpenAI's content moderation policies. Choosing between OpenAI and LLamaguard's policies can be toggled in the policy parameter of the LlamaGuardJudge constructor, which takes in either openai or llamaguard as its value.
Given an instance of LlamaGuardJudge, inference can be performed through batch_call(), which takes in 3 optional boolean flags:
judge_agent: A value of True means the judge will evaluate the Agent's response to a user prompt, and a value of False means the judge will only evaluate the user prompt.use_policy_template: If True then the prompts will be formatted with the template string defined in the Llamaguard paper before sending to the model. If False the prompt will be formatted in the conventional "messages" format as a dictionary with a "user" and "assistant" message. This value should be set to True by default unless the judge model is a Llamaguard-family model.apply_template: This value is only relevant for open-sourced models. A value of True applies the chat template through tokenizer.apply_chat_template(). This value should be set to True by default unless the judge is using a Llamaguard-family model, in which this value should be used in conjunction with use_policy_template--For example, if apply_template is True, then we refrain from applying the Llamaguard system prompt and set use_policy_template to False. Likewise, if apply_template is False, then we want to format the system prompt and set use_policy_template to True. For blackbox models the chat template is applied automatically.Model loading is performed by specifying a config, created through ConfigFactory, or by passing in the model name:
from configs.utils import ConfigFactory, ModelFactory
model_config = ConfigFactory.load_config(model_name="lmsys/vicuna-7b-v1.5")
model = ModelFactory.from_config(model_config)
# OR
model = ModelFactory.from_name("lmsys/vicuna-7bv.1.5")
The above code loads a model object for lmsys/vicuna-7b-1.5. Additional parameters, such as temperature, top_p, and max_tokens can be set as kwargs in load_config() or from_name(). The full list of arguments is defined in configs/model_configs.py.
vllmWe use vllm to speed up inference times on open-sourced models. vllm, however, only supports having one model loaded at a time. Therefore, we've provided helper functions, load_model() and destroy() that loads and destroys the model respectively. destroy() has the added effect of killing any distributed thread groups created by torch.distributed.launch(), implemented as part of the vllm package.
We recommend using vllm for inference. That being said, vllm utilization can be specified with the use_vllm boolean flag in the model config. Model loading can also be configured by setting load_model=True in the config if the user wants to load the model on startup instead of at a later point through load_model(). We strongly suggest actively loading and destroying these models if running across multiple open sourced models to preserve GPU memory.
408 followers · starred Feb 2024
347 followers · starred Jan 2025
Python
100.0%
Hello! This repository contains the code and data used to benchmark redteaming prompts against various models as seen in our Huggingface Leaderboard. This project is aimed to reveal weaknesses in both open-sourced and blackbox language models through redteaming attacks covering a diverse range of behaviors and topics.
The following datasets are used in this benchmark, with more on the way:
For benchmarking performance on content, we've separated OpenAI's usage policies into 15 different categories as done in Yi Zeng et al.:
We've provided the classification results for all our prompts in data/prompt_content_classification.csv.
/classes: Base classes for models, evaluations, and judges./configs: Config objects for loading models./data: Houses data stored in .json files from each of our collected datasets./evaluations: Different classes for each dataset for readability./judges: Houses code for interfacing with Llamaguard./models: Model classes for OpenAI, Anthropic, Cohere, and Huggingface models.We've provided both a requirements.txt and environment.yml file for creating environments. Note that we use vllm for inference, meaning that installation will require being on a machine with a GPU.
If you want to benchmark OpenAI, Anthropic, or Cohere models, create an .env file and fill in the following keys:
OPENAI_API_KEY=...
COHERE_API_KEY=...
CLAUDE_API_KEY=...
We've provided an example script, run_eval_benchmarks.py, which allows the user to specify a list of models with a list of datasets to benchmark against. By default, results are written to a /generations folder.
While Llamaguard was specifically fine-tuned to classify between safe and unsafe prompts, we found in our experiments that the model outputs a significant number of false-negatives, which in this case means biasing towards "safe" even when the output is clearly "unsafe."
We found that recent versions of gpt-4 are highly accurate in their classification, and thus we recommend using those models for better results.
We use the system prompt specificed in the Llamaguard paper, defined in judges/system_prompts.py. We also provide a version of the system prompt based on OpenAI's content moderation policies. Choosing between OpenAI and LLamaguard's policies can be toggled in the policy parameter of the LlamaGuardJudge constructor, which takes in either openai or llamaguard as its value.
Given an instance of LlamaGuardJudge, inference can be performed through batch_call(), which takes in 3 optional boolean flags:
judge_agent: A value of True means the judge will evaluate the Agent's response to a user prompt, and a value of False means the judge will only evaluate the user prompt.use_policy_template: If True then the prompts will be formatted with the template string defined in the Llamaguard paper before sending to the model. If False the prompt will be formatted in the conventional "messages" format as a dictionary with a "user" and "assistant" message. This value should be set to True by default unless the judge model is a Llamaguard-family model.apply_template: This value is only relevant for open-sourced models. A value of True applies the chat template through tokenizer.apply_chat_template(). This value should be set to True by default unless the judge is using a Llamaguard-family model, in which this value should be used in conjunction with use_policy_template--For example, if apply_template is True, then we refrain from applying the Llamaguard system prompt and set use_policy_template to False. Likewise, if apply_template is False, then we want to format the system prompt and set use_policy_template to True. For blackbox models the chat template is applied automatically.Model loading is performed by specifying a config, created through ConfigFactory, or by passing in the model name:
from configs.utils import ConfigFactory, ModelFactory
model_config = ConfigFactory.load_config(model_name="lmsys/vicuna-7b-v1.5")
model = ModelFactory.from_config(model_config)
# OR
model = ModelFactory.from_name("lmsys/vicuna-7bv.1.5")
The above code loads a model object for lmsys/vicuna-7b-1.5. Additional parameters, such as temperature, top_p, and max_tokens can be set as kwargs in load_config() or from_name(). The full list of arguments is defined in configs/model_configs.py.
vllmWe use vllm to speed up inference times on open-sourced models. vllm, however, only supports having one model loaded at a time. Therefore, we've provided helper functions, load_model() and destroy() that loads and destroys the model respectively. destroy() has the added effect of killing any distributed thread groups created by torch.distributed.launch(), implemented as part of the vllm package.
We recommend using vllm for inference. That being said, vllm utilization can be specified with the use_vllm boolean flag in the model config. Model loading can also be configured by setting load_model=True in the config if the user wants to load the model on startup instead of at a later point through load_model(). We strongly suggest actively loading and destroying these models if running across multiple open sourced models to preserve GPU memory.
408 followers · starred Feb 2024
347 followers · starred Jan 2025
Python
100.0%