🔝fix: Re-order System Message to Top for Mistral API Payloads (#1678)

* fix: re-order System Message if Mistral AI API as it only allows System Message at start of Payload

* fix: re-introduce singular system message change role to `user` if `system`
This commit is contained in:
Danny Avila 2024-01-30 10:13:32 -05:00 committed by GitHub
parent c4fd8a38e3
commit 9fad1b2cae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -976,9 +976,22 @@ ${convo}
...opts,
});
/* hacky fix for Mistral AI API not allowing a singular system message in payload */
/* hacky fixes for Mistral AI API:
- Re-orders system message to the top of the messages payload, as not allowed anywhere else
- If there is only one message and it's a system message, change the role to user
*/
if (opts.baseURL.includes('https://api.mistral.ai/v1') && modelOptions.messages) {
const { messages } = modelOptions;
const systemMessageIndex = messages.findIndex((msg) => msg.role === 'system');
if (systemMessageIndex > 0) {
const [systemMessage] = messages.splice(systemMessageIndex, 1);
messages.unshift(systemMessage);
}
modelOptions.messages = messages;
if (messages.length === 1 && messages[0].role === 'system') {
modelOptions.messages[0].role = 'user';
}