From 7ea4945088fa9509591f0537c27bfc645ab49f89 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Wed, 17 May 2023 03:11:34 +0900 Subject: [PATCH] [feat] added streaming partly --- src/lang/en.ts | 3 ++- src/ts/database.ts | 1 + src/ts/process/index.ts | 7 +++++-- src/ts/process/request.ts | 19 +++++++++++++++++++ src/ts/process/stableDiff.ts | 4 ++-- 5 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/lang/en.ts b/src/lang/en.ts index 5dd3dcaa..34378231 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -247,5 +247,6 @@ export const languageEnglish = { SuperMemory:"SupaMemory", useExperimental: "Able Experimental Features", showMemoryLimit: "Show Memory Limit", - roundIcons: "Round Icons" + roundIcons: "Round Icons", + useStreaming: "Use Streaming" } diff --git a/src/ts/database.ts b/src/ts/database.ts index 92fd863e..773f6027 100644 --- a/src/ts/database.ts +++ b/src/ts/database.ts @@ -407,6 +407,7 @@ export interface Database{ useExperimental:boolean showMemoryLimit:boolean roundIcons:boolean + useStreaming:boolean } diff --git a/src/ts/process/index.ts b/src/ts/process/index.ts index 5b564118..9c7f778c 100644 --- a/src/ts/process/index.ts +++ b/src/ts/process/index.ts @@ -337,6 +337,9 @@ export async function sendChat(chatProcessIndex = -1):Promise { if(req.type === 'fail'){ alertError(req.result) return false + } + else if(req.type === 'streaming'){ + } else{ const result2 = processScriptFull(currentChar, reformatContent(req.result), 'editoutput') @@ -429,8 +432,8 @@ export async function sendChat(chatProcessIndex = -1):Promise { maxTokens: 30, }, 'submodel') - if(rq.type === 'fail'){ - alertError(rq.result) + if(rq.type === 'fail' || rq.type === 'streaming'){ + alertError(`${rq.result}`) return true } else{ diff --git a/src/ts/process/request.ts b/src/ts/process/request.ts index 31e17e84..ae5881d6 100644 --- a/src/ts/process/request.ts +++ b/src/ts/process/request.ts @@ -19,6 +19,9 @@ interface requestDataArgument{ type requestDataResponse = { type: 'success'|'fail' result: string +}|{ + type: "streaming", + result: ReadableStreamDefaultReader } export async function requestChatData(arg:requestDataArgument, model:'model'|'submodel'):Promise { @@ -68,6 +71,22 @@ export async function requestChatDataMain(arg:requestDataArgument, model:'model' replacerURL += 'chat/completions' } + if(db.useStreaming){ + const da = await fetch(replacerURL, { + body: JSON.stringify(body), + headers: { + "Authorization": "Bearer " + db.openAIKey + }, + }) + + const reader = da.body.getReader() + + return { + type: 'streaming', + result: reader + } + } + const res = await globalFetch(replacerURL, { body: body, headers: { diff --git a/src/ts/process/stableDiff.ts b/src/ts/process/stableDiff.ts index 978bb479..58b33a91 100644 --- a/src/ts/process/stableDiff.ts +++ b/src/ts/process/stableDiff.ts @@ -80,8 +80,8 @@ export async function stableDiff(currentChar:character,prompt:string){ }, 'submodel') - if(rq.type === 'fail'){ - alertError(rq.result) + if(rq.type === 'fail' || rq.type === 'streaming'){ + alertError(`${rq.result}`) return false } else{