๐ฌ LLM Sandboxing and SecurityJuly 18, 2026โ
Tests passing
LLM Output Filter
LLM Output Filter acts as a proxy that filters the output of large language models to detect and redact potentially harmful or sensitive content before it's displayed. This tool is essential for developers looking to prevent model misuse or unintentional leakage of sensitive information.
What It Does
- Customizable Filtering Rules: Define your own filtering rules using regular expressions or load them from a JSON file.
- Inline Replacement or Output Blocking: Replace sensitive content inline or block it entirely.
- Real-Time Analysis: Processes text with minimal latency, making it suitable for real-time applications.
Installation
Install the required dependencies using pip:
pip install transformers==4.33.0Usage
Here is an example of a JSON rules file:
{
"\\b\\d{4}-\\d{4}-\\d{4}-\\d{4}\\b": "[REDACTED]",
"\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b": "[REDACTED]"
}Source Code
import re
import json
from transformers import pipeline
def filter_output(text, rules=None, rules_file=None):
"""
Filters the output text based on provided rules or a rules file.
Args:
text (str): The text to be filtered.
rules (dict, optional): A dictionary of filtering rules.
rules_file (str, optional): Path to a JSON file containing filtering rules.
Returns:
str: The filtered text with sensitive content redacted.
"""
if not text or not isinstance(text, str):
raise ValueError("Input text must be a non-empty string.")
if rules_file:
try:
with open(rules_file, 'r') as f:
rules = json.load(f)
except (FileNotFoundError, json.JSONDecodeError) as e:
raise ValueError("Invalid rules file.") from e
if not rules or not isinstance(rules, dict):
raise ValueError("Rules must be a dictionary or a valid JSON file.")
for pattern, replacement in rules.items():
try:
text = re.sub(pattern, replacement, text)
except re.error as e:
raise ValueError(f"Invalid regex pattern: {pattern}") from e
return text
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description="LLM Output Filter Tool")
parser.add_argument("text", type=str, help="The text to filter.")
parser.add_argument("--rules", type=str, help="JSON string of filtering rules.")
parser.add_argument("--rules_file", type=str, help="Path to a JSON file containing filtering rules.")
args = parser.parse_args()
rules = None
if args.rules:
try:
rules = json.loads(args.rules)
except json.JSONDecodeError:
print("Invalid JSON string for rules.")
exit(1)
try:
filtered_text = filter_output(args.text, rules=rules, rules_file=args.rules_file)
print(filtered_text)
except ValueError as e:
print(f"Error: {e}")
exit(1)Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- llm_output_filter
- Category
- LLM Sandboxing and Security
- Generated
- July 18, 2026
- Tests
- Passing โ
Quick Install
Clone just this tool:
git clone --depth 1 --filter=blob:none --sparse \ https://github.com/ptulin/autoaiforge.git cd autoaiforge git sparse-checkout set generated_tools/2026-07-18/llm_output_filter cd generated_tools/2026-07-18/llm_output_filter pip install -r requirements.txt 2>/dev/null || true python llm_output_filter.py