Skip to content

Commit

Permalink
🔝fix: Re-order System Message to Top for Mistral API Payloads (danny-…
Browse files Browse the repository at this point in the history
…avila#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`
  • Loading branch information
danny-avila authored Jan 30, 2024
1 parent 42509d8 commit c854577
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down

0 comments on commit c854577

Please sign in to comment.