Django app that adds AI-powered chat to Django admin interface
Project description
Django AI Admin Chat
A Django app that adds AI-powered chat functionality to Django admin interface. The chat assistant can answer questions about your Django models and database using natural language queries.
Features
- 🤖 AI-powered chat interface integrated into Django admin
- 🔍 Database search using SQL database chain
- 💾 Chat history tracking with session management
- 📊 Token usage monitoring for cost tracking
- 🎯 Model filtering - control which models are accessible to the AI
- 📝 SQL query logging - see generated SQL queries when using SQLDatabaseChain
- 🚀 Streaming responses via Server-Sent Events (SSE)
Installation
Development Installation
To install the package in development mode (editable):
pip install -e .
Production Installation
pip install django-ai-admin-chat
Configuration
1. Add to INSTALLED_APPS
Add django_ai_admin_chat to your INSTALLED_APPS in Django settings:
INSTALLED_APPS = [
# ... other apps
'django_ai_admin_chat',
]
2. Include URLs
Include the app URLs in your main urls.py:
from django.urls import include, path
urlpatterns = [
# ... other URL patterns
path('', include('django_ai_admin_chat.urls')),
]
3. Run Migrations
Create and apply database migrations:
python manage.py migrate
4. AI Configuration
Configure the admin chat integration in your Django settings:
# Required: AI Provider Configuration
DJANGO_AI_ADMIN_CHAT_PROVIDER = "openai"
DJANGO_AI_ADMIN_CHAT_API_KEY = "your-openai-api-key"
DJANGO_AI_ADMIN_CHAT_MODEL = "gpt-3.5-turbo" # or "gpt-4", "gpt-4-turbo", etc.
DJANGO_AI_ADMIN_CHAT_TEMPERATURE = 0.7
DJANGO_AI_ADMIN_CHAT_MAX_TOKENS = 500
# Search Configuration
DJANGO_AI_ADMIN_CHAT_SEARCH_TYPE = "sql_database_chain"
DJANGO_AI_ADMIN_CHAT_MAX_SEARCH_RESULTS = 10
# Optional: Model Filtering
DJANGO_AI_ADMIN_CHAT_ALLOWED_MODELS = [] # Empty list = all models allowed (except excluded)
DJANGO_AI_ADMIN_CHAT_EXCLUDED_MODELS = [] # Models to exclude from search
Search Types
The app uses a single search strategy:
sql_database_chain(default) - Uses LangChain SQLDatabaseChain to generate SQL queries and return database results directly in context. Best for complex queries and data analysis.
Usage
Admin Interface
Once configured, a chat button (💬) will appear in the bottom-right corner of your Django admin interface. Click it to open the chat panel and start asking questions about your data.
Chat History
All chat interactions are stored in the ChatHistory model, accessible via Django admin. Each entry includes:
- User query and AI response
- Search type used
- Search results and context
- Token usage (prompt, completion, total)
- Generated SQL query (if using SQLDatabaseChain)
- Session ID for conversation tracking
API Endpoints
The app provides the following endpoints (all require staff member authentication):
POST /admin-chat/echo/- Send a message and get a complete responseGET /admin-chat/history/- Get chat history for current sessionGET|POST /admin-chat/stream/- Stream chat response via Server-Sent EventsPOST /admin-chat/clear/- Clear chat history and start a new session
Requirements
- Python >= 3.10
- Django >= 4.2
- langchain >= 0.1.0
- langchain-community
- langchain-experimental
- langchain-openai >= 0.0.5
- openai >= 1.0.0
- SQLAlchemy
See pyproject.toml for complete dependency list.
Development
This package is currently in early development. Contributions are welcome!
License
MIT License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_ai_admin_chat-0.1.0.tar.gz.
File metadata
- Download URL: django_ai_admin_chat-0.1.0.tar.gz
- Upload date:
- Size: 20.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5a65a4691cf14597b587cb7e4e891475a1bdf5be95861e35c25eaebcc22ba6cf
|
|
| MD5 |
7bd766efaf006c5cc2d66d0201ab9555
|
|
| BLAKE2b-256 |
24b2c84d72d02a6179233363fafb86eb1eb7c795e2f1dfc0f7a45e71fe677598
|
File details
Details for the file django_ai_admin_chat-0.1.0-py3-none-any.whl.
File metadata
- Download URL: django_ai_admin_chat-0.1.0-py3-none-any.whl
- Upload date:
- Size: 24.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b44a5bcb332af6e854b7f2a9cbd7328d826472c3af8930e87f7b55045b219cd
|
|
| MD5 |
33eb9f93f9a812b16fea352021190d69
|
|
| BLAKE2b-256 |
8fa35cd63557c1ab097e3b5772f6fad7fecfe88a132905a6cf1dfc8d436b0417
|