handle chat edge-case for response object in text field

This commit is contained in:
timothycarambat 2024-07-31 11:31:22 -07:00
parent 466bf7dc9c
commit 3f5b2485d7

View file

@ -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>