Phase 4: Support LID identity scheme and fix incoming message parsing

This commit is contained in:
Hamza-Ayed
2026-05-21 23:44:25 +03:00
parent 0a1752ce44
commit ec7c9ffc29
9 changed files with 790 additions and 20 deletions

View File

@@ -0,0 +1,51 @@
<?php
namespace App\Services;
class GeminiService
{
/**
* Call Gemini API to generate a response
*/
public static function generateResponse(string $apiKey, string $systemPrompt, string $userMessage): ?string
{
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-lite-latest:generateContent?key=' . $apiKey;
$payload = json_encode([
'contents' => [
[
'role' => 'user',
'parts' => [
['text' => $userMessage]
]
]
],
'systemInstruction' => [
'parts' => [
['text' => $systemPrompt]
]
]
]);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json'
]);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode !== 200) {
error_log("[Gemini API Error] HTTP " . $httpCode . " | Response: " . $response);
return null;
}
$data = json_decode($response, true);
return $data['candidates'][0]['content']['parts'][0]['text'] ?? null;
}
}