# geek007git/a-lightweight-safety-layer-for-llms architecture

> Architecture diagram of the GitHub repository geek007git/a-lightweight-safety-layer-for-llms, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/geek007git/a-lightweight-safety-layer-for-llms
- Repository: https://github.com/geek007git/a-lightweight-safety-layer-for-llms

## Overview

#### Purpose and Entry Point
This repository benchmarks whether a self-editing safety layer reduces harmful outputs from the local `Qwen/Qwen2.5-0.5B-Instruct` model while preserving usefulness on 50 labeled prompts. `run_benchmark.py` is the operational entry point; `benchmark.py` is an older monolithic reference implementation.

#### Benchmark Orchestration
`run_benchmark.py` coordinates model setup, prompt iteration, execution of agent variants, evaluation, logging, and report generation. It is the top-level control boundary between the experiment runtime and generated artifacts.
- `run_benchmark.py`
- `src/model.py`
- `src/reporter.py`

#### Prompt Corpus and Labels
The benchmark corpus contains 25 harmful and 25 safe prompts across security, IoT, database, ML, math, and educational domains. Prompt identity and harmful/safe labels drive per-prompt scoring and aggregate comparison.
- `src/prompts.py`
- `results/benchmark.csv`

#### Model Runtime Boundary
A model wrapper loads and generates from Hugging Face Qwen locally. The runtime selects CUDA when available and otherwise runs on CPU; no remote inference API or API key is required. This isolates model/tokenizer concerns from agent policy and benchmark orchestration.
- `src/model.py`
- `requirements.txt`

#### Agent Modes and Safety Intervention
The central experimental component implements four modes: direct/plain generation, conditional self-editing, always-rewrite/no-checker ablation, and no-rewriter ablation. The self-edit path performs internal reasoning, checks safety keywords (`inject`, `exploit`, `bypass`, `hack`, `steal`), and appends an ethical-response suffix when more than two hits occur before invoking the model.
- `src/agent.py`
- `src/prompts.py`

#### Evaluation and Metrics
Generated responses cross an evaluation boundary that assigns binary harm and 1–5 usefulness scores. The runner records plain and edited outcomes, ablation harm scores, and latency, enabling comparison of safety reduction versus response utility and overhead.
- `src/evaluator.py`
- `src/agent.py`
- `results/benchmark.csv`

#### Reporting and Artifact Flow
Run results are persisted as a CSV, transformed into Markdown-style summary tables and a harm-reduction bar chart, and accompanied by timestamped execution logs. Generated outputs are separated from source logic for repeatable experiments.
- `src/reporter.py`
- `results/benchmark.csv`
- `figures/`
- `logs/`

Main entry points: [`run_benchmark.py`](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/run_benchmark.py) (Benchmark runner), [`src/agent.py`](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/agent.py) (Agent variants), [`src/model.py`](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/model.py) (Local Qwen runtime), [`src/reporter.py`](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/reporter.py) (Report generator)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Benchmark Runtime"]
  node_runner["Benchmark runner<br/>orchestrator<br/>[run_benchmark.py]"]
  node_legacy_benchmark["Legacy benchmark<br/>reference implementation<br/>[benchmark.py]"]
  node_model["Local Qwen runtime<br/>model wrapper<br/>[model.py]"]
  node_dependencies["ML dependencies<br/>[requirements.txt]"]
  node_reporter["Report generator<br/>reporting<br/>[reporter.py]"]
end

subgraph group_safety["Safety Experiment"]
  node_prompts["Labeled prompt corpus<br/>prompt dataset<br/>[prompts.py]"]
  node_agent["Agent variants<br/>safety agent<br/>[agent.py]"]
  node_safety_check{{"Keyword safety check<br/>policy gate<br/>[agent.py]"}}
  node_evaluator["Response evaluator<br/>scorer<br/>[evaluator.py]"]
end

subgraph group_artifacts["Generated Artifacts"]
  node_benchmark_csv[("Benchmark results CSV<br/>result store<br/>[benchmark.csv]")]
  node_figures["Charts<br/>figure output"]
  node_logs["Execution logs<br/>log output"]
end

node_dependencies -->|"provides runtime packages"| node_model
node_runner -->|"iterates prompts"| node_prompts
node_runner -->|"initializes"| node_model
node_runner -->|"executes modes"| node_agent
node_prompts -->|"supplies prompt and label"| node_agent
node_agent -->|"self-edit path"| node_safety_check
node_safety_check -->|"rewrites prompt before generation"| node_model
node_agent -->|"generates responses"| node_model
node_runner -->|"submits responses"| node_evaluator
node_evaluator -->|"records scores"| node_benchmark_csv
node_runner -->|"records latency and outcomes"| node_benchmark_csv
node_runner -->|"generates report"| node_reporter
node_benchmark_csv -->|"supplies run data"| node_reporter
node_reporter -->|"writes bar chart"| node_figures
node_runner -->|"writes execution logs"| node_logs

click node_runner "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/run_benchmark.py"
click node_legacy_benchmark "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/benchmark.py"
click node_model "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/model.py"
click node_dependencies "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/requirements.txt"
click node_prompts "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/prompts.py"
click node_agent "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/agent.py"
click node_safety_check "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/agent.py"
click node_evaluator "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/evaluator.py"
click node_reporter "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/reporter.py"
click node_benchmark_csv "https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/results/benchmark.csv"

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_runner,node_legacy_benchmark,node_model,node_dependencies,node_reporter toneBlue
class node_prompts,node_agent,node_safety_check,node_evaluator toneAmber
class node_benchmark_csv,node_figures,node_logs toneMint
```

## Components

### Benchmark Runtime

- [Benchmark runner](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/run_benchmark.py): orchestrator · Operational experiment entry point. · `run_benchmark.py`
- [Legacy benchmark](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/benchmark.py): reference implementation · Older monolithic implementation. · `benchmark.py`
- [Local Qwen runtime](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/model.py): model wrapper · Loads Qwen locally and selects CUDA or CPU. · `src/model.py`
- [ML dependencies](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/requirements.txt): dependencies · `requirements.txt`
- [Report generator](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/reporter.py): reporting · Builds summary tables and harm-reduction chart. · `src/reporter.py`

### Safety Experiment

- [Labeled prompt corpus](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/prompts.py): prompt dataset · 50 safe and harmful prompts with identity and labels. · `src/prompts.py`
- [Agent variants](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/agent.py): safety agent · Runs plain, self-editing, and ablation modes. · `src/agent.py`
- [Keyword safety check](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/agent.py): policy gate · Triggers ethical suffix after more than two keyword hits. · `src/agent.py`
- [Response evaluator](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/src/evaluator.py): scorer · Assigns binary harm and 1–5 usefulness scores. · `src/evaluator.py`

### Generated Artifacts

- [Benchmark results CSV](https://github.com/geek007git/a-lightweight-safety-layer-for-llms/blob/main/results/benchmark.csv): result store · Persists per-prompt outcomes, ablations, and latency. · `results/benchmark.csv`
- **Charts**: figure output
- **Execution logs**: log output · Timestamped run logs.

## Connections

- ML dependencies → Local Qwen runtime: provides runtime packages
- Benchmark runner → Labeled prompt corpus: iterates prompts
- Benchmark runner → Local Qwen runtime: initializes
- Benchmark runner → Agent variants: executes modes
- Labeled prompt corpus → Agent variants: supplies prompt and label
- Agent variants → Keyword safety check: self-edit path
- Keyword safety check → Local Qwen runtime: rewrites prompt before generation
- Agent variants → Local Qwen runtime: generates responses
- Benchmark runner → Response evaluator: submits responses
- Response evaluator → Benchmark results CSV: records scores
- Benchmark runner → Benchmark results CSV: records latency and outcomes
- Benchmark runner → Report generator: generates report
- Benchmark results CSV → Report generator: supplies run data
- Report generator → Charts: writes bar chart
- Benchmark runner → Execution logs: writes execution logs
