Skip to content
Snippets Groups Projects
Unverified Commit c8fe254d authored by Timothy Carambat's avatar Timothy Carambat Committed by GitHub
Browse files

Omit invalid `response.text` values and `prompts` (#2127)

* Omit invalid `response.text` values and `prompts`
resolves #2108

* remove import
parent df248d40
No related branches found
No related tags found
No related merge requests found
...@@ -63,9 +63,24 @@ function handleDefaultStreamResponseV2(response, stream, responseProps) { ...@@ -63,9 +63,24 @@ function handleDefaultStreamResponseV2(response, stream, responseProps) {
function convertToChatHistory(history = []) { function convertToChatHistory(history = []) {
const formattedHistory = []; const formattedHistory = [];
history.forEach((history) => { for (const record of history) {
const { prompt, response, createdAt, feedbackScore = null, id } = history; const { prompt, response, createdAt, feedbackScore = null, id } = record;
const data = JSON.parse(response); 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([ formattedHistory.push([
{ {
role: "user", role: "user",
...@@ -84,21 +99,36 @@ function convertToChatHistory(history = []) { ...@@ -84,21 +99,36 @@ function convertToChatHistory(history = []) {
feedbackScore, feedbackScore,
}, },
]); ]);
}); }
return formattedHistory.flat(); return formattedHistory.flat();
} }
function convertToPromptHistory(history = []) { function convertToPromptHistory(history = []) {
const formattedHistory = []; const formattedHistory = [];
history.forEach((history) => { for (const record of history) {
const { prompt, response } = history; const { prompt, response } = record;
const data = JSON.parse(response); 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([ formattedHistory.push([
{ role: "user", content: prompt }, { role: "user", content: prompt },
{ role: "assistant", content: data.text }, { role: "assistant", content: data.text },
]); ]);
}); }
return formattedHistory.flat(); return formattedHistory.flat();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment