[feat] improve supa/hypa memory

This commit is contained in:
kwaroran
2023-09-08 01:58:28 +09:00
parent 677bd51657
commit e109cd2dc8
6 changed files with 60 additions and 596 deletions

View File

@@ -7,6 +7,7 @@ import { cloneDeep } from "lodash";
import { HypaProcesser } from "./hypamemory";
import { stringlizeChat } from "../stringlize";
import { globalFetch } from "src/ts/storage/globalApi";
import { runSummarizer } from "../embedding/transformers";
export async function supaMemory(
chats:OpenAIChat[],
@@ -131,10 +132,39 @@ export async function supaMemory(
}
let hypaResult = ""
if(arg.asHyper){
const hypa = new HypaProcesser(db.hypaModel)
hypa.oaikey = db.supaMemoryKey
hypa.vectors = []
hypaChunks = hypaChunks.filter((value) => value.length > 1)
if(hypaChunks.length > 0){
await hypa.addText(hypaChunks.filter((value, index, self) => {
return self.indexOf(value) === index;
}))
const filteredChat = chats.filter((r) => r.role !== 'system' && r.role !== 'function')
const s = await hypa.similaritySearch(stringlizeChat(filteredChat.slice(0, 4), char?.name ?? '', false))
hypaResult = "past events: " + s.slice(0,3).join("\n")
currentTokens += await tokenizer.tokenizeChat({
role: "assistant",
content: hypaResult,
memo: "hypaMemory"
})
currentTokens += 10
}
}
if(currentTokens < maxContextTokens){
chats.unshift({
role: "system",
content: supaMemory
content: supaMemory,
memo: "supaMemory"
})
chats.unshift({
role: "system",
content: hypaResult,
memo: "hypaMemory"
})
return {
currentTokens: currentTokens,
@@ -145,6 +175,21 @@ export async function supaMemory(
async function summarize(stringlizedChat:string){
if(db.supaMemoryType === 'distilbart'){
try {
const sum = await runSummarizer(stringlizedChat)
return sum[0].summary_text
} catch (error) {
return {
currentTokens: currentTokens,
chats: chats,
error: "SupaMemory: Summarizer: " + `${error}`
}
}
}
const supaPrompt = db.supaMemoryPrompt === '' ?
"[Summarize the ongoing role story, It must also remove redundancy and unnecessary text and content from the output to reduce tokens for gpt3 and other sublanguage models]\n"
: db.supaMemoryPrompt
@@ -170,7 +215,15 @@ export async function supaMemory(
console.log(da)
result = (await da.data).choices[0].text.trim()
result = (await da.data)?.choices[0]?.text?.trim()
if(!result){
return {
currentTokens: currentTokens,
chats: chats,
error: "SupaMemory: HTTP: " + await da.data
}
}
}
else {
const promptbody:OpenAIChat[] = [
@@ -199,26 +252,6 @@ export async function supaMemory(
return result
}
let hypaResult = ""
if(arg.asHyper){
const hypa = new HypaProcesser(db.hypaModel)
hypa.oaikey = db.supaMemoryKey
hypa.vectors = []
hypaChunks = hypaChunks.filter((value) => value.length > 1)
await hypa.addText(hypaChunks.filter((value, index, self) => {
return self.indexOf(value) === index;
}))
const filteredChat = chats.filter((r) => r.role !== 'system' && r.role !== 'function')
const s = await hypa.similaritySearch(stringlizeChat(filteredChat.slice(0, 4)))
hypaResult = "past events: " + s.slice(0,3).join("\n")
currentTokens += await tokenizer.tokenizeChat({
role: "assistant",
content: hypaResult,
memo: "hypaMemory"
})
currentTokens += 10
}
while(currentTokens > maxContextTokens){
const beforeToken = currentTokens
@@ -322,8 +355,9 @@ export async function supaMemory(
if(arg.asHyper){
if(hypaResult !== ''){
chats.unshift({
role: "assistant",
content: hypaResult
role: "system",
content: hypaResult,
memo: "hypaMemory"
})
}