django admin Groups reorg, Frontend udpates for site settings, #Migration runs
This commit is contained in:
@@ -44,73 +44,59 @@ class AIProcessor:
|
||||
|
||||
def __init__(self, account=None):
|
||||
"""
|
||||
Initialize AIProcessor. Can optionally load API keys and model from IntegrationSettings for account.
|
||||
Initialize AIProcessor.
|
||||
|
||||
API keys come from IntegrationProvider (centralized, platform-wide).
|
||||
Model comes from AIModelConfig (is_default=True).
|
||||
AI parameters come from SystemAISettings with AccountSettings overrides.
|
||||
|
||||
Args:
|
||||
account: Optional account object to load API keys and model from IntegrationSettings
|
||||
account: Optional account object for per-account setting overrides
|
||||
"""
|
||||
self.account = account
|
||||
|
||||
# Get API keys from IntegrationSettings if account provided, else fallback to Django settings
|
||||
self.openai_api_key = self._get_api_key('openai', self.account)
|
||||
self.runware_api_key = self._get_api_key('runware', self.account)
|
||||
# Get model from IntegrationSettings if account provided, else fallback to Django settings
|
||||
self.default_model = self._get_model('openai', self.account)
|
||||
# Get API keys from IntegrationProvider (centralized)
|
||||
self.openai_api_key = self._get_api_key('openai')
|
||||
self.runware_api_key = self._get_api_key('runware')
|
||||
# Get model from AIModelConfig (is_default=True)
|
||||
self.default_model = self._get_model('openai')
|
||||
|
||||
# Use global model rates
|
||||
self.model_rates = MODEL_RATES
|
||||
self.image_model_rates = IMAGE_MODEL_RATES
|
||||
|
||||
def _get_api_key(self, integration_type: str, account=None) -> Optional[str]:
|
||||
"""Get API key from IntegrationSettings or Django settings"""
|
||||
if account:
|
||||
try:
|
||||
from igny8_core.modules.system.models import IntegrationSettings
|
||||
settings_obj = IntegrationSettings.objects.filter(
|
||||
integration_type=integration_type,
|
||||
account=account,
|
||||
is_active=True
|
||||
).first()
|
||||
if settings_obj and settings_obj.config:
|
||||
api_key = settings_obj.config.get('apiKey')
|
||||
if api_key:
|
||||
logger.info(f"Loaded {integration_type} API key from IntegrationSettings for account {account.id}")
|
||||
return api_key
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load {integration_type} API key from IntegrationSettings: {e}", exc_info=True)
|
||||
def _get_api_key(self, integration_type: str) -> Optional[str]:
|
||||
"""Get API key from IntegrationProvider (centralized)"""
|
||||
try:
|
||||
from igny8_core.ai.model_registry import ModelRegistry
|
||||
api_key = ModelRegistry.get_api_key(integration_type)
|
||||
if api_key:
|
||||
logger.debug(f"Loaded {integration_type} API key from IntegrationProvider")
|
||||
return api_key
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load {integration_type} API key from IntegrationProvider: {e}")
|
||||
|
||||
# Fallback to Django settings
|
||||
# Fallback to Django settings (for backward compatibility)
|
||||
if integration_type == 'openai':
|
||||
return getattr(settings, 'OPENAI_API_KEY', None)
|
||||
elif integration_type == 'runware':
|
||||
return getattr(settings, 'RUNWARE_API_KEY', None)
|
||||
return None
|
||||
|
||||
def _get_model(self, integration_type: str, account=None) -> str:
|
||||
"""Get model from IntegrationSettings or Django settings"""
|
||||
if account and integration_type == 'openai':
|
||||
try:
|
||||
from igny8_core.modules.system.models import IntegrationSettings
|
||||
settings_obj = IntegrationSettings.objects.filter(
|
||||
integration_type=integration_type,
|
||||
account=account,
|
||||
is_active=True
|
||||
).first()
|
||||
if settings_obj and settings_obj.config:
|
||||
model = settings_obj.config.get('model')
|
||||
if model:
|
||||
# Validate model is in our supported list
|
||||
if model in MODEL_RATES:
|
||||
logger.info(f"Using model '{model}' from IntegrationSettings for account {account.id}")
|
||||
return model
|
||||
else:
|
||||
logger.warning(f"Model '{model}' from IntegrationSettings not in supported models, using default")
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load {integration_type} model from IntegrationSettings: {e}", exc_info=True)
|
||||
def _get_model(self, integration_type: str) -> str:
|
||||
"""Get default model from AIModelConfig (is_default=True)"""
|
||||
try:
|
||||
from igny8_core.ai.model_registry import ModelRegistry
|
||||
default_model = ModelRegistry.get_default_model('text')
|
||||
if default_model:
|
||||
logger.debug(f"Using model '{default_model}' from AIModelConfig")
|
||||
return default_model
|
||||
except Exception as e:
|
||||
logger.warning(f"Could not load default model from AIModelConfig: {e}")
|
||||
|
||||
# Fallback to Django settings or default
|
||||
default_model = getattr(settings, 'DEFAULT_AI_MODEL', 'gpt-4.1')
|
||||
logger.info(f"Using default model '{default_model}' (no IntegrationSettings found)")
|
||||
logger.info(f"Using fallback model '{default_model}'")
|
||||
return default_model
|
||||
|
||||
def _call_openai(
|
||||
|
||||
Reference in New Issue
Block a user