Skip to main content

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 in settings.py:
inventario/settings.py

Environment Variables

Add your OpenAI API key to .env:
.env
OpenAI API calls are charged per token. Monitor your usage in the OpenAI dashboard to avoid unexpected costs.

Getting an OpenAI API Key

1

Create OpenAI Account

Sign up at platform.openai.com and verify your email.
2

Add Payment Method

Go to Settings > Billing and add a payment method.OpenAI requires prepaid credits for API usage.
3

Generate API Key

Navigate to API Keys and click Create new secret key.Copy the key immediately (it won’t be shown again).
4

Add to Environment

Add the key to your .env file:

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
For business recommendations, 0.4 is ideal - professional yet not robotic.

Max Tokens

Limits response length to control costs. 300 tokens ≈ 225 words in Spanish.

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 per insight: ~0.0002−0.0002 - 0.0004 (less than $0.001)

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

Use AI to suggest optimal pricing based on:
  • Historical sales data
  • Competitor pricing
  • Seasonality
  • Stock levels
Analyze customer behavior and segment for targeted campaigns:
Predict future sales based on historical patterns:
Let users ask questions in plain language:

Error Handling

Graceful Fallbacks

Always handle API failures gracefully:

Common Errors

Solution: Verify OPENAI_API_KEY in .env is correct and active.
Solution: Implement caching and reduce request frequency. Consider upgrading OpenAI tier.
Solution: Add credits to your OpenAI account or switch to a free tier model temporarily.

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