Metadata-Version: 2.4
Name: aifw
Version: 0.3.0
Summary: Django AI Services Framework — DB-driven LLM provider, model & usage management
Project-URL: Homepage, https://github.com/achimdehnert/platform
Project-URL: Repository, https://github.com/achimdehnert/platform/tree/main/packages/aifw
Author-email: Achim Dehnert <achim@dehnert.com>
License: MIT
Keywords: ai,ai-services,anthropic,django,litellm,llm,openai
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: Django :: 5.0
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
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Requires-Python: >=3.11
Requires-Dist: asgiref>=3.7
Requires-Dist: django<6.0,>=5.0
Requires-Dist: litellm>=1.30
Requires-Dist: tenacity>=8.2
Provides-Extra: dev
Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
Requires-Dist: pytest-django>=4.8; extra == 'dev'
Requires-Dist: pytest-mock>=3.12; extra == 'dev'
Requires-Dist: pytest>=8.0; extra == 'dev'
Provides-Extra: streaming
Requires-Dist: litellm[streaming]>=1.30; extra == 'streaming'
Provides-Extra: testing
Requires-Dist: pytest-asyncio>=0.23; extra == 'testing'
Requires-Dist: pytest-django>=4.8; extra == 'testing'
Requires-Dist: pytest-mock>=3.12; extra == 'testing'
Requires-Dist: pytest>=8.0; extra == 'testing'
Description-Content-Type: text/markdown

# aifw — Django AI Services Framework

DB-driven LLM provider, model & usage management for Django projects.

## Installation

```bash
pip install aifw
```

## Quick Start

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

# Run migrations
python manage.py migrate aifw
```

```python
# Use in views, tasks, management commands
from aifw.service import sync_completion, LLMResult

result: LLMResult = sync_completion(
    action_code="story_writing",
    messages=[{"role": "user", "content": "Write a short story about a dragon."}],
)

if result.success:
    print(result.content)
```

## Features

- **DB-driven model routing** — swap LLM providers/models via Django Admin, zero code changes
- **Multi-provider** — OpenAI, Anthropic, Google, Ollama, any LiteLLM-compatible provider
- **Async & sync** — `completion()` (async), `sync_completion()` (sync), `completion_with_fallback()`
- **Usage logging** — automatic token & cost tracking per action type
- **Fallback models** — configure primary + fallback model per action type

## Models

- `LLMProvider` — provider config (API key env var, base URL)
- `LLMModel` — model config (max tokens, cost per million tokens)
- `AIActionType` — action → model mapping with fallback
- `AIUsageLog` — token/cost/latency tracking per request

## Management Commands

```bash
python manage.py init_llm_config   # seed default providers & models
```
