Skip to content
Snippets Groups Projects
Commit 3f5b2485 authored by timothycarambat's avatar timothycarambat
Browse files

handle chat edge-case for response object in text field

parent 466bf7dc
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,22 @@ import System from "@/models/system";
import ModalWrapper from "@/components/ModalWrapper";
import { useModal } from "@/hooks/useModal";
// Some LLMs may return a "valid" response that truncation fails to truncate because
// it stored an Object as opposed to a string for the `text` field.
function parseText(jsonResponse = "") {
try {
const json = JSON.parse(jsonResponse);
if (!json.hasOwnProperty("text"))
throw new Error('JSON response has no property "text".');
return typeof json.text !== "string"
? JSON.stringify(json.text)
: json.text;
} catch (e) {
console.error(e);
return "--failed to parse--";
}
}
export default function ChatRow({ chat, onDelete }) {
const {
isOpen: isPromptOpen,
......@@ -47,7 +63,7 @@ export default function ChatRow({ chat, onDelete }) {
onClick={openResponseModal}
className="px-6 py-4 cursor-pointer transform transition-transform duration-200 hover:scale-105 hover:shadow-lg"
>
{truncate(JSON.parse(chat.response)?.text, 40)}
{truncate(parseText(chat.response), 40)}
</td>
<td className="px-6 py-4">{chat.createdAt}</td>
<td className="px-6 py-4 flex items-center gap-x-6">
......@@ -64,7 +80,7 @@ export default function ChatRow({ chat, onDelete }) {
</ModalWrapper>
<ModalWrapper isOpen={isResponseOpen}>
<TextPreview
text={JSON.parse(chat.response)?.text}
text={parseText(chat.response)}
closeModal={closeResponseModal}
/>
</ModalWrapper>
......
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