Skip to main content

An answer-or-ABSTAIN gate for RAG/agent answers, driven by grounding-DROP sensitivity - catches poisoned-context errors that confidence misses. Zero dependencies.

Project description

grounding-firewall

An answer-or-ABSTAIN gate for RAG / agent answers, driven by grounding-drop sensitivity instead of confidence. Zero dependencies (Python stdlib only).

v0.2 adds gate_freeform() for open-ended (free-form) RAG answers, not just A/B multiple choice - this is the mode for real RAG. Validated on glm-5.2 (mixed clean/poison retrieval): drop-sensitivity corr with correctness +1.00 vs confidence -0.21; 0% wrong at 50% coverage (AUC 0.187 vs 0.424).

Why

A model's confidence is blind exactly when it is confidently wrong: when a retrieved document is poisoned (asserts a plausible-but-false answer), frontier models follow it at full confidence. The firewall instead measures how much the answer depends on the retrieved doc:

sensitivity = | p(answer | context) - p(answer | context dropped) |

An answer that flips when you remove its evidence is grounded in the doc, not in the model's knowledge - so if the doc is wrong, the answer is wrong, and confidence won't warn you. The firewall abstains on high-sensitivity answers.

Measured (frontier models, realistic mixed retrieval)

Each factual question given once a clean doc and once a poisoned doc (50/50), on glm-5.2 and deepseek-v4-flash:

signal glm-5.2 deepseek-v4-flash
confidence corr with correctness -0.07 (blind) +0.21 (blind)
drop-sensitivity corr with correctness +0.97 +1.00
confidence: wrong-rate @ 50% coverage ~42% ~50%
firewall: wrong-rate @ 50% coverage 0% 0%
risk-coverage AUC (lower better) 0.216 vs 0.427 0.261 vs 0.489

The firewall keeps every clean-doc answer and abstains on every poisoned one, where confidence ships ~half wrong (poisoned and clean answers are both high-confidence). Under all-poison retrieval, frontier models defer ~94-100% at full confidence and the firewall correctly abstains on ~everything.

Honest scope: strong direct-assertion poison, 2-option factual questions; the coverage you keep tracks the fraction of clean docs in your retrieval. The real deploy cost is one extra (context-dropped) query.

Install

pip install grounding-firewall

Use

import grounding_firewall as gf
cfg = {"endpoint": "https://your-llm/v1", "model": "<model>", "api_key": "<key>", "logprobs": True, "k": 5}

# free-form (real RAG) — v0.2:
gf.gate_freeform(cfg, question="What is the capital of Australia?",
                 context="Doc: the capital is Sydney.")
# -> {'answer': 'Sydney', 'answer_without_doc': 'Canberra', 'sensitivity': 1.0, 'decision': 'ABSTAIN', ...}

# multiple-choice:
gf.gate(cfg, question="What is the capital of Australia?",
        context="Doc: the capital is Sydney.", a="Canberra", b="Sydney")
# -> {'answer': 'Sydney', 'confidence': 1.0, 'sensitivity': 1.0, 'decision': 'ABSTAIN', ...}

CLI:

# reproduce the poisoning self-test on your own model:
grounding-firewall --endpoint <url> --model <m> --demo
# gate one answer:
grounding-firewall --endpoint <url> --model <m> \
    --question "What is the capital of Australia?" --context "Doc: the capital is Sydney." \
    --a Canberra --b Sydney

Part of Agora - see the verification ledger / Folklore Index. License: MIT.

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

grounding_firewall-0.2.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

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

grounding_firewall-0.2.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file grounding_firewall-0.2.0.tar.gz.

File metadata

  • Download URL: grounding_firewall-0.2.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for grounding_firewall-0.2.0.tar.gz
Algorithm Hash digest
SHA256 e0f4f16736c166716394385611a03bd4ae2f06ffc0a55311eaa00f005dc3a860
MD5 68157863cf8e5fd926dbbf4bf21f9eed
BLAKE2b-256 ecab8ca482bb892bb43b60efb5e89397b6a935416309da9bb4d442e719dbcba4

See more details on using hashes here.

File details

Details for the file grounding_firewall-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for grounding_firewall-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 19388c900a89382dc700ad18144b9f0985f10cf5562753944b46c6e112d26575
MD5 ab6ac649c7a7ed788f0259c5e7aef909
BLAKE2b-256 89762dd6364744f2499064bcd9e79a3d3feeeb5d15dc4fbde8a57843cd60e2e3

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 Pingdom Monitoring Sentry Error logging StatusPage Status page