[feat] improved supa/hypa memory
This commit is contained in:
@@ -1,18 +1,21 @@
|
|||||||
import localforage from "localforage";
|
import localforage from "localforage";
|
||||||
import { similarity } from "ml-distance";
|
import { similarity } from "ml-distance";
|
||||||
import { globalFetch } from "src/ts/storage/globalApi";
|
import { globalFetch } from "src/ts/storage/globalApi";
|
||||||
|
import { runEmbedding } from "../embedding/transformers";
|
||||||
|
|
||||||
|
|
||||||
export class HypaProcesser{
|
export class HypaProcesser{
|
||||||
oaikey:string
|
oaikey:string
|
||||||
vectors:memoryVector[]
|
vectors:memoryVector[]
|
||||||
forage:LocalForage
|
forage:LocalForage
|
||||||
|
model:'ada'|'MiniLM'
|
||||||
|
|
||||||
constructor(){
|
constructor(model:'ada'|'MiniLM'){
|
||||||
this.forage = localforage.createInstance({
|
this.forage = localforage.createInstance({
|
||||||
name: "hypaVector"
|
name: "hypaVector"
|
||||||
})
|
})
|
||||||
this.vectors = []
|
this.vectors = []
|
||||||
|
this.model = model
|
||||||
}
|
}
|
||||||
|
|
||||||
async embedDocuments(texts: string[]): Promise<number[][]> {
|
async embedDocuments(texts: string[]): Promise<number[][]> {
|
||||||
@@ -33,6 +36,25 @@ export class HypaProcesser{
|
|||||||
|
|
||||||
|
|
||||||
async getEmbeds(input:string[]|string) {
|
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", {
|
const gf = await globalFetch("https://api.openai.com/v1/embeddings", {
|
||||||
headers: {
|
headers: {
|
||||||
"Authorization": "Bearer " + this.oaikey
|
"Authorization": "Bearer " + this.oaikey
|
||||||
|
|||||||
@@ -202,7 +202,7 @@ export async function supaMemory(
|
|||||||
let hypaResult = ""
|
let hypaResult = ""
|
||||||
|
|
||||||
if(arg.asHyper){
|
if(arg.asHyper){
|
||||||
const hypa = new HypaProcesser()
|
const hypa = new HypaProcesser('MiniLM')
|
||||||
hypa.oaikey = db.supaMemoryKey
|
hypa.oaikey = db.supaMemoryKey
|
||||||
hypa.vectors = []
|
hypa.vectors = []
|
||||||
hypaChunks = hypaChunks.filter((value) => value.length > 1)
|
hypaChunks = hypaChunks.filter((value) => value.length > 1)
|
||||||
@@ -216,6 +216,7 @@ export async function supaMemory(
|
|||||||
role: "assistant",
|
role: "assistant",
|
||||||
content: hypaResult
|
content: hypaResult
|
||||||
})
|
})
|
||||||
|
currentTokens += 10
|
||||||
}
|
}
|
||||||
|
|
||||||
while(currentTokens > maxContextTokens){
|
while(currentTokens > maxContextTokens){
|
||||||
@@ -264,10 +265,11 @@ export async function supaMemory(
|
|||||||
const tokens = await tokenizer.tokenizeChat(cont)
|
const tokens = await tokenizer.tokenizeChat(cont)
|
||||||
if((chunkSize + tokens) > maxChunkSize){
|
if((chunkSize + tokens) > maxChunkSize){
|
||||||
if(stringlizedChat === ''){
|
if(stringlizedChat === ''){
|
||||||
|
|
||||||
|
|
||||||
if(cont.role !== 'function' && cont.role !== 'system'){
|
if(cont.role !== 'function' && cont.role !== 'system'){
|
||||||
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n`
|
stringlizedChat += `${cont.role === 'assistant' ? char.type === 'group' ? '' : char.name : db.username}: ${cont.content}\n\n`
|
||||||
|
spiceLen += 1
|
||||||
|
currentTokens -= tokens
|
||||||
|
chunkSize += tokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lastId = cont.memo
|
lastId = cont.memo
|
||||||
@@ -288,30 +290,21 @@ export async function supaMemory(
|
|||||||
}
|
}
|
||||||
|
|
||||||
const tokenz = await tokenize(result + '\n\n')
|
const tokenz = await tokenize(result + '\n\n')
|
||||||
currentTokens += tokenz
|
hypaChunks.push(result.replace(/\n+/g,'\n'))
|
||||||
supaMemory += result.replace(/\n+/g,'\n') + '\n\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)){
|
if(SupaMemoryList.length >= (arg.asHyper ? 3 : 4)){
|
||||||
const oldSupaMemory = supaMemory
|
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)
|
const result = await summarize(supaMemory)
|
||||||
if(typeof(result) !== 'string'){
|
if(typeof(result) !== 'string'){
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
supaMemory = result
|
||||||
modifies.unshift(result.replace(/\n+/g,'\n'))
|
|
||||||
supaMemory = modifies.join('\n\n') + '\n\n'
|
|
||||||
|
|
||||||
currentTokens -= await tokenize(oldSupaMemory)
|
currentTokens -= await tokenize(oldSupaMemory)
|
||||||
currentTokens += await tokenize(supaMemory)
|
currentTokens += await tokenize(supaMemory)
|
||||||
}
|
}
|
||||||
console.log(supaMemory)
|
currentTokens += tokenz
|
||||||
|
supaMemory += result.replace(/\n+/g,'\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user