From 14ddfec9b7dbc83e521c9ab149d1b08f5d24964d Mon Sep 17 00:00:00 2001 From: Danny Avila Date: Tue, 20 Aug 2024 07:00:43 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=A7=B9=20chore:=20address=20minor=20issue?= =?UTF-8?q?s=20(#3710)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * refactor(OpenAIClient): improve error handling in chat completion Improve error handling in the chat completion process to handle scenarios where the response has no choices or the message is undefined. This ensures that the intermediate reply is returned in such cases. Also, add logging statements for better debugging. * refactor(Markdown): only show blinking cursor for latest message * refactor: add submitting selector to prevent blinking cursor for empty, non-streaming, non-latest messages --- api/app/clients/OpenAIClient.js | 17 +++++++++++++---- .../Chat/Messages/Content/Markdown.tsx | 2 +- .../Chat/Messages/Content/MessageContent.tsx | 1 + .../components/Chat/Messages/Content/Part.tsx | 1 + client/src/style.css | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/api/app/clients/OpenAIClient.js b/api/app/clients/OpenAIClient.js index efb09a6ed3..0d9e43e196 100644 --- a/api/app/clients/OpenAIClient.js +++ b/api/app/clients/OpenAIClient.js @@ -1250,14 +1250,23 @@ ${convo} throw new Error('Chat completion failed'); } - const { message, finish_reason } = chatCompletion.choices[0]; - if (chatCompletion) { - this.metadata = { finish_reason }; + const { choices } = chatCompletion; + if (!Array.isArray(choices) || choices.length === 0) { + logger.warn('[OpenAIClient] Chat completion response has no choices'); + return intermediateReply; } + const { message, finish_reason } = choices[0] ?? {}; + this.metadata = { finish_reason }; + logger.debug('[OpenAIClient] chatCompletion response', chatCompletion); - if (!message?.content?.trim() && intermediateReply.length) { + if (!message) { + logger.warn('[OpenAIClient] Message is undefined in chatCompletion response'); + return intermediateReply; + } + + if (typeof message.content !== 'string' || message.content.trim() === '') { logger.debug( '[OpenAIClient] chatCompletion: using intermediateReply due to empty message.content', { intermediateReply }, diff --git a/client/src/components/Chat/Messages/Content/Markdown.tsx b/client/src/components/Chat/Messages/Content/Markdown.tsx index 07c64d6443..be66f61c8e 100644 --- a/client/src/components/Chat/Messages/Content/Markdown.tsx +++ b/client/src/components/Chat/Messages/Content/Markdown.tsx @@ -144,7 +144,7 @@ const Markdown = memo(({ content = '', isEdited, showCursor, isLatestMessage }: return (

- +

); diff --git a/client/src/components/Chat/Messages/Content/MessageContent.tsx b/client/src/components/Chat/Messages/Content/MessageContent.tsx index d191aecb87..e0f6b70d07 100644 --- a/client/src/components/Chat/Messages/Content/MessageContent.tsx +++ b/client/src/components/Chat/Messages/Content/MessageContent.tsx @@ -77,6 +77,7 @@ const DisplayMessage = ({ text, isCreatedByUser, message, showCursor }: TDisplay