From 546287978328a2d2b6490537f216eca5a1c74a3a Mon Sep 17 00:00:00 2001 From: dadaloop82 Date: Sun, 10 May 2026 14:19:41 +0000 Subject: [PATCH] fix: chat response truncated at 'Ingredienti:' (MAX_TOKENS) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Move system prompt to systemInstruction API field instead of injecting it as a fake user/model turn, saving the full turn's token count from the context window used for generation - Increase maxOutputTokens from 1500 to 4096 so full recipes (with ingredients + instructions) can complete without being cut off - Increase API timeout from 60 to 90 seconds for longer responses finish_reason changes from MAX_TOKENS → STOP, reply goes from 265 to 2108 chars --- api/index.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/api/index.php b/api/index.php index 591ad9a..ad8f9ae 100644 --- a/api/index.php +++ b/api/index.php @@ -2848,18 +2848,9 @@ REGOLE: PROMPT; // Build conversation for Gemini + // systemInstruction is passed separately in the payload; contents only contains the actual chat turns. $contents = []; - // System instruction as first user+model turn - $contents[] = [ - 'role' => 'user', - 'parts' => [['text' => $systemPrompt]] - ]; - $contents[] = [ - 'role' => 'model', - 'parts' => [['text' => 'Ciao! Sono il tuo assistente cucina. Conosco tutto quello che hai in dispensa e sono pronto ad aiutarti. Cosa ti va di preparare? 😊']] - ]; - // Add conversation history foreach ($history as $msg) { $role = ($msg['role'] === 'user') ? 'user' : 'model'; @@ -2877,13 +2868,16 @@ PROMPT; $payload = [ 'contents' => $contents, + 'systemInstruction' => [ + 'parts' => [['text' => $systemPrompt]] + ], 'generationConfig' => [ 'temperature' => 0.8, - 'maxOutputTokens' => 1500 + 'maxOutputTokens' => 4096 ] ]; - $result = callGeminiWithFallback($apiKey, $payload, 60); + $result = callGeminiWithFallback($apiKey, $payload, 90); $httpCode = $result['http_code']; if ($httpCode !== 200) {