From b402195e18bcd03071248a48d92c41b43f397a92 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 13 Jan 2024 10:41:12 +0900 Subject: [PATCH] Fix streaming response handling in sendChat function --- src/ts/process/index.ts | 10 +++++++++- src/ts/process/models/local.ts | 9 ++++++--- src/ts/process/request.ts | 23 ++++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 95301577..84b55303 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -951,10 +951,15 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n }) } db.characters[selectedChar].chats[selectedChat].isStreaming = true + let lastResponseChunk:{[key:string]:string} = {} while(abortSignal.aborted === false){ const readed = (await reader.read()) if(readed.value){ - result = readed.value + console.log(lastResponseChunk) + + lastResponseChunk = readed.value + const firstChunkKey = Object.keys(lastResponseChunk)[0] + result = lastResponseChunk[firstChunkKey] if(db.cipherChat){ result = decipherChat(result) } @@ -972,6 +977,9 @@ export async function sendChat(chatProcessIndex = -1,arg:{chatAdditonalTokens?:n } } + console.log(lastResponseChunk) + addRerolls(generationId, Object.values(lastResponseChunk)) + currentChat = db.characters[selectedChar].chats[selectedChat] const triggerResult = await runTrigger(currentChar, 'output', {chat:currentChat}) if(triggerResult && triggerResult.chat){ diff --git a/src/ts/process/models/local.ts b/src/ts/process/models/local.ts index d59e35ae..1396f21e 100644 --- a/src/ts/process/models/local.ts +++ b/src/ts/process/models/local.ts @@ -158,7 +158,6 @@ export async function runLocalModel(prompt:string){ export async function installPython(){ const appDir = await path.appDataDir() const completedPath = await path.join(appDir, 'python', 'completed.txt') - console.log(await resolveResource('/src-python/')) if(await exists(completedPath)){ alertMd("Python is already installed") return @@ -176,7 +175,11 @@ export async function installPython(){ await invoke('post_py_install', { path: appDir }) + const srvPath = await resolveResource('/src-python/') + await invoke('run_py_server', { + path: srvPath + + }) + alertClear() - - } \ No newline at end of file diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index 56da4ac0..a8ce4839 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -49,7 +49,7 @@ type requestDataResponse = { } }|{ type: "streaming", - result: ReadableStream, + result: ReadableStream, special?: { emotion?: string } @@ -61,6 +61,8 @@ type requestDataResponse = { } } +interface StreamResponseChunk{[key:string]:string} + interface OaiFunctions { name: string; description: string; @@ -503,7 +505,7 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' body.n = db.genTime } let throughProxi = (!isTauri) && (!isNodeServer) && (!db.usePlainFetch) && (!Capacitor.isNativePlatform()) - if(db.useStreaming && arg.useStreaming && (!multiGen)){ + if(db.useStreaming && arg.useStreaming){ body.stream = true let urlHost = new URL(replacerURL).host if(urlHost.includes("localhost") || urlHost.includes("172.0.0.1") || urlHost.includes("0.0.0.0")){ @@ -556,12 +558,12 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' let dataUint = new Uint8Array([]) - const transtream = new TransformStream( { + const transtream = new TransformStream( { async transform(chunk, control) { dataUint = Buffer.from(new Uint8Array([...dataUint, ...chunk])) try { const datas = dataUint.toString().split('\n') - let readed = '' + let readed:{[key:string]:string} = {} for(const data of datas){ if(data.startsWith("data: ")){ try { @@ -570,9 +572,16 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' control.enqueue(readed) return } - const chunk = JSON.parse(rawChunk).choices[0].delta.content - if(chunk){ - readed += chunk + const choices = JSON.parse(rawChunk).choices + for(const choice of choices){ + const chunk = choice.delta.content + const ind = choice.index.toString() + if(chunk){ + if(!readed[ind]){ + readed[ind] = "" + } + readed[ind] += chunk + } } } catch (error) {} }