fix: bug resolved
This commit is contained in:
@@ -213,7 +213,7 @@ export async function hypaMemoryV2(
|
|||||||
const maxSummarizationFailures = 3;
|
const maxSummarizationFailures = 3;
|
||||||
|
|
||||||
// Find the index to start summarizing from
|
// Find the index to start summarizing from
|
||||||
let idx = 0;
|
let idx = 2; // first two should not be considered
|
||||||
if (data.mainChunks.length > 0) {
|
if (data.mainChunks.length > 0) {
|
||||||
const lastMainChunk = data.mainChunks[data.mainChunks.length - 1];
|
const lastMainChunk = data.mainChunks[data.mainChunks.length - 1];
|
||||||
const lastChatMemo = lastMainChunk.lastChatMemo;
|
const lastChatMemo = lastMainChunk.lastChatMemo;
|
||||||
@@ -224,7 +224,7 @@ export async function hypaMemoryV2(
|
|||||||
}
|
}
|
||||||
// Starting chat index of new mainChunk to be generated
|
// Starting chat index of new mainChunk to be generated
|
||||||
|
|
||||||
// Token management loop(If current token exceeds allowed amount...)
|
// Token management loop (If current token usage exceeds allowed amount)
|
||||||
while (currentTokens >= maxContextTokens) {
|
while (currentTokens >= maxContextTokens) {
|
||||||
const halfData: OpenAIChat[] = [];
|
const halfData: OpenAIChat[] = [];
|
||||||
let halfDataTokens = 0;
|
let halfDataTokens = 0;
|
||||||
@@ -232,52 +232,65 @@ export async function hypaMemoryV2(
|
|||||||
const startIdx = idx;
|
const startIdx = idx;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"Entering summarization step:",
|
"Starting summarization iteration:",
|
||||||
"\nCurrent Tokens:", currentTokens,
|
"\nCurrent Tokens (before):", currentTokens,
|
||||||
"\nMax Context Tokens:", maxContextTokens,
|
"\nMax Context Tokens:", maxContextTokens,
|
||||||
"\nIndex Start:", startIdx
|
"\nStartIdx:", startIdx,
|
||||||
|
"\nchunkSize:", chunkSize
|
||||||
);
|
);
|
||||||
|
|
||||||
// Accumulate chats to summarize
|
// Accumulate chats to summarize
|
||||||
while (
|
while (
|
||||||
halfDataTokens < chunkSize &&
|
halfDataTokens < chunkSize &&
|
||||||
idx < chats.length - 2 // Ensure latest two chats are not added to summarization.
|
idx < chats.length - 2 // keep the last two chats from summarizing(else, the roles will be fucked up)
|
||||||
) {
|
) {
|
||||||
const chat = chats[idx];
|
const chat = chats[idx];
|
||||||
const chatTokens = await tokenizer.tokenizeChat(chat);
|
const chatTokens = await tokenizer.tokenizeChat(chat);
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"Evaluating chat for summarization:",
|
||||||
|
"\nIndex:", idx,
|
||||||
|
"\nRole:", chat.role,
|
||||||
|
"\nContent:", chat.content,
|
||||||
|
"\nchatTokens:", chatTokens,
|
||||||
|
"\nhalfDataTokens so far:", halfDataTokens,
|
||||||
|
"\nWould adding this exceed chunkSize?", (halfDataTokens + chatTokens > chunkSize)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Check if adding this chat would exceed our chunkSize limit
|
||||||
if (halfDataTokens + chatTokens > chunkSize) {
|
if (halfDataTokens + chatTokens > chunkSize) {
|
||||||
// If adding this chat would exceed chunkSize, break and summarize what we have
|
// Can't add this chat without going over chunkSize
|
||||||
|
// Break out, and summarize what we have so far.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add this chat to the halfData batch
|
||||||
halfData.push(chat);
|
halfData.push(chat);
|
||||||
halfDataTokens += chatTokens;
|
halfDataTokens += chatTokens;
|
||||||
idx++;
|
idx++;
|
||||||
}
|
}
|
||||||
|
|
||||||
const endIdx = idx - 1;
|
const endIdx = idx - 1;
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"Summarization batch ready:",
|
"Summarization batch chosen with this:",
|
||||||
"\nStartIdx:", startIdx,
|
"\nStartIdx:", startIdx,
|
||||||
"\nEndIdx:", endIdx,
|
"\nEndIdx:", endIdx,
|
||||||
"\nNumber of chats in halfData:", halfData.length,
|
"\nNumber of chats in halfData:", halfData.length,
|
||||||
"\nhalfDataTokens:", halfDataTokens,
|
"\nTotal tokens in halfData:", halfDataTokens,
|
||||||
"\nChats chosen for summarization:",
|
"\nChats selected:", halfData.map(h => ({role: h.role, content: h.content}))
|
||||||
halfData.map((c, i) => ({
|
|
||||||
index: startIdx + i,
|
|
||||||
role: c.role,
|
|
||||||
content: c.content
|
|
||||||
}))
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// If no chats were added, break to avoid infinite loop
|
||||||
if (halfData.length === 0) {
|
if (halfData.length === 0) {
|
||||||
console.log("No chats to summarize this round. Breaking out...");
|
console.log("No chats to summarize in this iteration, breaking out.");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
const stringlizedChat = halfData
|
const stringlizedChat = halfData
|
||||||
.map((e) => `${e.role}: ${e.content}`)
|
.map((e) => `${e.role}: ${e.content}`)
|
||||||
.join("\n");
|
.join("\n");
|
||||||
|
|
||||||
|
// Summarize the accumulated chunk
|
||||||
const summaryData = await summary(stringlizedChat);
|
const summaryData = await summary(stringlizedChat);
|
||||||
|
|
||||||
if (!summaryData.success) {
|
if (!summaryData.success) {
|
||||||
@@ -288,11 +301,11 @@ export async function hypaMemoryV2(
|
|||||||
return {
|
return {
|
||||||
currentTokens: currentTokens,
|
currentTokens: currentTokens,
|
||||||
chats: chats,
|
chats: chats,
|
||||||
error:
|
error: "Summarization failed multiple times. Aborting to prevent infinite loop.",
|
||||||
"Summarization failed multiple times. Aborting to prevent infinite loop.",
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
continue; // Retry summarizing next loop iteration
|
// If summarization fails, try again in next iteration
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
summarizationFailures = 0; // Reset on success
|
summarizationFailures = 0; // Reset on success
|
||||||
@@ -305,20 +318,23 @@ export async function hypaMemoryV2(
|
|||||||
console.log(
|
console.log(
|
||||||
"Summarization success:",
|
"Summarization success:",
|
||||||
"\nSummary Data:", summaryData.data,
|
"\nSummary Data:", summaryData.data,
|
||||||
"\nSummary Token Count:", summaryDataToken,
|
"\nSummary Token Count:", summaryDataToken
|
||||||
"\nBefore adjusting tokens:",
|
|
||||||
"\nCurrent Tokens:", currentTokens,
|
|
||||||
"\nAllocated Tokens:", allocatedTokens
|
|
||||||
);
|
);
|
||||||
|
|
||||||
mainPrompt += `\n\n${summaryData.data}`;
|
// **Token accounting fix:**
|
||||||
currentTokens -= halfDataTokens;
|
// Previous commits, the code likely have missed removing summarized chat's tokens.
|
||||||
allocatedTokens -= summaryDataToken;
|
// and never actually accounted for adding the summary tokens.
|
||||||
|
// Now we:
|
||||||
|
// 1. Remove old chats' tokens (they are replaced by summary)
|
||||||
|
// 2. Add summary tokens instead
|
||||||
|
currentTokens -= halfDataTokens; // remove original chats' tokens
|
||||||
|
currentTokens += summaryDataToken; // add the summary's tokens
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"After adjusting tokens:",
|
"After token adjustment:",
|
||||||
"\nCurrent Tokens:", currentTokens,
|
"\nRemoved halfDataTokens:", halfDataTokens,
|
||||||
"\nAllocated Tokens:", allocatedTokens
|
"\nAdded summaryDataToken:", summaryDataToken,
|
||||||
|
"\nCurrent Tokens (after):", currentTokens
|
||||||
);
|
);
|
||||||
|
|
||||||
// Update lastMainChunkId and create a new mainChunk
|
// Update lastMainChunkId and create a new mainChunk
|
||||||
@@ -341,11 +357,6 @@ export async function hypaMemoryV2(
|
|||||||
.map((e) => e.trim())
|
.map((e) => e.trim())
|
||||||
.filter((e) => e.length > 0);
|
.filter((e) => e.length > 0);
|
||||||
|
|
||||||
console.log(
|
|
||||||
"Splitting summary into chunks for memory:",
|
|
||||||
splitted
|
|
||||||
);
|
|
||||||
|
|
||||||
data.chunks.push(
|
data.chunks.push(
|
||||||
...splitted.map((e) => ({
|
...splitted.map((e) => ({
|
||||||
mainChunkID: newMainChunkId,
|
mainChunkID: newMainChunkId,
|
||||||
@@ -354,9 +365,10 @@ export async function hypaMemoryV2(
|
|||||||
);
|
);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
"End of iteration:",
|
"Chunks added:",
|
||||||
"\nData mainChunks count:", data.mainChunks.length,
|
splitted,
|
||||||
"\nData chunks count:", data.chunks.length
|
"\nUpdated mainChunks count:", data.mainChunks.length,
|
||||||
|
"\nUpdated chunks count:", data.chunks.length
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user