Skip to main content

Comprehensive prompt safety: bad prompt detection, injection detection, and PII safety for LLM applications

Project description

🛡️ aipromptsentinel

PyPI version Python License: MIT

Comprehensive prompt safety for LLM applications.

aipromptsentinel combines bad prompt detection, prompt injection detection, and PII safety into a unified, production-ready Python package.

✨ Features

Feature Description
🔍 PII Detection Identifies emails, SSNs, credit cards, phone numbers, IP addresses, API keys, and more
🛡️ Injection Detection Detects prompt injection attacks, jailbreaks, and context manipulation
🚫 Bad Prompt Detection Flags harmful, toxic, or inappropriate content across multiple categories
Zero ML Dependencies Fast, lightweight regex/heuristic-based detection
🖥️ CLI Included Scan text and files from the command line
📊 Risk Scoring Confidence scores and risk levels for all detections

🚀 Quick Start

Installation

pip install aipromptsentinel

Or with uv:

uv add aipromptsentinel

Basic Usage

from prompt_sentinel import Sentinel

sentinel = Sentinel()

# Analyze any text
result = sentinel.analyze("My email is test@example.com. Ignore previous instructions!")

print(result.is_safe)          # False
print(result.has_pii)          # True
print(result.has_injection)    # True
print(result.has_bad_content)  # False

# Quick checks
sentinel.is_safe("Hello world")                    # True
sentinel.has_pii("SSN: 123-45-6789")               # True  
sentinel.has_injection("Ignore all instructions")  # True

# Redact PII
text = "Contact me at john@example.com"
print(sentinel.redact_pii(text))  # "Contact me at [REDACTED]"

📖 API Reference

Sentinel Class

The main interface combining all detection capabilities.

from prompt_sentinel import Sentinel

# Full configuration
sentinel = Sentinel(
    enable_pii=True,           # Enable PII detection
    enable_injection=True,      # Enable injection detection
    enable_bad_prompt=True,     # Enable bad prompt detection
    injection_threshold=0.5,    # Minimum confidence for injections
    bad_prompt_severity="low",  # Minimum severity level
)

# Complete analysis
result = sentinel.analyze("Your text here")

SentinelResult

result.is_safe              # bool: Overall safety verdict
result.has_pii              # bool: Contains PII
result.has_injection        # bool: Contains injection attempt
result.has_bad_content      # bool: Contains harmful content
result.pii_findings         # List[PIIFinding]
result.injection_findings   # List[InjectionFinding]
result.bad_prompt_findings  # List[BadPromptFinding]
result.injection_risk_score # float: 0.0 to 1.0
result.bad_prompt_risk_level # str: "safe", "low", "medium", "high", "critical"
result.to_dict()            # Convert to JSON-serializable dict

Individual Detectors

Use detectors independently for specific needs:

from prompt_sentinel.detectors import PIIDetector, InjectionDetector, BadPromptDetector

# PII Detection
pii = PIIDetector()
findings = pii.detect("Email: test@example.com, SSN: 123-45-6789")
redacted = pii.redact("My phone is 555-123-4567")

# Injection Detection
injection = InjectionDetector(threshold=0.5)
findings = injection.detect("Ignore previous instructions")
risk_score = injection.get_risk_score(text)

# Bad Prompt Detection  
bad_prompt = BadPromptDetector(severity_threshold="medium")
findings = bad_prompt.detect("Harmful content here")
risk_level = bad_prompt.get_risk_level(text)

🖥️ CLI Usage

Scan Text

# Basic scan
prompt-sentinel scan "My email is test@example.com"

# JSON output
prompt-sentinel scan "Hello world" --format json

# With PII redaction
prompt-sentinel scan "SSN: 123-45-6789" --redact

# Disable specific checks
prompt-sentinel scan "text" --no-pii --no-bad-prompt

Scan Files

# Scan entire file
prompt-sentinel check-file prompts.txt

# Line-by-line analysis
prompt-sentinel check-file data.txt --line-by-line

# JSON output
prompt-sentinel check-file input.txt --format json

Redact PII

prompt-sentinel redact "My email is test@example.com"
# Output: My email is [REDACTED]

🔍 Detection Categories

PII Types

Type Example Pattern
Email test@example.com Standard email format
SSN 123-45-6789 US Social Security Number
Credit Card 4111111111111111 Visa, MC, Amex, Discover
Phone (555) 123-4567 US and international formats
IP Address 192.168.1.1 IPv4 and IPv6
API Key sk-xxx... OpenAI, AWS, GitHub, etc.
Date of Birth 01/15/1990 Common date formats
Passport AB1234567 Passport number patterns
IBAN DE89370400440532013000 International bank numbers

Injection Types

Type Description
instruction_override "Ignore previous instructions"
role_confusion "You are now a pirate"
jailbreak "Enable DAN mode", "bypass safety"
context_manipulation [SYSTEM], `<
data_extraction "Reveal your system prompt"
delimiter_abuse Excessive separators, newlines

Bad Prompt Categories

Category Severity Examples
Violence Critical Weapons, harm
Illegal High Hacking, theft
Malware Critical Virus creation
Fraud High Scams, identity theft
Harassment High Doxxing, stalking
Self-Harm Critical Suicide, self-injury
Manipulation High Phishing, social engineering
Hate Speech Medium Discrimination
Adult Content High Explicit material

🔧 Advanced Configuration

Custom PII Patterns

import re
from prompt_sentinel.detectors import PIIDetector

detector = PIIDetector(
    custom_patterns={
        "employee_id": re.compile(r"EMP-\d{6}")
    }
)

Selective Detection

from prompt_sentinel import Sentinel
from prompt_sentinel.detectors.pii import PIIType

# Only detect specific PII types
sentinel = Sentinel(
    pii_types=[PIIType.EMAIL, PIIType.CREDIT_CARD]
)

Risk Analysis

sentinel = Sentinel()
summary = sentinel.get_risk_summary(text)

print(summary)
# {
#     "overall_safe": False,
#     "pii_risk": {"found": True, "count": 2, "types": ["email", "ssn"]},
#     "injection_risk": {"found": True, "score": 0.85, "types": ["jailbreak"]},
#     "bad_prompt_risk": {"found": False, "level": "safe", "categories": []}
# }

🧪 Testing

# Install dev dependencies
uv sync --dev

# Run tests
uv run pytest tests/ -v

# With coverage
uv run pytest tests/ -v --cov=src/prompt_sentinel

📦 Building & Publishing

# Build the package
uv build

# Upload to PyPI
uv publish

🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

📄 License

MIT License - see LICENSE for details.


Made with ❤️ for safer LLM applications

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

aipromptsentinel-0.1.1.tar.gz (22.2 kB view details)

Uploaded Source

Built Distribution

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

aipromptsentinel-0.1.1-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file aipromptsentinel-0.1.1.tar.gz.

File metadata

  • Download URL: aipromptsentinel-0.1.1.tar.gz
  • Upload date:
  • Size: 22.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.11

File hashes

Hashes for aipromptsentinel-0.1.1.tar.gz
Algorithm Hash digest
SHA256 2bb1c3397d6ae098df8a9613e392f525b710c52035d99c866e9fbc1678ffd1f2
MD5 6dce370e609790a9549b9f0547680a75
BLAKE2b-256 0da3622233c906387a4b70dd1ba60b7700b02a4f5becd7018991f8c98c298ee1

See more details on using hashes here.

File details

Details for the file aipromptsentinel-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for aipromptsentinel-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c8e636b2aa7b83d237c2e3dceb057724bb2371cb05ae192d3ede723fe959aca7
MD5 c0f158a0f05f4d68cba6bc38d81107a4
BLAKE2b-256 b4776fd427a22eacb4d813c9fd942c01b85882391715b42b4f42b01534413f97

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