3.9 KiB
3.9 KiB
Django AI Analyst - README
This package provides a Django application that enables AI-powered analysis of Django models through natural language prompts. The AI agent can analyze model structures, relationships, and data to provide insights in JSON format.
Features
- Natural language prompt processing for model analysis
- Support for various types of insights:
- Count queries (e.g., "How many cars do we have?")
- Relationship analysis between models
- Performance optimization suggestions
- Statistical analysis of model fields
- General model structure analysis
- Dealer-specific data access controls
- Caching mechanism for improved performance
- Visualization data generation for frontend display
- Comprehensive test suite
Installation
- Add 'ai_analyst' to your INSTALLED_APPS setting:
INSTALLED_APPS = [
...
'ai_analyst',
]
- Include the ai_analyst URLconf in your project urls.py:
path('api/ai/', include('ai_analyst.urls')),
- Run migrations to create the AnalysisCache model:
python manage.py makemigrations ai_analyst
python manage.py migrate
Usage
Send POST requests to the /api/ai/analyze/ endpoint with a JSON body containing:
{
"prompt": "How many cars do we have?",
"dealer_id": 1 // Optional, for dealer-specific queries
}
The response will be a JSON object with insights based on the prompt:
{
"status": "success",
"request_id": "a1b2c3d4",
"timestamp": "2025-05-25T23:21:56Z",
"prompt": "How many cars do we have?",
"insights": [
{
"type": "count_analysis",
"results": [
{
"model": "Car",
"count": 42,
"filters_applied": {}
}
],
"visualization_data": {
"chart_type": "bar",
"labels": ["Car"],
"data": [42]
}
}
]
}
Customization
Cache Duration
You can customize the cache duration by setting the CACHE_DURATION class variable in the ModelAnalystView class:
# In your settings.py
AI_ANALYST_CACHE_DURATION = 7200 # 2 hours in seconds
# Then in views.py
class ModelAnalystView(View):
CACHE_DURATION = getattr(settings, 'AI_ANALYST_CACHE_DURATION', 3600)
# ...
Permission Logic
The _check_permissions method in ModelAnalystView can be customized to match your application's permission model:
def _check_permissions(self, user, dealer_id):
# Your custom permission logic here
return user.has_perm('ai_analyst.can_analyze_models')
Example Prompts
- "How many cars do we have?"
- "Show relationship between User and Order"
- "What is the average price of products?"
- "Count active users"
- "Identify performance issues in the Order model"
- "Show maximum age of customers"
Frontend Integration
The JSON responses include visualization_data that can be used with charting libraries like Chart.js:
// Example with Chart.js
fetch('/api/ai/analyze/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
prompt: 'How many cars do we have?',
dealer_id: 1
}),
})
.then(response => response.json())
.then(data => {
if (data.status === 'success' && data.insights.length > 0) {
const insight = data.insights[0];
const vizData = insight.visualization_data;
const ctx = document.getElementById('insightChart').getContext('2d');
new Chart(ctx, {
type: vizData.chart_type,
data: {
labels: vizData.labels,
datasets: [{
label: insight.type,
data: vizData.data,
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)'
],
borderWidth: 1
}]
}
});
}
});