Deploy: 2026-05-24 01:32:00
This commit is contained in:
@@ -4,7 +4,7 @@ namespace App\Services;
|
|||||||
|
|
||||||
class GeminiService
|
class GeminiService
|
||||||
{
|
{
|
||||||
public const DEFAULT_MODEL = 'gemini-3.1-flash-lite';
|
public const DEFAULT_MODEL = 'gemini-flash-lite-latest';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get a random Gemini API key from a comma-separated list of keys
|
* Get a random Gemini API key from a comma-separated list of keys
|
||||||
@@ -89,11 +89,19 @@ class GeminiService
|
|||||||
|
|
||||||
if ($httpCode !== 200) {
|
if ($httpCode !== 200) {
|
||||||
error_log("[Gemini API Error] HTTP " . $httpCode . " | Response: " . $response);
|
error_log("[Gemini API Error] HTTP " . $httpCode . " | Response: " . $response);
|
||||||
return null;
|
return "عذراً، الخادم الذكي يواجه ضغطاً أو مشكلة تقنية حالياً (خطأ API " . $httpCode . "). يرجى المحاولة بعد قليل.";
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = json_decode($response, true);
|
$data = json_decode($response, true);
|
||||||
return $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
$text = $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
|
||||||
|
|
||||||
|
if ($text === null) {
|
||||||
|
$reason = $data['candidates'][0]['finishReason'] ?? 'مجهول';
|
||||||
|
error_log("[Gemini Content Error] Reason: " . $reason . " | Response: " . $response);
|
||||||
|
return "عذراً، لم أتمكن من صياغة الرد لأسباب أمنية أو تقنية (السبب: " . $reason . ").";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $text;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
25
backend/test_gemini.php
Normal file
25
backend/test_gemini.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
require __DIR__ . '/vendor/autoload.php';
|
||||||
|
|
||||||
|
// Adjust path if needed
|
||||||
|
require __DIR__ . '/app/Services/GeminiService.php';
|
||||||
|
|
||||||
|
$apiKey = getenv('GEMINI_API_KEY');
|
||||||
|
if (empty($apiKey)) {
|
||||||
|
// try to load from .env
|
||||||
|
$env = parse_ini_file(__DIR__ . '/.env');
|
||||||
|
$apiKey = $env['GEMINI_API_KEY'] ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
if (empty($apiKey)) {
|
||||||
|
echo "NO API KEY FOUND\n";
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
echo "Testing Gemini API...\n";
|
||||||
|
$res = \App\Services\GeminiService::generateResponse($apiKey, "You are a helpful bot.", "hi");
|
||||||
|
if ($res) {
|
||||||
|
echo "SUCCESS: " . $res . "\n";
|
||||||
|
} else {
|
||||||
|
echo "FAILED.\n";
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user