Metadata-Version: 2.4
Name: rag-audit
Version: 0.1.1
Summary: CLI + library to audit and benchmark RAG pipelines
Project-URL: Homepage, https://github.com/andrey-pontes/rag-audit
Project-URL: Documentation, https://andrey-pontes.github.io/rag-audit
Project-URL: Repository, https://github.com/andrey-pontes/rag-audit
Project-URL: Bug Tracker, https://github.com/andrey-pontes/rag-audit/issues
License: MIT License
        
        Copyright (c) 2026 Andrey Pontes
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: audit,evaluation,llm,rag,retrieval
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.11
Requires-Dist: chromadb>=0.5
Requires-Dist: httpx>=0.27
Requires-Dist: langchain-anthropic>=0.1
Requires-Dist: langchain-openai>=0.1
Requires-Dist: langchain>=0.2
Requires-Dist: loguru>=0.7
Requires-Dist: pydantic>=2.0
Requires-Dist: rich>=13.0
Requires-Dist: typer>=0.12
Description-Content-Type: text/markdown

# rag-audit

CLI + library to audit and benchmark RAG pipelines. Detects hallucinations, measures retrieval quality, compares chunking strategies, and generates structured reports.

**[Documentation](https://andrey-pontes.github.io/rag-audit/)**

## Installation

```bash
pip install rag-audit
```

Or with [uv](https://docs.astral.sh/uv/):

```bash
uv add rag-audit
```

## Quickstart

**1. Create a pipeline config file (`pipeline.json`):**

```json
{
  "pipeline_id": "my-pipeline",
  "question": "What is the capital of France?",
  "answer": "Paris is the capital of France.",
  "contexts": [
    "Paris is the capital and largest city of France.",
    "France is a country in Western Europe."
  ],
  "relevant": [
    "Paris is the capital and largest city of France."
  ],
  "k": 2,
  "llm": {
    "provider": "openai",
    "model": "gpt-4o-mini"
  }
}
```

**2. Run the audit:**

```bash
export OPENAI_API_KEY=sk-...
rag-audit run pipeline.json -o result.json
```

**3. Generate a report:**

```bash
# Markdown (default)
rag-audit report result.json

# JSON
rag-audit report result.json --format json
```

## Config reference

| Field | Type | Description |
|---|---|---|
| `pipeline_id` | `string` | Identifier for the pipeline being audited |
| `question` | `string` | The question posed to the RAG pipeline |
| `answer` | `string` | The answer generated by the pipeline |
| `contexts` | `string[]` | Retrieved chunks, in rank order |
| `relevant` | `string[]` | Ground-truth relevant chunks (for retrieval metrics) |
| `k` | `int` | Number of top chunks to evaluate (default: `5`) |
| `llm.provider` | `"openai"` \| `"anthropic"` | LLM provider for the faithfulness judge |
| `llm.model` | `string` | Model name (e.g. `"gpt-4o-mini"`, `"claude-3-5-haiku-20241022"`) |

## Metrics

### Retrieval

| Metric | Description |
|---|---|
| **Precision@k** | Fraction of the top-k retrieved chunks that are relevant |
| **Recall@k** | Fraction of all relevant chunks that appear in the top-k |
| **MRR** | Mean Reciprocal Rank — how high the first relevant chunk ranks |

### Faithfulness

| Metric | Description |
|---|---|
| **Score** | 0.0–1.0 — how well the answer is grounded in the retrieved contexts |
| **Verdict** | `FAITHFUL` if score ≥ threshold (default `0.5`), otherwise `HALLUCINATION` |

## Python API

### Audit a pipeline

```python
from rag_audit.core.config import PipelineConfig, LLMConfig
from rag_audit.core.runner import AuditRunner
from rag_audit.report.renderer import ReportRenderer

config = PipelineConfig(
    pipeline_id="my-pipeline",
    question="What is the capital of France?",
    answer="Paris is the capital of France.",
    contexts=["Paris is the capital and largest city of France."],
    relevant=["Paris is the capital and largest city of France."],
    k=1,
    llm=LLMConfig(provider="openai", model="gpt-4o-mini"),
)

report = AuditRunner(config).run()
print(ReportRenderer().to_markdown(report))
```

### Compare chunking strategies

```python
from langchain_openai import OpenAIEmbeddings
from rag_audit.chunker import ChunkingEvaluator, FixedSizeChunker, RecursiveChunker, SemanticChunker

embeddings = OpenAIEmbeddings()
evaluator = ChunkingEvaluator(embeddings)

report = evaluator.evaluate(
    "Your long document text here...",
    {
        "fixed": FixedSizeChunker(chunk_size=500, overlap=50),
        "recursive": RecursiveChunker(chunk_size=500),
        "semantic": SemanticChunker(embeddings, similarity_threshold=0.8),
    },
)

print(f"Best strategy: {report.best_strategy}")
for s in report.strategies:
    print(f"  {s.strategy}: avg_cohesion={s.avg_cohesion:.3f}, chunks={s.chunk_count}")
```

### Use a vectorstore adapter

```python
from rag_audit.adapters import ChromaDBAdapter

adapter = ChromaDBAdapter("my-collection")
adapter.add(ids=["doc1"], texts=["Paris is in France."], embeddings=[[...]])
results = adapter.query(embedding=[...], k=1)
```

## Roadmap

- [x] CLI (`rag-audit run`, `rag-audit report`)
- [x] Hallucination detection (LLM-as-judge)
- [x] Retrieval metrics (Precision@k, Recall@k, MRR)
- [x] Structured audit reports (JSON + Markdown)
- [x] Chunking strategy benchmark (fixed-size vs recursive vs semantic)
- [x] Vectorstore adapters (ChromaDB — Pinecone and Qdrant coming soon)
- [x] Documentation (GitHub Pages)
- [ ] PyPI release

## Development

```bash
# Install dependencies
uv sync --group dev

# Run tests
uv run pytest

# Lint + format
uv run ruff check src/ tests/
uv run ruff format src/ tests/

# Type check
uv run mypy src/rag_audit

# Build docs locally
uv sync --group docs
uv run mkdocs serve
```
