From e43e771cc534092e04edf30289066f9fef138147 Mon Sep 17 00:00:00 2001 From: kingbri <8082010+kingbri1@users.noreply.github.com> Date: Fri, 13 Jun 2025 21:55:02 -0400 Subject: [PATCH 1/2] Emotions: Use the full context for main API To prevent prompt reprocessing per turn, use the full context when prompting with the main API. Doing so keeps the preserved prefix while adding an extra sentence rather than providing two different prompts for each turn. In addition, JSON schemas might need a prompt to coerce a proper schema return from the model. Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com> --- public/scripts/extensions/expressions/index.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 50ed07d7d..9788d2a53 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -1,6 +1,6 @@ import { Fuse } from '../../../lib.js'; -import { characters, eventSource, event_types, generateRaw, getRequestHeaders, main_api, online_status, saveSettingsDebounced, substituteParams, substituteParamsExtended, system_message_types, this_chid } from '../../../script.js'; +import { characters, eventSource, event_types, generateQuietPrompt, generateRaw, getRequestHeaders, main_api, online_status, saveSettingsDebounced, substituteParams, substituteParamsExtended, system_message_types, this_chid } from '../../../script.js'; import { dragElement, isMobile } from '../../RossAscends-mods.js'; import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; @@ -909,10 +909,6 @@ function sampleClassifyText(text) { * @returns {Promise} Prompt for the LLM API. */ async function getLlmPrompt(labels) { - if (isJsonSchemaSupported()) { - return ''; - } - const labelsString = labels.map(x => `"${x}"`).join(', '); const prompt = substituteParamsExtended(String(extension_settings.expressions.llmPrompt), { labels: labelsString }); return prompt; @@ -1047,7 +1043,14 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); - const emotionResponse = await generateRaw(text, main_api, false, false, prompt); + + let emotionResponse; + try { + inApiCall = true; + emotionResponse = await generateQuietPrompt(prompt, false, false); + } finally { + inApiCall = false; + } return parseLlmResponse(emotionResponse, expressionsList); } // Using WebLLM From 9efd40980fbac085c29b5d62013486e8c7ee4fd3 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Sat, 14 Jun 2025 20:01:59 +0300 Subject: [PATCH 2/2] Add prompt type selection for LLM expressions in settings --- public/scripts/extensions.js | 1 + .../scripts/extensions/expressions/index.js | 31 ++++++++++++++++++- .../extensions/expressions/settings.html | 15 ++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/public/scripts/extensions.js b/public/scripts/extensions.js index ab7d8c2f7..e0d9c38fa 100644 --- a/public/scripts/extensions.js +++ b/public/scripts/extensions.js @@ -178,6 +178,7 @@ export const extension_settings = { llmPrompt: undefined, allowMultiple: true, rerollIfSame: false, + promptType: 'raw', }, connectionManager: { selectedProfile: '', diff --git a/public/scripts/extensions/expressions/index.js b/public/scripts/extensions/expressions/index.js index 9788d2a53..d05fdcb69 100644 --- a/public/scripts/extensions/expressions/index.js +++ b/public/scripts/extensions/expressions/index.js @@ -86,6 +86,12 @@ const EXPRESSION_API = { none: 99, }; +/** @enum {string} */ +const PROMPT_TYPE = { + raw: 'raw', + full: 'full', +}; + let expressionsList = null; let lastCharacter = undefined; let lastMessage = null; @@ -1047,7 +1053,14 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin let emotionResponse; try { inApiCall = true; - emotionResponse = await generateQuietPrompt(prompt, false, false); + switch (extension_settings.expressions.promptType) { + case PROMPT_TYPE.raw: + emotionResponse = await generateRaw(text, main_api, false, false, prompt); + break; + case PROMPT_TYPE.full: + emotionResponse = await generateQuietPrompt(prompt, false, false); + break; + } } finally { inApiCall = false; } @@ -1705,6 +1718,7 @@ function onExpressionApiChanged() { if (tempApi) { extension_settings.expressions.api = Number(tempApi); $('.expression_llm_prompt_block').toggle([EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)); + $('.expression_prompt_type_block').toggle(extension_settings.expressions.api === EXPRESSION_API.llm); expressionsList = null; spriteCache = {}; moduleWorker(); @@ -2105,6 +2119,11 @@ function migrateSettings() { extension_settings.expressions.showDefault = false; saveSettingsDebounced(); } + + if (extension_settings.expressions.promptType === undefined) { + extension_settings.expressions.promptType = PROMPT_TYPE.raw; + saveSettingsDebounced(); + } } (async function () { @@ -2172,6 +2191,16 @@ function migrateSettings() { extension_settings.expressions.llmPrompt = DEFAULT_LLM_PROMPT; saveSettingsDebounced(); }); + $('#expression_prompt_raw').on('input', function () { + extension_settings.expressions.promptType = PROMPT_TYPE.raw; + saveSettingsDebounced(); + }); + $('#expression_prompt_full').on('input', function () { + extension_settings.expressions.promptType = PROMPT_TYPE.full; + saveSettingsDebounced(); + }); + $(`input[name="expression_prompt_type"][value="${extension_settings.expressions.promptType}"]`).prop('checked', true); + $('.expression_prompt_type_block').toggle(extension_settings.expressions.api === EXPRESSION_API.llm); $('#expression_custom_add').on('click', onClickExpressionAddCustom); $('#expression_custom_remove').on('click', onClickExpressionRemoveCustom); diff --git a/public/scripts/extensions/expressions/settings.html b/public/scripts/extensions/expressions/settings.html index 48ffba4e0..06827c57c 100644 --- a/public/scripts/extensions/expressions/settings.html +++ b/public/scripts/extensions/expressions/settings.html @@ -41,7 +41,20 @@ Will be used if the API doesn't support JSON schemas or function calling. - + + +
+
+ LLM Prompt Strategy +
+ +