feat(migrations): Rename indexes and update global integration settings fields for improved clarity and functionality
feat(admin): Add API monitoring, debug console, and system health templates for enhanced admin interface docs: Add AI system cleanup summary and audit report detailing architecture, token management, and recommendations docs: Introduce credits and tokens system guide outlining configuration, data flow, and monitoring strategies
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
"""
|
||||
AI Settings - Centralized model configurations and limits
|
||||
Uses IntegrationSettings only - no hardcoded defaults or fallbacks.
|
||||
Uses global settings with optional per-account overrides.
|
||||
"""
|
||||
from typing import Dict, Any
|
||||
import logging
|
||||
@@ -19,18 +19,23 @@ FUNCTION_ALIASES = {
|
||||
|
||||
def get_model_config(function_name: str, account) -> Dict[str, Any]:
|
||||
"""
|
||||
Get model configuration from IntegrationSettings.
|
||||
Falls back to system account (aws-admin) if user account doesn't have settings.
|
||||
Get model configuration for AI function.
|
||||
|
||||
Architecture:
|
||||
- API keys: ALWAYS from GlobalIntegrationSettings (platform-wide)
|
||||
- Model/params: From IntegrationSettings if account has override, else from global
|
||||
- Free plan: Cannot override, uses global defaults
|
||||
- Starter/Growth/Scale: Can override model, temperature, max_tokens, etc.
|
||||
|
||||
Args:
|
||||
function_name: Name of the AI function
|
||||
account: Account instance (required)
|
||||
|
||||
Returns:
|
||||
dict: Model configuration with 'model', 'max_tokens', 'temperature'
|
||||
dict: Model configuration with 'model', 'max_tokens', 'temperature', 'api_key'
|
||||
|
||||
Raises:
|
||||
ValueError: If account not provided or IntegrationSettings not configured
|
||||
ValueError: If account not provided or settings not configured
|
||||
"""
|
||||
if not account:
|
||||
raise ValueError("Account is required for model configuration")
|
||||
@@ -38,53 +43,57 @@ def get_model_config(function_name: str, account) -> Dict[str, Any]:
|
||||
# Resolve function alias
|
||||
actual_name = FUNCTION_ALIASES.get(function_name, function_name)
|
||||
|
||||
# Get IntegrationSettings for OpenAI - try user account first
|
||||
integration_settings = None
|
||||
try:
|
||||
from igny8_core.modules.system.global_settings_models import GlobalIntegrationSettings
|
||||
from igny8_core.modules.system.models import IntegrationSettings
|
||||
integration_settings = IntegrationSettings.objects.filter(
|
||||
integration_type='openai',
|
||||
account=account,
|
||||
is_active=True
|
||||
).first()
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load OpenAI settings for account {account.id}: {e}")
|
||||
|
||||
# Fallback to system account (aws-admin, default-account, or default)
|
||||
if not integration_settings:
|
||||
logger.info(f"No OpenAI settings for account {account.id}, falling back to system account")
|
||||
|
||||
# Get global settings (for API keys and defaults)
|
||||
global_settings = GlobalIntegrationSettings.get_instance()
|
||||
|
||||
if not global_settings.openai_api_key:
|
||||
raise ValueError(
|
||||
"Platform OpenAI API key not configured. "
|
||||
"Please configure GlobalIntegrationSettings in Django admin."
|
||||
)
|
||||
|
||||
# Start with global defaults
|
||||
model = global_settings.openai_model
|
||||
temperature = global_settings.openai_temperature
|
||||
max_tokens = global_settings.openai_max_tokens
|
||||
api_key = global_settings.openai_api_key # ALWAYS from global
|
||||
|
||||
# Check if account has overrides (only for Starter/Growth/Scale plans)
|
||||
# Free plan users cannot create IntegrationSettings records
|
||||
try:
|
||||
from igny8_core.auth.models import Account
|
||||
from igny8_core.modules.system.models import IntegrationSettings
|
||||
for slug in ['aws-admin', 'default-account', 'default']:
|
||||
system_account = Account.objects.filter(slug=slug).first()
|
||||
if system_account:
|
||||
integration_settings = IntegrationSettings.objects.filter(
|
||||
integration_type='openai',
|
||||
account=system_account,
|
||||
is_active=True
|
||||
).first()
|
||||
if integration_settings:
|
||||
logger.info(f"Using OpenAI settings from system account: {slug}")
|
||||
break
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load system account OpenAI settings: {e}")
|
||||
|
||||
# If still no settings found, raise error
|
||||
if not integration_settings:
|
||||
account_settings = IntegrationSettings.objects.get(
|
||||
account=account,
|
||||
integration_type='openai',
|
||||
is_active=True
|
||||
)
|
||||
|
||||
config = account_settings.config or {}
|
||||
|
||||
# Override model if specified (NULL = use global)
|
||||
if config.get('model'):
|
||||
model = config['model']
|
||||
|
||||
# Override temperature if specified
|
||||
if config.get('temperature') is not None:
|
||||
temperature = config['temperature']
|
||||
|
||||
# Override max_tokens if specified
|
||||
if config.get('max_tokens'):
|
||||
max_tokens = config['max_tokens']
|
||||
|
||||
except IntegrationSettings.DoesNotExist:
|
||||
# No account override, use global defaults (already set above)
|
||||
pass
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Could not load OpenAI settings for account {account.id}: {e}")
|
||||
raise ValueError(
|
||||
f"OpenAI IntegrationSettings not configured for account {account.id} or system account. "
|
||||
f"Please configure OpenAI settings in the integration page."
|
||||
)
|
||||
|
||||
config = integration_settings.config or {}
|
||||
|
||||
# Get model from config
|
||||
model = config.get('model')
|
||||
if not model:
|
||||
raise ValueError(
|
||||
f"Model not configured in IntegrationSettings for account {account.id}. "
|
||||
f"Please set 'model' in OpenAI integration settings."
|
||||
f"Could not load OpenAI configuration for account {account.id}. "
|
||||
f"Please configure GlobalIntegrationSettings."
|
||||
)
|
||||
|
||||
# Validate model is in our supported list (optional validation)
|
||||
@@ -96,15 +105,8 @@ def get_model_config(function_name: str, account) -> Dict[str, Any]:
|
||||
f"Supported models: {list(MODEL_RATES.keys())}"
|
||||
)
|
||||
except ImportError:
|
||||
# MODEL_RATES not available - skip validation
|
||||
pass
|
||||
|
||||
# Get max_tokens and temperature from config (standardized to 8192, 16384 for GPT-5.x)
|
||||
# GPT-5.1 and GPT-5.2 use 16384 max_tokens by default
|
||||
default_max_tokens = 16384 if model in ['gpt-5.1', 'gpt-5.2'] else 8192
|
||||
max_tokens = config.get('max_tokens', default_max_tokens)
|
||||
temperature = config.get('temperature', 0.7) # Reasonable default
|
||||
|
||||
# Build response format based on model (JSON mode for supported models)
|
||||
response_format = None
|
||||
try:
|
||||
@@ -112,7 +114,6 @@ def get_model_config(function_name: str, account) -> Dict[str, Any]:
|
||||
if model in JSON_MODE_MODELS:
|
||||
response_format = {"type": "json_object"}
|
||||
except ImportError:
|
||||
# JSON_MODE_MODELS not available - skip
|
||||
pass
|
||||
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user