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 = "مرحبا، هل متجركم مفتوح اليوم؟";
|
$testMsg = "مرحبا، هل متجركم مفتوح اليوم؟";
|
||||||
|
|
||||||
$candidateModels = [
|
$candidateModels = [
|
||||||
'gemini-2.5-flash-preview-tts',
|
|
||||||
'gemini-3.1-flash-tts-preview',
|
'gemini-3.1-flash-tts-preview',
|
||||||
|
'gemini-2.5-flash-preview-tts',
|
||||||
'gemini-2.0-flash',
|
'gemini-2.0-flash',
|
||||||
'gemini-2.5-flash',
|
'gemini-2.5-flash',
|
||||||
'gemini-3.5-flash',
|
'gemini-3.5-flash',
|
||||||
@@ -51,17 +51,32 @@ $successfulModel = null;
|
|||||||
$audioResponse = null;
|
$audioResponse = null;
|
||||||
|
|
||||||
foreach ($candidateModels as $model) {
|
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;
|
$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',
|
'role' => 'user',
|
||||||
'parts' => [['text' => $testMsg]]
|
'parts' => [['text' => $testMsg]]
|
||||||
]],
|
];
|
||||||
'systemInstruction' => [
|
}
|
||||||
'parts' => [['text' => $systemPrompt]]
|
|
||||||
],
|
$payloadData = [
|
||||||
|
'contents' => $contents,
|
||||||
'generationConfig' => [
|
'generationConfig' => [
|
||||||
'responseModalities' => ['AUDIO'],
|
'responseModalities' => ['AUDIO'],
|
||||||
'speechConfig' => [
|
'speechConfig' => [
|
||||||
@@ -72,7 +87,15 @@ foreach ($candidateModels as $model) {
|
|||||||
]
|
]
|
||||||
]
|
]
|
||||||
]
|
]
|
||||||
]);
|
];
|
||||||
|
|
||||||
|
if ($useSystemInstructionField) {
|
||||||
|
$payloadData['systemInstruction'] = [
|
||||||
|
'parts' => [['text' => $systemPrompt]]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$payload = json_encode($payloadData);
|
||||||
|
|
||||||
$ch = curl_init($url);
|
$ch = curl_init($url);
|
||||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||||
@@ -88,6 +111,7 @@ foreach ($candidateModels as $model) {
|
|||||||
$errorData = json_decode($response, true);
|
$errorData = json_decode($response, true);
|
||||||
$errMsg = $errorData['error']['message'] ?? 'Unknown error';
|
$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 {
|
} else {
|
||||||
$data = json_decode($response, true);
|
$data = json_decode($response, true);
|
||||||
$part = $data['candidates'][0]['content']['parts'][0] ?? null;
|
$part = $data['candidates'][0]['content']['parts'][0] ?? null;
|
||||||
@@ -97,10 +121,11 @@ foreach ($candidateModels as $model) {
|
|||||||
'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4'
|
'mimeType' => $part['inlineData']['mimeType'] ?? 'audio/mp4'
|
||||||
];
|
];
|
||||||
$successfulModel = $model;
|
$successfulModel = $model;
|
||||||
echo "✅ SUCCESS!\n";
|
echo " ✅ SUCCESS!\n\n";
|
||||||
break; // Stop at first successful model
|
break; // Stop at first successful model
|
||||||
} else {
|
} 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