From c8fe254d45134e2aeaaafbeed2f5ae84d93de4e4 Mon Sep 17 00:00:00 2001 From: Timothy Carambat <rambat1010@gmail.com> Date: Thu, 15 Aug 2024 14:22:27 -0700 Subject: [PATCH] Omit invalid `response.text` values and `prompts` (#2127) * Omit invalid `response.text` values and `prompts` resolves #2108 * remove import --- server/utils/helpers/chat/responses.js | 42 ++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 6 deletions(-) diff --git a/server/utils/helpers/chat/responses.js b/server/utils/helpers/chat/responses.js index 5eec61823..789b21242 100644 --- a/server/utils/helpers/chat/responses.js +++ b/server/utils/helpers/chat/responses.js @@ -63,9 +63,24 @@ function handleDefaultStreamResponseV2(response, stream, responseProps) { function convertToChatHistory(history = []) { const formattedHistory = []; - history.forEach((history) => { - const { prompt, response, createdAt, feedbackScore = null, id } = history; + for (const record of history) { + const { prompt, response, createdAt, feedbackScore = null, id } = record; const data = JSON.parse(response); + + // In the event that a bad response was stored - we should skip its entire record + // because it was likely an error and cannot be used in chats and will fail to render on UI. + if (typeof prompt !== "string") { + console.log( + `[convertToChatHistory] ChatHistory #${record.id} prompt property is not a string - skipping record.` + ); + continue; + } else if (typeof data.text !== "string") { + console.log( + `[convertToChatHistory] ChatHistory #${record.id} response.text property is not a string - skipping record.` + ); + continue; + } + formattedHistory.push([ { role: "user", @@ -84,21 +99,36 @@ function convertToChatHistory(history = []) { feedbackScore, }, ]); - }); + } return formattedHistory.flat(); } function convertToPromptHistory(history = []) { const formattedHistory = []; - history.forEach((history) => { - const { prompt, response } = history; + for (const record of history) { + const { prompt, response } = record; const data = JSON.parse(response); + + // In the event that a bad response was stored - we should skip its entire record + // because it was likely an error and cannot be used in chats and will fail to render on UI. + if (typeof prompt !== "string") { + console.log( + `[convertToPromptHistory] ChatHistory #${record.id} prompt property is not a string - skipping record.` + ); + continue; + } else if (typeof data.text !== "string") { + console.log( + `[convertToPromptHistory] ChatHistory #${record.id} response.text property is not a string - skipping record.` + ); + continue; + } + formattedHistory.push([ { role: "user", content: prompt }, { role: "assistant", content: data.text }, ]); - }); + } return formattedHistory.flat(); } -- GitLab