From 195cfd4f843e528b219011633c8ba477e4587b4d Mon Sep 17 00:00:00 2001 From: Rohit Das <43847374+therohitdas@users.noreply.github.com> Date: Mon, 11 Dec 2023 15:58:49 +0530 Subject: [PATCH 1/3] wip: displaying error in a toast --- src/routes/(app)/+page.svelte | 130 ++++++++++++++------------- src/routes/(app)/c/[id]/+page.svelte | 130 ++++++++++++++------------- 2 files changed, 140 insertions(+), 120 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index c0d210fc..e7d522b1 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -191,80 +191,90 @@ }) }); - const reader = res.body - .pipeThrough(new TextDecoderStream()) - .pipeThrough(splitStream('\n')) - .getReader(); - - while (true) { - const { value, done } = await reader.read(); - if (done || stopResponseFlag || _chatId !== $chatId) { - responseMessage.done = true; - messages = messages; - break; + if (!res.ok) { + const error = await res.json(); + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } else { + toast.error(error.error); } + } else { + const reader = res.body + .pipeThrough(new TextDecoderStream()) + .pipeThrough(splitStream('\n')) + .getReader(); - try { - let lines = value.split('\n'); + while (true) { + const { value, done } = await reader.read(); + if (done || stopResponseFlag || _chatId !== $chatId) { + responseMessage.done = true; + messages = messages; + break; + } - for (const line of lines) { - if (line !== '') { - console.log(line); - let data = JSON.parse(line); + try { + let lines = value.split('\n'); - if ('detail' in data) { - throw data; - } + for (const line of lines) { + if (line !== '') { + console.log(line); + let data = JSON.parse(line); - if (data.done == false) { - if (responseMessage.content == '' && data.message.content == '\n') { - continue; + if ('detail' in data) { + throw data; + } + + if (data.done == false) { + if (responseMessage.content == '' && data.message.content == '\n') { + continue; + } else { + responseMessage.content += data.message.content; + messages = messages; + } } else { - responseMessage.content += data.message.content; + responseMessage.done = true; + responseMessage.context = data.context ?? null; + responseMessage.info = { + total_duration: data.total_duration, + prompt_eval_count: data.prompt_eval_count, + prompt_eval_duration: data.prompt_eval_duration, + eval_count: data.eval_count, + eval_duration: data.eval_duration + }; messages = messages; } - } else { - responseMessage.done = true; - responseMessage.context = data.context ?? null; - responseMessage.info = { - total_duration: data.total_duration, - prompt_eval_count: data.prompt_eval_count, - prompt_eval_duration: data.prompt_eval_duration, - eval_count: data.eval_count, - eval_duration: data.eval_duration - }; - messages = messages; } } + } catch (error) { + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } + break; } - } catch (error) { - console.log(error); - if ('detail' in error) { - toast.error(error.detail); + + if (autoScroll) { + window.scrollTo({ top: document.body.scrollHeight }); } - break; - } - if (autoScroll) { - window.scrollTo({ top: document.body.scrollHeight }); + await $db.updateChatById(_chatId, { + title: title === '' ? 'New Chat' : title, + models: selectedModels, + system: $settings.system ?? undefined, + options: { + seed: $settings.seed ?? undefined, + temperature: $settings.temperature ?? undefined, + repeat_penalty: $settings.repeat_penalty ?? undefined, + top_k: $settings.top_k ?? undefined, + top_p: $settings.top_p ?? undefined, + num_ctx: $settings.num_ctx ?? undefined, + ...($settings.options ?? {}) + }, + messages: messages, + history: history + }); } - - await $db.updateChatById(_chatId, { - title: title === '' ? 'New Chat' : title, - models: selectedModels, - system: $settings.system ?? undefined, - options: { - seed: $settings.seed ?? undefined, - temperature: $settings.temperature ?? undefined, - repeat_penalty: $settings.repeat_penalty ?? undefined, - top_k: $settings.top_k ?? undefined, - top_p: $settings.top_p ?? undefined, - num_ctx: $settings.num_ctx ?? undefined, - ...($settings.options ?? {}) - }, - messages: messages, - history: history - }); } stopResponseFlag = false; diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index fd61a4dc..ff2a9b17 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -218,80 +218,90 @@ }) }); - const reader = res.body - .pipeThrough(new TextDecoderStream()) - .pipeThrough(splitStream('\n')) - .getReader(); - - while (true) { - const { value, done } = await reader.read(); - if (done || stopResponseFlag || _chatId !== $chatId) { - responseMessage.done = true; - messages = messages; - break; + if (!res.ok) { + const error = await res.json(); + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } else { + toast.error(error.error); } + } else { + const reader = res.body + .pipeThrough(new TextDecoderStream()) + .pipeThrough(splitStream('\n')) + .getReader(); - try { - let lines = value.split('\n'); + while (true) { + const { value, done } = await reader.read(); + if (done || stopResponseFlag || _chatId !== $chatId) { + responseMessage.done = true; + messages = messages; + break; + } - for (const line of lines) { - if (line !== '') { - console.log(line); - let data = JSON.parse(line); + try { + let lines = value.split('\n'); - if ('detail' in data) { - throw data; - } + for (const line of lines) { + if (line !== '') { + console.log(line); + let data = JSON.parse(line); - if (data.done == false) { - if (responseMessage.content == '' && data.message.content == '\n') { - continue; + if ('detail' in data) { + throw data; + } + + if (data.done == false) { + if (responseMessage.content == '' && data.message.content == '\n') { + continue; + } else { + responseMessage.content += data.message.content; + messages = messages; + } } else { - responseMessage.content += data.message.content; + responseMessage.done = true; + responseMessage.context = data.context ?? null; + responseMessage.info = { + total_duration: data.total_duration, + prompt_eval_count: data.prompt_eval_count, + prompt_eval_duration: data.prompt_eval_duration, + eval_count: data.eval_count, + eval_duration: data.eval_duration + }; messages = messages; } - } else { - responseMessage.done = true; - responseMessage.context = data.context ?? null; - responseMessage.info = { - total_duration: data.total_duration, - prompt_eval_count: data.prompt_eval_count, - prompt_eval_duration: data.prompt_eval_duration, - eval_count: data.eval_count, - eval_duration: data.eval_duration - }; - messages = messages; } } + } catch (error) { + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } + break; } - } catch (error) { - console.log(error); - if ('detail' in error) { - toast.error(error.detail); + + if (autoScroll) { + window.scrollTo({ top: document.body.scrollHeight }); } - break; - } - if (autoScroll) { - window.scrollTo({ top: document.body.scrollHeight }); + await $db.updateChatById(_chatId, { + title: title === '' ? 'New Chat' : title, + models: selectedModels, + system: $settings.system ?? undefined, + options: { + seed: $settings.seed ?? undefined, + temperature: $settings.temperature ?? undefined, + repeat_penalty: $settings.repeat_penalty ?? undefined, + top_k: $settings.top_k ?? undefined, + top_p: $settings.top_p ?? undefined, + num_ctx: $settings.num_ctx ?? undefined, + ...($settings.options ?? {}) + }, + messages: messages, + history: history + }); } - - await $db.updateChatById(_chatId, { - title: title === '' ? 'New Chat' : title, - models: selectedModels, - system: $settings.system ?? undefined, - options: { - seed: $settings.seed ?? undefined, - temperature: $settings.temperature ?? undefined, - repeat_penalty: $settings.repeat_penalty ?? undefined, - top_k: $settings.top_k ?? undefined, - top_p: $settings.top_p ?? undefined, - num_ctx: $settings.num_ctx ?? undefined, - ...($settings.options ?? {}) - }, - messages: messages, - history: history - }); } stopResponseFlag = false; From 5262156c248394a9e544eb0fe22b5e769970ae61 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 13 Dec 2023 13:05:31 -0800 Subject: [PATCH 2/3] feat: error handling for when ollama is not running --- src/routes/(app)/+page.svelte | 25 ++++++++++++++++--------- src/routes/(app)/c/[id]/+page.svelte | 25 ++++++++++++++++--------- 2 files changed, 32 insertions(+), 18 deletions(-) diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index e7d522b1..f6bf0e35 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -189,17 +189,12 @@ }, format: $settings.requestFormat ?? undefined }) + }).catch((err) => { + console.log(err); + return null; }); - if (!res.ok) { - const error = await res.json(); - console.log(error); - if ('detail' in error) { - toast.error(error.detail); - } else { - toast.error(error.error); - } - } else { + if (res && res.ok) { const reader = res.body .pipeThrough(new TextDecoderStream()) .pipeThrough(splitStream('\n')) @@ -275,6 +270,18 @@ history: history }); } + } else { + if (res !== null) { + const error = await res.json(); + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } else { + toast.error(error.error); + } + } else { + toast.error(`Uh-oh! There was an issue connecting to Ollama.`); + } } stopResponseFlag = false; diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index ff2a9b17..3eec7a5e 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -216,17 +216,12 @@ }, format: $settings.requestFormat ?? undefined }) + }).catch((err) => { + console.log(err); + return null; }); - if (!res.ok) { - const error = await res.json(); - console.log(error); - if ('detail' in error) { - toast.error(error.detail); - } else { - toast.error(error.error); - } - } else { + if (res && res.ok) { const reader = res.body .pipeThrough(new TextDecoderStream()) .pipeThrough(splitStream('\n')) @@ -302,6 +297,18 @@ history: history }); } + } else { + if (res !== null) { + const error = await res.json(); + console.log(error); + if ('detail' in error) { + toast.error(error.detail); + } else { + toast.error(error.error); + } + } else { + toast.error(`Uh-oh! There was an issue connecting to Ollama.`); + } } stopResponseFlag = false; From 75fb94ec68b4846fa5a9831c4c724550c1f058b1 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Wed, 13 Dec 2023 15:31:07 -0800 Subject: [PATCH 3/3] feat: show error message in chat --- src/lib/components/chat/Messages.svelte | 27 ++++++++++++++++++++++++- src/routes/(app)/+page.svelte | 8 ++++++++ src/routes/(app)/c/[id]/+page.svelte | 8 ++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/lib/components/chat/Messages.svelte b/src/lib/components/chat/Messages.svelte index aa7bde72..ad691bf1 100644 --- a/src/lib/components/chat/Messages.svelte +++ b/src/lib/components/chat/Messages.svelte @@ -655,7 +655,32 @@ {:else}
- {@html marked(message.content.replace('\\\\', '\\\\\\'))} + {#if message?.error === true} +
+ + + + +
+ {message.content} +
+
+ {:else} + {@html marked(message.content.replace('\\\\', '\\\\\\'))} + {/if} {#if message.done}
diff --git a/src/routes/(app)/+page.svelte b/src/routes/(app)/+page.svelte index f6bf0e35..44c92397 100644 --- a/src/routes/(app)/+page.svelte +++ b/src/routes/(app)/+page.svelte @@ -276,12 +276,20 @@ console.log(error); if ('detail' in error) { toast.error(error.detail); + responseMessage.content = error.detail; } else { toast.error(error.error); + responseMessage.content = error.error; } } else { toast.error(`Uh-oh! There was an issue connecting to Ollama.`); + responseMessage.content = `Uh-oh! There was an issue connecting to Ollama.`; } + + responseMessage.error = true; + responseMessage.content = `Uh-oh! There was an issue connecting to Ollama.`; + responseMessage.done = true; + messages = messages; } stopResponseFlag = false; diff --git a/src/routes/(app)/c/[id]/+page.svelte b/src/routes/(app)/c/[id]/+page.svelte index 3eec7a5e..c2685af5 100644 --- a/src/routes/(app)/c/[id]/+page.svelte +++ b/src/routes/(app)/c/[id]/+page.svelte @@ -303,12 +303,20 @@ console.log(error); if ('detail' in error) { toast.error(error.detail); + responseMessage.content = error.detail; } else { toast.error(error.error); + responseMessage.content = error.error; } } else { toast.error(`Uh-oh! There was an issue connecting to Ollama.`); + responseMessage.content = `Uh-oh! There was an issue connecting to Ollama.`; } + + responseMessage.error = true; + responseMessage.content = `Uh-oh! There was an issue connecting to Ollama.`; + responseMessage.done = true; + messages = messages; } stopResponseFlag = false;