Add WebLLM extension summarization
| @@ -25,6 +25,7 @@ import { SlashCommandParser } from '../../slash-commands/SlashCommandParser.js'; | ||
| 25 | 25 | import { SlashCommand } from '../../slash-commands/SlashCommand.js'; |
| 26 | 26 | import { ARGUMENT_TYPE, SlashCommandArgument, SlashCommandNamedArgument } from '../../slash-commands/SlashCommandArgument.js'; |
| 27 | 27 | import { MacrosParser } from '../../macros.js'; |
| 28 | +import { countWebLlmTokens, generateWebLlmChatPrompt, getWebLlmContextSize, isWebLlmSupported } from '../shared.js'; | |
| 28 | 29 | export { MODULE_NAME }; |
| 29 | 30 | |
| 30 | 31 | const MODULE_NAME = '1_memory'; |
| @@ -36,6 +37,40 @@ let lastMessageHash = null; | ||
| 36 | 37 | let lastMessageId = null; |
| 37 | 38 | let inApiCall = false; |
| 38 | 39 | |
| 40 | +/** | |
| 41 | + * Count the number of tokens in the provided text. | |
| 42 | + * @param {string} text Text to count tokens for | |
| 43 | + * @returns {Promise<number>} Number of tokens in the text | |
| 44 | + */ | |
| 45 | +async function countSourceTokens(text, padding = 0) { | |
| 46 | + if (extension_settings.memory.source === summary_sources.webllm) { | |
| 47 | + const count = await countWebLlmTokens(text); | |
| 48 | + return count + padding; | |
| 49 | + } | |
| 50 | + | |
| 51 | + if (extension_settings.memory.source === summary_sources.extras) { | |
| 52 | + const count = getTextTokens(tokenizers.GPT2, text).length; | |
| 53 | + return count + padding; | |
| 54 | + } | |
| 55 | + | |
| 56 | + return await getTokenCountAsync(text, padding); | |
| 57 | +} | |
| 58 | + | |
| 59 | +async function getSourceContextSize() { | |
| 60 | + const overrideLength = extension_settings.memory.overrideResponseLength; | |
| 61 | + | |
| 62 | + if (extension_settings.memory.source === summary_sources.webllm) { | |
| 63 | + const maxContext = await getWebLlmContextSize(); | |
| 64 | + return overrideLength > 0 ? (maxContext - overrideLength) : Math.round(maxContext * 0.75); | |
| 65 | + } | |
| 66 | + | |
| 67 | + if (extension_settings.source === summary_sources.extras) { | |
| 68 | + return 1024; | |
| 69 | + } | |
| 70 | + | |
| 71 | + return getMaxContextSize(overrideLength); | |
| 72 | +} | |
| 73 | + | |
| 39 | 74 | const formatMemoryValue = function (value) { |
| 40 | 75 | if (!value) { |
| 41 | 76 | return ''; |
| @@ -55,6 +90,7 @@ const saveChatDebounced = debounce(() => getContext().saveChat(), debounce_timeo | ||
| 55 | 90 | const summary_sources = { |
| 56 | 91 | 'extras': 'extras', |
| 57 | 92 | 'main': 'main', |
| 93 | + 'webllm': 'webllm', | |
| 58 | 94 | }; |
| 59 | 95 | |
| 60 | 96 | const prompt_builders = { |
| @@ -130,12 +166,12 @@ function loadSettings() { | ||
| 130 | 166 | |
| 131 | 167 | async function onPromptForceWordsAutoClick() { |
| 132 | 168 | const context = getContext(); |
| 133 | 169 | const maxPromptLength = getMaxContextSizeawait getSourceContextSize(extension_settings.memory.overrideResponseLength); |
| 134 | 170 | const chat = context.chat; |
| 135 | 171 | const allMessages = chat.filter(m => !m.is_system && m.mes).map(m => m.mes); |
| 136 | 172 | const messagesWordCount = allMessages.map(m => extractAllWords(m)).flat().length; |
| 137 | 173 | const averageMessageWordCount = messagesWordCount / allMessages.length; |
| 138 | 174 | const tokensPerWord = await getTokenCountAsynccountSourceTokens(allMessages.join('\n')) / messagesWordCount; |
| 139 | 175 | const wordsPerToken = 1 / tokensPerWord; |
| 140 | 176 | const maxPromptLengthWords = Math.round(maxPromptLength * wordsPerToken); |
| 141 | 177 | // How many words should pass so that messages will start be dropped out of context; |
| @@ -168,15 +204,15 @@ async function onPromptForceWordsAutoClick() { | ||
| 168 | 204 | |
| 169 | 205 | async function onPromptIntervalAutoClick() { |
| 170 | 206 | const context = getContext(); |
| 171 | 207 | const maxPromptLength = getMaxContextSizeawait getSourceContextSize(extension_settings.memory.overrideResponseLength); |
| 172 | 208 | const chat = context.chat; |
| 173 | 209 | const allMessages = chat.filter(m => !m.is_system && m.mes).map(m => m.mes); |
| 174 | 210 | const messagesWordCount = allMessages.map(m => extractAllWords(m)).flat().length; |
| 175 | 211 | const messagesTokenCount = await getTokenCountAsynccountSourceTokens(allMessages.join('\n')); |
| 176 | 212 | const tokensPerWord = messagesTokenCount / messagesWordCount; |
| 177 | 213 | const averageMessageTokenCount = messagesTokenCount / allMessages.length; |
| 178 | 214 | const targetSummaryTokens = Math.round(extension_settings.memory.promptWords * tokensPerWord); |
| 179 | 215 | const promptTokens = await getTokenCountAsynccountSourceTokens(extension_settings.memory.prompt); |
| 180 | 216 | const promptAllowance = maxPromptLength - promptTokens - targetSummaryTokens; |
| 181 | 217 | const maxMessagesPerSummary = extension_settings.memory.maxMessagesPerRequest || 0; |
| 182 | 218 | const averageMessagesPerPrompt = Math.floor(promptAllowance / averageMessageTokenCount); |
| @@ -213,8 +249,8 @@ function onSummarySourceChange(event) { | ||
| 213 | 249 | |
| 214 | 250 | function switchSourceControls(value) { |
| 215 | 251 | $('#memory_settings [data-summary-source]').each((_, element) => { |
| 216 | 252 | const source = $(element).datadataset.summarySource.split('summary-source,').map(s => s.trim()); |
| 217 | 253 | $(element).toggle(source === .includes(value)); |
| 218 | 254 | }); |
| 219 | 255 | } |
| 220 | 256 | |
| @@ -359,6 +395,12 @@ async function onChatEvent() { | ||
| 359 | 395 | } |
| 360 | 396 | } |
| 361 | 397 | |
| 398 | + if (extension_settings.memory.source === summary_sources.webllm) { | |
| 399 | + if (!isWebLlmSupported()) { | |
| 400 | + return; | |
| 401 | + } | |
| 402 | + } | |
| 403 | + | |
| 362 | 404 | const context = getContext(); |
| 363 | 405 | const chat = context.chat; |
| 364 | 406 | |
| @@ -431,8 +473,12 @@ async function forceSummarizeChat() { | ||
| 431 | 473 | return ''; |
| 432 | 474 | } |
| 433 | 475 | |
| 434 | 476 | const toast = toastr.info('Summarizing chat...', 'Please wait', { timeOut: 0, extendedTimeOut: 0 }); |
| 435 | - const value = await summarizeChatMain(context, true, skipWIAN); | |
| 477 | + const value = extension_settings.memory.source === summary_sources.main | |
| 478 | + ? await summarizeChatMain(context, true, skipWIAN) | |
| 479 | + : await summarizeChatWebLLM(context, true); | |
| 480 | + | |
| 481 | + toastr.clear(toast); | |
| 436 | 482 | |
| 437 | 483 | if (!value) { |
| 438 | 484 | toastr.warning('Failed to summarize chat'); |
| @@ -484,16 +530,25 @@ async function summarizeChat(context) { | ||
| 484 | 530 | case summary_sources.main: |
| 485 | 531 | await summarizeChatMain(context, false, skipWIAN); |
| 486 | 532 | break; |
| 533 | + case summary_sources.webllm: | |
| 534 | + await summarizeChatWebLLM(context, false); | |
| 535 | + break; | |
| 487 | 536 | default: |
| 488 | 537 | break; |
| 489 | 538 | } |
| 490 | 539 | } |
| 491 | 540 | |
| 492 | -async function summarizeChatMain(context, force, skipWIAN) { | |
| 541 | +/** | |
| 493 | - | |
| 542 | + * Check if the chat should be summarized based on the current conditions. | |
| 543 | + * Return summary prompt if it should be summarized. | |
| 544 | + * @param {any} context ST context | |
| 545 | + * @param {boolean} force Summarize the chat regardless of the conditions | |
| 546 | + * @returns {Promise<string>} Summary prompt or empty string | |
| 547 | + */ | |
| 548 | +async function getSummaryPromptForNow(context, force) { | |
| 494 | 549 | if (extension_settings.memory.promptInterval === 0 && !force) { |
| 495 | 550 | console.debug('Prompt interval is set to 0, skipping summarization'); |
| 496 | 551 | return ''; |
| 497 | 552 | } |
| 498 | 553 | |
| 499 | 554 | try { |
| @@ -505,17 +560,17 @@ async function summarizeChatMain(context, force, skipWIAN) { | ||
| 505 | 560 | waitUntilCondition(() => is_send_press === false, 30000, 100); |
| 506 | 561 | } catch { |
| 507 | 562 | console.debug('Timeout waiting for is_send_press'); |
| 508 | 563 | return ''; |
| 509 | 564 | } |
| 510 | 565 | |
| 511 | 566 | if (!context.chat.length) { |
| 512 | 567 | console.debug('No messages in chat to summarize'); |
| 513 | 568 | return ''; |
| 514 | 569 | } |
| 515 | 570 | |
| 516 | 571 | if (context.chat.length < extension_settings.memory.promptInterval && !force) { |
| 517 | 572 | console.debug(`Not enough messages in chat to summarize (chat: ${context.chat.length}, interval: ${extension_settings.memory.promptInterval})`); |
| 518 | 573 | return ''; |
| 519 | 574 | } |
| 520 | 575 | |
| 521 | 576 | let messagesSinceLastSummary = 0; |
| @@ -539,7 +594,7 @@ async function summarizeChatMain(context, force, skipWIAN) { | ||
| 539 | 594 | |
| 540 | 595 | if (!conditionSatisfied && !force) { |
| 541 | 596 | console.debug(`Summary conditions not satisfied (messages: ${messagesSinceLastSummary}, interval: ${extension_settings.memory.promptInterval}, words: ${wordsSinceLastSummary}, force words: ${extension_settings.memory.promptForceWords})`); |
| 542 | 597 | return ''; |
| 543 | 598 | } |
| 544 | 599 | |
| 545 | 600 | console.log('Summarizing chat, messages since last summary: ' + messagesSinceLastSummary, 'words since last summary: ' + wordsSinceLastSummary); |
| @@ -547,6 +602,63 @@ async function summarizeChatMain(context, force, skipWIAN) { | ||
| 547 | 602 | |
| 548 | 603 | if (!prompt) { |
| 549 | 604 | console.debug('Summarization prompt is empty. Skipping summarization.'); |
| 605 | + return ''; | |
| 606 | + } | |
| 607 | + | |
| 608 | + return prompt; | |
| 609 | +} | |
| 610 | + | |
| 611 | +async function summarizeChatWebLLM(context, force) { | |
| 612 | + if (!isWebLlmSupported()) { | |
| 613 | + return; | |
| 614 | + } | |
| 615 | + | |
| 616 | + const prompt = await getSummaryPromptForNow(context, force); | |
| 617 | + | |
| 618 | + if (!prompt) { | |
| 619 | + return; | |
| 620 | + } | |
| 621 | + | |
| 622 | + const { rawPrompt, lastUsedIndex } = await getRawSummaryPrompt(context, prompt); | |
| 623 | + | |
| 624 | + if (lastUsedIndex === null || lastUsedIndex === -1) { | |
| 625 | + if (force) { | |
| 626 | + toastr.info('To try again, remove the latest summary.', 'No messages found to summarize'); | |
| 627 | + } | |
| 628 | + | |
| 629 | + return null; | |
| 630 | + } | |
| 631 | + | |
| 632 | + const messages = [ | |
| 633 | + { role: 'system', content: prompt }, | |
| 634 | + { role: 'user', content: rawPrompt }, | |
| 635 | + ]; | |
| 636 | + | |
| 637 | + const params = {}; | |
| 638 | + | |
| 639 | + if (extension_settings.memory.overrideResponseLength > 0) { | |
| 640 | + params.max_tokens = extension_settings.memory.overrideResponseLength; | |
| 641 | + } | |
| 642 | + | |
| 643 | + const summary = await generateWebLlmChatPrompt(messages, params); | |
| 644 | + const newContext = getContext(); | |
| 645 | + | |
| 646 | + // something changed during summarization request | |
| 647 | + if (newContext.groupId !== context.groupId || | |
| 648 | + newContext.chatId !== context.chatId || | |
| 649 | + (!newContext.groupId && (newContext.characterId !== context.characterId))) { | |
| 650 | + console.log('Context changed, summary discarded'); | |
| 651 | + return; | |
| 652 | + } | |
| 653 | + | |
| 654 | + setMemoryContext(summary, true, lastUsedIndex); | |
| 655 | + return summary; | |
| 656 | +} | |
| 657 | + | |
| 658 | +async function summarizeChatMain(context, force, skipWIAN) { | |
| 659 | + const prompt = await getSummaryPromptForNow(context, force); | |
| 660 | + | |
| 661 | + if (!prompt) { | |
| 550 | 662 | return; |
| 551 | 663 | } |
| 552 | 664 | |
| @@ -634,7 +746,7 @@ async function getRawSummaryPrompt(context, prompt) { | ||
| 634 | 746 | chat.pop(); // We always exclude the last message from the buffer |
| 635 | 747 | const chatBuffer = []; |
| 636 | 748 | const PADDING = 64; |
| 637 | 749 | const PROMPT_SIZE = getMaxContextSizeawait getSourceContextSize(extension_settings.memory.overrideResponseLength); |
| 638 | 750 | let latestUsedMessage = null; |
| 639 | 751 | |
| 640 | 752 | for (let index = latestSummaryIndex + 1; index < chat.length; index++) { |
| @@ -651,7 +763,7 @@ async function getRawSummaryPrompt(context, prompt) { | ||
| 651 | 763 | const entry = `${message.name}:\n${message.mes}`; |
| 652 | 764 | chatBuffer.push(entry); |
| 653 | 765 | |
| 654 | 766 | const tokens = await getTokenCountAsynccountSourceTokens(getMemoryString(true), PADDING); |
| 655 | 767 | |
| 656 | 768 | if (tokens > PROMPT_SIZE) { |
| 657 | 769 | chatBuffer.pop(); |
| @@ -13,6 +13,7 @@ | ||
| 13 | 13 | <select id="summary_source"> |
| 14 | 14 | <option value="main" data-i18n="ext_sum_main_api">Main API</option> |
| 15 | 15 | <option value="extras">Extras API</option> |
| 16 | + <option value="webllm" data-i18n="ext_sum_webllm">WebLLM Extension</option> | |
| 16 | 17 | </select><br> |
| 17 | 18 | |
| 18 | 19 | <div class="flex-container justifyspacebetween alignitemscenter"> |
| @@ -24,7 +25,7 @@ | ||
| 24 | 25 | |
| 25 | 26 | <textarea id="memory_contents" class="text_pole textarea_compact" rows="6" data-i18n="[placeholder]ext_sum_memory_placeholder" placeholder="Summary will be generated here..."></textarea> |
| 26 | 27 | <div class="memory_contents_controls"> |
| 27 | 28 | <div id="memory_force_summarize" data-summary-source="main,webllm" class="menu_button menu_button_icon" title="Trigger a summary update right now." data-i18n="[title]ext_sum_force_tip"> |
| 28 | 29 | <i class="fa-solid fa-database"></i> |
| 29 | 30 | <span data-i18n="ext_sum_force_text">Summarize now</span> |
| 30 | 31 | </div> |
| @@ -58,7 +59,7 @@ | ||
| 58 | 59 | <span data-i18n="ext_sum_prompt_builder_3">Classic, blocking</span> |
| 59 | 60 | </label> |
| 60 | 61 | </div> |
| 61 | 62 | <div data-summary-source="main,webllm"> |
| 62 | 63 | <label for="memory_prompt" class="title_restorable"> |
| 63 | 64 | <span data-i18n="Summary Prompt">Summary Prompt</span> |
| 64 | 65 | <div id="memory_prompt_restore" data-i18n="[title]ext_sum_restore_default_prompt_tip" title="Restore default prompt" class="right_menu_button"> |
| @@ -74,7 +75,7 @@ | ||
| 74 | 75 | </label> |
| 75 | 76 | <input id="memory_override_response_length" type="range" value="{{defaultSettings.overrideResponseLength}}" min="{{defaultSettings.overrideResponseLengthMin}}" max="{{defaultSettings.overrideResponseLengthMax}}" step="{{defaultSettings.overrideResponseLengthStep}}" /> |
| 76 | 77 | <label for="memory_max_messages_per_request"> |
| 77 | 78 | <span data-i18n="ext_sum_raw_max_msg">[Raw/WebLLM] Max messages per request</span> (<span id="memory_max_messages_per_request_value"></span>) |
| 78 | 79 | <small class="memory_disabled_hint" data-i18n="ext_sum_0_unlimited">0 = unlimited</small> |
| 79 | 80 | </label> |
| 80 | 81 | <input id="memory_max_messages_per_request" type="range" value="{{defaultSettings.maxMessagesPerRequest}}" min="{{defaultSettings.maxMessagesPerRequestMin}}" max="{{defaultSettings.maxMessagesPerRequestMax}}" step="{{defaultSettings.maxMessagesPerRequestStep}}" /> |
| @@ -183,15 +183,21 @@ function throwIfInvalidModel(useReverseProxy) { | ||
| 183 | 183 | */ |
| 184 | 184 | export function isWebLlmSupported() { |
| 185 | 185 | if (!('gpu' in navigator)) { |
| 186 | + const warningKey = 'webllm_browser_warning_shown'; | |
| 187 | + if (!sessionStorage.getItem(warningKey)) { | |
| 186 | 188 | toastr.error('Your browser does not support the WebGPU API. Please use a different browser.', 'WebLLM', { |
| 187 | 189 | preventDuplicates: true, |
| 188 | 190 | timeOut: 0, |
| 189 | 191 | extendedTimeOut: 0, |
| 190 | 192 | }); |
| 193 | + sessionStorage.setItem(warningKey, '1'); | |
| 194 | + } | |
| 191 | 195 | return false; |
| 192 | 196 | } |
| 193 | 197 | |
| 194 | 198 | if (!('llm' in SillyTavern)) { |
| 199 | + const warningKey = 'webllm_extension_warning_shown'; | |
| 200 | + if (!sessionStorage.getItem(warningKey)) { | |
| 195 | 201 | toastr.error('WebLLM extension is not installed. Click here to install it.', 'WebLLM', { |
| 196 | 202 | timeOut: 0, |
| 197 | 203 | extendedTimeOut: 0, |
| @@ -209,6 +215,8 @@ export function isWebLlmSupported() { | ||
| 209 | 215 | } |
| 210 | 216 | }, |
| 211 | 217 | }); |
| 218 | + sessionStorage.setItem(warningKey, '1'); | |
| 219 | + } | |
| 212 | 220 | return false; |
| 213 | 221 | } |
| 214 | 222 | |
| @@ -218,15 +226,16 @@ export function isWebLlmSupported() { | ||
| 218 | 226 | /** |
| 219 | 227 | * Generates text in response to a chat prompt using WebLLM. |
| 220 | 228 | * @param {any[]} messages Messages to use for generating |
| 229 | + * @param {object} params Additional parameters | |
| 221 | 230 | * @returns {Promise<string>} Generated response |
| 222 | 231 | */ |
| 223 | 232 | export async function generateWebLlmChatPrompt(messages, params = {}) { |
| 224 | 233 | if (!isWebLlmSupported()) { |
| 225 | 234 | throw new Error('WebLLM extension is not installed.'); |
| 226 | 235 | } |
| 227 | 236 | |
| 228 | 237 | const engine = SillyTavern.llm; |
| 229 | 238 | const response = await engine.generateChatPrompt(messages, params); |
| 230 | 239 | return response; |
| 231 | 240 | } |
| 232 | 241 | |