Skip to main content

PrivRAG-Guard

Privacy-preserving middleware that sanitizes vector embeddings before they reach a database. Defends RAG pipelines against embedding inversion attacks using calibrated differential-privacy noise, semantic subspace projection, and keyed orthogonal rotation — while preserving >98% cosine retrieval accuracy.

Install

pip install -e ".[dev]"

Library usage

from privrag import PrivRAGGuard

guard = PrivRAGGuard(epsilon=1.0, noise_fraction=0.10, passphrase="shared-secret")
protected = guard.protect_batch(document_embeddings)
query = guard.protect_vector(query_embedding)

metrics = guard.benchmark_utility(document_embeddings, protected, k=5)

Sharing keys across services

Derive deterministically from a passphrase (inject via secret manager):

guard = PrivRAGGuard(passphrase=os.environ["PRIVRAG_KEY"])

Or persist a key file from a protected volume:

guard.protect_vector(sample)  # initializes rotation
guard.save_key("/run/secrets/privrag.key")

other = PrivRAGGuard.from_key_file("/run/secrets/privrag.key")

Adapters

from privrag.adapters import ChromaPrivGuard, LangChainPrivGuardEmbeddings

collection = ChromaPrivGuard.from_client(client, "notes", guard)
safe_embeddings = LangChainPrivGuardEmbeddings(provider, guard)

CLI

# Simulate a dictionary inversion attack
privrag attack "patient diabetes ssn 12345"

# Benchmark retrieval retention
privrag benchmark --samples 500 --epsilon 1.0

# Sanitize an embedding batch
privrag protect --input raw.npy --output safe.npy --passphrase secret

Security notes

This is a practical defense layer, not a formal end-to-end DP guarantee. The bounded perturbation intentionally trades strict DP calibration for retrieval utility. Use DifferentialPrivacyEngine.add_noise() for textbook unbounded DP when formal guarantees are required.

Keep rotation keys outside the vector store. Apply one configuration to all documents and queries in an index. Restrict database access. Encrypt at rest and in transit.

Development

pytest

Download files

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

Source Distribution

privrag_guard-0.1.0.tar.gz (18.5 kB view details)

Uploaded Source

Built Distribution

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

privrag_guard-0.1.0-py3-none-any.whl (17.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: privrag_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 18.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for privrag_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a3483a9c86aa7cd8bdacefbf784dca8dd02273435084ab5a7d53826975d32d14
MD5 95d98ad198e7d0656f667bb07ad047a6
BLAKE2b-256 49e9f4c440b5d127fa2142519e1662330d12ee16e81538616cfbb471934a9471

See more details on using hashes here.

File details

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

File metadata

  • Download URL: privrag_guard-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 17.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.10

File hashes

Hashes for privrag_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a4a4bd7587461a9d0622643a29710483cab11c676f997bc826864875d1c206a
MD5 d475f085b610e2341d24aa5b5f45508b
BLAKE2b-256 cd80b793c18826db8a28d51bc16523b8748481a151d6d76d7ddb7a4e9cfb3756

See more details on using hashes here.

Supported by

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