Skip to main content

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

Installation

pip install safeslm

Usage

Python API

from safeslm import SafeSLM

# Train a new LoRA adapter on safety data
model = SafeSLM("gpt2", precision="8bit", device="cuda")
model.train(output_dir="./adapter_out", num_train_epochs=3, per_device_train_batch_size=16)

# Load the adapter for inference
safe_model = SafeSLM(
    "gpt2",
    precision="8bit",
    device="cuda",
    adapter_path="./adapter_out/lora_adapter"
)

# Run prompts
output = safe_model("Is it safe to share my password online?")
print(output)

CLI

# Train LoRA adapter
safeslm train --model gpt2 --out_dir ./adapter_out --epochs 3 --batch_size 16 --precision 8bit --device cuda

# Run inference
safeslm infer --model gpt2 --adapter ./adapter_out/lora_adapter --prompt "What are safe browsing practices?" --precision 8bit --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"))
print(adapter_b("Some potentially unsafe prompt"))

Pipeline Overview

The SafeSLM pipeline consists of:

  1. Base Model Loading – Load the HuggingFace model in desired precision.
  2. Optional LoRA Adapter – Fine-tuned weights for safety, loaded separately.
  3. Router & Encoder – Encodes prompts for safety classification or routing.
  4. Pipeline Execution – Handles prompt routing, LoRA injection, and generation.
Flowchart

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.

Contributing

Contributions are welcome! Please submit issues or pull requests. For major changes, open a discussion first.


License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safeslm-0.1.0.tar.gz (16.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safeslm-0.1.0-py3-none-any.whl (21.9 kB view details)

Uploaded Python 3

File details

Details for the file safeslm-0.1.0.tar.gz.

File metadata

  • Download URL: safeslm-0.1.0.tar.gz
  • Upload date:
  • Size: 16.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for safeslm-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f344f82d809e0f1ef88cd74e56c3c20f932cc68581f31ddc16c9395b99019a9e
MD5 a40bb2624fab465a40263ad17123eb81
BLAKE2b-256 4dfd42570ddb4684f90e5bb943eb3f2e9b37b6543fbe7b5c7fc6c2fc53e02e9d

See more details on using hashes here.

File details

Details for the file safeslm-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: safeslm-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 21.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for safeslm-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f0eec228ba254f32a9947cac1cca21290e66d16de54e007134061a1bf11c24d1
MD5 d38d512881c648c8c78f1eb2306d3f43
BLAKE2b-256 4e0d863975c060e10fd2b994ebe93972c6f2ecd799a642784404e0d67d03845b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page