AI MODELS & final updates - feat: Implement AI Model Configuration with dynamic pricing and REST API

- Added AIModelConfig model to manage AI model configurations in the database.
- Created serializers and views for AI model configurations, enabling read-only access via REST API.
- Implemented filtering capabilities for model type, provider, and default status in the API.
- Seeded initial data for text and image models, including pricing and capabilities.
- Updated Django Admin interface for managing AI models with enhanced features and bulk actions.
- Added validation methods for model and image size checks.
- Comprehensive migration created to establish the AIModelConfig model and seed initial data.
- Documented implementation and validation results in summary and report files.
This commit is contained in:
IGNY8 VPS (Salman)
2025-12-24 13:37:36 +00:00
parent 355b0ac897
commit 02d4f1fa46
9 changed files with 1531 additions and 28 deletions

View File

@@ -0,0 +1,264 @@
# Generated by Django 5.2.9 on 2025-12-24 01:20
import django.core.validators
import django.db.models.deletion
import simple_history.models
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models
def seed_ai_models(apps, schema_editor):
"""Seed AIModelConfig with data from constants.py"""
AIModelConfig = apps.get_model('billing', 'AIModelConfig')
# Text Models (from MODEL_RATES in constants.py)
text_models = [
{
'model_name': 'gpt-4o-mini',
'display_name': 'GPT-4o mini - Fast & Affordable',
'model_type': 'text',
'provider': 'openai',
'input_cost_per_1m': Decimal('0.1500'),
'output_cost_per_1m': Decimal('0.6000'),
'context_window': 128000,
'max_output_tokens': 16000,
'supports_json_mode': True,
'supports_vision': False,
'supports_function_calling': True,
'is_active': True,
'is_default': True, # Default text model
'sort_order': 1,
'description': 'Fast and cost-effective model for most tasks. Best balance of speed and quality.',
},
{
'model_name': 'gpt-4.1',
'display_name': 'GPT-4.1 - Legacy Model',
'model_type': 'text',
'provider': 'openai',
'input_cost_per_1m': Decimal('2.0000'),
'output_cost_per_1m': Decimal('8.0000'),
'context_window': 8192,
'max_output_tokens': 4096,
'supports_json_mode': False,
'supports_vision': False,
'supports_function_calling': False,
'is_active': True,
'is_default': False,
'sort_order': 10,
'description': 'Legacy GPT-4 model. Higher cost but reliable.',
},
{
'model_name': 'gpt-4o',
'display_name': 'GPT-4o - High Quality with Vision',
'model_type': 'text',
'provider': 'openai',
'input_cost_per_1m': Decimal('2.5000'),
'output_cost_per_1m': Decimal('10.0000'),
'context_window': 128000,
'max_output_tokens': 4096,
'supports_json_mode': True,
'supports_vision': True,
'supports_function_calling': True,
'is_active': True,
'is_default': False,
'sort_order': 5,
'description': 'Most capable GPT-4 variant with vision capabilities. Best for complex tasks.',
},
{
'model_name': 'gpt-5.1',
'display_name': 'GPT-5.1 - Advanced (16K context)',
'model_type': 'text',
'provider': 'openai',
'input_cost_per_1m': Decimal('1.2500'),
'output_cost_per_1m': Decimal('10.0000'),
'context_window': 16000,
'max_output_tokens': 16000,
'supports_json_mode': True,
'supports_vision': False,
'supports_function_calling': True,
'is_active': True,
'is_default': False,
'sort_order': 20,
'description': 'Advanced GPT-5 model with 16K context window.',
},
{
'model_name': 'gpt-5.2',
'display_name': 'GPT-5.2 - Most Advanced (16K context)',
'model_type': 'text',
'provider': 'openai',
'input_cost_per_1m': Decimal('1.7500'),
'output_cost_per_1m': Decimal('14.0000'),
'context_window': 16000,
'max_output_tokens': 16000,
'supports_json_mode': True,
'supports_vision': False,
'supports_function_calling': True,
'is_active': True,
'is_default': False,
'sort_order': 30,
'description': 'Most advanced GPT-5 variant. Highest quality output.',
},
]
# Image Models (from IMAGE_MODEL_RATES in constants.py)
image_models = [
{
'model_name': 'dall-e-3',
'display_name': 'DALL-E 3 - High Quality Images',
'model_type': 'image',
'provider': 'openai',
'cost_per_image': Decimal('0.0400'),
'valid_sizes': ['1024x1024', '1024x1792', '1792x1024'],
'supports_json_mode': False,
'supports_vision': False,
'supports_function_calling': False,
'is_active': True,
'is_default': True, # Default image model
'sort_order': 1,
'description': 'Latest DALL-E model with best quality and prompt adherence.',
},
{
'model_name': 'dall-e-2',
'display_name': 'DALL-E 2 - Standard Quality',
'model_type': 'image',
'provider': 'openai',
'cost_per_image': Decimal('0.0200'),
'valid_sizes': ['256x256', '512x512', '1024x1024'],
'supports_json_mode': False,
'supports_vision': False,
'supports_function_calling': False,
'is_active': True,
'is_default': False,
'sort_order': 10,
'description': 'Cost-effective image generation with good quality.',
},
{
'model_name': 'gpt-image-1',
'display_name': 'GPT Image 1 (Not compatible with OpenAI)',
'model_type': 'image',
'provider': 'openai',
'cost_per_image': Decimal('0.0420'),
'valid_sizes': ['1024x1024'],
'supports_json_mode': False,
'supports_vision': False,
'supports_function_calling': False,
'is_active': False, # Not valid for OpenAI endpoint
'is_default': False,
'sort_order': 20,
'description': 'Not compatible with OpenAI /v1/images/generations endpoint.',
},
{
'model_name': 'gpt-image-1-mini',
'display_name': 'GPT Image 1 Mini (Not compatible with OpenAI)',
'model_type': 'image',
'provider': 'openai',
'cost_per_image': Decimal('0.0110'),
'valid_sizes': ['1024x1024'],
'supports_json_mode': False,
'supports_vision': False,
'supports_function_calling': False,
'is_active': False, # Not valid for OpenAI endpoint
'is_default': False,
'sort_order': 30,
'description': 'Not compatible with OpenAI /v1/images/generations endpoint.',
},
]
# Create all models
for model_data in text_models + image_models:
AIModelConfig.objects.create(**model_data)
def reverse_seed(apps, schema_editor):
"""Remove seeded data"""
AIModelConfig = apps.get_model('billing', 'AIModelConfig')
AIModelConfig.objects.all().delete()
class Migration(migrations.Migration):
dependencies = [
('billing', '0019_populate_token_based_config'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='HistoricalAIModelConfig',
fields=[
('id', models.BigIntegerField(auto_created=True, blank=True, db_index=True, verbose_name='ID')),
('model_name', models.CharField(db_index=True, help_text="Model identifier used in API calls (e.g., 'gpt-4o-mini', 'dall-e-3')", max_length=100)),
('display_name', models.CharField(help_text="Human-readable name shown in UI (e.g., 'GPT-4o mini - Fast & Affordable')", max_length=200)),
('model_type', models.CharField(choices=[('text', 'Text Generation'), ('image', 'Image Generation'), ('embedding', 'Embedding')], db_index=True, help_text='Type of model - determines which pricing fields are used', max_length=20)),
('provider', models.CharField(choices=[('openai', 'OpenAI'), ('anthropic', 'Anthropic'), ('runware', 'Runware'), ('google', 'Google')], db_index=True, help_text='AI provider (OpenAI, Anthropic, etc.)', max_length=50)),
('input_cost_per_1m', models.DecimalField(blank=True, decimal_places=4, help_text='Cost per 1 million input tokens (USD). For text models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('output_cost_per_1m', models.DecimalField(blank=True, decimal_places=4, help_text='Cost per 1 million output tokens (USD). For text models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('context_window', models.IntegerField(blank=True, help_text='Maximum input tokens (context length). For text models only.', null=True, validators=[django.core.validators.MinValueValidator(1)])),
('max_output_tokens', models.IntegerField(blank=True, help_text='Maximum output tokens per request. For text models only.', null=True, validators=[django.core.validators.MinValueValidator(1)])),
('cost_per_image', models.DecimalField(blank=True, decimal_places=4, help_text='Fixed cost per image generation (USD). For image models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('valid_sizes', models.JSONField(blank=True, help_text='Array of valid image sizes (e.g., ["1024x1024", "1024x1792"]). For image models only.', null=True)),
('supports_json_mode', models.BooleanField(default=False, help_text='True for models with JSON response format support')),
('supports_vision', models.BooleanField(default=False, help_text='True for models that can analyze images')),
('supports_function_calling', models.BooleanField(default=False, help_text='True for models with function calling capability')),
('is_active', models.BooleanField(db_index=True, default=True, help_text='Enable/disable model without deleting')),
('is_default', models.BooleanField(db_index=True, default=False, help_text='Mark as default model for its type (only one per type)')),
('sort_order', models.IntegerField(default=0, help_text='Control order in dropdown lists (lower numbers first)')),
('description', models.TextField(blank=True, help_text='Admin notes about model usage, strengths, limitations')),
('release_date', models.DateField(blank=True, help_text='When model was released/added', null=True)),
('deprecation_date', models.DateField(blank=True, help_text='When model will be removed', null=True)),
('created_at', models.DateTimeField(blank=True, editable=False)),
('updated_at', models.DateTimeField(blank=True, editable=False)),
('history_id', models.AutoField(primary_key=True, serialize=False)),
('history_date', models.DateTimeField(db_index=True)),
('history_change_reason', models.CharField(max_length=100, null=True)),
('history_type', models.CharField(choices=[('+', 'Created'), ('~', 'Changed'), ('-', 'Deleted')], max_length=1)),
('history_user', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to=settings.AUTH_USER_MODEL)),
('updated_by', models.ForeignKey(blank=True, db_constraint=False, help_text='Admin who last updated', null=True, on_delete=django.db.models.deletion.DO_NOTHING, related_name='+', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'historical AI Model Configuration',
'verbose_name_plural': 'historical AI Model Configurations',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': ('history_date', 'history_id'),
},
bases=(simple_history.models.HistoricalChanges, models.Model),
),
migrations.CreateModel(
name='AIModelConfig',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('model_name', models.CharField(db_index=True, help_text="Model identifier used in API calls (e.g., 'gpt-4o-mini', 'dall-e-3')", max_length=100, unique=True)),
('display_name', models.CharField(help_text="Human-readable name shown in UI (e.g., 'GPT-4o mini - Fast & Affordable')", max_length=200)),
('model_type', models.CharField(choices=[('text', 'Text Generation'), ('image', 'Image Generation'), ('embedding', 'Embedding')], db_index=True, help_text='Type of model - determines which pricing fields are used', max_length=20)),
('provider', models.CharField(choices=[('openai', 'OpenAI'), ('anthropic', 'Anthropic'), ('runware', 'Runware'), ('google', 'Google')], db_index=True, help_text='AI provider (OpenAI, Anthropic, etc.)', max_length=50)),
('input_cost_per_1m', models.DecimalField(blank=True, decimal_places=4, help_text='Cost per 1 million input tokens (USD). For text models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('output_cost_per_1m', models.DecimalField(blank=True, decimal_places=4, help_text='Cost per 1 million output tokens (USD). For text models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('context_window', models.IntegerField(blank=True, help_text='Maximum input tokens (context length). For text models only.', null=True, validators=[django.core.validators.MinValueValidator(1)])),
('max_output_tokens', models.IntegerField(blank=True, help_text='Maximum output tokens per request. For text models only.', null=True, validators=[django.core.validators.MinValueValidator(1)])),
('cost_per_image', models.DecimalField(blank=True, decimal_places=4, help_text='Fixed cost per image generation (USD). For image models only.', max_digits=10, null=True, validators=[django.core.validators.MinValueValidator(Decimal('0.0001'))])),
('valid_sizes', models.JSONField(blank=True, help_text='Array of valid image sizes (e.g., ["1024x1024", "1024x1792"]). For image models only.', null=True)),
('supports_json_mode', models.BooleanField(default=False, help_text='True for models with JSON response format support')),
('supports_vision', models.BooleanField(default=False, help_text='True for models that can analyze images')),
('supports_function_calling', models.BooleanField(default=False, help_text='True for models with function calling capability')),
('is_active', models.BooleanField(db_index=True, default=True, help_text='Enable/disable model without deleting')),
('is_default', models.BooleanField(db_index=True, default=False, help_text='Mark as default model for its type (only one per type)')),
('sort_order', models.IntegerField(default=0, help_text='Control order in dropdown lists (lower numbers first)')),
('description', models.TextField(blank=True, help_text='Admin notes about model usage, strengths, limitations')),
('release_date', models.DateField(blank=True, help_text='When model was released/added', null=True)),
('deprecation_date', models.DateField(blank=True, help_text='When model will be removed', null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('updated_by', models.ForeignKey(blank=True, help_text='Admin who last updated', null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='ai_model_updates', to=settings.AUTH_USER_MODEL)),
],
options={
'verbose_name': 'AI Model Configuration',
'verbose_name_plural': 'AI Model Configurations',
'db_table': 'igny8_ai_model_config',
'ordering': ['model_type', 'sort_order', 'model_name'],
'indexes': [models.Index(fields=['model_type', 'is_active'], name='igny8_ai_mo_model_t_1eef71_idx'), models.Index(fields=['provider', 'is_active'], name='igny8_ai_mo_provide_fbda6c_idx'), models.Index(fields=['is_default', 'model_type'], name='igny8_ai_mo_is_defa_95bfb9_idx')],
},
),
# Seed initial model data
migrations.RunPython(seed_ai_models, reverse_seed),
]