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
OPENAI_API_KEY = os.environ.get('OPENAI_API_KEY', '')

Environment Variables

Add your OpenAI API key to .env:
.env
OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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:
OPENAI_API_KEY=sk-proj-your-key-here

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
from openai import OpenAI
from django.conf import settings

client = OpenAI(api_key=settings.OPENAI_API_KEY)

def generar_resumen_ia(contexto):
    """
    Genera recomendaciones accionables usando OpenAI
    """
    prompt = f"""
Eres un asistente experto en ventas, inventarios y finanzas para pequeños negocios.

Analiza los siguientes datos y genera recomendaciones CLARAS, CORTAS y ACCIONABLES.
No expliques teoría. Usa emojis moderados.

DATOS:
{contexto}

RESPONDE EN ESPAÑOL.
Formato:
- Recomendaciones
- Alertas
- Oportunidades
"""
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Eres un asesor de negocios."},
            {"role": "user", "content": prompt},
        ],
        temperature=0.4,
        max_tokens=300,
    )
    
    return response.choices[0].message.content

2. Advanced Insights Interpreter

Location: applications/cuentas/services/openai_service.py Converts raw metrics into human-readable recommendations:
applications/cuentas/services/openai_service.py
def interpretar_insights_ia(data):
    """
    Convierte métricas duras en recomendaciones humanas
    """
    prompt = f"""
Eres un asesor de negocios experto en ventas e inventarios.
Habla de forma clara, breve y accionable.

Datos del negocio:
Recomendaciones: {data['recomendaciones']}
Predicciones: {data['predicciones']}
Alertas: {data['alertas']}

Genera:
1. Un resumen ejecutivo (máx 3 líneas)
2. 3 acciones concretas para hoy
3. 1 advertencia crítica si aplica
"""
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Eres un gerente comercial experto."},
            {"role": "user", "content": prompt},
        ],
        temperature=0.4,
        max_tokens=300,
    )
    
    return response.choices[0].message.content

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
from applications.cuentas.services.ia_openai import generar_resumen_ia

# Prepare business context
contexto = f"""
Ventas del día: ${ventas_hoy}
Productos con bajo stock: {productos_bajo_stock}
Clientes activos: {clientes_activos}
Ganancia del mes: ${ganancia_mes}
"""

# Generate AI insights
insights = generar_resumen_ia(contexto)

Sample AI Output

📊 Recomendaciones:
- Aumenta el pedido de "Producto X" - se está vendiendo 3x más rápido
- Considera descuentos en "Producto Y" - lleva 45 días sin rotación
- Tu margen de ganancia subió 12% vs. mes pasado ¡Sigue así!

⚠️ Alertas:
- 5 productos con stock crítico (menos de 10 unidades)
- Gastos operativos aumentaron 8% esta semana

💡 Oportunidades:
- Los clientes de la zona norte compraron 40% más - considera promociones
- Jueves y viernes son tus mejores días - optimiza inventario para esos días

Model Selection

Inventario uses GPT-4o-mini for cost-effective AI generation:
model="gpt-4o-mini"

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:
model="gpt-4o"  # Higher quality, higher cost

AI Parameters

Temperature

temperature=0.4
  • 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

max_tokens=300
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.00020.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:
from django.core.cache import cache

cache_key = f'ai_insights_{user.id}'
insights = cache.get(cache_key)

if not insights:
    insights = generar_resumen_ia(contexto)
    cache.set(cache_key, insights, 900)  # 15 min

Conditional Generation

Only generate insights when there’s new data:
if ventas_nuevas or inventario_actualizado:
    insights = generar_resumen_ia(contexto)

Batch Processing

Generate insights for all users once per day instead of real-time:
# Django management command
python manage.py generate_daily_insights

User Preferences

Let users enable/disable AI features:
if user.configuracion.ai_enabled:
    insights = generar_resumen_ia(contexto)

Extending AI Features

Adding New AI Functions

Create new AI-powered features by following the existing pattern:
applications/productos/ai_service.py
from openai import OpenAI
from django.conf import settings

client = OpenAI(api_key=settings.OPENAI_API_KEY)

def optimizar_inventario(productos):
    """
    Genera recomendaciones de inventario usando IA
    """
    datos_productos = "\n".join([
        f"- {p.nombre}: {p.cantidad} unidades, {p.ventas_mes} ventas/mes"
        for p in productos
    ])
    
    prompt = f"""
Analiza estos productos y recomienda cuántas unidades comprar:

{datos_productos}

Considera:
- Velocidad de rotación
- Stock de seguridad
- Tendencias estacionales

Responde en formato:
Producto | Cantidad Sugerida | Razón
"""
    
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Eres un experto en gestión de inventarios."},
            {"role": "user", "content": prompt}
        ],
        temperature=0.3,  # More deterministic for inventory
        max_tokens=500
    )
    
    return response.choices[0].message.content

Use Cases for AI Expansion

Use AI to suggest optimal pricing based on:
  • Historical sales data
  • Competitor pricing
  • Seasonality
  • Stock levels
def sugerir_precios(producto):
    # Analyze sales velocity, competition, margins
    # Return AI-suggested pricing strategy
Analyze customer behavior and segment for targeted campaigns:
def segmentar_clientes(clientes):
    # VIP customers, at-risk customers, new customers
    # Return segments with recommended actions
Predict future sales based on historical patterns:
def predecir_ventas(producto, dias=30):
    # Analyze trends, seasonality, events
    # Return predicted sales for next N days
Let users ask questions in plain language:
def consultar_negocio(pregunta, contexto_negocio):
    # "¿Cuál es mi producto más rentable?"
    # "¿Qué debo comprar esta semana?"
    # Return natural language answer

Error Handling

Graceful Fallbacks

Always handle API failures gracefully:
def generar_resumen_ia(contexto):
    try:
        client = OpenAI(api_key=settings.OPENAI_API_KEY)
        response = client.chat.completions.create(...)
        return response.choices[0].message.content
    except Exception as e:
        print(f"Error en OpenAI: {e}")
        return """
        📊 Insights no disponibles temporalmente.
        Revisa tu clave API o intenta más tarde.
        """

Common Errors

openai.AuthenticationError: Incorrect API key provided
Solution: Verify OPENAI_API_KEY in .env is correct and active.
openai.RateLimitError: Rate limit reached for requests
Solution: Implement caching and reduce request frequency. Consider upgrading OpenAI tier.
openai.InsufficientQuotaError: You exceeded your current quota
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:
python manage.py shell
>>> from applications.cuentas.services.ia_openai import generar_resumen_ia
>>> 
>>> contexto = """
... Ventas del día: $450,000
... Productos con bajo stock: 3
... Clientes activos: 25
... """
>>> 
>>> resultado = generar_resumen_ia(contexto)
>>> print(resultado)

Unit Tests

Create tests for AI services:
tests/test_ia_service.py
from django.test import TestCase
from unittest.mock import patch, MagicMock
from applications.cuentas.services.ia_openai import generar_resumen_ia

class IAServiceTest(TestCase):
    @patch('applications.cuentas.services.ia_openai.client')
    def test_generar_resumen(self, mock_client):
        # Mock OpenAI response
        mock_response = MagicMock()
        mock_response.choices[0].message.content = "Test insight"
        mock_client.chat.completions.create.return_value = mock_response
        
        # Test function
        resultado = generar_resumen_ia("contexto de prueba")
        
        # Assertions
        self.assertEqual(resultado, "Test insight")
        mock_client.chat.completions.create.assert_called_once()

Next Steps

Reports

See AI insights in reports and analytics

Notifications

Configure email and SMS alerts

Build docs developers (and LLMs) love