Add WebLLM source to /summarize command
| @@ -40,6 +40,7 @@ let inApiCall = false; | ||
| 40 | 40 | /** |
| 41 | 41 | * Count the number of tokens in the provided text. |
| 42 | 42 | * @param {string} text Text to count tokens for |
| 43 | + * @param {number} padding Number of additional tokens to add to the count | |
| 43 | 44 | * @returns {Promise<number>} Number of tokens in the text |
| 44 | 45 | */ |
| 45 | 46 | async function countSourceTokens(text, padding = 0) { |
| @@ -389,17 +390,14 @@ function getIndexOfLatestChatSummary(chat) { | ||
| 389 | 390 | |
| 390 | 391 | async function onChatEvent() { |
| 391 | 392 | // Module not enabled |
| 392 | 393 | if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) { |
| 393 | - if (!modules.includes('summarize')) { | |
| 394 | 394 | return; |
| 395 | 395 | } |
| 396 | - } | |
| 397 | 396 | |
| 398 | - if (extension_settings.memory.source === summary_sources.webllm) { | |
| 397 | + // WebLLM is not supported | |
| 399 | 398 | if (extension_settings.memory.source === summary_sources.webllm && !isWebLlmSupported()) { |
| 400 | 399 | return; |
| 401 | 400 | } |
| 402 | - } | |
| 403 | 401 | |
| 404 | 402 | const context = getContext(); |
| 405 | 403 | const chat = context.chat; |
| @@ -510,6 +508,11 @@ async function summarizeCallback(args, text) { | ||
| 510 | 508 | return await callExtrasSummarizeAPI(text); |
| 511 | 509 | case summary_sources.main: |
| 512 | 510 | return await generateRaw(text, '', false, false, prompt, extension_settings.memory.overrideResponseLength); |
| 511 | + case summary_sources.webllm: { | |
| 512 | + const messages = [{ role: 'system', content: prompt }, { role: 'user', content: text }].filter(m => m.content); | |
| 513 | + const params = extension_settings.memory.overrideResponseLength > 0 ? { max_tokens: extension_settings.memory.overrideResponseLength } : {}; | |
| 514 | + return await generateWebLlmChatPrompt(messages, params); | |
| 515 | + } | |
| 513 | 516 | default: |
| 514 | 517 | toastr.warning('Invalid summarization source specified'); |
| 515 | 518 | return ''; |
| @@ -1045,7 +1048,7 @@ jQuery(async function () { | ||
| 1045 | 1048 | name: 'summarize', |
| 1046 | 1049 | callback: summarizeCallback, |
| 1047 | 1050 | namedArgumentList: [ |
| 1048 | 1051 | new SlashCommandNamedArgument('source', 'API to use for summarization', [ARGUMENT_TYPE.STRING], false, false, '', ['main', 'extras']Object.values(summary_sources)), |
| 1049 | 1052 | SlashCommandNamedArgument.fromProps({ |
| 1050 | 1053 | name: 'prompt', |
| 1051 | 1054 | description: 'prompt to use for summarization', |