bugfix/deleted feature restored

Resolved bug when entire chat context is deleted and hypaV2Data becomes empty, throwing a undefined index error. Also changed it to typescript index.

Hopefully resolved issue where same chat is summarized over and over again by adding another field to mainChunks.
This commit is contained in:
HyperBlaze
2024-12-04 22:10:16 -08:00
parent 2728b9c5f5
commit ea6d7dada1

View File

@@ -18,6 +18,7 @@ export interface HypaV2Data {
id: number; id: number;
text: string; text: string;
chatMemos: Set<string>; // UUIDs of summarized chats chatMemos: Set<string>; // UUIDs of summarized chats
lastChatMemo: string;
}[]; }[];
chunks: { // split mainChunks for retrieval or something. Although quite uncomfortable logic, so maybe I will delete it soon or later. chunks: { // split mainChunks for retrieval or something. Although quite uncomfortable logic, so maybe I will delete it soon or later.
mainChunkID: number; mainChunkID: number;
@@ -205,11 +206,21 @@ export async function hypaMemoryV2(
const lastTwoChats = chats.slice(-2); const lastTwoChats = chats.slice(-2);
let summarizationFailures = 0; let summarizationFailures = 0;
const maxSummarizationFailures = 3; const maxSummarizationFailures = 3;
const summarizedMemos = new Set<string>();
// Find the index to start summarizing from
let idx = 0;
if (data.mainChunks.length > 0) {
const lastMainChunk = data.mainChunks[data.mainChunks.length - 1];
const lastChatMemo = lastMainChunk.lastChatMemo;
const lastChatIndex = chats.findIndex(chat => chat.memo === lastChatMemo);
if (lastChatIndex !== -1) {
idx = lastChatIndex + 1;
}
}
// Starting chat index of new mainChunk to be generated
// Token management loop // Token management loop(where using of )
while (currentTokens >= maxContextTokens) { while (currentTokens >= maxContextTokens) {
let idx = 0;
const halfData: OpenAIChat[] = []; const halfData: OpenAIChat[] = [];
let halfDataTokens = 0; let halfDataTokens = 0;
@@ -217,20 +228,16 @@ export async function hypaMemoryV2(
while ( while (
halfDataTokens < chunkSize && halfDataTokens < chunkSize &&
idx < chats.length - 2 // Ensure latest two chats are not added to summarization. idx < chats.length - 2 // Ensure latest two chats are not added to summarization.
) { ) {
const chat = chats[idx]; const chat = chats[idx];
if (!summarizedMemos.has(chat.memo)) {
halfDataTokens += await tokenizer.tokenizeChat(chat);
halfData.push(chat);
}
idx++; idx++;
halfDataTokens += await tokenizer.tokenizeChat(chat);
halfData.push(chat);
} }
// End index gone due to using UUID sets
// Last two chats must not be summarized, else request will be broken
if (halfData.length < 3) break; if (halfData.length === 0) break;
const stringlizedChat = halfData // please change this name to something else const stringlizedChat = halfData
.map((e) => `${e.role}: ${e.content}`) .map((e) => `${e.role}: ${e.content}`)
.join("\n"); .join("\n");
const summaryData = await summary(stringlizedChat); const summaryData = await summary(stringlizedChat);
@@ -258,18 +265,21 @@ export async function hypaMemoryV2(
currentTokens -= halfDataTokens; currentTokens -= halfDataTokens;
allocatedTokens -= summaryDataToken; allocatedTokens -= summaryDataToken;
// lastMainChunkId updating(increment) // Update lastMainChunkId and create a new mainChunk
data.lastMainChunkId++; data.lastMainChunkId++;
const newMainChunkId = data.lastMainChunkId; const newMainChunkId = data.lastMainChunkId;
const chatMemos = new Set(halfData.map((chat) => chat.memo)); const chatMemos = new Set(halfData.map((chat) => chat.memo));
const lastChatMemo = halfData[halfData.length - 1].memo;
data.mainChunks.push({ data.mainChunks.push({
id: newMainChunkId, id: newMainChunkId,
text: summaryData.data, text: summaryData.data,
chatMemos: chatMemos, chatMemos: chatMemos,
lastChatMemo: lastChatMemo,
}); });
// Split the summary into chunks based on double line breaks // Split the summary into chunks
const splitted = summaryData.data const splitted = summaryData.data
.split("\n\n") .split("\n\n")
.map((e) => e.trim()) .map((e) => e.trim())
@@ -282,11 +292,6 @@ export async function hypaMemoryV2(
text: e, text: e,
})) }))
); );
// Mark the chats as summarized
for (const memo of chatMemos) {
summarizedMemos.add(memo);
}
} }
// Construct the mainPrompt from mainChunks // Construct the mainPrompt from mainChunks
@@ -356,9 +361,7 @@ export async function hypaMemoryV2(
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>`;
// Filter out summarized chats // Filter out summarized chats
const unsummarizedChats = chats.filter( const unsummarizedChats = chats.slice(idx);
(chat) => !summarizedMemos.has(chat.memo)
);
// Insert the memory system prompt at the beginning // Insert the memory system prompt at the beginning
unsummarizedChats.unshift({ unsummarizedChats.unshift({
@@ -367,9 +370,6 @@ export async function hypaMemoryV2(
memo: "supaMemory", memo: "supaMemory",
}); });
// Add the last two chats back if they were removed
const lastTwoChatsSet = new Set(lastTwoChats.map((chat) => chat.memo));
console.log(lastTwoChatsSet) // Not so sure if chat.memo is unique id.
for (const chat of lastTwoChats) { for (const chat of lastTwoChats) {
if (!unsummarizedChats.find((c) => c.memo === chat.memo)) { if (!unsummarizedChats.find((c) => c.memo === chat.memo)) {
unsummarizedChats.push(chat); unsummarizedChats.push(chat);