fix: chat response truncated at 'Ingredienti:' (MAX_TOKENS)
- 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
This commit is contained in:
+6
-12
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user