perf: optimized output (#962)
* Update index.ts * Update index.vue * Update index.ts
This commit is contained in:
parent
c0a9fd5208
commit
78bcf7f4ce
|
@ -25,12 +25,21 @@ router.post('/chat-process', [auth, limiter], async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const { prompt, options = {}, systemMessage } = req.body as RequestProps
|
const { prompt, options = {}, systemMessage } = req.body as RequestProps
|
||||||
let firstChunk = true
|
let firstChunk = true
|
||||||
|
let chatLength = 0
|
||||||
|
let newChatLength = 0
|
||||||
await chatReplyProcess({
|
await chatReplyProcess({
|
||||||
message: prompt,
|
message: prompt,
|
||||||
lastContext: options,
|
lastContext: options,
|
||||||
process: (chat: ChatMessage) => {
|
process: (chat: ChatMessage) => {
|
||||||
res.write(firstChunk ? JSON.stringify(chat) : `\n${JSON.stringify(chat)}`)
|
if (firstChunk) {
|
||||||
|
res.write(`${JSON.stringify(chat)}t1h1i4s5i1s4a1s9i1l9l8y1s0plit`)
|
||||||
firstChunk = false
|
firstChunk = false
|
||||||
|
}
|
||||||
|
else if (chatLength !== chat.text.length) {
|
||||||
|
newChatLength = chat.text.length
|
||||||
|
res.write(chat.text.substring(chatLength, newChatLength))
|
||||||
|
chatLength = newChatLength
|
||||||
|
}
|
||||||
},
|
},
|
||||||
systemMessage,
|
systemMessage,
|
||||||
})
|
})
|
||||||
|
|
|
@ -107,7 +107,9 @@ async function onConversation() {
|
||||||
scrollToBottom()
|
scrollToBottom()
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let lastText = ''
|
const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit'
|
||||||
|
let renderText = ''
|
||||||
|
let firstTime = true
|
||||||
const fetchChatAPIOnce = async () => {
|
const fetchChatAPIOnce = async () => {
|
||||||
await fetchChatAPIProcess<Chat.ConversationResponse>({
|
await fetchChatAPIProcess<Chat.ConversationResponse>({
|
||||||
prompt: message,
|
prompt: message,
|
||||||
|
@ -117,42 +119,49 @@ async function onConversation() {
|
||||||
const xhr = event.target
|
const xhr = event.target
|
||||||
const { responseText } = xhr
|
const { responseText } = xhr
|
||||||
// Always process the final line
|
// Always process the final line
|
||||||
const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2)
|
|
||||||
let chunk = responseText
|
const splitIndexBegin = responseText.search(magicSplit)
|
||||||
if (lastIndex !== -1)
|
if (splitIndexBegin !== -1) {
|
||||||
chunk = responseText.substring(lastIndex)
|
const splitIndexEnd = splitIndexBegin + magicSplit.length
|
||||||
|
|
||||||
|
const firstChunk = responseText.substring(0, splitIndexBegin)
|
||||||
|
const deltaText = responseText.substring(splitIndexEnd)
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(chunk)
|
const data = JSON.parse(firstChunk)
|
||||||
|
if (firstTime) {
|
||||||
|
firstTime = false
|
||||||
|
renderText = data.text ?? ''
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
renderText = deltaText ?? ''
|
||||||
|
}
|
||||||
updateChat(
|
updateChat(
|
||||||
+uuid,
|
+uuid,
|
||||||
dataSources.value.length - 1,
|
dataSources.value.length - 1,
|
||||||
{
|
{
|
||||||
dateTime: new Date().toLocaleString(),
|
dateTime: new Date().toLocaleString(),
|
||||||
text: lastText + data.text ?? '',
|
text: renderText,
|
||||||
inversion: false,
|
inversion: false,
|
||||||
error: false,
|
error: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
|
conversationOptions: { conversationId: data.conversationId, parentMessageId: data.id },
|
||||||
requestOptions: { prompt: message, options: { ...options } },
|
requestOptions: { prompt: message, ...options },
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
|
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
|
||||||
options.parentMessageId = data.id
|
options.parentMessageId = data.id
|
||||||
lastText = data.text
|
|
||||||
message = ''
|
message = ''
|
||||||
return fetchChatAPIOnce()
|
return fetchChatAPIOnce()
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollToBottomIfAtBottom()
|
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
await fetchChatAPIOnce()
|
await fetchChatAPIOnce()
|
||||||
}
|
}
|
||||||
catch (error: any) {
|
catch (error: any) {
|
||||||
|
@ -237,7 +246,9 @@ async function onRegenerate(index: number) {
|
||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let lastText = ''
|
const magicSplit = 't1h1i4s5i1s4a1s9i1l9l8y1s0plit'
|
||||||
|
let renderText = ''
|
||||||
|
let firstTime = true
|
||||||
const fetchChatAPIOnce = async () => {
|
const fetchChatAPIOnce = async () => {
|
||||||
await fetchChatAPIProcess<Chat.ConversationResponse>({
|
await fetchChatAPIProcess<Chat.ConversationResponse>({
|
||||||
prompt: message,
|
prompt: message,
|
||||||
|
@ -247,18 +258,28 @@ async function onRegenerate(index: number) {
|
||||||
const xhr = event.target
|
const xhr = event.target
|
||||||
const { responseText } = xhr
|
const { responseText } = xhr
|
||||||
// Always process the final line
|
// Always process the final line
|
||||||
const lastIndex = responseText.lastIndexOf('\n', responseText.length - 2)
|
|
||||||
let chunk = responseText
|
const splitIndexBegin = responseText.search(magicSplit)
|
||||||
if (lastIndex !== -1)
|
if (splitIndexBegin !== -1) {
|
||||||
chunk = responseText.substring(lastIndex)
|
const splitIndexEnd = splitIndexBegin + magicSplit.length
|
||||||
|
|
||||||
|
const firstChunk = responseText.substring(0, splitIndexBegin)
|
||||||
|
const deltaText = responseText.substring(splitIndexEnd)
|
||||||
try {
|
try {
|
||||||
const data = JSON.parse(chunk)
|
const data = JSON.parse(firstChunk)
|
||||||
|
if (firstTime) {
|
||||||
|
firstTime = false
|
||||||
|
renderText = data.text ?? ''
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
renderText = deltaText ?? ''
|
||||||
|
}
|
||||||
updateChat(
|
updateChat(
|
||||||
+uuid,
|
+uuid,
|
||||||
index,
|
index,
|
||||||
{
|
{
|
||||||
dateTime: new Date().toLocaleString(),
|
dateTime: new Date().toLocaleString(),
|
||||||
text: lastText + data.text ?? '',
|
text: renderText,
|
||||||
inversion: false,
|
inversion: false,
|
||||||
error: false,
|
error: false,
|
||||||
loading: false,
|
loading: false,
|
||||||
|
@ -269,7 +290,6 @@ async function onRegenerate(index: number) {
|
||||||
|
|
||||||
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
|
if (openLongReply && data.detail.choices[0].finish_reason === 'length') {
|
||||||
options.parentMessageId = data.id
|
options.parentMessageId = data.id
|
||||||
lastText = data.text
|
|
||||||
message = ''
|
message = ''
|
||||||
return fetchChatAPIOnce()
|
return fetchChatAPIOnce()
|
||||||
}
|
}
|
||||||
|
@ -277,6 +297,7 @@ async function onRegenerate(index: number) {
|
||||||
catch (error) {
|
catch (error) {
|
||||||
//
|
//
|
||||||
}
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -467,20 +488,13 @@ onUnmounted(() => {
|
||||||
<template>
|
<template>
|
||||||
<div class="flex flex-col w-full h-full">
|
<div class="flex flex-col w-full h-full">
|
||||||
<HeaderComponent
|
<HeaderComponent
|
||||||
v-if="isMobile"
|
v-if="isMobile" :using-context="usingContext" @export="handleExport"
|
||||||
:using-context="usingContext"
|
|
||||||
@export="handleExport"
|
|
||||||
@toggle-using-context="toggleUsingContext"
|
@toggle-using-context="toggleUsingContext"
|
||||||
/>
|
/>
|
||||||
<main class="flex-1 overflow-hidden">
|
<main class="flex-1 overflow-hidden">
|
||||||
|
<div id="scrollRef" ref="scrollRef" class="h-full overflow-hidden overflow-y-auto">
|
||||||
<div
|
<div
|
||||||
id="scrollRef"
|
id="image-wrapper" class="w-full max-w-screen-xl m-auto dark:bg-[#101014]"
|
||||||
ref="scrollRef"
|
|
||||||
class="h-full overflow-hidden overflow-y-auto"
|
|
||||||
>
|
|
||||||
<div
|
|
||||||
id="image-wrapper"
|
|
||||||
class="w-full max-w-screen-xl m-auto dark:bg-[#101014]"
|
|
||||||
:class="[isMobile ? 'p-2' : 'p-4']"
|
:class="[isMobile ? 'p-2' : 'p-4']"
|
||||||
>
|
>
|
||||||
<template v-if="!dataSources.length">
|
<template v-if="!dataSources.length">
|
||||||
|
@ -492,14 +506,8 @@ onUnmounted(() => {
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<div>
|
<div>
|
||||||
<Message
|
<Message
|
||||||
v-for="(item, index) of dataSources"
|
v-for="(item, index) of dataSources" :key="index" :date-time="item.dateTime" :text="item.text"
|
||||||
:key="index"
|
:inversion="item.inversion" :error="item.error" :loading="item.loading" @regenerate="onRegenerate(index)"
|
||||||
:date-time="item.dateTime"
|
|
||||||
:text="item.text"
|
|
||||||
:inversion="item.inversion"
|
|
||||||
:error="item.error"
|
|
||||||
:loading="item.loading"
|
|
||||||
@regenerate="onRegenerate(index)"
|
|
||||||
@delete="handleDelete(index)"
|
@delete="handleDelete(index)"
|
||||||
/>
|
/>
|
||||||
<div class="sticky bottom-0 left-0 flex justify-center">
|
<div class="sticky bottom-0 left-0 flex justify-center">
|
||||||
|
@ -536,15 +544,9 @@ onUnmounted(() => {
|
||||||
<NAutoComplete v-model:value="prompt" :options="searchOptions" :render-label="renderOption">
|
<NAutoComplete v-model:value="prompt" :options="searchOptions" :render-label="renderOption">
|
||||||
<template #default="{ handleInput, handleBlur, handleFocus }">
|
<template #default="{ handleInput, handleBlur, handleFocus }">
|
||||||
<NInput
|
<NInput
|
||||||
ref="inputRef"
|
ref="inputRef" v-model:value="prompt" type="textarea" :placeholder="placeholder"
|
||||||
v-model:value="prompt"
|
:autosize="{ minRows: 1, maxRows: isMobile ? 4 : 8 }" @input="handleInput" @focus="handleFocus"
|
||||||
type="textarea"
|
@blur="handleBlur" @keypress="handleEnter"
|
||||||
:placeholder="placeholder"
|
|
||||||
:autosize="{ minRows: 1, maxRows: isMobile ? 4 : 8 }"
|
|
||||||
@input="handleInput"
|
|
||||||
@focus="handleFocus"
|
|
||||||
@blur="handleBlur"
|
|
||||||
@keypress="handleEnter"
|
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
</NAutoComplete>
|
</NAutoComplete>
|
||||||
|
|
Loading…
Reference in New Issue