[feat] improved supa/hypa memory

This commit is contained in:
kwaroran
2023-08-06 12:38:18 +09:00
parent dbbc8d25ac
commit bc018a97cf
2 changed files with 33 additions and 18 deletions

View File

@@ -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<number[][]> {
@@ -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<inputs.length;i++){
const res = await runEmbedding(inputs[i])
results.push(res)
}
//convert to number[][]
const result:number[][] = []
for(let i=0;i<results.length;i++){
const res = results[i]
const arr:number[] = []
for(let j=0;j<res.length;j++){
arr.push(res[j])
}
result.push(arr)
}
return result
}
const gf = await globalFetch("https://api.openai.com/v1/embeddings", {
headers: {
"Authorization": "Bearer " + this.oaikey

View File

@@ -202,7 +202,7 @@ export async function supaMemory(
let hypaResult = ""
if(arg.asHyper){
const hypa = new HypaProcesser()
const hypa = new HypaProcesser('MiniLM')
hypa.oaikey = db.supaMemoryKey
hypa.vectors = []
hypaChunks = hypaChunks.filter((value) => 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')
}
}