[FIX] Validate messages schema for gemini provider ()

validate messages schema for gemini provider
This commit is contained in:
Sean Hatfield 2024-05-10 17:33:25 -07:00 committed by GitHub
parent 7b18a36288
commit 948ac8a3dd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -114,6 +114,24 @@ class GeminiLLM {
allMessages[allMessages.length - 1].role === "user"
)
allMessages.pop();
// Validate that after every user message, there is a model message
// sometimes when using gemini we try to compress messages in order to retain as
// much context as possible but this may mess up the order of the messages that the gemini model expects
// we do this check to work around the edge case where 2 user prompts may be next to each other, in the message array
for (let i = 0; i < allMessages.length; i++) {
if (
allMessages[i].role === "user" &&
i < allMessages.length - 1 &&
allMessages[i + 1].role !== "model"
) {
allMessages.splice(i + 1, 0, {
role: "model",
parts: [{ text: "Okay." }],
});
}
}
return allMessages;
}