๐ฌ On-Device Privacy for LLMsJune 4, 2026โ
Tests passing
Local Privacy Guard for LLMs
This tool monitors and intercepts communication between a user and a local LLM instance to block any outgoing sensitive data. It acts as a middleware that flags data leakage risks in real-time, providing warnings or blocking the operation entirely. Developers can use it to enforce strict privacy constraints during LLM interactions.
What It Does
- Middleware to intercept and analyze outgoing LLM data.
- Support for predefined or custom privacy rules.
- Alerting and blocking mechanisms for sensitive data.
- Simple function wrapping for seamless integration.
Installation
pip install -r requirements.txtUsage
Example
from local_privacy_guard import PrivacyGuard
# Initialize PrivacyGuard with rules
guard = PrivacyGuard(rules=[r'\bpassword\b', r'\bsecret\b'])
# Check text for sensitive data
try:
sanitized_text = guard.check("This is a test with password.")
except ValueError as e:
print(e)
# Wrap a function to intercept its input
@guard.wrap
def example_function(input_text):
return f"Processed: {input_text}"
try:
result = example_function("This contains secret information.")
except ValueError as e:
print(e)Source Code
import re
import logging
from inspect import signature
class PrivacyGuard:
def __init__(self, rules=None):
"""
Initialize the PrivacyGuard with a set of rules.
:param rules: List of regex patterns to identify sensitive data.
"""
self.rules = rules or []
self.logger = logging.getLogger('PrivacyGuard')
logging.basicConfig(level=logging.INFO)
def add_rule(self, rule):
"""
Add a new regex rule for detecting sensitive data.
:param rule: A regex pattern as a string.
"""
self.rules.append(rule)
def check(self, text):
"""
Analyze the given text for sensitive data based on the rules.
:param text: The text to analyze.
:return: The sanitized text if no sensitive data is detected.
:raises ValueError: If sensitive data is detected.
"""
for rule in self.rules:
if re.search(rule, text):
self.logger.warning(f"Sensitive data detected: {rule}")
raise ValueError("Sensitive data detected based on privacy rules.")
return text
def wrap(self, func):
"""
Wrap a function to intercept its text input and check for sensitive data.
:param func: The function to wrap.
:return: A wrapped function.
"""
def wrapped(*args, **kwargs):
sig = signature(func)
bound_args = sig.bind(*args, **kwargs)
bound_args.apply_defaults()
for name, value in bound_args.arguments.items():
if isinstance(value, str):
self.check(value)
return func(*args, **kwargs)
return wrapped
if __name__ == "__main__":
# Example usage
guard = PrivacyGuard(rules=[r'\bpassword\b', r'\bsecret\b'])
try:
print(guard.check("This is a test with password."))
except ValueError as e:
print(e)
@guard.wrap
def example_function(input_text):
return f"Processed: {input_text}"
try:
print(example_function("This contains secret information."))
except ValueError as e:
print(e)Community
Downloads
ยทยทยท
Rate this tool
No ratings yet โ be the first!
Details
- Tool Name
- local_privacy_guard
- Category
- On-Device Privacy for LLMs
- Generated
- June 4, 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-06-04/local_privacy_guard cd generated_tools/2026-06-04/local_privacy_guard pip install -r requirements.txt 2>/dev/null || true python local_privacy_guard.py