ai fucntiosn adn otehr atuoamtion fixes
This commit is contained in:
@@ -156,7 +156,7 @@ class CreditService:
|
||||
raise CreditCalculationError(f"Error calculating credits: {e}")
|
||||
|
||||
@staticmethod
|
||||
def calculate_credits_from_tokens(operation_type, tokens_input, tokens_output):
|
||||
def calculate_credits_from_tokens(operation_type, tokens_input, tokens_output, model_name=None):
|
||||
"""
|
||||
Calculate credits from actual token usage using configured ratio.
|
||||
This is the ONLY way credits are calculated in the system.
|
||||
@@ -165,6 +165,7 @@ class CreditService:
|
||||
operation_type: Type of operation
|
||||
tokens_input: Input tokens used
|
||||
tokens_output: Output tokens used
|
||||
model_name: Optional AI model name (e.g., 'gpt-4o') for model-specific tokens_per_credit
|
||||
|
||||
Returns:
|
||||
int: Credits to deduct
|
||||
@@ -174,7 +175,7 @@ class CreditService:
|
||||
"""
|
||||
import logging
|
||||
import math
|
||||
from igny8_core.business.billing.models import CreditCostConfig, BillingConfiguration
|
||||
from igny8_core.business.billing.models import CreditCostConfig, BillingConfiguration, AIModelConfig
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -184,15 +185,32 @@ class CreditService:
|
||||
is_active=True
|
||||
).first()
|
||||
|
||||
if not config:
|
||||
# Use global billing config as fallback
|
||||
billing_config = BillingConfiguration.get_config()
|
||||
# Get tokens_per_credit from AIModelConfig if model_name provided
|
||||
billing_config = BillingConfiguration.get_config()
|
||||
tokens_per_credit = None
|
||||
|
||||
if model_name:
|
||||
# Try to get model-specific tokens_per_credit from AIModelConfig
|
||||
model_config = AIModelConfig.objects.filter(
|
||||
model_name=model_name,
|
||||
is_active=True
|
||||
).first()
|
||||
if model_config and model_config.tokens_per_credit:
|
||||
tokens_per_credit = model_config.tokens_per_credit
|
||||
logger.info(f"Using model-specific tokens_per_credit: {tokens_per_credit} for {model_name}")
|
||||
|
||||
# Fallback to global billing config
|
||||
if tokens_per_credit is None:
|
||||
tokens_per_credit = billing_config.default_tokens_per_credit
|
||||
logger.info(f"Using global default tokens_per_credit: {tokens_per_credit}")
|
||||
|
||||
if not config:
|
||||
min_credits = 1
|
||||
logger.info(f"No config for {operation_type}, using default: {tokens_per_credit} tokens/credit")
|
||||
logger.info(f"No config for {operation_type}, using default: {tokens_per_credit} tokens/credit, min 1 credit")
|
||||
else:
|
||||
tokens_per_credit = config.tokens_per_credit
|
||||
min_credits = config.min_credits
|
||||
# Use base_credits as minimum for this operation
|
||||
min_credits = config.base_credits
|
||||
logger.info(f"Config for {operation_type}: {tokens_per_credit} tokens/credit, min {min_credits} credits")
|
||||
|
||||
# Calculate total tokens
|
||||
total_tokens = (tokens_input or 0) + (tokens_output or 0)
|
||||
@@ -250,8 +268,8 @@ class CreditService:
|
||||
).first()
|
||||
|
||||
if config:
|
||||
# Use minimum credits as estimate for token-based operations
|
||||
required = config.min_credits
|
||||
# Use base_credits as estimate for token-based operations
|
||||
required = config.base_credits
|
||||
else:
|
||||
# Fallback to constants
|
||||
required = CREDIT_COSTS.get(operation_type, 1)
|
||||
@@ -377,10 +395,22 @@ class CreditService:
|
||||
metadata=metadata or {}
|
||||
)
|
||||
|
||||
# Convert site_id to Site instance if needed
|
||||
site_instance = None
|
||||
if site is not None:
|
||||
from igny8_core.auth.models import Site
|
||||
if isinstance(site, int):
|
||||
try:
|
||||
site_instance = Site.objects.get(id=site)
|
||||
except Site.DoesNotExist:
|
||||
logger.warning(f"Site with id {site} not found for credit usage log")
|
||||
else:
|
||||
site_instance = site
|
||||
|
||||
# Create CreditUsageLog
|
||||
CreditUsageLog.objects.create(
|
||||
account=account,
|
||||
site=site,
|
||||
site=site_instance,
|
||||
operation_type=operation_type,
|
||||
credits_used=amount,
|
||||
cost_usd=cost_usd,
|
||||
@@ -442,9 +472,9 @@ class CreditService:
|
||||
f"Got: tokens_input={tokens_input}, tokens_output={tokens_output}"
|
||||
)
|
||||
|
||||
# Calculate credits from actual token usage
|
||||
# Calculate credits from actual token usage (pass model_used for model-specific rate)
|
||||
credits_required = CreditService.calculate_credits_from_tokens(
|
||||
operation_type, tokens_input, tokens_output
|
||||
operation_type, tokens_input, tokens_output, model_name=model_used
|
||||
)
|
||||
|
||||
# Check sufficient credits
|
||||
|
||||
Reference in New Issue
Block a user