Overview
Inventario integrates with OpenAI’s GPT models to provide AI-powered business insights, recommendations, and analysis. These features help business owners make data-driven decisions based on their sales, inventory, and financial data.OpenAI Configuration
Settings
OpenAI integration is configured insettings.py:
inventario/settings.py
Environment Variables
Add your OpenAI API key to.env:
.env
Getting an OpenAI API Key
Create OpenAI Account
Sign up at platform.openai.com and verify your email.
Add Payment Method
Go to Settings > Billing and add a payment method.OpenAI requires prepaid credits for API usage.
Generate API Key
Navigate to API Keys and click Create new secret key.Copy the key immediately (it won’t be shown again).
AI Service Architecture
Inventario implements two main AI services:1. Dashboard Insights Generator
Location:applications/cuentas/services/ia_openai.py
Generates actionable recommendations based on business metrics:
applications/cuentas/services/ia_openai.py
2. Advanced Insights Interpreter
Location:applications/cuentas/services/openai_service.py
Converts raw metrics into human-readable recommendations:
applications/cuentas/services/openai_service.py
AI Features in Action
Dashboard AI Insights
The main dashboard (/dashboard/) displays AI-generated insights based on:
- Sales performance: Daily, weekly, monthly revenue trends
- Inventory levels: Low stock alerts, overstock warnings
- Profit margins: Product profitability analysis
- Customer behavior: Top customers, purchase patterns
- Financial health: Cash flow, expenses vs. revenue
Example Usage
applications/cuentas/views.py
Sample AI Output
Model Selection
Inventario uses GPT-4o-mini for cost-effective AI generation:Why GPT-4o-mini?
Cost-Effective
~60% cheaper than GPT-4, suitable for frequent dashboard updates
Fast Response
Lower latency for real-time insights generation
Sufficient Quality
More than adequate for business recommendations and summaries
Token Efficient
Max 300 tokens keeps responses concise and costs low
Alternative Models
You can modify the model in the service files:AI Parameters
Temperature
- Lower (0.0-0.3): More deterministic, consistent responses
- Current (0.4): Balanced creativity with consistency
- Higher (0.7-1.0): More creative but less predictable
Max Tokens
Cost Management
Token Usage Estimation
Per AI insight generation:- Input tokens: ~200-400 (context + prompt)
- Output tokens: 300 (max_tokens limit)
- Total: ~500-700 tokens per request
Pricing (GPT-4o-mini)
- Input: $0.15 / 1M tokens
- Output: $0.60 / 1M tokens
Cost Optimization Tips
Cache Results
Cache AI insights for 15-30 minutes to avoid regenerating on every page load:
Conditional Generation
Only generate insights when there’s new data:
Batch Processing
Generate insights for all users once per day instead of real-time:
User Preferences
Let users enable/disable AI features:
Extending AI Features
Adding New AI Functions
Create new AI-powered features by following the existing pattern:applications/productos/ai_service.py
Use Cases for AI Expansion
Price Optimization
Price Optimization
Use AI to suggest optimal pricing based on:
- Historical sales data
- Competitor pricing
- Seasonality
- Stock levels
Customer Segmentation
Customer Segmentation
Analyze customer behavior and segment for targeted campaigns:
Sales Forecasting
Sales Forecasting
Predict future sales based on historical patterns:
Natural Language Queries
Natural Language Queries
Let users ask questions in plain language:
Error Handling
Graceful Fallbacks
Always handle API failures gracefully:Common Errors
Invalid API Key
Invalid API Key
OPENAI_API_KEY in .env is correct and active.Rate Limit Exceeded
Rate Limit Exceeded
Insufficient Quota
Insufficient Quota
Testing AI Features
Manual Testing
Test AI generation from Django shell:Unit Tests
Create tests for AI services:tests/test_ia_service.py
Next Steps
Reports
See AI insights in reports and analytics
Notifications
Configure email and SMS alerts