Fix automatic summarization

Closes #2746
This commit is contained in:
Cohee 2024-09-01 19:10:46 +03:00
parent f1a6303760
commit 9ed436fbb2

View File

@ -15,6 +15,7 @@ import {
generateRaw, generateRaw,
getMaxContextSize, getMaxContextSize,
setExtensionPrompt, setExtensionPrompt,
streamingProcessor,
} from '../../../script.js'; } from '../../../script.js';
import { is_group_generating, selected_group } from '../../group-chats.js'; import { is_group_generating, selected_group } from '../../group-chats.js';
import { loadMovingUIState } from '../../power-user.js'; import { loadMovingUIState } from '../../power-user.js';
@ -408,8 +409,8 @@ async function onChatEvent() {
return; return;
} }
// Generation is in progress, summary prevented // Streaming in-progress
if (is_send_press) { if (streamingProcessor && !streamingProcessor.isFinished) {
return; return;
} }
@ -446,15 +447,9 @@ async function onChatEvent() {
delete chat[chat.length - 1].extra.memory; delete chat[chat.length - 1].extra.memory;
} }
try { summarizeChat(context)
await summarizeChat(context); .catch(console.error)
} .finally(saveLastValues);
catch (error) {
console.log(error);
}
finally {
saveLastValues();
}
} }
/** /**
@ -567,7 +562,7 @@ async function getSummaryPromptForNow(context, force) {
await waitUntilCondition(() => is_group_generating === false, 1000, 10); await waitUntilCondition(() => is_group_generating === false, 1000, 10);
} }
// Wait for the send button to be released // Wait for the send button to be released
waitUntilCondition(() => is_send_press === false, 30000, 100); await waitUntilCondition(() => is_send_press === false, 30000, 100);
} catch { } catch {
console.debug('Timeout waiting for is_send_press'); console.debug('Timeout waiting for is_send_press');
return ''; return '';
@ -650,9 +645,16 @@ async function summarizeChatWebLLM(context, force) {
params.max_tokens = extension_settings.memory.overrideResponseLength; params.max_tokens = extension_settings.memory.overrideResponseLength;
} }
try {
inApiCall = true;
const summary = await generateWebLlmChatPrompt(messages, params); const summary = await generateWebLlmChatPrompt(messages, params);
const newContext = getContext(); const newContext = getContext();
if (!summary) {
console.warn('Empty summary received');
return;
}
// something changed during summarization request // something changed during summarization request
if (newContext.groupId !== context.groupId || if (newContext.groupId !== context.groupId ||
newContext.chatId !== context.chatId || newContext.chatId !== context.chatId ||
@ -663,6 +665,9 @@ async function summarizeChatWebLLM(context, force) {
setMemoryContext(summary, true, lastUsedIndex); setMemoryContext(summary, true, lastUsedIndex);
return summary; return summary;
} finally {
inApiCall = false;
}
} }
async function summarizeChatMain(context, force, skipWIAN) { async function summarizeChatMain(context, force, skipWIAN) {
@ -677,12 +682,18 @@ async function summarizeChatMain(context, force, skipWIAN) {
let index = null; let index = null;
if (prompt_builders.DEFAULT === extension_settings.memory.prompt_builder) { if (prompt_builders.DEFAULT === extension_settings.memory.prompt_builder) {
try {
inApiCall = true;
summary = await generateQuietPrompt(prompt, false, skipWIAN, '', '', extension_settings.memory.overrideResponseLength); summary = await generateQuietPrompt(prompt, false, skipWIAN, '', '', extension_settings.memory.overrideResponseLength);
} finally {
inApiCall = false;
}
} }
if ([prompt_builders.RAW_BLOCKING, prompt_builders.RAW_NON_BLOCKING].includes(extension_settings.memory.prompt_builder)) { if ([prompt_builders.RAW_BLOCKING, prompt_builders.RAW_NON_BLOCKING].includes(extension_settings.memory.prompt_builder)) {
const lock = extension_settings.memory.prompt_builder === prompt_builders.RAW_BLOCKING; const lock = extension_settings.memory.prompt_builder === prompt_builders.RAW_BLOCKING;
try { try {
inApiCall = true;
if (lock) { if (lock) {
deactivateSendButtons(); deactivateSendButtons();
} }
@ -700,12 +711,18 @@ async function summarizeChatMain(context, force, skipWIAN) {
summary = await generateRaw(rawPrompt, '', false, false, prompt, extension_settings.memory.overrideResponseLength); summary = await generateRaw(rawPrompt, '', false, false, prompt, extension_settings.memory.overrideResponseLength);
index = lastUsedIndex; index = lastUsedIndex;
} finally { } finally {
inApiCall = false;
if (lock) { if (lock) {
activateSendButtons(); activateSendButtons();
} }
} }
} }
if (!summary) {
console.warn('Empty summary received');
return;
}
const newContext = getContext(); const newContext = getContext();
// something changed during summarization request // something changed during summarization request
@ -840,6 +857,11 @@ async function summarizeChatExtras(context) {
const summary = await callExtrasSummarizeAPI(resultingString); const summary = await callExtrasSummarizeAPI(resultingString);
const newContext = getContext(); const newContext = getContext();
if (!summary) {
console.warn('Empty summary received');
return;
}
// something changed during summarization request // something changed during summarization request
if (newContext.groupId !== context.groupId if (newContext.groupId !== context.groupId
|| newContext.chatId !== context.chatId || newContext.chatId !== context.chatId