87 lines
2.4 KiB
PHP
Executable File
87 lines
2.4 KiB
PHP
Executable File
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\AiModel;
|
|
use App\Models\AiProvider;
|
|
use App\Services\ChatService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\View\View;
|
|
|
|
class ChatController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly ChatService $chatService
|
|
) {}
|
|
|
|
public function index(): View
|
|
{
|
|
$providers = AiProvider::where('is_active', true)
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
$history = $this->chatService->getHistory();
|
|
|
|
$defaultProviderId = 1;
|
|
$defaultModelId = 2;
|
|
|
|
return view('chat.index', compact('providers', 'history', 'defaultProviderId', 'defaultModelId'));
|
|
}
|
|
|
|
public function send(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'message' => 'required|string',
|
|
'provider_id' => 'required|exists:ai_providers,id',
|
|
'model_id' => 'required|exists:ai_models,id',
|
|
'temperature' => 'nullable|numeric|min:0|max:1',
|
|
'max_tokens' => 'nullable|integer|min:1|max:128000',
|
|
]);
|
|
|
|
$provider = AiProvider::where('id', $request->provider_id)
|
|
->where('is_active', true)
|
|
->firstOrFail();
|
|
|
|
$model = AiModel::where('id', $request->model_id)
|
|
->where('provider_id', $provider->id)
|
|
->where('is_active', true)
|
|
->firstOrFail();
|
|
|
|
$result = $this->chatService->sendMessage(
|
|
userMessage: $request->message,
|
|
provider: $provider,
|
|
model: $model,
|
|
temperature: (float) ($request->temperature ?? 0.7),
|
|
maxTokens: (int) ($request->max_tokens ?? 8192),
|
|
);
|
|
|
|
return response()->json($result);
|
|
}
|
|
|
|
public function reset(): RedirectResponse
|
|
{
|
|
$this->chatService->reset();
|
|
|
|
return redirect()->route('chat.index')
|
|
->with('success', 'Conversa reiniciada.');
|
|
}
|
|
|
|
public function getModels(Request $request)
|
|
{
|
|
$request->validate([
|
|
'provider_id' => 'required|exists:ai_providers,id',
|
|
]);
|
|
|
|
$models = AiModel::where('provider_id', $request->provider_id)
|
|
->where('is_active', true)
|
|
->orderBy('name')
|
|
->get(['id', 'name', 'slug']);
|
|
|
|
return response()->json($models);
|
|
}
|
|
}
|