From bc018a97cf41bbbfc3bde714a64e5fda9825e98b Mon Sep 17 00:00:00 2001 From: kwaroran Date: Sun, 6 Aug 2023 12:38:18 +0900 Subject: [PATCH] [feat] improved supa/hypa memory --- src/ts/process/memory/hypamemory.ts | 24 +++++++++++++++++++++++- src/ts/process/memory/supaMemory.ts | 27 ++++++++++----------------- 2 files changed, 33 insertions(+), 18 deletions(-) diff --git a/src/ts/process/memory/hypamemory.ts b/src/ts/process/memory/hypamemory.ts index 5d6b8078..7b43d39a 100644 --- a/src/ts/process/memory/hypamemory.ts +++ b/src/ts/process/memory/hypamemory.ts @@ -1,18 +1,21 @@ import localforage from "localforage"; import { similarity } from "ml-distance"; import { globalFetch } from "src/ts/storage/globalApi"; +import { runEmbedding } from "../embedding/transformers"; export class HypaProcesser{ oaikey:string vectors:memoryVector[] forage:LocalForage + model:'ada'|'MiniLM' - constructor(){ + constructor(model:'ada'|'MiniLM'){ this.forage = localforage.createInstance({ name: "hypaVector" }) this.vectors = [] + this.model = model } async embedDocuments(texts: string[]): Promise { @@ -33,6 +36,25 @@ export class HypaProcesser{ async getEmbeds(input:string[]|string) { + if(this.model === 'MiniLM'){ + const inputs:string[] = Array.isArray(input) ? input : [input] + let results:Float32Array[] = [] + for(let i=0;i value.length > 1) @@ -216,6 +216,7 @@ export async function supaMemory( role: "assistant", content: hypaResult }) + currentTokens += 10 } while(currentTokens > maxContextTokens){ @@ -264,10 +265,11 @@ export async function supaMemory( const tokens = await tokenizer.tokenizeChat(cont) if((chunkSize + tokens) > maxChunkSize){ if(stringlizedChat === ''){ - - if(cont.role !== 'function' && cont.role !== 'system'){ stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n` + spiceLen += 1 + currentTokens -= tokens + chunkSize += tokens } } lastId = cont.memo @@ -288,30 +290,21 @@ export async function supaMemory( } const tokenz = await tokenize(result + '\n\n') - currentTokens += tokenz - supaMemory += result.replace(/\n+/g,'\n') + '\n\n' + hypaChunks.push(result.replace(/\n+/g,'\n')) - let SupaMemoryList = supaMemory.split('\n\n') + let SupaMemoryList = supaMemory.split('\n\n').filter((value) => value.length > 1) if(SupaMemoryList.length >= (arg.asHyper ? 3 : 4)){ const oldSupaMemory = supaMemory - let modifies:string[] = [] - for(let i=0;i<3;i++){ - modifies.push(SupaMemoryList.shift()) - } - hypaChunks.push(...modifies) - const result = await summarize(supaMemory) if(typeof(result) !== 'string'){ return result } - - modifies.unshift(result.replace(/\n+/g,'\n')) - supaMemory = modifies.join('\n\n') + '\n\n' - + supaMemory = result currentTokens -= await tokenize(oldSupaMemory) currentTokens += await tokenize(supaMemory) } - console.log(supaMemory) + currentTokens += tokenz + supaMemory += result.replace(/\n+/g,'\n') } }