- Umfassende Analyse der aktuellen Implementierung durchgeführt - Identifiziert: Zwei getrennte User-Systeme (users vs gateway_users) - Problem: API verwendet falsche Tabelle (users statt gateway_users) - Lösung: Kompletter Implementierungsplan für korrekte Architektur - Dokument: ARCHITEKTUR.md mit 6-Tage-Umsetzungsplan erstellt - Enthält: Custom API-Key Guard, Gateway-User-Credentials, Budget-System - Swagger/Scramble Paket hinzugefügt (für spätere API-Dokumentation) Status: Bereit für Implementierung (Start: Tag 1 - Datenbank & Models)
94 lines
3.3 KiB
PHP
94 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\ChatCompletionRequest;
|
|
use App\Services\LLM\GatewayService;
|
|
use App\Exceptions\{ProviderException, InsufficientBudgetException, RateLimitExceededException};
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class ChatCompletionController extends Controller
|
|
{
|
|
public function __construct(
|
|
private GatewayService $gatewayService
|
|
) {}
|
|
|
|
/**
|
|
* Create a chat completion
|
|
*
|
|
* Accepts OpenAI-compatible chat completion requests and routes them to the appropriate
|
|
* LLM provider (OpenAI, Anthropic, DeepSeek, Google Gemini, or Mistral AI).
|
|
*
|
|
* The request uses the authenticated user's API keys for the specified provider.
|
|
* Cost tracking, budget checking, and rate limiting are applied automatically.
|
|
*
|
|
* Returns an OpenAI-compatible response with usage statistics and cost information.
|
|
*
|
|
* @tags Chat
|
|
*
|
|
* @param ChatCompletionRequest $request
|
|
* @return JsonResponse
|
|
*/
|
|
public function create(ChatCompletionRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$user = $request->user();
|
|
|
|
$result = $this->gatewayService->chatCompletion(
|
|
user: $user,
|
|
provider: $request->input('provider'),
|
|
model: $request->input('model'),
|
|
messages: $request->input('messages'),
|
|
options: $request->only(['temperature', 'max_tokens', 'top_p', 'frequency_penalty', 'presence_penalty', 'stop']),
|
|
ipAddress: $request->ip(),
|
|
userAgent: $request->userAgent()
|
|
);
|
|
|
|
return response()->json($result, 200);
|
|
|
|
} catch (InsufficientBudgetException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'error' => 'budget_exceeded',
|
|
'message' => $e->getMessage(),
|
|
], 402); // Payment Required
|
|
|
|
} catch (RateLimitExceededException $e) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'error' => 'rate_limit_exceeded',
|
|
'message' => $e->getMessage(),
|
|
'retry_after' => $e->getRetryAfter(),
|
|
], 429);
|
|
|
|
} catch (ProviderException $e) {
|
|
Log::error('Provider error in chat completion', [
|
|
'user_id' => $request->user()->id,
|
|
'provider' => $request->input('provider'),
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'error' => 'provider_error',
|
|
'message' => $e->getMessage(),
|
|
], $e->getCode() ?: 500);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Unexpected error in chat completion', [
|
|
'user_id' => $request->user()->id,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'error' => 'internal_error',
|
|
'message' => 'An unexpected error occurred. Please try again.',
|
|
], 500);
|
|
}
|
|
}
|
|
}
|