Skip to main content

Security scanning and monitoring for LlamaIndex applications - part of Weave Protocol

Project description

🔗 Weave Protocol - LlamaIndex Integration

PyPI version License

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

weave_protocol_llamaindex-0.1.0.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

weave_protocol_llamaindex-0.1.0-py3-none-any.whl (18.2 kB view details)

Uploaded Python 3

File details

Details for the file weave_protocol_llamaindex-0.1.0.tar.gz.

File metadata

File hashes

Hashes for weave_protocol_llamaindex-0.1.0.tar.gz
Algorithm Hash digest
SHA256 66f192d21b22bdd3d19fb90127bf72dc2a4f68d52224fc58bb2126725fa8f015
MD5 a920506f14a4e31691c506d4fe4feffa
BLAKE2b-256 583d9fa3b14a0e269d5bf25bd0e8c5b928e3ab82b5615e8647f152c477d8bf14

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file weave_protocol_llamaindex-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for weave_protocol_llamaindex-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 07e878fd9305f7f86bd6cbf30368d3ce61aee6372c670ccbe07d3604937ddaed
MD5 ea01e69fe7ed4c8f89722502a5c584c2
BLAKE2b-256 af4ff0bbc1f55a5fd0a372143264dd25d9f103af0a5e05dd28e2eb8183e4cbf0

See more details on using hashes here.

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

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page