Add Gemini API as an AI provider
This commit is contained in:
@@ -28,6 +28,10 @@ class AiService
|
||||
'openai/gpt-oss-20b' => 'GPT OSS 20B',
|
||||
'llama-3.3-70b-versatile' => 'Llama 3.3 70B Versatile',
|
||||
],
|
||||
'gemini' => [
|
||||
'gemini-3.8-flash' => 'Gemini 3.8 Flash',
|
||||
'gemini-3.1-pro-preview' => 'Gemini 3.1 Pro (Preview)',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -38,6 +42,7 @@ class AiService
|
||||
$config = match ($provider) {
|
||||
'grok' => ['url' => 'https://api.x.ai/v1/responses'],
|
||||
'groq' => ['url' => 'https://api.groq.com/openai/v1/chat/completions'],
|
||||
'gemini' => ['url' => 'https://generativelanguage.googleapis.com/v1beta/models/' . rawurlencode($model) . ':generateContent'],
|
||||
default => ['url' => 'https://api.openai.com/v1/responses'],
|
||||
};
|
||||
|
||||
@@ -45,7 +50,7 @@ class AiService
|
||||
throw new \RuntimeException('AI_NOT_CONFIGURED');
|
||||
}
|
||||
|
||||
if (!in_array($provider, ['openai', 'grok', 'groq'], true)) {
|
||||
if (!in_array($provider, ['openai', 'grok', 'groq', 'gemini'], true)) {
|
||||
throw new \RuntimeException('Nieprawidłowy dostawca AI: ' . $provider);
|
||||
}
|
||||
|
||||
@@ -61,29 +66,40 @@ class AiService
|
||||
. "Odpowiedź ucznia:\n" . ($answer !== '' ? $answer : 'Brak odpowiedzi') . "\n\n"
|
||||
. "Zwróć wyłącznie poprawny obiekt JSON w formacie {\"awarded_points\": liczba_całkowita, \"feedback\": \"krótka ocena po polsku\"}. W ocenie opisz poprawne i brakujące elementy. Nie zmieniaj wzorca odpowiedzi.";
|
||||
|
||||
$requestData = $provider === 'groq'
|
||||
? [
|
||||
$requestData = match ($provider) {
|
||||
'groq' => [
|
||||
'model' => $model,
|
||||
'messages' => [['role' => 'user', 'content' => $input]],
|
||||
'temperature' => 0.1,
|
||||
]
|
||||
: [
|
||||
],
|
||||
'gemini' => [
|
||||
'contents' => [['parts' => [['text' => $input]]]],
|
||||
'generationConfig' => ['responseMimeType' => 'application/json'],
|
||||
],
|
||||
default => [
|
||||
'model' => $model,
|
||||
'input' => $input,
|
||||
];
|
||||
],
|
||||
};
|
||||
// GPT-5.x reasoning models do not accept a custom temperature.
|
||||
if ($provider === 'grok' || ($provider === 'openai' && $model === 'gpt-4.1-mini')) {
|
||||
$requestData['temperature'] = 0.1;
|
||||
}
|
||||
$payload = json_encode($requestData, JSON_UNESCAPED_UNICODE);
|
||||
|
||||
$headers = [
|
||||
'Content-Type' => 'application/json',
|
||||
];
|
||||
if ($provider === 'gemini') {
|
||||
$headers['x-goog-api-key'] = trim($apiKey);
|
||||
} else {
|
||||
$headers['Authorization'] = 'Bearer ' . trim($apiKey);
|
||||
}
|
||||
|
||||
$response = HttpFactory::getHttp()->post(
|
||||
$config['url'],
|
||||
$payload,
|
||||
[
|
||||
'Authorization' => 'Bearer ' . trim($apiKey),
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
$headers,
|
||||
60
|
||||
);
|
||||
$body = json_decode((string) $response->body, true);
|
||||
@@ -132,13 +148,15 @@ class AiService
|
||||
return null;
|
||||
}
|
||||
|
||||
$inputTokens = max(0, (int) ($usage['input_tokens'] ?? $usage['prompt_tokens'] ?? 0));
|
||||
$outputTokens = max(0, (int) ($usage['output_tokens'] ?? $usage['completion_tokens'] ?? 0));
|
||||
if ((!isset($usage['input_tokens']) && !isset($usage['prompt_tokens']))
|
||||
|| (!isset($usage['output_tokens']) && !isset($usage['completion_tokens']))) {
|
||||
$inputTokens = max(0, (int) ($usage['input_tokens'] ?? $usage['prompt_tokens'] ?? $usage['promptTokenCount'] ?? 0));
|
||||
$outputTokens = max(0, (int) ($usage['output_tokens'] ?? $usage['completion_tokens'] ?? (
|
||||
(int) ($usage['candidatesTokenCount'] ?? 0) + (int) ($usage['thoughtsTokenCount'] ?? 0)
|
||||
)));
|
||||
if ((!isset($usage['input_tokens']) && !isset($usage['prompt_tokens']) && !isset($usage['promptTokenCount']))
|
||||
|| (!isset($usage['output_tokens']) && !isset($usage['completion_tokens']) && !isset($usage['candidatesTokenCount']))) {
|
||||
return null;
|
||||
}
|
||||
$cachedTokens = min($inputTokens, max(0, (int) ($usage['input_tokens_details']['cached_tokens'] ?? 0)));
|
||||
$cachedTokens = min($inputTokens, max(0, (int) ($usage['input_tokens_details']['cached_tokens'] ?? $usage['cachedContentTokenCount'] ?? 0)));
|
||||
$cost = null;
|
||||
$costSource = '';
|
||||
|
||||
@@ -159,7 +177,7 @@ class AiService
|
||||
'model' => $model,
|
||||
'input_tokens' => $inputTokens,
|
||||
'output_tokens' => $outputTokens,
|
||||
'total_tokens' => (int) ($usage['total_tokens'] ?? ($inputTokens + $outputTokens)),
|
||||
'total_tokens' => (int) ($usage['total_tokens'] ?? $usage['totalTokenCount'] ?? ($inputTokens + $outputTokens)),
|
||||
'cached_input_tokens' => $cachedTokens,
|
||||
'cost_usd' => $cost,
|
||||
'cost_source' => $costSource,
|
||||
@@ -179,6 +197,16 @@ class AiService
|
||||
|
||||
private function extractText(array $body): string
|
||||
{
|
||||
if (isset($body['candidates'][0]['content']['parts']) && is_array($body['candidates'][0]['content']['parts'])) {
|
||||
$parts = [];
|
||||
foreach ($body['candidates'][0]['content']['parts'] as $part) {
|
||||
if (isset($part['text']) && is_string($part['text'])) {
|
||||
$parts[] = $part['text'];
|
||||
}
|
||||
}
|
||||
return trim(implode("\n", $parts));
|
||||
}
|
||||
|
||||
if (isset($body['choices'][0]['message']['content']) && is_string($body['choices'][0]['message']['content'])) {
|
||||
return trim($body['choices'][0]['message']['content']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user