Skip to content

Commit 2f47231

Browse files
authored
Adapt Chinese characters (#327)
Signed-off-by: Yue, Wenjiao <[email protected]>
1 parent 6a3e9db commit 2f47231

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

ChatQnA/docker/ui/svelte/src/routes/+page.svelte

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,20 +65,43 @@
6565
}
6666
6767
function storeMessages() {
68-
6968
localStorage.setItem(
7069
LOCAL_STORAGE_KEY.STORAGE_CHAT_KEY,
71-
JSON.stringify(chatMessages)
70+
JSON.stringify(chatMessages),
7271
);
7372
}
7473
74+
function decodeEscapedBytes(str: string): string {
75+
const byteArray = str
76+
.split("\\x")
77+
.slice(1)
78+
.map((byte) => parseInt(byte, 16));
79+
return new TextDecoder("utf-8").decode(new Uint8Array(byteArray));
80+
}
81+
82+
function decodeUnicode(str: string): string {
83+
return str.replace(/\\u[\dA-Fa-f]{4}/g, (match) => {
84+
return String.fromCharCode(parseInt(match.replace(/\\u/g, ""), 16));
85+
});
86+
}
87+
7588
const callTextStream = async (query: string, startSendTime: number) => {
7689
const eventSource = await fetchTextStream(query, knowledge_1);
7790
7891
eventSource.addEventListener("message", (e: any) => {
7992
let Msg = e.data;
8093
if (Msg.startsWith("b")) {
81-
const currentMsg = Msg.slice(2, -1);
94+
let currentMsg = Msg.slice(2, -1);
95+
96+
if (/\\x[\dA-Fa-f]{2}/.test(currentMsg)) {
97+
currentMsg = decodeEscapedBytes(currentMsg);
98+
} else if (/\\u[\dA-Fa-f]{4}/.test(currentMsg)) {
99+
currentMsg = decodeUnicode(currentMsg);
100+
}
101+
if (currentMsg !== "</s>") {
102+
currentMsg = currentMsg.replace(/\\n/g, "\n");
103+
}
104+
82105
if (chatMessages[chatMessages.length - 1].role == MessageRole.User) {
83106
chatMessages = [
84107
...chatMessages,
@@ -99,7 +122,7 @@
99122
100123
loading = false;
101124
let totalTime = parseFloat(
102-
((getCurrentTimeStamp() - startTime) / 1000).toFixed(2)
125+
((getCurrentTimeStamp() - startTime) / 1000).toFixed(2),
103126
);
104127
105128
if (chatMessages.length - 1 !== -1) {

0 commit comments

Comments
 (0)