Prompt injection detection and prevention library for LLM applications
Project description
ProventraCore
A Python library for detecting and preventing prompt injection attacks in LLM applications.
Features
- Text safety classification using transformer models
- Content sanitization using LLMs
- Modular architecture with clear interfaces
- Flexible configuration options
Requirements
- Python 3.11 or higher
- For GPU acceleration: PyTorch with CUDA support
Installation
# Basic installation
pip install proventra-core
# With specific LLM provider
pip install proventra-core[google]
# With multiple providers
pip install proventra-core[openai,anthropic]
# With all providers
pip install proventra-core[all]
# For development
pip install proventra-core[all,dev]
Quick Start
from proventra_core import GuardService, TransformersAnalyzer, LLMSanitizer
# Initialize components
analyzer = TransformersAnalyzer(
model_name="path/to/classification/model",
unsafe_label="unsafe"
)
sanitizer = LLMSanitizer(
provider="google",
model_name="gemini-2.0-flash",
temperature=0.1,
max_tokens=4096
api_key="your-llm-provider-api-key"
)
# Create service
guard = GuardService(analyzer, sanitizer)
# Analyze text
analysis = guard.analyze("Some potentially unsafe text")
print(f"Unsafe: {analysis.unsafe}")
# Analyze and sanitize
result = guard.analyze_and_sanitize("Some potentially unsafe text")
if result.unsafe:
print("Text contains prompt injection")
if result.sanitized:
print(f"Sanitized version: {result.sanitized}")
else:
print("Text is safe")
Hosted API
For quick implementation without setup, use our hosted API service at https://api.proventra-ai.com/docs.
Core Components
The library is organized into the following modules:
-
Models (
proventra_core.models)- Base interfaces (
TextAnalyzer,TextSanitizer) - Result models (
AnalysisResult,SanitizationResult,FullAnalysisResult)
- Base interfaces (
-
Analyzers (
proventra_core.analyzers)TransformersAnalyzer- HuggingFace-based text safety analysis
-
Sanitizers (
proventra_core.sanitizers)LLMSanitizer- LLM-based text sanitization
-
Providers (
proventra_core.providers)- LLM provider factory and configuration
- Supports: Google, OpenAI, Anthropic, Mistral
-
Services (
proventra_core.services)GuardService- Main service combining analysis and sanitization
Advanced Usage
Custom Analyzer
from proventra_core import TextAnalyzer, GuardService
from typing import Dict, Any
class CustomAnalyzer(TextAnalyzer):
def __init__(self, threshold=0.5):
self.threshold = threshold
def analyze(self, text: str) -> Dict[str, Any]:
# Your custom analysis logic
unsafe = any(bad_word in text.lower() for bad_word in ["hack", "ignore", "system"])
return {
"unsafe": unsafe,
# You can include additional properties
"matched_keywords": [word for word in ["hack", "ignore", "system"] if word in text.lower()]
}
@property
def max_tokens(self):
return 1024
@property
def chunk_overlap(self):
return 128
# Use with existing service
guard = GuardService(CustomAnalyzer(threshold=0.7), existing_sanitizer)
Deployment Examples
The repository includes examples for deploying the library:
FastAPI Server
cd examples/api
pip install -e "../../[api,all]"
uvicorn main:app --reload
RunPod Serverless
cd examples/runpod
pip install -e "../../[runpod,all]"
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file proventra_core-0.1.2.tar.gz.
File metadata
- Download URL: proventra_core-0.1.2.tar.gz
- Upload date:
- Size: 20.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b275f375b3ca71465438758cc0a099061b4132bf41723f87b993c2cb3ac8b7d
|
|
| MD5 |
36fad01da9d9ccefe6e4aaaf42247942
|
|
| BLAKE2b-256 |
506977f435d875a5377d1cfb5b315102c26417404f7160a1897f982f67dc2ceb
|
File details
Details for the file proventra_core-0.1.2-py3-none-any.whl.
File metadata
- Download URL: proventra_core-0.1.2-py3-none-any.whl
- Upload date:
- Size: 12.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
274b59fbe4f172e5dbb6f56ac1539fd54db477c8703ac13e3a050782dc48fbac
|
|
| MD5 |
3e09c24de2b64bf60483f701ff810509
|
|
| BLAKE2b-256 |
2b204c7b17156d1afeb8cba8f47604a896253d49267a6bf6f9f2b68a167c1986
|