# thu-coai/aisafetylab architecture

> Architecture diagram of the GitHub repository thu-coai/aisafetylab, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thu-coai/aisafetylab
- Repository: https://github.com/thu-coai/aisafetylab

## Overview

AISafetyLab is a Python framework for AI safety research, organized around attack generation, inference- and training-time defense, and safety scoring/evaluation, with shared datasets and model backends. A typical documented workflow runs an attack, applies defenses to a query or attack results, then scores outputs or evaluates saved attack results. The README establishes these capabilities and workflows; source excerpts are partial, so internal orchestration links not directly evidenced are omitted or marked unverified.

Main entry points: [`aisafetylab/attack/attackers`](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/attackers) (Attack methods), [`aisafetylab/defense/inference_defense`](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense) (Inference defense), [`aisafetylab/models/base_model.py`](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/base_model.py) (Model interface), [`aisafetylab/models/local_model.py`](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/local_model.py) (Local model)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_attack["Attack generation"]
  node_attack_methods["Attack methods"]
  node_attack_feedback["Attack feedback"]
  node_attack_mutations["Prompt mutations"]
  node_attack_selection["Candidate selection"]
  node_attack_prompting["Prompt management"]
  node_initialization["Attack initialization"]
end

subgraph group_defense["Safety defenses"]
  node_inference_defense["Inference defense"]
  node_preprocess_defenders["Input preprocessing"]
  node_intraprocess_defenders["Generation defenses"]
  node_postprocess_defenders["Output defenses"]
  node_defense_templates["Safety templates<br/>[defender_texts.py]"]
end

subgraph group_evaluation["Scoring and evaluation"]
  node_scorers["Safety scorers"]
  node_pattern_scorer["Pattern scorer<br/>[pattern_scorer.py]"]
  node_prompted_scorer["Prompted LLM scorer"]
end

subgraph group_shared["Shared runtime"]
  node_dataset[("Attack dataset<br/>[attack_dataset.py]")]
  node_example["Dataset examples<br/>[example.py]"]
  node_model_api["Model interface<br/>[base_model.py]"]
  node_local_model["Local model<br/>[local_model.py]"]
  node_openai_model["OpenAI model<br/>[openai_model.py]"]
  node_vllm_model["vLLM model<br/>[vllm_model.py]"]
  node_sequences["Token sequences<br/>[sequence.py]"]
  node_utilities["Model and text utilities"]
  node_logger["Experiment logging"]
end

subgraph group_training["Defense training"]
  node_training_data[("Training data<br/>[dataset.py]")]
  node_training_workflows["Training workflows"]
  node_training_model["Training model loading<br/>[load.py]"]
end

node_researcher(("Researcher"))

node_researcher -->|"runs attacks"| node_attack_methods
node_attack_methods -.->|"uses examples"| node_dataset
node_attack_methods -.->|"requests feedback"| node_attack_feedback
node_attack_methods -.->|"mutates prompts"| node_attack_mutations
node_attack_methods -.->|"selects candidates"| node_attack_selection
node_attack_methods -.->|"manages prompts"| node_attack_prompting
node_attack_methods -.->|"initializes attacks"| node_initialization
node_attack_methods -.->|"queries models"| node_model_api
node_researcher -->|"runs defenses"| node_inference_defense
node_inference_defense -.->|"preprocesses input"| node_preprocess_defenders
node_inference_defense -.->|"defends generation"| node_intraprocess_defenders
node_inference_defense -.->|"filters output"| node_postprocess_defenders
node_inference_defense -.->|"uses templates"| node_defense_templates
node_inference_defense -.->|"uses model"| node_model_api
node_researcher -->|"trains defenses"| node_training_workflows
node_training_workflows -.->|"uses training data"| node_training_data
node_training_workflows -.->|"loads model"| node_training_model
node_researcher -->|"scores outputs"| node_scorers
node_researcher -->|"evaluates results"| node_pattern_scorer
node_researcher -->|"scores with prompts"| node_prompted_scorer
node_prompted_scorer -->|"calls model"| node_openai_model
node_intraprocess_defenders -->|"scores responses"| node_pattern_scorer
node_pattern_scorer -->|"implements scorer"| node_scorers
node_prompted_scorer -->|"extends scorer"| node_scorers
node_local_model -->|"implements interface"| node_model_api
node_vllm_model -->|"implements interface"| node_model_api
node_local_model -->|"uses sequences"| node_sequences
node_vllm_model -->|"uses sequences"| node_sequences
node_local_model -->|"uses utilities"| node_utilities
node_attack_methods -.->|"records experiments"| node_logger
node_dataset -->|"returns examples"| node_example

click node_attack_methods "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/attackers"
click node_attack_feedback "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/feedback"
click node_attack_mutations "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/mutation"
click node_attack_selection "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/selection"
click node_attack_prompting "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/prompt_manager"
click node_initialization "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/initialization"
click node_dataset "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/dataset/attack_dataset.py"
click node_example "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/dataset/example.py"
click node_model_api "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/base_model.py"
click node_local_model "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/local_model.py"
click node_openai_model "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/openai_model.py"
click node_vllm_model "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/vllm_model.py"
click node_sequences "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/sequence.py"
click node_utilities "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/utils"
click node_inference_defense "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense"
click node_preprocess_defenders "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/preprocess_defenders"
click node_intraprocess_defenders "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/intraprocess_defenders"
click node_postprocess_defenders "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/postprocess_defenders"
click node_defense_templates "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/inference_defense/defender_texts.py"
click node_training_data "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/training_defense/data/dataset.py"
click node_training_workflows "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/training_defense/train"
click node_training_model "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/training_defense/model/load.py"
click node_scorers "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/evaluation/scorers"
click node_pattern_scorer "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/evaluation/scorers/pattern_scorer.py"
click node_prompted_scorer "https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/evaluation/scorers/prompted_llm_scorer.py"
click node_logger "https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/logging"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_attack_methods,node_attack_feedback,node_attack_mutations,node_attack_selection,node_attack_prompting,node_initialization toneBlue
class node_inference_defense,node_preprocess_defenders,node_intraprocess_defenders,node_postprocess_defenders,node_defense_templates toneAmber
class node_scorers,node_pattern_scorer,node_prompted_scorer toneMint
class node_dataset,node_example,node_model_api,node_local_model,node_openai_model,node_vllm_model,node_sequences,node_utilities,node_logger toneRose
class node_training_data,node_training_workflows,node_training_model,node_researcher toneIndigo
```

## Components

### Attack generation

- [Attack methods](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/attackers): `aisafetylab/attack/attackers`
- [Attack feedback](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/feedback): `aisafetylab/attack/feedback`
- [Prompt mutations](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/mutation): `aisafetylab/attack/mutation`
- [Candidate selection](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/selection): `aisafetylab/attack/selection`
- [Prompt management](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/prompt_manager): `aisafetylab/attack/prompt_manager`
- [Attack initialization](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/attack/initialization): `aisafetylab/attack/initialization`

### Safety defenses

- [Inference defense](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense): `aisafetylab/defense/inference_defense`
- [Input preprocessing](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/preprocess_defenders): `aisafetylab/defense/inference_defense/preprocess_defenders`
- [Generation defenses](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/intraprocess_defenders): `aisafetylab/defense/inference_defense/intraprocess_defenders`
- [Output defenses](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/inference_defense/postprocess_defenders): `aisafetylab/defense/inference_defense/postprocess_defenders`
- [Safety templates](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/inference_defense/defender_texts.py): `aisafetylab/defense/inference_defense/defender_texts.py`

### Scoring and evaluation

- [Safety scorers](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/evaluation/scorers): `aisafetylab/evaluation/scorers`
- [Pattern scorer](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/evaluation/scorers/pattern_scorer.py): `aisafetylab/evaluation/scorers/pattern_scorer.py`
- [Prompted LLM scorer](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/evaluation/scorers/prompted_llm_scorer.py): `aisafetylab/evaluation/scorers/prompted_llm_scorer.py`

### Shared runtime

- [Attack dataset](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/dataset/attack_dataset.py): `aisafetylab/dataset/attack_dataset.py`
- [Dataset examples](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/dataset/example.py): `aisafetylab/dataset/example.py`
- [Model interface](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/base_model.py): `aisafetylab/models/base_model.py`
- [Local model](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/local_model.py): `aisafetylab/models/local_model.py`
- [OpenAI model](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/openai_model.py): `aisafetylab/models/openai_model.py`
- [vLLM model](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/vllm_model.py): `aisafetylab/models/vllm_model.py`
- [Token sequences](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/models/sequence.py): `aisafetylab/models/sequence.py`
- [Model and text utilities](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/utils): `aisafetylab/utils`
- [Experiment logging](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/logging): `aisafetylab/logging`

### Defense training

- [Training data](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/training_defense/data/dataset.py): `aisafetylab/defense/training_defense/data/dataset.py`
- [Training workflows](https://github.com/thu-coai/aisafetylab/tree/main/aisafetylab/defense/training_defense/train): `aisafetylab/defense/training_defense/train`
- [Training model loading](https://github.com/thu-coai/aisafetylab/blob/main/aisafetylab/defense/training_defense/model/load.py): `aisafetylab/defense/training_defense/model/load.py`

### Other components

- **Researcher**

## Connections

- Researcher → Attack methods: runs attacks
- Attack methods → Attack dataset: uses examples
- Attack methods → Attack feedback: requests feedback
- Attack methods → Prompt mutations: mutates prompts
- Attack methods → Candidate selection: selects candidates
- Attack methods → Prompt management: manages prompts
- Attack methods → Attack initialization: initializes attacks
- Attack methods → Model interface: queries models
- Researcher → Inference defense: runs defenses
- Inference defense → Input preprocessing: preprocesses input (evidence: `aisafetylab/defense/inference_defense/preprocess_defenders/ppl.py`)
- Inference defense → Generation defenses: defends generation (evidence: `aisafetylab/defense/inference_defense/intraprocess_defenders/smoothllm.py`)
- Inference defense → Output defenses: filters output (evidence: `aisafetylab/defense/inference_defense/postprocess_defenders/parden.py`)
- Inference defense → Safety templates: uses templates (evidence: `aisafetylab/defense/inference_defense/intraprocess_defenders/smoothllm.py`)
- Inference defense → Model interface: uses model (evidence: `aisafetylab/defense/inference_defense/postprocess_defenders/parden.py`)
- Researcher → Training workflows: trains defenses
- Training workflows → Training data: uses training data
- Training workflows → Training model loading: loads model
- Researcher → Safety scorers: scores outputs
- Researcher → Pattern scorer: evaluates results
- Researcher → Prompted LLM scorer: scores with prompts
- Prompted LLM scorer → OpenAI model: calls model (evidence: `aisafetylab/evaluation/scorers/prompted_llm_scorer.py`)
- Generation defenses → Pattern scorer: scores responses (evidence: `aisafetylab/defense/inference_defense/intraprocess_defenders/smoothllm.py`)
- Pattern scorer → Safety scorers: implements scorer (evidence: `aisafetylab/evaluation/scorers/pattern_scorer.py`)
- Prompted LLM scorer → Safety scorers: extends scorer (evidence: `aisafetylab/evaluation/scorers/prompted_llm_scorer.py`)
- Local model → Model interface: implements interface (evidence: `aisafetylab/models/local_model.py`)
- vLLM model → Model interface: implements interface (evidence: `aisafetylab/models/vllm_model.py`)
- Local model → Token sequences: uses sequences (evidence: `aisafetylab/models/local_model.py`)
- vLLM model → Token sequences: uses sequences (evidence: `aisafetylab/models/vllm_model.py`)
- Local model → Model and text utilities: uses utilities (evidence: `aisafetylab/models/local_model.py`)
- Attack methods → Experiment logging: records experiments (evidence: `aisafetylab/attack/attackers/renellm.py`)
- Attack dataset → Dataset examples: returns examples (evidence: `aisafetylab/dataset/attack_dataset.py`)
