diff --git a/app/Http/Livewire/Chat/ShowConfiguracionChat.php b/app/Http/Livewire/Chat/ShowConfiguracionChat.php index dfe885b..acb4ff4 100755 --- a/app/Http/Livewire/Chat/ShowConfiguracionChat.php +++ b/app/Http/Livewire/Chat/ShowConfiguracionChat.php @@ -107,7 +107,10 @@ class ShowConfiguracionChat extends Component ->withHeaders(['Content-Type' => 'application/json']) ->post("{$url}?key={$key}", [ 'contents' => [['parts' => [['text' => 'Responde solo "ok"']]]], - 'generationConfig' => ['maxOutputTokens' => 5, 'temperature' => 0], + 'generationConfig' => array_merge( + ['maxOutputTokens' => 5, 'temperature' => 0], + preg_match('/gemini-(2\.5|3[\.\d]*)/', $model) ? ['thinkingConfig' => ['thinkingBudget' => 0]] : [] + ), ]); $ms = (int) ((microtime(true) - $inicio) * 1000); diff --git a/app/Services/GeminiIntentService.php b/app/Services/GeminiIntentService.php index 4a1f319..1229bea 100644 --- a/app/Services/GeminiIntentService.php +++ b/app/Services/GeminiIntentService.php @@ -12,11 +12,23 @@ class GeminiIntentService private string $apiKey; private string $apiUrl; + private string $model; + public function __construct() { $this->apiKey = ChatConfig::get('gemini_api_key', ''); - $model = ChatConfig::get('gemini_model', 'gemini-2.0-flash'); - $this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent"; + $this->model = ChatConfig::get('gemini_model', 'gemini-2.0-flash'); + $this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent"; + } + + private function generationConfig(array $extra = []): array + { + $config = array_merge(['temperature' => 0.1, 'maxOutputTokens' => 100], $extra); + // 2.5+ models require thinkingConfig to avoid response format issues + if (preg_match('/gemini-(2\.5|3[\.\d]*)/', $this->model)) { + $config['thinkingConfig'] = ['thinkingBudget' => 0]; + } + return $config; } /** @@ -37,10 +49,7 @@ class GeminiIntentService ->timeout(8) ->post("{$this->apiUrl}?key={$this->apiKey}", [ 'contents' => [['parts' => [['text' => $prompt]]]], - 'generationConfig' => [ - 'temperature' => 0.1, - 'maxOutputTokens' => 100, - ], + 'generationConfig' => $this->generationConfig(), ]); $tiempoMs = (int) ((microtime(true) - $inicio) * 1000); diff --git a/app/Services/GeminiVisionService.php b/app/Services/GeminiVisionService.php index 8811825..c2e2bc0 100644 --- a/app/Services/GeminiVisionService.php +++ b/app/Services/GeminiVisionService.php @@ -12,11 +12,22 @@ class GeminiVisionService private string $apiKey; private string $apiUrl; + private string $model; + public function __construct() { $this->apiKey = ChatConfig::get('gemini_api_key', ''); - $model = ChatConfig::get('gemini_model', 'gemini-2.0-flash'); - $this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$model}:generateContent"; + $this->model = ChatConfig::get('gemini_model', 'gemini-2.0-flash'); + $this->apiUrl = "https://generativelanguage.googleapis.com/v1beta/models/{$this->model}:generateContent"; + } + + private function generationConfig(array $extra = []): array + { + $config = array_merge(['temperature' => 0.1, 'maxOutputTokens' => 200], $extra); + if (preg_match('/gemini-(2\.5|3[\.\d]*)/', $this->model)) { + $config['thinkingConfig'] = ['thinkingBudget' => 0]; + } + return $config; } /** @@ -46,10 +57,7 @@ class GeminiVisionService ['inline_data' => ['mime_type' => $mimeType, 'data' => $base64]], ], ]], - 'generationConfig' => [ - 'temperature' => 0.1, - 'maxOutputTokens' => 200, - ], + 'generationConfig' => $this->generationConfig(), ]); $tiempoMs = (int) ((microtime(true) - $inicio) * 1000);