Update test_audio to ignore system instructions for TTS
This commit is contained in:
@@ -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";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user