Skip to content

Commit

Permalink
Merge pull request ChatGPTNextWeb#2028 from Yidadaa/bugfix-0618
Browse files Browse the repository at this point in the history
fix: ChatGPTNextWeb#1771 should not lose chat context when sumindex > n - count
  • Loading branch information
Yidadaa committed Jun 17, 2023
2 parents 0a2cccf + db63a5a commit 1c7f3e6
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/store/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,28 +370,30 @@ export const useChatStore = create<ChatStore>()(
context.push(memoryPrompt);
}

// get short term and unmemoried long term memory
// get short term and unmemorized long term memory
const shortTermMemoryMessageIndex = Math.max(
0,
n - modelConfig.historyMessageCount,
);
const longTermMemoryMessageIndex = session.lastSummarizeIndex;
const mostRecentIndex = Math.max(

// try to concat history messages
const memoryStartIndex = Math.min(
shortTermMemoryMessageIndex,
longTermMemoryMessageIndex,
);
const threshold = modelConfig.compressMessageLengthThreshold * 2;
const threshold = modelConfig.max_tokens;

// get recent messages as many as possible
const reversedRecentMessages = [];
for (
let i = n - 1, count = 0;
i >= mostRecentIndex && count < threshold;
i >= memoryStartIndex && count < threshold;
i -= 1
) {
const msg = messages[i];
if (!msg || msg.isError) continue;
count += msg.content.length;
count += estimateTokenLength(msg.content);
reversedRecentMessages.push(msg);
}

Expand Down

0 comments on commit 1c7f3e6

Please sign in to comment.