diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js index d1b908efbb..96f4bb1b04 100644 --- a/api/app/clients/OpenAIClient.js +++ b/api/app/clients/OpenAIClient.js @@ -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'; }