From 6035d1e01dc65e3386b60b326c7c10116c59fcd2 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 20 May 2023 03:25:55 +0900 Subject: [PATCH 1/4] [feat] better supaMemory --- src/ts/process/supaMemory.ts | 97 +++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 28 deletions(-) diff --git a/src/ts/process/supaMemory.ts b/src/ts/process/supaMemory.ts index 2909e762..1443bb69 100644 --- a/src/ts/process/supaMemory.ts +++ b/src/ts/process/supaMemory.ts @@ -66,33 +66,11 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont let lastId = '' - while(currentTokens > maxContextTokens){ - const maxChunkSize = maxContextTokens > 3000 ? 1200 : Math.floor(maxContextTokens / 2.5) - let chunkSize = 0 - let stringlizedChat = '' - - while(true){ - const cont = chats[0] - if(!cont){ - return { - currentTokens: currentTokens, - chats: chats, - error: "Not Enough Chunks" - } - } - const tokens = await tokenize(cont.content) + 1 - if((chunkSize + tokens) > maxChunkSize){ - lastId = cont.memo - break - } - stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n` - chats.splice(0, 1) - currentTokens -= tokens - chunkSize += tokens - } - + + async function summarize(stringlizedChat:string){ + const supaPrompt = db.supaMemoryPrompt === '' ? - "[Summarize the ongoing role story. It must also remove redundancy and unnecessary content from the prompt so that gpt3 and other sublanguage models]\n" + "[Summarize the ongoing role story, including as many events from the past as possible, using assistant as a narrative helper;do not analyze. include all of the characters' names, statuses, thoughts, relationships, and attire. Be sure to include dialogue exchanges and context by referencing previous statements and reactions. assistant's summary should provide an objective overview of the story while also considering relevant past conversations and events. It must also remove redundancy and unnecessary content from the prompt so that gpt3 and other sublanguage models]\n" : db.supaMemoryPrompt let result = '' @@ -147,12 +125,74 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont } result = da.result } + return result + } + if(supaMemory.split('\n\n').length >= 4){ + const result = await summarize(supaMemory) + if(typeof(result) !== 'string'){ + return result + } + currentTokens -= await tokenize(supaMemory) + currentTokens += await tokenize(result + '\n\n') + supaMemory = result + '\n\n' + } + while(currentTokens > maxContextTokens){ + let maxChunkSize = maxContextTokens > 3500 ? 1200 : Math.floor(maxContextTokens / 3) + while((currentTokens - (maxChunkSize * 0.7)) > maxContextTokens){ + maxChunkSize = Math.floor(maxChunkSize * 0.7) + if(maxChunkSize < 500){ + return { + currentTokens: currentTokens, + chats: chats, + error: "Not Enough Tokens" + } + } + } + + let chunkSize = 0 + let stringlizedChat = '' + + while(true){ + const cont = chats[0] + if(!cont){ + return { + currentTokens: currentTokens, + chats: chats, + error: "Not Enough Tokens" + } + } + const tokens = await tokenize(cont.content) + 1 + if((chunkSize + tokens) > maxChunkSize){ + lastId = cont.memo + break + } + stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n` + chats.splice(0, 1) + currentTokens -= tokens + chunkSize += tokens + } + + const result = await summarize(stringlizedChat) + + if(typeof(result) !== 'string'){ + return result + } const tokenz = await tokenize(result + '\n\n') + 5 currentTokens += tokenz - supaMemory += result + '\n\n' + supaMemory += result.replace(/\n+/g,'\n') + '\n\n' + if(supaMemory.split('\n\n').length >= 4){ + const result = await summarize(supaMemory) + if(typeof(result) !== 'string'){ + return result + } + currentTokens -= await tokenize(supaMemory) + currentTokens += await tokenize(result + '\n\n') + supaMemory = result + '\n\n' + } + } chats.unshift({ @@ -171,4 +211,5 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont currentTokens: currentTokens, chats: chats } -} \ No newline at end of file +} + From 0e1e63e63ccfaee70f90308d94dc710311de2a38 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 20 May 2023 04:10:54 +0900 Subject: [PATCH 2/4] [feat] better drive load and supamemory --- src/ts/drive/drive.ts | 15 ++++-- src/ts/process/supaMemory.ts | 91 +++++++++++++++++++----------------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/ts/drive/drive.ts b/src/ts/drive/drive.ts index 19a14517..d89d874c 100644 --- a/src/ts/drive/drive.ts +++ b/src/ts/drive/drive.ts @@ -3,7 +3,7 @@ import { alertError, alertInput, alertNormal, alertSelect, alertStore } from ".. import { DataBase, setDatabase, type Database } from "../database"; import { forageStorage, getUnpargeables, isTauri } from "../globalApi"; import pako from "pako"; -import { BaseDirectory, readBinaryFile, readDir, writeBinaryFile } from "@tauri-apps/api/fs"; +import { BaseDirectory, exists, readBinaryFile, readDir, writeBinaryFile } from "@tauri-apps/api/fs"; import { language } from "../../lang"; import { relaunch } from '@tauri-apps/api/process'; import { open } from '@tauri-apps/api/shell'; @@ -167,11 +167,16 @@ async function loadDrive(ACCESS_TOKEN:string) { let loadedForageKeys = false async function checkImageExists(images:string) { - if(!loadedForageKeys){ - foragekeys = await forageStorage.keys() - loadedForageKeys = true + if(isTauri){ + return await exists(`assets/` + images, {dir: BaseDirectory.AppData}) + } + else{ + if(!loadedForageKeys){ + foragekeys = await forageStorage.keys() + loadedForageKeys = true + } + return foragekeys.includes('assets/' + images) } - return foragekeys.includes('assets/' + images) } const fileNames = files.map((d) => { return d.name diff --git a/src/ts/process/supaMemory.ts b/src/ts/process/supaMemory.ts index 1443bb69..5810de36 100644 --- a/src/ts/process/supaMemory.ts +++ b/src/ts/process/supaMemory.ts @@ -25,6 +25,7 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont } let supaMemory = '' + let lastId = '' if(room.supaMemoryData && room.supaMemoryData.length > 4){ const splited = room.supaMemoryData.split('\n') @@ -41,6 +42,7 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont } } if(chats[0].memo === id){ + lastId = id break } currentTokens -= (await tokenize(chats[0].content) + 1) @@ -64,8 +66,6 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont } } - let lastId = '' - async function summarize(stringlizedChat:string){ @@ -87,7 +87,7 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont body: JSON.stringify({ "model": db.supaMemoryType === 'curie' ? "text-curie-001" : "text-davinci-003", "prompt": promptbody, - "max_tokens": 500, + "max_tokens": 600, "temperature": 0 }) }) @@ -128,40 +128,48 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont return result } - if(supaMemory.split('\n\n').length >= 4){ - const result = await summarize(supaMemory) - if(typeof(result) !== 'string'){ - return result - } - currentTokens -= await tokenize(supaMemory) - currentTokens += await tokenize(result + '\n\n') - supaMemory = result + '\n\n' - } - while(currentTokens > maxContextTokens){ + const beforeToken = currentTokens let maxChunkSize = maxContextTokens > 3500 ? 1200 : Math.floor(maxContextTokens / 3) - while((currentTokens - (maxChunkSize * 0.7)) > maxContextTokens){ - maxChunkSize = Math.floor(maxChunkSize * 0.7) - if(maxChunkSize < 500){ - return { - currentTokens: currentTokens, - chats: chats, - error: "Not Enough Tokens" - } - } - } - + let summarized = false let chunkSize = 0 let stringlizedChat = '' - + let spiceLen = 0 while(true){ - const cont = chats[0] + const cont = chats[spiceLen] if(!cont){ - return { - currentTokens: currentTokens, - chats: chats, - error: "Not Enough Tokens" + currentTokens = beforeToken + stringlizedChat = '' + chunkSize = 0 + spiceLen = 0 + if(summarized){ + if(maxChunkSize < 500){ + return { + currentTokens: currentTokens, + chats: chats, + error: "Not Enough Tokens" + } + } + maxChunkSize = maxChunkSize * 0.7 } + else{ + const result = await summarize(supaMemory) + if(typeof(result) !== 'string'){ + return result + } + + console.log(currentTokens) + currentTokens -= await tokenize(supaMemory) + currentTokens += await tokenize(result + '\n\n') + console.log(currentTokens) + + supaMemory = result + '\n\n' + summarized = true + if(currentTokens <= maxContextTokens){ + break + } + } + continue } const tokens = await tokenize(cont.content) + 1 if((chunkSize + tokens) > maxChunkSize){ @@ -169,30 +177,25 @@ export async function supaMemory(chats:OpenAIChat[],currentTokens:number,maxCont break } stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n` - chats.splice(0, 1) + spiceLen += 1 currentTokens -= tokens chunkSize += tokens } - - const result = await summarize(stringlizedChat) + chats.splice(0, spiceLen) - if(typeof(result) !== 'string'){ - return result - } + if(stringlizedChat !== ''){ + const result = await summarize(stringlizedChat) - const tokenz = await tokenize(result + '\n\n') + 5 - currentTokens += tokenz - supaMemory += result.replace(/\n+/g,'\n') + '\n\n' - if(supaMemory.split('\n\n').length >= 4){ - const result = await summarize(supaMemory) if(typeof(result) !== 'string'){ return result } - currentTokens -= await tokenize(supaMemory) - currentTokens += await tokenize(result + '\n\n') - supaMemory = result + '\n\n' + + const tokenz = await tokenize(result + '\n\n') + 5 + currentTokens += tokenz + supaMemory += result.replace(/\n+/g,'\n') + '\n\n' } + } chats.unshift({ From d44bb2a936b8bf169bfa894d141633f06cd0b473 Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sat, 20 May 2023 04:30:00 +0900 Subject: [PATCH 3/4] [feat] supermemory helps --- src/lang/en.ts | 10 +++++++--- src/lang/ko.ts | 4 ++-- src/lib/Setting/Pages/OtherBotSettings.svelte | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lang/en.ts b/src/lang/en.ts index 38cf3bd9..d9023791 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -72,7 +72,11 @@ export const languageEnglish = { scenario: "A brief description about character's scenario. \n\n**It is not recommended to use this option. Describe it in character description instead.**", utilityBot: "When activated, it ignores main prompt. \n\n**It is not recommended to use this option. Modifiy system prompt instead.**", loreSelective: "If Selective mode is toggled, both Activation Key and Secondary key should have a match to activate the lore.", - additionalAssets: "Additional assets to display in your chat. \n\n - use `{{raw::}}` to use as path.\n - use `{{img::}}` to use as image" + additionalAssets: "Additional assets to display in your chat. \n\n - use `{{raw::}}` to use as path.\n - use `{{img::}}` to use as image", + superMemory: "SuperMemory makes your character memorize more by giving summarized data to AI.\n\n" + + "SuperMemory model is a model that summarizes that text. davinci is recommended, and Auxiliary models are not recommended unless it is an unfiltered model with over 2000 tokens with great summarizing skill.\n\n" + + "SuperMemory Prompt decides what prompt should be sent to summarize. if you leave it blank, it will use the default prompt. leaving blank is recommended.\n\n" + + "After it is all setup, you can able it in the setting of a character." }, setup: { chooseProvider: "Choose AI Provider", @@ -244,8 +248,8 @@ export const languageEnglish = { creator: "Creator", CharVersion: "Character Version", Speech: "Speech", - ToggleSuperMemory: "Toggle SupaMemory", - SuperMemory:"SupaMemory", + ToggleSuperMemory: "Toggle SuperMemory", + SuperMemory:"SuperMemory", useExperimental: "Able Experimental Features", showMemoryLimit: "Show Memory Limit", roundIcons: "Round Icons", diff --git a/src/lang/ko.ts b/src/lang/ko.ts index d30b2511..3e84c563 100644 --- a/src/lang/ko.ts +++ b/src/lang/ko.ts @@ -229,8 +229,8 @@ export const languageKorean = { creator: "제작자", CharVersion: "캐릭터 버전", Speech: "음성", - ToggleSuperMemory: "SupaMemory 토글", - SuperMemory:"SupaMemory", + ToggleSuperMemory: "슈퍼메모리 토글", + SuperMemory:"슈퍼메모리", useExperimental: "실험적 요소 보이기", showMemoryLimit: "기억 한계치 보이기", roundIcons: "둥근 아이콘", diff --git a/src/lib/Setting/Pages/OtherBotSettings.svelte b/src/lib/Setting/Pages/OtherBotSettings.svelte index a4a5929a..cd5c5545 100644 --- a/src/lib/Setting/Pages/OtherBotSettings.svelte +++ b/src/lib/Setting/Pages/OtherBotSettings.svelte @@ -59,7 +59,7 @@ -SupaMemory +{language.SuperMemory} {language.SuperMemory} {language.model}