From ffd8c6f2a5b6b8cf32c5a7121c20aa02375170c6 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Fri, 22 May 2026 15:27:09 +0300 Subject: [PATCH] Update test_audio to ignore system instructions for TTS --- backend/public/test_audio.php | 49 ++++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/backend/public/test_audio.php b/backend/public/test_audio.php index aa09477..9bc05a2 100644 --- a/backend/public/test_audio.php +++ b/backend/public/test_audio.php @@ -39,8 +39,8 @@ $systemPrompt = "You are a friendly customer service assistant. Speak in a warm, $testMsg = "مرحبا، هل متجركم مفتوح اليوم؟"; $candidateModels = [ - 'gemini-2.5-flash-preview-tts', 'gemini-3.1-flash-tts-preview', + 'gemini-2.5-flash-preview-tts', 'gemini-2.0-flash', 'gemini-2.5-flash', 'gemini-3.5-flash', @@ -51,17 +51,32 @@ $successfulModel = null; $audioResponse = null; foreach ($candidateModels as $model) { - echo "Testing model: models/{$model} ... "; + echo "Testing model: models/{$model} ...\n"; $url = 'https://generativelanguage.googleapis.com/v1beta/models/' . $model . ':generateContent?key=' . $apiKey; - $payload = json_encode([ - 'contents' => [[ + + // For TTS models, we do not send systemInstruction because they don't support it (Developer instruction not enabled) + // Instead we prepend it to the user message parts + $useSystemInstructionField = !str_contains($model, 'tts'); + + $contents = []; + if (!$useSystemInstructionField) { + $contents[] = [ + 'role' => 'user', + 'parts' => [ + ['text' => "System instruction: " . $systemPrompt], + ['text' => $testMsg] + ] + ]; + } else { + $contents[] = [ 'role' => 'user', 'parts' => [['text' => $testMsg]] - ]], - 'systemInstruction' => [ - 'parts' => [['text' => $systemPrompt]] - ], + ]; + } + + $payloadData = [ + 'contents' => $contents, 'generationConfig' => [ 'responseModalities' => ['AUDIO'], 'speechConfig' => [ @@ -72,7 +87,15 @@ foreach ($candidateModels as $model) { ] ] ] - ]); + ]; + + if ($useSystemInstructionField) { + $payloadData['systemInstruction'] = [ + 'parts' => [['text' => $systemPrompt]] + ]; + } + + $payload = json_encode($payloadData); $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); @@ -87,7 +110,8 @@ foreach ($candidateModels as $model) { if ($httpCode !== 200) { $errorData = json_decode($response, true); $errMsg = $errorData['error']['message'] ?? 'Unknown error'; - echo "❌ Failed (HTTP {$httpCode}: {$errMsg})\n"; + echo " ❌ Failed (HTTP {$httpCode}: {$errMsg})\n"; + echo " Response: " . substr($response, 0, 500) . "\n\n"; } else { $data = json_decode($response, true); $part = $data['candidates'][0]['content']['parts'][0] ?? null; @@ -97,10 +121,11 @@ foreach ($candidateModels as $model) { 'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4' ]; $successfulModel = $model; - echo "✅ SUCCESS!\n"; + echo " ✅ SUCCESS!\n\n"; break; // Stop at first successful model } else { - echo "❌ Failed (No inlineData in response)\n"; + echo " ❌ Failed (No inlineData in response)\n"; + echo " Response: " . substr($response, 0, 500) . "\n\n"; } } }