Fügt 21 neue API-Endpoints in 4 Phasen hinzu:
Phase 1 - Foundation (Provider & Models):
- GET /api/providers - Liste aller Provider
- GET /api/providers/{provider} - Provider-Details
- GET /api/models - Liste aller Models mit Filtering/Sorting
- GET /api/models/{provider}/{model} - Model-Details
Phase 2 - Core Features (Credentials, Budget, Pricing):
- GET/POST/PUT/DELETE /api/credentials - Credential-Management
- POST /api/credentials/{id}/test - Connection Testing
- GET /api/budget - Budget-Status mit Projektionen
- GET /api/budget/history - Budget-Historie
- GET /api/pricing - Model-Pricing-Listen
- GET /api/pricing/calculator - Kosten-Kalkulator
- GET /api/pricing/compare - Preis-Vergleich
Phase 3 - Analytics (Usage Statistics):
- GET /api/usage/summary - Umfassende Statistiken
- GET /api/usage/requests - Request-History mit Pagination
- GET /api/usage/requests/{id} - Request-Details
- GET /api/usage/charts - Chart-Daten (4 Typen)
Phase 4 - Account (Account Info & Activity):
- GET /api/account - User-Informationen
- GET /api/account/activity - Activity-Log
Features:
- Vollständige Scramble/Swagger-Dokumentation
- Consistent Error-Handling
- API-Key Authentication
- Filtering, Sorting, Pagination
- Budget-Tracking mit Alerts
- Provider-Breakdown
- Performance-Metriken
- Chart-Ready-Data
Controller erstellt:
- ProviderController
- ModelController
- CredentialController
- BudgetController
- PricingController
- UsageController
- AccountController
Dokumentation:
- API_KONZEPT.md - Vollständiges API-Konzept
- API_IMPLEMENTATION_STATUS.txt - Implementation-Tracking
- API_IMPLEMENTATION_SUMMARY.md - Zusammenfassung und Workflows
376 lines
12 KiB
PHP
376 lines
12 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\ModelPricing;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Validator;
|
|
|
|
class PricingController extends Controller
|
|
{
|
|
/**
|
|
* Get current pricing for all models
|
|
*
|
|
* Returns pricing information for all available models, with optional
|
|
* filtering and sorting.
|
|
*
|
|
* ## Query Parameters
|
|
*
|
|
* - `provider` (optional) - Filter by provider (openai, anthropic, gemini, deepseek, mistral)
|
|
* - `sort` (optional) - Sort by: price, name, provider (default: name)
|
|
*
|
|
* ## Example Response
|
|
*
|
|
* ```json
|
|
* {
|
|
* "data": [
|
|
* {
|
|
* "provider": "openai",
|
|
* "provider_name": "OpenAI",
|
|
* "model": "gpt-4-turbo",
|
|
* "model_name": "GPT-4 Turbo",
|
|
* "pricing": {
|
|
* "input_per_1k_tokens": 0.01,
|
|
* "output_per_1k_tokens": 0.03,
|
|
* "currency": "USD"
|
|
* },
|
|
* "last_updated": "2024-11-01T00:00:00Z"
|
|
* }
|
|
* ],
|
|
* "meta": {
|
|
* "total_models": 42,
|
|
* "providers_count": 5,
|
|
* "last_sync": "2025-11-19T10:00:00Z"
|
|
* }
|
|
* }
|
|
* ```
|
|
*
|
|
* @tags Pricing
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'provider' => 'sometimes|string|in:openai,anthropic,gemini,deepseek,mistral',
|
|
'sort' => 'sometimes|string|in:price,name,provider',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => [
|
|
'code' => 'validation_error',
|
|
'message' => 'Invalid query parameters',
|
|
'status' => 422,
|
|
'details' => $validator->errors(),
|
|
],
|
|
], 422);
|
|
}
|
|
|
|
$query = ModelPricing::where('is_active', true);
|
|
|
|
// Apply filters
|
|
if ($request->has('provider')) {
|
|
$query->where('provider', $request->input('provider'));
|
|
}
|
|
|
|
// Apply sorting
|
|
$sort = $request->input('sort', 'name');
|
|
switch ($sort) {
|
|
case 'price':
|
|
$query->orderBy('output_price_per_1k');
|
|
break;
|
|
case 'provider':
|
|
$query->orderBy('provider')->orderBy('display_name');
|
|
break;
|
|
default:
|
|
$query->orderBy('display_name');
|
|
}
|
|
|
|
$models = $query->get();
|
|
|
|
$data = $models->map(function ($model) {
|
|
return [
|
|
'provider' => $model->provider,
|
|
'provider_name' => $this->getProviderName($model->provider),
|
|
'model' => $model->model_id,
|
|
'model_name' => $model->display_name,
|
|
'pricing' => [
|
|
'input_per_1k_tokens' => $model->input_price_per_1k,
|
|
'output_per_1k_tokens' => $model->output_price_per_1k,
|
|
'currency' => 'USD',
|
|
],
|
|
'last_updated' => $model->updated_at->toIso8601String(),
|
|
];
|
|
});
|
|
|
|
$providersCount = $models->pluck('provider')->unique()->count();
|
|
$lastSync = $models->max('updated_at');
|
|
|
|
return response()->json([
|
|
'data' => $data,
|
|
'meta' => [
|
|
'total_models' => $data->count(),
|
|
'providers_count' => $providersCount,
|
|
'last_sync' => $lastSync?->toIso8601String(),
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Calculate costs for hypothetical requests
|
|
*
|
|
* Cost calculator that shows estimated costs for a given number of tokens
|
|
* with a specific model.
|
|
*
|
|
* ## Query Parameters
|
|
*
|
|
* - `model` (required) - Model ID (e.g., gpt-4-turbo, claude-3-5-sonnet-20241022)
|
|
* - `input_tokens` (required) - Number of input tokens
|
|
* - `output_tokens` (required) - Number of output tokens
|
|
*
|
|
* ## Example Request
|
|
*
|
|
* ```
|
|
* GET /api/pricing/calculator?model=gpt-4-turbo&input_tokens=1000&output_tokens=500
|
|
* ```
|
|
*
|
|
* ## Example Response
|
|
*
|
|
* ```json
|
|
* {
|
|
* "data": {
|
|
* "model": "gpt-4-turbo",
|
|
* "provider": "openai",
|
|
* "input_tokens": 1000,
|
|
* "output_tokens": 500,
|
|
* "pricing": {
|
|
* "input_per_1k": 0.01,
|
|
* "output_per_1k": 0.03,
|
|
* "currency": "USD"
|
|
* },
|
|
* "calculation": {
|
|
* "input_cost": 0.01,
|
|
* "output_cost": 0.015,
|
|
* "total_cost": 0.025,
|
|
* "currency": "USD"
|
|
* },
|
|
* "examples": {
|
|
* "10_requests": 0.25,
|
|
* "100_requests": 2.50,
|
|
* "1000_requests": 25.00
|
|
* }
|
|
* }
|
|
* }
|
|
* ```
|
|
*
|
|
* @tags Pricing
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function calculator(Request $request): JsonResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'model' => 'required|string',
|
|
'input_tokens' => 'required|integer|min:0',
|
|
'output_tokens' => 'required|integer|min:0',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => [
|
|
'code' => 'validation_error',
|
|
'message' => 'Invalid query parameters',
|
|
'status' => 422,
|
|
'details' => $validator->errors(),
|
|
],
|
|
], 422);
|
|
}
|
|
|
|
$modelId = $request->input('model');
|
|
$inputTokens = $request->input('input_tokens');
|
|
$outputTokens = $request->input('output_tokens');
|
|
|
|
// Find the model
|
|
$model = ModelPricing::where('model_id', $modelId)
|
|
->where('is_active', true)
|
|
->first();
|
|
|
|
if (!$model) {
|
|
return response()->json([
|
|
'error' => [
|
|
'code' => 'not_found',
|
|
'message' => "Model '{$modelId}' not found",
|
|
'status' => 404,
|
|
],
|
|
], 404);
|
|
}
|
|
|
|
// Calculate costs
|
|
$inputCost = ($inputTokens / 1000) * $model->input_price_per_1k;
|
|
$outputCost = ($outputTokens / 1000) * $model->output_price_per_1k;
|
|
$totalCost = $inputCost + $outputCost;
|
|
|
|
// Calculate examples for different request volumes
|
|
$examples = [
|
|
'1_request' => round($totalCost, 4),
|
|
'10_requests' => round($totalCost * 10, 4),
|
|
'100_requests' => round($totalCost * 100, 2),
|
|
'1000_requests' => round($totalCost * 1000, 2),
|
|
];
|
|
|
|
return response()->json([
|
|
'data' => [
|
|
'model' => $model->model_id,
|
|
'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,
|
|
'currency' => 'USD',
|
|
],
|
|
'calculation' => [
|
|
'input_cost' => round($inputCost, 6),
|
|
'output_cost' => round($outputCost, 6),
|
|
'total_cost' => round($totalCost, 6),
|
|
'currency' => 'USD',
|
|
],
|
|
'examples' => $examples,
|
|
'context' => [
|
|
'tokens_per_page' => 750, // Approximate
|
|
'estimated_pages_input' => round($inputTokens / 750, 1),
|
|
'estimated_pages_output' => round($outputTokens / 750, 1),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Compare pricing across multiple models
|
|
*
|
|
* Compare costs for the same token counts across different models.
|
|
*
|
|
* ## Query Parameters
|
|
*
|
|
* - `models` (required) - Comma-separated list of model IDs
|
|
* - `input_tokens` (required) - Number of input tokens
|
|
* - `output_tokens` (required) - Number of output tokens
|
|
*
|
|
* ## Example Request
|
|
*
|
|
* ```
|
|
* GET /api/pricing/compare?models=gpt-4-turbo,claude-3-5-sonnet-20241022&input_tokens=1000&output_tokens=500
|
|
* ```
|
|
*
|
|
* @tags Pricing
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function compare(Request $request): JsonResponse
|
|
{
|
|
$validator = Validator::make($request->all(), [
|
|
'models' => 'required|string',
|
|
'input_tokens' => 'required|integer|min:0',
|
|
'output_tokens' => 'required|integer|min:0',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json([
|
|
'error' => [
|
|
'code' => 'validation_error',
|
|
'message' => 'Invalid query parameters',
|
|
'status' => 422,
|
|
'details' => $validator->errors(),
|
|
],
|
|
], 422);
|
|
}
|
|
|
|
$modelIds = explode(',', $request->input('models'));
|
|
$inputTokens = $request->input('input_tokens');
|
|
$outputTokens = $request->input('output_tokens');
|
|
|
|
// Get all models
|
|
$models = ModelPricing::whereIn('model_id', $modelIds)
|
|
->where('is_active', true)
|
|
->get();
|
|
|
|
if ($models->isEmpty()) {
|
|
return response()->json([
|
|
'error' => [
|
|
'code' => 'not_found',
|
|
'message' => 'No valid models found',
|
|
'status' => 404,
|
|
],
|
|
], 404);
|
|
}
|
|
|
|
// 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;
|
|
$totalCost = $inputCost + $outputCost;
|
|
|
|
return [
|
|
'model' => $model->model_id,
|
|
'model_name' => $model->display_name,
|
|
'provider' => $model->provider,
|
|
'provider_name' => $this->getProviderName($model->provider),
|
|
'costs' => [
|
|
'input_cost' => round($inputCost, 6),
|
|
'output_cost' => round($outputCost, 6),
|
|
'total_cost' => round($totalCost, 6),
|
|
],
|
|
'pricing' => [
|
|
'input_per_1k' => $model->input_price_per_1k,
|
|
'output_per_1k' => $model->output_price_per_1k,
|
|
],
|
|
];
|
|
})->sortBy('costs.total_cost')->values();
|
|
|
|
// Find cheapest and most expensive
|
|
$cheapest = $comparisons->first();
|
|
$mostExpensive = $comparisons->last();
|
|
$savings = $mostExpensive['costs']['total_cost'] - $cheapest['costs']['total_cost'];
|
|
$savingsPercent = $mostExpensive['costs']['total_cost'] > 0
|
|
? ($savings / $mostExpensive['costs']['total_cost']) * 100
|
|
: 0;
|
|
|
|
return response()->json([
|
|
'data' => [
|
|
'input_tokens' => $inputTokens,
|
|
'output_tokens' => $outputTokens,
|
|
'comparisons' => $comparisons,
|
|
'summary' => [
|
|
'cheapest' => $cheapest['model'],
|
|
'cheapest_cost' => $cheapest['costs']['total_cost'],
|
|
'most_expensive' => $mostExpensive['model'],
|
|
'most_expensive_cost' => $mostExpensive['costs']['total_cost'],
|
|
'max_savings' => round($savings, 6),
|
|
'max_savings_percent' => round($savingsPercent, 1),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get human-readable provider name
|
|
*/
|
|
private function getProviderName(string $provider): string
|
|
{
|
|
return match ($provider) {
|
|
'openai' => 'OpenAI',
|
|
'anthropic' => 'Anthropic',
|
|
'gemini' => 'Google Gemini',
|
|
'deepseek' => 'DeepSeek',
|
|
'mistral' => 'Mistral AI',
|
|
default => ucfirst($provider),
|
|
};
|
|
}
|
|
}
|