Metadata-Version: 2.5
Name: gatrag-context-classifier
Version: 1.0.0
Summary: NLI cross-encoder gate that scores whether retrieved RAG context is sufficient to answer a query.
Project-URL: Homepage, https://github.com/YOUR_USER/gatrag-context-classifier
Author-email: Gattupalli Saketh <18saketh@gmail.com>
License-Expression: MIT
Keywords: classifier,context,cross-encoder,nli,rag
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Requires-Dist: numpy>=1.24
Provides-Extra: model
Requires-Dist: sentence-transformers>=3.0; extra == 'model'
Requires-Dist: torch>=2.0; extra == 'model'
Description-Content-Type: text/markdown

GATRAG

Guarded Abstention / Answering for Retrieval-Augmented Generation (RAG)

GATRAG is a lightweight context-sufficiency gate for RAG pipelines. It sits between the retriever and the LLM/generator, evaluates whether retrieved passages contain enough information to answer a user's query, filters weak passages, and provides an explicit generate / abstain decision.

Instead of always sending retrieved documents to an LLM, GATRAG uses an NLI cross-encoder to estimate whether the retrieved context actually supports the query.

Core idea: retrieve → validate context → generate only when the context is sufficient.

This can help RAG applications reduce unnecessary generation, expose an inspectable confidence signal, and avoid confidently answering when retrieval produced weak or irrelevant context.

Features

NLI-based context sufficiency classification.

Per-document relevance/sufficiency scoring.

Document filtering and ranking.

Optional whole-context ("packed") scoring.

Explicit SUFFICIENT, INSUFFICIENT, and UNCERTAIN results.

Simple boolean generation gate through validate_context_simple().

Batch validation through batch_validate().

Supports plain strings, SimpleDocument, and document-like objects exposing page_content and optional metadata.

Configurable score thresholds, uncertainty margins, model, device, batching, context length, and ranking limits.

Optional conservative fallback when model inference fails.

Compatible with CPU and CUDA environments.

Small public API with typed configuration and inspectable results.

Installation

Basic installation

pip install gatrag-context-classifier

Model-enabled installation

The default classifier loads an NLI model through sentence-transformers. Install the model extra:

pip install "gatrag[model]"

Or with uv:

uv add "gatrag[model]"

The default model is:

cross-encoder/nli-deberta-v3-small

The model is loaded automatically when ContextSufficientClassifier is created without a custom model.