Security scanning and monitoring for LlamaIndex applications - part of Weave Protocol
Project description
🔗 Weave Protocol - LlamaIndex Integration
Security scanning and monitoring for LlamaIndex applications. Part of the Weave Protocol AI security suite.
Features
- 🛡️ Security Callback Handler - Monitor all LlamaIndex events for threats
- 🔧 Secure Tools - Wrap FunctionTools with input/output scanning
- 📚 Secure Retriever - Scan and filter retrieved documents
- 🔍 PII Detection & Redaction - Automatically detect and redact sensitive data
- ⚡ Local & Remote Scanning - Use local patterns or connect to Weave Protocol API
- 🎯 Configurable Severity - Block, warn, or log based on threat severity
Installation
pip install weave-protocol-llamaindex
For remote scanning support:
pip install weave-protocol-llamaindex[remote]
Quick Start
1. Security Callback Handler
Monitor all LlamaIndex operations:
from weave_protocol_llamaindex import WeaveSecurityHandler
from llama_index.core.callbacks import CallbackManager
from llama_index.core import Settings
# Create security handler
handler = WeaveSecurityHandler()
# Attach to LlamaIndex globally
Settings.callback_manager = CallbackManager([handler])
# All LlamaIndex operations are now monitored!
# Prompts, responses, embeddings, retrievals - everything is scanned
2. Secure Tools
Wrap your tools with security scanning:
from weave_protocol_llamaindex import SecureFunctionTool
def search_database(query: str) -> str:
"""Search the company database."""
# Your implementation
return results
# Create secure version
secure_tool = SecureFunctionTool.from_defaults(
fn=search_database,
name="search_database",
description="Search the company database"
)
# Use with an agent
from llama_index.core.agent import ReActAgent
agent = ReActAgent.from_tools([secure_tool])
3. Secure Retriever
Scan and filter retrieved documents:
from weave_protocol_llamaindex import SecureRetriever
from llama_index.core import VectorStoreIndex
# Create your index
index = VectorStoreIndex.from_documents(documents)
base_retriever = index.as_retriever()
# Wrap with security
secure_retriever = SecureRetriever(
retriever=base_retriever,
filter_unsafe=True, # Remove documents with high-severity threats
redact_pii=True # Redact PII from retrieved content
)
# Use in queries
query_engine = index.as_query_engine(retriever=secure_retriever)
Configuration
Security Config
from weave_protocol_llamaindex import SecurityConfig, WeaveSecurityHandler
config = SecurityConfig(
# What to block
block_on_critical=True,
block_on_high=True,
block_on_medium=False,
block_on_low=False,
# What to scan
scan_inputs=True,
scan_outputs=True,
scan_tool_calls=True,
scan_retrieved_docs=True,
# PII handling
redact_pii=True,
pii_types_to_redact=["email", "phone", "ssn", "credit_card"],
# Callbacks
on_threat_detected=lambda event: print(f"Threat: {event}"),
)
handler = WeaveSecurityHandler(config=config)
Preset Handlers
from weave_protocol_llamaindex import (
create_strict_handler, # Blocks medium+ severity
create_warning_handler, # Only logs, never blocks
create_production_handler, # Blocks high+ severity
)
# For development - see all threats
handler = create_warning_handler()
# For production - block dangerous content
handler = create_production_handler()
Custom Scanner
Use remote Weave Protocol API for advanced scanning:
from weave_protocol_llamaindex import RemoteScanner, WeaveSecurityHandler
scanner = RemoteScanner(
api_url="https://api.weaveprotocol.dev",
api_key="your-api-key"
)
handler = WeaveSecurityHandler(scanner=scanner)
Threat Detection
The scanner detects:
| Category | Threats |
|---|---|
| Injection | Prompt injection, jailbreak attempts, role override |
| PII | Emails, phone numbers, SSN, credit cards |
| Secrets | API keys, AWS keys, private keys, passwords |
| Code Injection | SQL injection, command injection, path traversal |
Adding Custom Patterns
from weave_protocol_llamaindex import LocalScanner, PatternDefinition, ThreatType, Severity
custom_pattern = PatternDefinition(
name="internal_id",
pattern=r"INTERNAL-[A-Z]{3}-\d{6}",
threat_type=ThreatType.PII_EXPOSURE,
severity=Severity.MEDIUM,
description="Internal ID detected"
)
scanner = LocalScanner(additional_patterns=[custom_pattern])
Handling Security Events
from weave_protocol_llamaindex import (
WeaveSecurityHandler,
SecurityBlockError,
SecurityConfig
)
def on_threat(event):
# Log to your security system
log_security_event(
threat_type=event.scan_result.findings[0].threat_type,
severity=event.scan_result.findings[0].severity,
content_preview=event.content_preview,
)
config = SecurityConfig(on_threat_detected=on_threat)
handler = WeaveSecurityHandler(config=config)
# Handle blocked content
try:
response = query_engine.query("malicious query...")
except SecurityBlockError as e:
print(f"Blocked: {e.result.findings[0].description}")
Statistics
# Get security stats
stats = handler.get_stats()
print(f"Total scans: {stats['total_scans']}")
print(f"Blocked: {stats['blocked_count']}")
print(f"Threats by type: {stats['threats_by_type']}")
# Get recent threats
threats = handler.get_recent_threats(limit=10)
for threat in threats:
print(f"{threat.severity}: {threat.description}")
Integration with Weave Protocol
This package is part of the Weave Protocol suite:
| Package | Purpose |
|---|---|
| 🛡️ Mund | Threat scanning, authentication |
| 🏛️ Hord | Secure storage, context integrity |
| ⚖️ Domere | Compliance (GDPR, CCPA, SOC2) |
| 👥 Witan | Consensus and governance |
| 🔍 Hundredmen | MCP security inspection |
Requirements
- Python >= 3.9
- llama-index-core >= 0.10.0
- httpx >= 0.24 (for remote scanning)
License
Apache 2.0 - See LICENSE
Links
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 weave_protocol_llamaindex-0.1.0.tar.gz.
File metadata
- Download URL: weave_protocol_llamaindex-0.1.0.tar.gz
- Upload date:
- Size: 16.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66f192d21b22bdd3d19fb90127bf72dc2a4f68d52224fc58bb2126725fa8f015
|
|
| MD5 |
a920506f14a4e31691c506d4fe4feffa
|
|
| BLAKE2b-256 |
583d9fa3b14a0e269d5bf25bd0e8c5b928e3ab82b5615e8647f152c477d8bf14
|
Provenance
The following attestation bundles were made for weave_protocol_llamaindex-0.1.0.tar.gz:
Publisher:
publish-python.yml on Tyox-all/Weave_Protocol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weave_protocol_llamaindex-0.1.0.tar.gz -
Subject digest:
66f192d21b22bdd3d19fb90127bf72dc2a4f68d52224fc58bb2126725fa8f015 - Sigstore transparency entry: 1344757086
- Sigstore integration time:
-
Permalink:
Tyox-all/Weave_Protocol@66c074922deb16299dedc1effc57d051a87fb88d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Tyox-all
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@66c074922deb16299dedc1effc57d051a87fb88d -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file weave_protocol_llamaindex-0.1.0-py3-none-any.whl.
File metadata
- Download URL: weave_protocol_llamaindex-0.1.0-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
07e878fd9305f7f86bd6cbf30368d3ce61aee6372c670ccbe07d3604937ddaed
|
|
| MD5 |
ea01e69fe7ed4c8f89722502a5c584c2
|
|
| BLAKE2b-256 |
af4ff0bbc1f55a5fd0a372143264dd25d9f103af0a5e05dd28e2eb8183e4cbf0
|
Provenance
The following attestation bundles were made for weave_protocol_llamaindex-0.1.0-py3-none-any.whl:
Publisher:
publish-python.yml on Tyox-all/Weave_Protocol
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
weave_protocol_llamaindex-0.1.0-py3-none-any.whl -
Subject digest:
07e878fd9305f7f86bd6cbf30368d3ce61aee6372c670ccbe07d3604937ddaed - Sigstore transparency entry: 1344757201
- Sigstore integration time:
-
Permalink:
Tyox-all/Weave_Protocol@66c074922deb16299dedc1effc57d051a87fb88d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Tyox-all
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-python.yml@66c074922deb16299dedc1effc57d051a87fb88d -
Trigger Event:
workflow_dispatch
-
Statement type: