From 83e6a23441c0b18991967b54414c2605ff99b226 Mon Sep 17 00:00:00 2001 From: WenjiaoYue Date: Thu, 27 Jun 2024 10:24:09 +0800 Subject: [PATCH] update chinese response format (#332) * ChatQnA chinese version Signed-off-by: Yue, Wenjiao * format chinese response * update chinese format response * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Signed-off-by: Yue, Wenjiao Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- .../src/lib/modules/chat/ChatMessage.svelte | 2 +- .../docker/ui/svelte/src/routes/+page.svelte | 31 ++++++++++++------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/ChatQnA/docker/ui/svelte/src/lib/modules/chat/ChatMessage.svelte b/ChatQnA/docker/ui/svelte/src/lib/modules/chat/ChatMessage.svelte index cdba13b87..b13699708 100644 --- a/ChatQnA/docker/ui/svelte/src/lib/modules/chat/ChatMessage.svelte +++ b/ChatQnA/docker/ui/svelte/src/lib/modules/chat/ChatMessage.svelte @@ -44,7 +44,7 @@

{@html msg.content}

diff --git a/ChatQnA/docker/ui/svelte/src/routes/+page.svelte b/ChatQnA/docker/ui/svelte/src/routes/+page.svelte index 10a71cb05..9e520f563 100644 --- a/ChatQnA/docker/ui/svelte/src/routes/+page.svelte +++ b/ChatQnA/docker/ui/svelte/src/routes/+page.svelte @@ -46,7 +46,6 @@ let scrollToDiv: HTMLDivElement; // ········· let chatMessages: Message[] = data.chatMsg ? data.chatMsg : []; - console.log("chatMessages", chatMessages); // ·············· @@ -59,15 +58,13 @@ }); function handleTop() { - console.log("top"); - scrollToTop(scrollToDiv); } function storeMessages() { localStorage.setItem( LOCAL_STORAGE_KEY.STORAGE_CHAT_KEY, - JSON.stringify(chatMessages), + JSON.stringify(chatMessages) ); } @@ -76,13 +73,17 @@ .split("\\x") .slice(1) .map((byte) => parseInt(byte, 16)); - return new TextDecoder("utf-8").decode(new Uint8Array(byteArray)); + const decoded = new TextDecoder("utf-8").decode(new Uint8Array(byteArray)); + + return decoded; } function decodeUnicode(str: string): string { - return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => { + const decoded = str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => { return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16)); }); + + return decoded; } const callTextStream = async (query: string, startSendTime: number) => { @@ -92,16 +93,22 @@ let Msg = e.data; if (Msg.startsWith("b")) { let currentMsg = Msg.slice(2, -1); + const containsNewLine = /\\n/.test(currentMsg); + let requiresDecoding = false; + + currentMsg = currentMsg.replace(/\\n/g, "\n"); if (/\\x[\dA-Fa-f]{2}/.test(currentMsg)) { currentMsg = decodeEscapedBytes(currentMsg); + requiresDecoding = true; } else if (/\\u[\dA-Fa-f]{4}/.test(currentMsg)) { currentMsg = decodeUnicode(currentMsg); - } - if (currentMsg !== "") { - currentMsg = currentMsg.replace(/\\n/g, "\n"); + requiresDecoding = true; } + if (containsNewLine && requiresDecoding) { + currentMsg += "\n"; + } if (chatMessages[chatMessages.length - 1].role == MessageRole.User) { chatMessages = [ ...chatMessages, @@ -122,7 +129,7 @@ loading = false; let totalTime = parseFloat( - ((getCurrentTimeStamp() - startTime) / 1000).toFixed(2), + ((getCurrentTimeStamp() - startTime) / 1000).toFixed(2) ); if (chatMessages.length - 1 !== -1) { @@ -136,7 +143,6 @@ }; const handleTextSubmit = async () => { - console.log("handleTextSubmit"); loading = true; const newMessage = { @@ -234,7 +240,8 @@ >CLEARCLEAR