From 8b9e99a8a1fcc9b4ee022d64a6852b7f01814c11 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sun, 28 Jan 2024 14:47:45 -0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20lm=20studio=20bug=20when?= =?UTF-8?q?=20empty=20messages=20are=20sent?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/llm/llms/OpenAI.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/core/llm/llms/OpenAI.ts b/core/llm/llms/OpenAI.ts index 2e4681d46..7c79ea515 100644 --- a/core/llm/llms/OpenAI.ts +++ b/core/llm/llms/OpenAI.ts @@ -178,6 +178,15 @@ class OpenAI extends BaseLLM { return; } + let body = { + ...this._convertArgs(options, messages), + stream: true, + }; + // Empty messages cause an error in LM Studio + body.messages = body.messages.map((m) => ({ + ...m, + content: m.content === "" ? " " : m.content, + })) as any; const response = await this.fetch(this._getChatUrl(), { method: "POST", headers: { @@ -185,10 +194,7 @@ class OpenAI extends BaseLLM { Authorization: `Bearer ${this.apiKey}`, "api-key": this.apiKey || "", // For Azure }, - body: JSON.stringify({ - ...this._convertArgs(options, messages), - stream: true, - }), + body: JSON.stringify(body), }); for await (const value of streamSse(response)) {