From bd409a5f8e7fad37b4dd7a0049988149a2037182 Mon Sep 17 00:00:00 2001 From: assassinliujie <68693675+assassinliujie@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:37:27 +0800 Subject: [PATCH 1/3] Update index.ts --- service/src/index.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/service/src/index.ts b/service/src/index.ts index 28041d1c70..64564c9e05 100644 --- a/service/src/index.ts +++ b/service/src/index.ts @@ -25,12 +25,21 @@ router.post('/chat-process', [auth, limiter], async (req, res) => { try { const { prompt, options = {}, systemMessage } = req.body as RequestProps let firstChunk = true + let chatLength = 0 + let newChatLength = 0 await chatReplyProcess({ message: prompt, lastContext: options, process: (chat: ChatMessage) => { - res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`) - firstChunk = false + if (firstChunk) { + res.write(`${JSON.stringify(chat)}t1h1i4s5i1s4a1s9i1l9l8y1s0plit`) + firstChunk = false + } + else if (chatLength != chat.text.length) { + newChatLength = chat.text.length + res.write(chat.text.substring(chatLength, newChatLength)) + chatLength = newChatLength + } }, systemMessage, }) From 3447ca5d3239ae758fe5bb8da2601e15ba6f4072 Mon Sep 17 00:00:00 2001 From: assassinliujie <68693675+assassinliujie@users.noreply.github.com> Date: Tue, 28 Mar 2023 14:39:37 +0800 Subject: [PATCH 2/3] Update index.vue --- src/views/chat/index.vue | 178 ++++++++++++++++++++------------------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/src/views/chat/index.vue b/src/views/chat/index.vue index c37466ae81..c511921f2e 100644 --- a/src/views/chat/index.vue +++ b/src/views/chat/index.vue @@ -107,7 +107,9 @@ async function onConversation() { scrollToBottom() try { - let lastText = '' + const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit' + let renderText = '' + let firstTime = true const fetchChatAPIOnce = async () => { await fetchChatAPIProcess({ prompt: message, @@ -117,42 +119,49 @@ async function onConversation() { const xhr = event.target const { responseText } = xhr // Always process the final line - 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) { - // + 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) { + // + } } }, }) } - await fetchChatAPIOnce() } catch (error: any) { @@ -237,7 +246,9 @@ async function onRegenerate(index: number) { ) try { - let lastText = '' + const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit' + let renderText = '' + let firstTime = true const fetchChatAPIOnce = async () => { await fetchChatAPIProcess({ prompt: message, @@ -247,35 +258,45 @@ async function onRegenerate(index: number) { const xhr = event.target const { responseText } = xhr // Always process the final line - 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() + + 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) { + // } - } - catch (error) { - // } }, }) @@ -467,20 +488,13 @@ onUnmounted(() => {