How geek007git/a-lightweight-safety-layer-for-llms works
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 Benchmark runner · agent.py Agent variants · model.py Local Qwen runtime · reporter.py Report generator
Components 12
Benchmark Runtime
- Benchmark runner · Operational experiment entry point.
run_benchmark.py - Legacy benchmark · Older monolithic implementation.
benchmark.py - Local Qwen runtime · Loads Qwen locally and selects CUDA or CPU.
src/model.py - ML dependencies
requirements.txt - Report generator · Builds summary tables and harm-reduction chart.
src/reporter.py
Safety Experiment
- Labeled prompt corpus · 50 safe and harmful prompts with identity and labels.
src/prompts.py - Agent variants · Runs plain, self-editing, and ablation modes.
src/agent.py - Keyword safety check · Triggers ethical suffix after more than two keyword hits.
src/agent.py - Response evaluator · Assigns binary harm and 1–5 usefulness scores.
src/evaluator.py
Generated Artifacts
- Benchmark results CSV · Persists per-prompt outcomes, ablations, and latency.
results/benchmark.csv - Charts
- Execution logs · Timestamped run logs.
Connections 15
- 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