updated hypav2.ts
back to original logic, only model selection is implemented. Previous issue: the mainChunks object holds list of summarized texts, but for somewhat reason they are deleted after usage, and then re-added again, summarizing same chats.
This commit is contained in:
@@ -20,6 +20,7 @@ export interface HypaV2Data {
|
|||||||
|
|
||||||
async function summary(stringlizedChat: string): Promise<{ success: boolean; data: string }> {
|
async function summary(stringlizedChat: string): Promise<{ success: boolean; data: string }> {
|
||||||
const db = get(DataBase);
|
const db = get(DataBase);
|
||||||
|
console.log("Summarizing with supa model: " + db.supaModelType);
|
||||||
/**
|
/**
|
||||||
* Generates a summary of a given chat by using either the OpenAI API or a submodel or distilbart summarizer.
|
* Generates a summary of a given chat by using either the OpenAI API or a submodel or distilbart summarizer.
|
||||||
*
|
*
|
||||||
@@ -137,20 +138,23 @@ export async function hypaMemoryV2(
|
|||||||
let mainPrompt = "";
|
let mainPrompt = "";
|
||||||
|
|
||||||
// Processing mainChunks
|
// Processing mainChunks
|
||||||
if (data.mainChunks.length > 0) {
|
while(data.mainChunks.length > 0){
|
||||||
const chunk = data.mainChunks[0];
|
const chunk = data.mainChunks[0]
|
||||||
const ind = chats.findIndex(e => e.memo === chunk.targetId);
|
const ind = chats.findIndex(e => e.memo === chunk.targetId)
|
||||||
if (ind !== -1) {
|
if(ind === -1){
|
||||||
const removedChats = chats.splice(0, ind);
|
data.mainChunks.shift()
|
||||||
for (const chat of removedChats) {
|
continue
|
||||||
currentTokens -= await tokenizer.tokenizeChat(chat);
|
|
||||||
}
|
}
|
||||||
chats = chats.slice(ind);
|
|
||||||
mainPrompt = chunk.text;
|
const removedChats = chats.splice(0, ind)
|
||||||
const mpToken = await tokenizer.tokenizeChat({ role: 'system', content: mainPrompt });
|
for(const chat of removedChats){
|
||||||
allocatedTokens -= mpToken;
|
currentTokens -= await tokenizer.tokenizeChat(chat)
|
||||||
}
|
}
|
||||||
data.mainChunks.shift();
|
chats = chats.slice(ind)
|
||||||
|
mainPrompt = chunk.text
|
||||||
|
const mpToken = await tokenizer.tokenizeChat({role:'system', content:mainPrompt})
|
||||||
|
allocatedTokens -= mpToken
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token management loop
|
// Token management loop
|
||||||
@@ -166,6 +170,7 @@ export async function hypaMemoryV2(
|
|||||||
halfData.push(chat);
|
halfData.push(chat);
|
||||||
idx++;
|
idx++;
|
||||||
targetId = chat.memo;
|
targetId = chat.memo;
|
||||||
|
console.log("current target chat: ", chat)
|
||||||
}
|
}
|
||||||
|
|
||||||
const stringlizedChat = halfData.map(e => `${e.role}: ${e.content}`).join('\n');
|
const stringlizedChat = halfData.map(e => `${e.role}: ${e.content}`).join('\n');
|
||||||
@@ -213,7 +218,11 @@ export async function hypaMemoryV2(
|
|||||||
|
|
||||||
const processor = new HypaProcesser(db.hypaModel);
|
const processor = new HypaProcesser(db.hypaModel);
|
||||||
processor.oaikey = db.supaMemoryKey;
|
processor.oaikey = db.supaMemoryKey;
|
||||||
await processor.addText(data.chunks.filter(v => v.text.trim().length > 0).map(v => "search_document: " + v.text.trim()));
|
await processor.addText(data.chunks.filter(v => {
|
||||||
|
return v.text.trim().length > 0
|
||||||
|
}).map((v) => {
|
||||||
|
return "search_document: " + v.text.trim()
|
||||||
|
}))
|
||||||
|
|
||||||
let scoredResults: { [key: string]: number } = {};
|
let scoredResults: { [key: string]: number } = {};
|
||||||
for (let i = 0; i < 3; i++) {
|
for (let i = 0; i < 3; i++) {
|
||||||
@@ -221,32 +230,44 @@ export async function hypaMemoryV2(
|
|||||||
if (!pop) break;
|
if (!pop) break;
|
||||||
const searched = await processor.similaritySearchScored(`search_query: ${pop.content}`);
|
const searched = await processor.similaritySearchScored(`search_query: ${pop.content}`);
|
||||||
for (const result of searched) {
|
for (const result of searched) {
|
||||||
const score = result[1] / (i + 1);
|
const score = result[1]/(i+1)
|
||||||
scoredResults[result[0]] = (scoredResults[result[0]] || 0) + score;
|
if(scoredResults[result[0]]){
|
||||||
|
scoredResults[result[0]] += score
|
||||||
|
}else{
|
||||||
|
scoredResults[result[0]] = score
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const scoredArray = Object.entries(scoredResults).sort((a, b) => b[1] - a[1]);
|
const scoredArray = Object.entries(scoredResults).sort((a, b) => b[1] - a[1]);
|
||||||
|
|
||||||
let chunkResultPrompts = "";
|
let chunkResultPrompts = "";
|
||||||
while (allocatedTokens > 0 && scoredArray.length > 0) {
|
while(allocatedTokens > 0){
|
||||||
const target = scoredArray.shift();
|
const target = scoredArray.shift()
|
||||||
const tokenized = await tokenizer.tokenizeChat({ role: 'system', content: target[0].substring(14) });
|
if(!target){
|
||||||
if (tokenized > allocatedTokens) break;
|
break
|
||||||
chunkResultPrompts += target[0].substring(14) + '\n\n';
|
}
|
||||||
allocatedTokens -= tokenized;
|
const tokenized = await tokenizer.tokenizeChat({
|
||||||
|
role: 'system',
|
||||||
|
content: target[0].substring(14)
|
||||||
|
})
|
||||||
|
if(tokenized > allocatedTokens){
|
||||||
|
break
|
||||||
|
}
|
||||||
|
chunkResultPrompts += target[0].substring(14) + '\n\n'
|
||||||
|
allocatedTokens -= tokenized
|
||||||
}
|
}
|
||||||
|
|
||||||
const fullResult = `<Past Events Summary>${mainPrompt}</Past Events Summary>\n<Past Events Details>${chunkResultPrompts}</Past Events Details>`;
|
const fullResult = `<Past Events Summary>${mainPrompt}</Past Events Summary>\n<Past Events Details>${chunkResultPrompts}</Past Events Details>`;
|
||||||
console.log(fullResult, " This chat is added to system prompt for memory. ");
|
|
||||||
chats.unshift({
|
chats.unshift({
|
||||||
role: "system",
|
role: "system",
|
||||||
content: fullResult,
|
content: fullResult,
|
||||||
memo: "supaMemory"
|
memo: "supaMemory"
|
||||||
});
|
});
|
||||||
|
console.log("model being used: ", db.hypaModel, db.supaModelType, "\nCurrent session tokens: ", currentTokens, "\nAll chats, including memory system prompt: ", chats,"\nMemory data, with all the chunks: ", data);
|
||||||
return {
|
return {
|
||||||
currentTokens: currentTokens,
|
currentTokens: currentTokens,
|
||||||
chats: chats,
|
chats: chats,
|
||||||
memory: data
|
memory: data
|
||||||
};
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user