Add Gemini audio model failover logic to handle HTTP 429 rate limits
This commit is contained in:
@@ -218,59 +218,66 @@ class GeminiService
|
|||||||
*/
|
*/
|
||||||
public static function generateAudioResponse(string $apiKey, string $systemPrompt, string $userMessage, string $voiceName = 'Puck'): ?array
|
public static function generateAudioResponse(string $apiKey, string $systemPrompt, string $userMessage, string $voiceName = 'Puck'): ?array
|
||||||
{
|
{
|
||||||
$url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-3.1-flash-tts-preview:generateContent?key=' . $apiKey;
|
$models = [
|
||||||
|
'gemini-3.1-flash-tts-preview',
|
||||||
|
'gemini-2.5-flash-preview-tts'
|
||||||
|
];
|
||||||
|
|
||||||
$parts = [];
|
foreach ($models as $model) {
|
||||||
if (!empty($systemPrompt)) {
|
$url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':generateContent?key=' . $apiKey;
|
||||||
$parts[] = ['text' => "System instruction: " . $systemPrompt];
|
|
||||||
}
|
|
||||||
$parts[] = ['text' => $userMessage];
|
|
||||||
|
|
||||||
$payload = json_encode([
|
$parts = [];
|
||||||
'contents' => [
|
if (!empty($systemPrompt)) {
|
||||||
[
|
$parts[] = ['text' => "System instruction: " . $systemPrompt];
|
||||||
'role' => 'user',
|
}
|
||||||
'parts' => $parts
|
$parts[] = ['text' => $userMessage];
|
||||||
]
|
|
||||||
],
|
$payload = json_encode([
|
||||||
'generationConfig' => [
|
'contents' => [
|
||||||
'responseModalities' => ['AUDIO'],
|
[
|
||||||
'speechConfig' => [
|
'role' => 'user',
|
||||||
'voiceConfig' => [
|
'parts' => $parts
|
||||||
'prebuiltVoiceConfig' => [
|
]
|
||||||
'voiceName' => $voiceName
|
],
|
||||||
|
'generationConfig' => [
|
||||||
|
'responseModalities' => ['AUDIO'],
|
||||||
|
'speechConfig' => [
|
||||||
|
'voiceConfig' => [
|
||||||
|
'prebuiltVoiceConfig' => [
|
||||||
|
'voiceName' => $voiceName
|
||||||
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]);
|
||||||
]);
|
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($ch, CURLOPT_POST, true);
|
curl_setopt($ch, CURLOPT_POST, true);
|
||||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
|
||||||
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
||||||
'Content-Type: application/json'
|
'Content-Type: application/json'
|
||||||
]);
|
]);
|
||||||
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
curl_close($ch);
|
curl_close($ch);
|
||||||
|
|
||||||
if ($httpCode !== 200) {
|
if ($httpCode === 200) {
|
||||||
error_log("[Gemini Audio API Error] HTTP " . $httpCode . " | Response: " . $response);
|
$data = json_decode($response, true);
|
||||||
return null;
|
$part = $data['candidates'][0]['content']['parts'][0] ?? null;
|
||||||
|
if ($part && isset($part['inlineData'])) {
|
||||||
|
return [
|
||||||
|
'audio' => $part['inlineData']['data'],
|
||||||
|
'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4'
|
||||||
|
];
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
error_log("[Gemini Audio API Error] Model " . $model . " failed with HTTP " . $httpCode . " | Response: " . $response);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$data = json_decode($response, true);
|
|
||||||
$part = $data['candidates'][0]['content']['parts'][0] ?? null;
|
|
||||||
if ($part && isset($part['inlineData'])) {
|
|
||||||
return [
|
|
||||||
'audio' => $part['inlineData']['data'],
|
|
||||||
'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4'
|
|
||||||
];
|
|
||||||
}
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user