From 07123b70ada23f2ee2c7620b8710f7ce0dc86a79 Mon Sep 17 00:00:00 2001 From: ChenZhaoYu <790348264@qq.com> Date: Tue, 28 Mar 2023 13:20:55 +0800 Subject: [PATCH] chore: rolled back to clear the impact --- service/src/index.ts | 12 +-- src/views/chat/index.vue | 178 +++++++++++++++++++-------------------- 2 files changed, 90 insertions(+), 100 deletions(-) diff --git a/service/src/index.ts b/service/src/index.ts index f2e0e68..28041d1 100644 --- a/service/src/index.ts +++ b/service/src/index.ts @@ -29,16 +29,8 @@ router.post('/chat-process', [auth, limiter], async (req, res) => { message: prompt, lastContext: options, process: (chat: ChatMessage) => { - if (firstChunk) { - res.write(`${JSON.stringify(chat)}t1h1i4s5i1s4a1s9i1l9l8y1s0plit`) - firstChunk = false - } - else { - let tmp = chat.delta - if (!(chat.delta)) - tmp = '' - res.write(tmp) - } + res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`) + firstChunk = false }, systemMessage, }) diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index c511921..c37466a 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -107,9 +107,7 @@ async function onConversation() { scrollToBottom() try { - const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit' - let renderText = '' - let firstTime = true + let lastText = '' const fetchChatAPIOnce = async () => { await fetchChatAPIProcess({ prompt: message, @@ -119,49 +117,42 @@ async function onConversation() { const xhr = event.target const { responseText } = xhr // Always process the final line - - const splitIndexBegin = responseText.search(magicSplit) - if (splitIndexBegin !== -1) { - const splitIndexEnd = splitIndexBegin + magicSplit.length - - const firstChunk = responseText.substring(0, splitIndexBegin) - const deltaText = responseText.substring(splitIndexEnd) - try { - const data = JSON.parse(firstChunk) - if (firstTime) { - firstTime = false - renderText = data.text ?? '' - } - else { - renderText = deltaText ?? '' - } - updateChat( - +uuid, - dataSources.value.length - 1, - { - dateTime: new Date().toLocaleString(), - text: renderText, - inversion: false, - error: false, - loading: false, - conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, - requestOptions: { prompt: message, ...options }, - }, - ) - - if (openLongReply && data.detail.choices[0].finish_reason === 'length') { - options.parentMessageId = data.id - message = '' - return fetchChatAPIOnce() - } - } - catch (error) { - // + const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2) + let chunk = responseText + if (lastIndex !== -1) + chunk = responseText.substring(lastIndex) + try { + const data = JSON.parse(chunk) + updateChat( + +uuid, + dataSources.value.length - 1, + { + dateTime: new Date().toLocaleString(), + text: lastText + data.text ?? '', + inversion: false, + error: false, + loading: false, + conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, + requestOptions: { prompt: message, options: { ...options } }, + }, + ) + + if (openLongReply && data.detail.choices[0].finish_reason === 'length') { + options.parentMessageId = data.id + lastText = data.text + message = '' + return fetchChatAPIOnce() } + + scrollToBottomIfAtBottom() + } + catch (error) { + // } }, }) } + await fetchChatAPIOnce() } catch (error: any) { @@ -246,9 +237,7 @@ async function onRegenerate(index: number) { ) try { - const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit' - let renderText = '' - let firstTime = true + let lastText = '' const fetchChatAPIOnce = async () => { await fetchChatAPIProcess({ prompt: message, @@ -258,46 +247,36 @@ async function onRegenerate(index: number) { const xhr = event.target const { responseText } = xhr // Always process the final line - - const splitIndexBegin = responseText.search(magicSplit) - if (splitIndexBegin !== -1) { - const splitIndexEnd = splitIndexBegin + magicSplit.length - - const firstChunk = responseText.substring(0, splitIndexBegin) - const deltaText = responseText.substring(splitIndexEnd) - try { - const data = JSON.parse(firstChunk) - if (firstTime) { - firstTime = false - renderText = data.text ?? '' - } - else { - renderText = deltaText ?? '' - } - updateChat( - +uuid, - index, - { - dateTime: new Date().toLocaleString(), - text: renderText, - inversion: false, - error: false, - loading: false, - conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, - requestOptions: { prompt: message, ...options }, - }, - ) - - if (openLongReply && data.detail.choices[0].finish_reason === 'length') { - options.parentMessageId = data.id - message = '' - return fetchChatAPIOnce() - } - } - catch (error) { - // + const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2) + let chunk = responseText + if (lastIndex !== -1) + chunk = responseText.substring(lastIndex) + try { + const data = JSON.parse(chunk) + updateChat( + +uuid, + index, + { + dateTime: new Date().toLocaleString(), + text: lastText + data.text ?? '', + inversion: false, + error: false, + loading: false, + conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id }, + requestOptions: { prompt: message, ...options }, + }, + ) + + if (openLongReply && data.detail.choices[0].finish_reason === 'length') { + options.parentMessageId = data.id + lastText = data.text + message = '' + return fetchChatAPIOnce() } } + catch (error) { + // + } }, }) } @@ -488,13 +467,20 @@ onUnmounted(() => {