Add WebLLM source to /summarize command

30c19eb8fdaae54fd2d6db31469f91b2a1bbcbcb

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +10 -7Showing whitespace changes
public/scripts/extensions/memory/index.js+10 -7
@@ -40,6 +40,7 @@ let inApiCall = false;
4040/**
4141 * Count the number of tokens in the provided text.
4242 * @param {string} text Text to count tokens for
43+ * @param {number} padding Number of additional tokens to add to the count
4344 * @returns {Promise<number>} Number of tokens in the text
4445 */
4546async function countSourceTokens(text, padding = 0) {
@@ -389,17 +390,14 @@ function getIndexOfLatestChatSummary(chat) {
389390
390391async function onChatEvent() {
391392 // Module not enabled
392393 if (extension_settings.memory.source === summary_sources.extras && !modules.includes('summarize')) {
393- if (!modules.includes('summarize')) {
394394 return;
395395 }
396- }
397396
398- if (extension_settings.memory.source === summary_sources.webllm) {
397+ // WebLLM is not supported
399398 if (extension_settings.memory.source === summary_sources.webllm && !isWebLlmSupported()) {
400399 return;
401400 }
402- }
403401
404402 const context = getContext();
405403 const chat = context.chat;
@@ -510,6 +508,11 @@ async function summarizeCallback(args, text) {
510508 return await callExtrasSummarizeAPI(text);
511509 case summary_sources.main:
512510 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+ }
513516 default:
514517 toastr.warning('Invalid summarization source specified');
515518 return '';
@@ -1045,7 +1048,7 @@ jQuery(async function () {
10451048 name: 'summarize',
10461049 callback: summarizeCallback,
10471050 namedArgumentList: [
10481051 new SlashCommandNamedArgument('source', 'API to use for summarization', [ARGUMENT_TYPE.STRING], false, false, '', ['main', 'extras']Object.values(summary_sources)),
10491052 SlashCommandNamedArgument.fromProps({
10501053 name: 'prompt',
10511054 description: 'prompt to use for summarization',