feat: IA configurable — probar conexión, activar/inactivar
- Toggle activo/inactivo en configuración → guarda ia_activa en lab_config - Botón "Probar" llama api/lab/test_gemini.php con la clave actual - test_gemini.php envía pregunta mínima a Gemini y confirma respuesta - Dashboard oculta botón y panel IA cuando ia_activa != '1' Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
3b5c26214c
commit
cd3d3bb9c0
@@ -17,6 +17,7 @@ $permitidas = [
|
||||
'empresa_telefono', 'empresa_email', 'empresa_ciudad',
|
||||
'doc_color', 'doc_logo_base64', 'doc_pie_pagina',
|
||||
'gemini_api_key',
|
||||
'ia_activa',
|
||||
];
|
||||
|
||||
$guardadas = 0;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* POST /api/lab/test_gemini.php
|
||||
* Prueba la clave de Gemini con una pregunta mínima.
|
||||
* Body JSON: { key: string }
|
||||
*/
|
||||
require_once __DIR__ . '/_helpers.php';
|
||||
requireMethod('POST');
|
||||
requireAdmin();
|
||||
|
||||
$datos = inputJson();
|
||||
$key = trim($datos['key'] ?? '');
|
||||
if (!$key) jsonError('Token vacío.');
|
||||
|
||||
$payload = json_encode([
|
||||
'contents' => [['role' => 'user', 'parts' => [['text' => 'Responde solo con la palabra: OK']]]],
|
||||
'generationConfig' => ['maxOutputTokens' => 10, 'temperature' => 0],
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$ch = curl_init("https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={$key}");
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => $payload,
|
||||
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
|
||||
CURLOPT_TIMEOUT => 10,
|
||||
CURLOPT_SSL_VERIFYPEER => true,
|
||||
]);
|
||||
$raw = curl_exec($ch);
|
||||
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if (!$raw) jsonError('Sin respuesta del servidor de Google.', 502);
|
||||
|
||||
$gemini = json_decode($raw, true);
|
||||
if ($code !== 200) {
|
||||
jsonError($gemini['error']['message'] ?? "Error HTTP {$code}", 502);
|
||||
}
|
||||
|
||||
jsonOk(['modelo' => 'gemini-2.0-flash', 'respuesta' => $gemini['candidates'][0]['content']['parts'][0]['text'] ?? '']);
|
||||
Reference in New Issue
Block a user