From c854577930889fd703131c74a5a9a3e3024d3594 Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 30 Jan 2024 10:13:32 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=9Dfix:=20Re-order=20System=20Message?= =?UTF-8?q?=20to=20Top=20for=20Mistral=20API=20Payloads=20(#1678)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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` --- app/clients/OpenAIClient.js | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/app/clients/OpenAIClient.js b/app/clients/OpenAIClient.js index d1b908efbb5..96f4bb1b04f 100644 --- a/app/clients/OpenAIClient.js +++ b/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'; }