Skip to main content

A package for sanitizing and securing user inputs to LLMs

Project description

LLM Input Sanitizer

A Python package for sanitizing and securing user inputs before sending them to large language models (LLMs).

Features

  • PII Detection & Masking: Automatically detects and masks emails, phone numbers, SSNs, and credit card numbers
  • Profanity Filtering: Removes or masks profanity and inappropriate language
  • Input Truncation: Prevents excessively long inputs
  • Unicode Normalization: Handles special characters and ensures consistent text encoding
  • Prompt Injection Defense: Detects and blocks common LLM prompt injection attacks
  • Jailbreak Prevention: Identifies attempts to bypass LLM safety measures

Installation

pip install llm-input-sanitizer

Quick Start

from llm_input_sanitizer import InputSanitizer, prepare_llm_messages, is_input_appropriate

# Initialize the sanitizer
sanitizer = InputSanitizer(max_length=1000)

# Sanitize user input
user_input = "My email is john@example.com and my phone is 555-123-4567"
sanitized_input = sanitizer.sanitize_input(user_input)
# Result: "My email is [EMAIL] and my phone is [PHONE]"

# Check if input is appropriate (no injection attempts)
if is_input_appropriate(sanitized_input):
    # Prepare messages for the LLM
    messages = prepare_llm_messages(
        sanitized_input, 
        system_message="You are a helpful assistant."
    )
    # Send messages to your LLM
else:
    print("Potentially harmful input detected")

Custom Profanity list

sanitizer = InputSanitizer(profanity_file="path/to/profanity_words.txt")

Custom forbidden paths

from llm_input_sanitizer import is_input_appropriate

my_patterns = [
    r'custom_pattern_1',
    r'custom_pattern_2',
]

is_safe = is_input_appropriate(text, forbidden_patterns=my_patterns)

Integration with OpenAI

import openai
from llm_input_sanitizer import InputSanitizer, prepare_llm_messages, is_input_appropriate

sanitizer = InputSanitizer()

def safe_llm_call(user_input):
    # Sanitize the input
    clean_input = sanitizer.sanitize_input(user_input)
    
    # Check if appropriate
    if not is_input_appropriate(clean_input):
        return "I'm sorry, I can't process that request."
    
    # Prepare messages
    messages = prepare_llm_messages(clean_input)
    
    # Call the LLM API
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=messages
    )
    
    return response.choices[0].message.content

This package provides a baseline of protection against common attacks but is not a complete security solution. Always implement defense in depth for production systems:

  • Server-side validation
  • Rate limiting
  • Monitoring for unusual patterns
  • Regular updates to security patterns

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

llm_input_sanitizer-0.2.0.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

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

llm_input_sanitizer-0.2.0-py3-none-any.whl (24.4 kB view details)

Uploaded Python 3

File details

Details for the file llm_input_sanitizer-0.2.0.tar.gz.

File metadata

  • Download URL: llm_input_sanitizer-0.2.0.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for llm_input_sanitizer-0.2.0.tar.gz
Algorithm Hash digest
SHA256 94d41de2f3dcd72c72b1443421df92eafe0754ea05d60dec18a35c32ce899b9e
MD5 b7384bbd9bcdecf2363ab3d0bd128582
BLAKE2b-256 9d186b61b1da53d8d32b1787aac68cd178df8f691dd62cfdd6209f9026b1874e

See more details on using hashes here.

File details

Details for the file llm_input_sanitizer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for llm_input_sanitizer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 51054ef66b4a0faa7f378dd508a0bdf8860cb27dac27a1273a3938e9e9c3692f
MD5 b82e3a0bdcd4f7553f5727ae2005658b
BLAKE2b-256 e9fb1ec820ae279b14efdc3406cb7dfa0cb059048a7257ef5f4ccd00298d03aa

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