TrustGuard integration for LlamaIndex - protect your RAG pipeline from malicious content
Project description
LlamaIndex TrustGuard
Security integration for LlamaIndex - protect your RAG pipeline from prompt injection and malicious content.
Installation
pip install llama-index-trustguard
Features
- TrustGuardReader - Scan documents before indexing
- TrustGuardNodePostprocessor - Scan retrieved nodes before using as context
Quick Start
Protected Document Loading
Scan documents for threats before indexing:
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index_trustguard import TrustGuardReader
# Wrap your reader with threat protection
base_reader = SimpleDirectoryReader("./documents")
reader = TrustGuardReader(
base_reader,
api_key="ta_xxx...",
on_threat="filter", # Skip documents with threats
)
# Only safe documents are loaded
documents = reader.load_data()
# Build index with safe documents only
index = VectorStoreIndex.from_documents(documents)
Protected Query Engine
Scan retrieved nodes before using them as context:
from llama_index.core import VectorStoreIndex
from llama_index_trustguard import TrustGuardNodePostprocessor
# Create your index
index = VectorStoreIndex.from_documents(documents)
# Add TrustGuard postprocessor
postprocessor = TrustGuardNodePostprocessor(
api_key="ta_xxx...",
on_threat="filter", # Filter out threatening nodes
)
# Create protected query engine
query_engine = index.as_query_engine(
node_postprocessors=[postprocessor]
)
# Poisoned nodes are automatically filtered
response = query_engine.query("What is the company policy?")
Full Protected RAG Pipeline
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index_trustguard import TrustGuardReader, TrustGuardNodePostprocessor
# 1. Protected document loading
reader = TrustGuardReader(
SimpleDirectoryReader("./knowledge_base"),
api_key="ta_xxx...",
on_threat="filter",
)
documents = reader.load_data()
# 2. Build index
index = VectorStoreIndex.from_documents(documents)
# 3. Protected retrieval
postprocessor = TrustGuardNodePostprocessor(
api_key="ta_xxx...",
on_threat="filter",
)
query_engine = index.as_query_engine(
node_postprocessors=[postprocessor]
)
# Your RAG pipeline is now protected at both indexing and retrieval
response = query_engine.query("Tell me about our products")
API Reference
TrustGuardReader
Wraps any LlamaIndex reader with threat scanning.
TrustGuardReader(
reader: BaseReader, # The reader to wrap
api_key: str = None, # TrustGuard API key
on_threat: str = "warn", # "block", "warn", "filter", "tag"
min_block_level: ThreatLevel = ThreatLevel.HIGH,
content_type: ContentSource = ContentSource.DOCUMENT,
)
on_threat options:
"block"- RaiseThreatInDocumentErroron threat"warn"- Log warning and continue"filter"- Skip documents with threats"tag"- Add threat info to document metadata
Methods:
documents = reader.load_data() # Load and scan documents
stats = reader.get_stats() # Get scanning statistics
TrustGuardNodePostprocessor
Postprocessor that scans retrieved nodes.
TrustGuardNodePostprocessor(
api_key: str = None, # TrustGuard API key
on_threat: str = "warn", # Same options as reader
min_block_level: ThreatLevel = ThreatLevel.HIGH,
)
Usage:
query_engine = index.as_query_engine(
node_postprocessors=[postprocessor]
)
# Or with a retriever
retriever = index.as_retriever(
node_postprocessors=[postprocessor]
)
Examples
Web Content Scanning
from llama_index.readers.web import SimpleWebPageReader
from llama_index_trustguard import TrustGuardReader
from agent_trust import ContentSource
reader = TrustGuardReader(
SimpleWebPageReader(),
api_key="ta_xxx...",
content_type=ContentSource.WEB, # Optimized for web content
on_threat="filter",
)
documents = reader.load_data(urls=["https://example.com/docs"])
Tagging Instead of Filtering
# Tag documents with threat info instead of filtering
reader = TrustGuardReader(
base_reader,
api_key="ta_xxx...",
on_threat="tag",
)
documents = reader.load_data()
for doc in documents:
if doc.metadata.get("trust_guard", {}).get("safe") == False:
print(f"Document has threats: {doc.metadata['trust_guard']['threats']}")
Strict Mode (Block on Medium Threats)
from agent_trust import ThreatLevel
reader = TrustGuardReader(
base_reader,
api_key="ta_xxx...",
on_threat="block",
min_block_level=ThreatLevel.MEDIUM, # Stricter blocking
)
Error Handling
from llama_index_trustguard import TrustGuardReader, ThreatInDocumentError
reader = TrustGuardReader(
base_reader,
api_key="ta_xxx...",
on_threat="block",
)
try:
documents = reader.load_data()
except ThreatInDocumentError as e:
print(f"Threat in: {e.document_id}")
print(f"Verdict: {e.guard_result.verdict}")
print(f"Threats: {[t.pattern_name for t in e.guard_result.threats]}")
Statistics
# Reader stats
reader_stats = reader.get_stats()
print(f"Scanned: {reader_stats['scanned_count']}")
print(f"Threats: {reader_stats['threat_count']}")
print(f"Filtered: {reader_stats['filtered_count']}")
# Postprocessor stats
pp_stats = postprocessor.get_stats()
print(f"Nodes scanned: {pp_stats['scanned_count']}")
License
MIT License
Links
- TrustAgents: https://trustagents.dev
- LlamaIndex: https://www.llamaindex.ai/
- GitHub: https://github.com/jd-delatorre/trustlayer
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 llama_index_trustguard-0.1.0.tar.gz.
File metadata
- Download URL: llama_index_trustguard-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
865da4cde139626faaa130063a0da2946d40b200bd170e2613ba994ac711629b
|
|
| MD5 |
b3874a59a8862f6e034f545a8316e306
|
|
| BLAKE2b-256 |
1c997d20c27990fa9d85f56306c109a4503e73c587ab8707259e730682181c93
|
File details
Details for the file llama_index_trustguard-0.1.0-py3-none-any.whl.
File metadata
- Download URL: llama_index_trustguard-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
532ee02cdc465279b45c983ee479a2952cb6ed453030411a71c52ed7648e9d1d
|
|
| MD5 |
fec9cfbb34a7603544e0fcc287e9d7b9
|
|
| BLAKE2b-256 |
67d3c6931bd74d5eb5c79c584c8ec840650cb5d2f01fcaa3c8ecbc0a7543a1fe
|