Fix API controllers to use correct database column names
- Fix model_pricing table references (model_id -> model, display_name -> model)
- Fix price columns (output_price_per_1k -> output_price_per_million)
- Add price conversion (per_million / 1000 = per_1k) in all API responses
- Add whereNotNull('model') filters to exclude invalid entries
- Add getModelDisplayName() helper method to all controllers
- Fix AccountController to use gateway_users budget fields directly
- Remove Budget model dependencies from AccountController
- Add custom Scramble server URL configuration for API docs
- Create ScrambleServiceProvider to set correct /api prefix
- Add migration to rename user_id to gateway_user_id in llm_requests
- Add custom ApiGuard for gateway_users authentication
- Update all API controllers: AccountController, ModelController, PricingController, ProviderController
All API endpoints now working correctly:
- GET /api/account
- GET /api/models
- GET /api/pricing
- GET /api/providers/{provider}
This commit is contained in:
@@ -70,7 +70,8 @@ class PricingController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
$query = ModelPricing::where('is_active', true);
|
||||
$query = ModelPricing::where('is_active', true)
|
||||
->whereNotNull('model');
|
||||
|
||||
// Apply filters
|
||||
if ($request->has('provider')) {
|
||||
@@ -81,13 +82,13 @@ class PricingController extends Controller
|
||||
$sort = $request->input('sort', 'name');
|
||||
switch ($sort) {
|
||||
case 'price':
|
||||
$query->orderBy('output_price_per_1k');
|
||||
$query->orderBy('output_price_per_million');
|
||||
break;
|
||||
case 'provider':
|
||||
$query->orderBy('provider')->orderBy('display_name');
|
||||
$query->orderBy('provider')->orderBy('model');
|
||||
break;
|
||||
default:
|
||||
$query->orderBy('display_name');
|
||||
$query->orderBy('model');
|
||||
}
|
||||
|
||||
$models = $query->get();
|
||||
@@ -96,11 +97,11 @@ class PricingController extends Controller
|
||||
return [
|
||||
'provider' => $model->provider,
|
||||
'provider_name' => $this->getProviderName($model->provider),
|
||||
'model' => $model->model_id,
|
||||
'model_name' => $model->display_name,
|
||||
'model' => $model->model,
|
||||
'model_name' => $this->getModelDisplayName($model->model),
|
||||
'pricing' => [
|
||||
'input_per_1k_tokens' => $model->input_price_per_1k,
|
||||
'output_per_1k_tokens' => $model->output_price_per_1k,
|
||||
'input_per_1k_tokens' => round($model->input_price_per_million / 1000, 6),
|
||||
'output_per_1k_tokens' => round($model->output_price_per_million / 1000, 6),
|
||||
'currency' => 'USD',
|
||||
],
|
||||
'last_updated' => $model->updated_at->toIso8601String(),
|
||||
@@ -196,7 +197,7 @@ class PricingController extends Controller
|
||||
$outputTokens = $request->input('output_tokens');
|
||||
|
||||
// Find the model
|
||||
$model = ModelPricing::where('model_id', $modelId)
|
||||
$model = ModelPricing::where('model', $modelId)
|
||||
->where('is_active', true)
|
||||
->first();
|
||||
|
||||
@@ -210,9 +211,12 @@ class PricingController extends Controller
|
||||
], 404);
|
||||
}
|
||||
|
||||
// Calculate costs
|
||||
$inputCost = ($inputTokens / 1000) * $model->input_price_per_1k;
|
||||
$outputCost = ($outputTokens / 1000) * $model->output_price_per_1k;
|
||||
// Calculate costs (convert from per-million to per-1k)
|
||||
$inputPricePer1k = $model->input_price_per_million / 1000;
|
||||
$outputPricePer1k = $model->output_price_per_million / 1000;
|
||||
|
||||
$inputCost = ($inputTokens / 1000) * $inputPricePer1k;
|
||||
$outputCost = ($outputTokens / 1000) * $outputPricePer1k;
|
||||
$totalCost = $inputCost + $outputCost;
|
||||
|
||||
// Calculate examples for different request volumes
|
||||
@@ -225,13 +229,13 @@ class PricingController extends Controller
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'model' => $model->model_id,
|
||||
'model' => $model->model,
|
||||
'provider' => $model->provider,
|
||||
'input_tokens' => $inputTokens,
|
||||
'output_tokens' => $outputTokens,
|
||||
'pricing' => [
|
||||
'input_per_1k' => $model->input_price_per_1k,
|
||||
'output_per_1k' => $model->output_price_per_1k,
|
||||
'input_per_1k' => round($inputPricePer1k, 6),
|
||||
'output_per_1k' => round($outputPricePer1k, 6),
|
||||
'currency' => 'USD',
|
||||
],
|
||||
'calculation' => [
|
||||
@@ -296,7 +300,7 @@ class PricingController extends Controller
|
||||
$outputTokens = $request->input('output_tokens');
|
||||
|
||||
// Get all models
|
||||
$models = ModelPricing::whereIn('model_id', $modelIds)
|
||||
$models = ModelPricing::whereIn('model', $modelIds)
|
||||
->where('is_active', true)
|
||||
->get();
|
||||
|
||||
@@ -312,13 +316,16 @@ class PricingController extends Controller
|
||||
|
||||
// Calculate costs for each model
|
||||
$comparisons = $models->map(function ($model) use ($inputTokens, $outputTokens) {
|
||||
$inputCost = ($inputTokens / 1000) * $model->input_price_per_1k;
|
||||
$outputCost = ($outputTokens / 1000) * $model->output_price_per_1k;
|
||||
$inputPricePer1k = $model->input_price_per_million / 1000;
|
||||
$outputPricePer1k = $model->output_price_per_million / 1000;
|
||||
|
||||
$inputCost = ($inputTokens / 1000) * $inputPricePer1k;
|
||||
$outputCost = ($outputTokens / 1000) * $outputPricePer1k;
|
||||
$totalCost = $inputCost + $outputCost;
|
||||
|
||||
return [
|
||||
'model' => $model->model_id,
|
||||
'model_name' => $model->display_name,
|
||||
'model' => $model->model,
|
||||
'model_name' => $this->getModelDisplayName($model->model),
|
||||
'provider' => $model->provider,
|
||||
'provider_name' => $this->getProviderName($model->provider),
|
||||
'costs' => [
|
||||
@@ -327,8 +334,8 @@ class PricingController extends Controller
|
||||
'total_cost' => round($totalCost, 6),
|
||||
],
|
||||
'pricing' => [
|
||||
'input_per_1k' => $model->input_price_per_1k,
|
||||
'output_per_1k' => $model->output_price_per_1k,
|
||||
'input_per_1k' => round($inputPricePer1k, 6),
|
||||
'output_per_1k' => round($outputPricePer1k, 6),
|
||||
],
|
||||
];
|
||||
})->sortBy('costs.total_cost')->values();
|
||||
@@ -372,4 +379,14 @@ class PricingController extends Controller
|
||||
default => ucfirst($provider),
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Get model display name from model ID
|
||||
*/
|
||||
private function getModelDisplayName(string $modelId): string
|
||||
{
|
||||
// Convert model ID to a readable display name
|
||||
// e.g., "gpt-4-turbo" -> "GPT-4 Turbo"
|
||||
return ucwords(str_replace(['-', '_'], ' ', $modelId));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user