Metadata-Version: 2.4
Name: django-agent-governance
Version: 0.1.0
Summary: AI governance and PII protection for Django — decorators, audit logs, and LLM-agnostic proxy.
Project-URL: Homepage, https://github.com/vaultagent/django-agent-governance
Project-URL: Documentation, https://vaultagent.github.io/django-agent-governance
Project-URL: Repository, https://github.com/vaultagent/django-agent-governance
Project-URL: Issues, https://github.com/vaultagent/django-agent-governance/issues
Project-URL: Changelog, https://github.com/vaultagent/django-agent-governance/blob/main/CHANGELOG.md
Author-email: VaultAgent <hello@vaultagent.dev>
License: MIT
License-File: LICENSE
Keywords: ai,audit,django,governance,langchain,llm,pii,redaction
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Django
Classifier: Framework :: Django :: 4.2
Classifier: Framework :: Django :: 5.0
Classifier: Framework :: Django :: 5.1
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Security
Requires-Python: >=3.10
Requires-Dist: django>=4.2
Requires-Dist: httpx>=0.27
Provides-Extra: anthropic
Requires-Dist: anthropic>=0.28; extra == 'anthropic'
Provides-Extra: dev
Requires-Dist: black>=24.0; extra == 'dev'
Requires-Dist: django-stubs[compatible-mypy]>=5.0; extra == 'dev'
Requires-Dist: django>=5.0; extra == 'dev'
Requires-Dist: factory-boy>=3.3; extra == 'dev'
Requires-Dist: mkdocs-material>=9.5; extra == 'dev'
Requires-Dist: mkdocs>=1.6; extra == 'dev'
Requires-Dist: mkdocstrings[python]>=0.25; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pre-commit>=3.7; extra == 'dev'
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest-cov>=5.0; extra == 'dev'
Requires-Dist: pytest-django>=4.8; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Requires-Dist: responses>=0.25; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: openai
Requires-Dist: openai>=1.30; extra == 'openai'
Provides-Extra: presidio
Requires-Dist: presidio-analyzer>=2.2; extra == 'presidio'
Requires-Dist: presidio-anonymizer>=2.2; extra == 'presidio'
Requires-Dist: spacy>=3.7; extra == 'presidio'
Description-Content-Type: text/markdown

# django-agent-governance

<p align="center">
  <img src="https://img.shields.io/pypi/v/django-agent-governance?style=for-the-badge&logo=pypi" />
  <img src="https://img.shields.io/pypi/pyversions/django-agent-governance?style=for-the-badge" />
  <img src="https://img.shields.io/github/actions/workflow/status/vaultagent/django-agent-governance/ci.yml?style=for-the-badge&label=CI" />
  <img src="https://img.shields.io/codecov/c/github/vaultagent/django-agent-governance?style=for-the-badge" />
  <img src="https://img.shields.io/pypi/l/django-agent-governance?style=for-the-badge" />
</p>

> **AI governance and PII protection for Django — plug in, don't rip out.**  
> Like `django-allauth` for AI security. Add decorators, audit logs, and a unified LLM proxy to any Django project in minutes.

---

## Features

| Feature | Description |
|---|---|
| `@redact_pii` | Strip emails, phones, SSNs, credit cards from any view or function |
| `@audit_llm_call` | Automatically log every LLM prompt, response, token count, and latency |
| **LLMProxy** | One interface for Ollama, OpenAI, Anthropic, and Gemini — swap with one config line |
| **AgentAdmin** | Django Admin dashboard with token cost estimates, PII hit rates, and latency charts |
| **PIIRedactionMiddleware** | Auto-redact all incoming request bodies without touching your views |
| **TokenBudget** | Per-user/per-app monthly token limits with automatic enforcement |

---

## Install

**Lightweight (regex PII engine — no extra downloads):**
```bash
pip install django-agent-governance
```

**Enterprise (Presidio NLP engine — ~500MB spaCy model):**
```bash
pip install django-agent-governance[presidio]
python -m spacy download en_core_web_lg
```

**With cloud LLM SDKs:**
```bash
pip install django-agent-governance[openai,anthropic]
```

---

## Quick Start

### 1. Add to `INSTALLED_APPS`

```python
# settings.py
INSTALLED_APPS = [
    ...
    "agent_governance",
]

AGENT_GOVERNANCE = {
    "LLM_BACKEND": "ollama",            # Default: local Ollama. No API key needed.
    "OLLAMA_BASE_URL": "http://localhost:11434",
    "OLLAMA_MODEL": "llama3.2:1b",
    # Uncomment to use cloud:
    # "LLM_BACKEND": "openai",
    # "OPENAI_API_KEY": env("OPENAI_API_KEY"),
}
```

### 2. Run migrations

```bash
python manage.py migrate agent_governance
```

### 3. Use the decorators

```python
from agent_governance.decorators import redact_pii, audit_llm_call
from agent_governance.proxy import LLMProxy

proxy = LLMProxy()

@redact_pii
@audit_llm_call
def chat_view(request):
    # request body is already PII-free ✅
    prompt = request.POST.get("message", "")
    response = proxy.chat(prompt)
    return JsonResponse({"reply": response.content})
```

### 4. View the Admin dashboard

Visit `/admin/agent_governance/` to see your LLM cost tracker, PII hit rates, and response quality.

---

## Configuration Reference

```python
AGENT_GOVERNANCE = {
    # ── LLM Backend ───────────────────────────────────────────────
    "LLM_BACKEND": "ollama",       # "ollama" | "openai" | "anthropic" | "gemini"

    # ── Ollama (default — local, free) ────────────────────────────
    "OLLAMA_BASE_URL": "http://localhost:11434",
    "OLLAMA_MODEL": "llama3.2:1b",

    # ── OpenAI ────────────────────────────────────────────────────
    "OPENAI_API_KEY": "",          # or set env var OPENAI_API_KEY
    "OPENAI_MODEL": "gpt-4o-mini",

    # ── Anthropic ─────────────────────────────────────────────────
    "ANTHROPIC_API_KEY": "",       # or set env var ANTHROPIC_API_KEY
    "ANTHROPIC_MODEL": "claude-3-haiku-20240307",

    # ── Google Gemini ─────────────────────────────────────────────
    "GEMINI_API_KEY": "",          # or set env var GEMINI_API_KEY
    "GEMINI_MODEL": "gemini-1.5-flash",

    # ── PII Engine ────────────────────────────────────────────────
    "PII_ENGINE": "regex",         # "regex" (default) | "presidio" (install [presidio] extra)
    "PII_ENTITIES": None,          # None = all; or ["EMAIL_ADDRESS", "PHONE_NUMBER"]

    # ── Governance ────────────────────────────────────────────────
    "TOKEN_BUDGET_ENABLED": False,
    "AUDIT_LOG_RETENTION_DAYS": 90,
}
```

---

## LLM-Agnostic Proxy

Switch providers without changing a line of your application code:

```python
from agent_governance.proxy import LLMProxy

proxy = LLMProxy()

# Talks to Ollama by default
response = proxy.chat("Summarize this document.")

# Override per-call
response = proxy.chat("Summarize this.", backend="anthropic", model="claude-3-opus-20240229")

print(response.content)         # The completion text
print(response.input_tokens)    # Token counts
print(response.latency_ms)      # Latency
```

---

## Supported PII Entities (Regex Engine)

| Entity | Example |
|---|---|
| `EMAIL_ADDRESS` | `user@example.com` |
| `PHONE_NUMBER` | `(555) 867-5309` |
| `CREDIT_CARD` | `4111111111111111` |
| `US_SSN` | `123-45-6789` |
| `IP_ADDRESS` | `192.168.1.100` |
| `URL` | `https://internal-server/api` |
| `UK_NIN` | `AB123456C` |

Upgrade to `[presidio]` for: `PERSON`, `ORG`, `LOCATION`, `DATE_TIME`, `NRP`, and 50+ more.

---

## Contributing

See [CONTRIBUTING.md](CONTRIBUTING.md). PRs welcome!

## License

[MIT](LICENSE)
