SafesLM — safe LLM routing with LoRA adapters
Project description
SafeSLM
SafeSLM is a Python library designed to make small language models (SLMs) safer for deployment by fine-tuning them on safety datasets using LoRA adapters. It provides an easy-to-use class-based API and CLI for training, loading, and inference while keeping safety-related logic separate from reasoning or other tasks.
Motivation
Small language models, because they contain far fewer parameters, tend to memorize less context. This makes them more vulnerable to simple prompt-based jailbreaks. Large language models, on the other hand, have more weights and capacity, which allows them to:
- Detect malicious prompt manipulations
- Resist unsafe instructions
- Maintain high accuracy on legitimate tasks
However, SLMs perform best when focused on a single task. Forcing an SLM to perform multiple tasks—such as safety enforcement and reasoning—can degrade its performance.
SafeSLM’s approach:
- Keep tasks separate by training small models specifically for safety detection.
- For a given task, the model considers all related prompts safe and others unsafe.
- LoRA adapters allow fine-tuning safety behavior without modifying the base model, preserving the original weights.
Features
- Fine-tune small language models on safety datasets using LoRA adapters
- Simple class-based Python API for training and inference
- CLI for reproducible training and inference pipelines
- Supports multiple adapters for the same base model
- Keeps tasks separate to avoid degrading reasoning or other capabilities
- Flexible precision: full, 8-bit, or 4-bit
- Supports custom HuggingFace datasets
- Task-specific inference with a
taskargument
Installation
# First install semantic-router for routing functionality
!pip install -qU "semantic-router[fastembed]==0.1.0"
# Install SafeSLM
pip install safeslm
Note: If you are on Google Colab, restart your session after installation.
Usage
Python API
from safeslm import SafeSLM
# Train a new LoRA adapter on safety data
model = SafeSLM("gpt2", precision="full", device="cuda")
model.train(
output_dir="./adapter_out",
dataset_name="your_dataset_name_here", # optional, defaults to Anthropic/hh-rlhf
num_train_epochs=5,
per_device_train_batch_size=4
)
# Load the adapter for inference
safe_model = SafeSLM(
"gpt2",
precision="full",
device="cuda",
adapter_path="./adapter_out/lora_adapter"
)
# Run prompts with a task (currently supports:"default","coding","content_creation","education",)
output = safe_model("Is it safe to share my password online?", task="default")
print(output)
CLI
# Train LoRA adapter
safeslm train --model gpt2 --out_dir ./adapter_out --dataset your_dataset_name_here --epochs 5 --batch_size 4 --precision full --device cuda
# Run inference
safeslm infer --model gpt2 --adapter ./adapter_out/lora_adapter --prompt "What are safe browsing practices?" --task default --precision full --device cuda
Multi-Adapter Support
You can train multiple LoRA adapters on different safety datasets or domains and switch between them without reloading the base model:
# Load adapter A
adapter_a = SafeSLM("gpt2", adapter_path="./adapters/safety_v1/lora_adapter")
# Load adapter B
adapter_b = SafeSLM("gpt2", adapter_path="./adapters/safety_v2/lora_adapter")
# Run inference on both
print(adapter_a("Some potentially unsafe prompt", task="safety"))
print(adapter_b("Some potentially unsafe prompt", task="safety"))
Pipeline Overview
The SafeSLM pipeline consists of:
- Base Model Loading – Load the HuggingFace model in desired precision.
- Optional LoRA Adapter – Fine-tuned weights for safety, loaded separately.
- Router & Encoder – Encodes prompts for safety classification or routing.
- Pipeline Execution – Handles prompt routing, LoRA injection, and generation.
Why SafeSLM?
- Task separation ensures small models remain accurate for their intended purpose.
- LoRA adapters allow multiple safety profiles on the same base model.
- Easy integration via class-based API or CLI.
- Supports multiple precisions for faster inference on limited hardware.
- Supports custom datasets for training on your own data.
- Task-aware inference using the
taskargument.
Contributing
Contributions are welcome! Please submit issues or pull requests. For major changes, open a discussion first.
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safeslm-0.1.1.tar.gz.
File metadata
- Download URL: safeslm-0.1.1.tar.gz
- Upload date:
- Size: 18.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4462ee53d97a8dee65f292f8ea308041089cbfbb6f334a7eb0dcbb154016417
|
|
| MD5 |
aeeadedbed32b291e78eb7597cb911ef
|
|
| BLAKE2b-256 |
b757df9de26645d7c736faf5744063e998a62061f84cb2b99facd639b73b8c56
|
File details
Details for the file safeslm-0.1.1-py3-none-any.whl.
File metadata
- Download URL: safeslm-0.1.1-py3-none-any.whl
- Upload date:
- Size: 23.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cd53b2c27cb8bc8112cd43967a74c9e3f6cf78813ca34d7588038595a78cdef
|
|
| MD5 |
c12aab68cdf064c598ce6f82050d28fe
|
|
| BLAKE2b-256 |
c9b680e5205c7f191d21501348dae1f8a5c077a0c38704da87104a474dcc685f
|