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