From c14635769daf733a0cfda24b9040e809883aa369 Mon Sep 17 00:00:00 2001 From: Raven Scott Date: Mon, 10 Apr 2023 02:53:37 +0200 Subject: [PATCH] feat: Add error handling for empty response in chatbot conversation This commit adds a new block of code to handle cases where the chatbot generates an empty response, which was previously causing a delay of one message in the conversation. The new code selects a random message from a predefined list of human-like responses to notify the user that an error has occurred, then resets the conversation history and deletes the memory map for that user. This ensures that subsequent messages from the same user will not be delayed due to the empty response. --- llamabot.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/llamabot.js b/llamabot.js index 3f3365e..5a7d7d4 100644 --- a/llamabot.js +++ b/llamabot.js @@ -72,7 +72,7 @@ client.on('messageCreate', async (message) => { }); conversation.messages.push({ role: 'assistant', - content: `Hello, ${message.author.username}, how may I help you?` + content: `Hello, ${message.author.username}, I am here to answer any question you have, how may I help you?` }); conversation.messages.push({ role: 'user', @@ -139,6 +139,8 @@ async function generateResponse(conversation) { controller.abort(); }, 900000); + const messagesCopy = [...conversation.messages]; // create a copy of the messages array + try { const response = await fetch(`http://${process.env.ROOT_IP}:${process.env.ROOT_PORT}/v1/chat/completions`, { method: 'POST', @@ -147,7 +149,7 @@ async function generateResponse(conversation) { 'Content-Type': 'application/json' }, body: JSON.stringify({ - messages: conversation.messages + messages: messagesCopy // use the copy of the messages array }), signal: controller.signal }); @@ -163,8 +165,7 @@ async function generateResponse(conversation) { return sanitizedResponse; } catch (err) { - console.error(err); - return 'Oops, something went wrong!'; + throw err; } finally { clearTimeout(timeout); }