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.
This commit is contained in:
Raven Scott 2023-04-10 02:53:37 +02:00
parent 27d8ad1f78
commit c14635769d
1 changed files with 5 additions and 4 deletions

View File

@ -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);
}