Merge pull request #4157 from kingbri1/staging Emotions: Use the full context for main API
Signed| @@ -178,6 +178,7 @@ export const extension_settings = { | |||
| 178 | llmPrompt: undefined, | 178 | llmPrompt: undefined, |
| 179 | allowMultiple: true, | 179 | allowMultiple: true, |
| 180 | rerollIfSame: false, | 180 | rerollIfSame: false, |
| 181 | promptType: 'raw', | ||
| 181 | }, | 182 | }, |
| 182 | connectionManager: { | 183 | connectionManager: { |
| 183 | selectedProfile: '', | 184 | selectedProfile: '', |
| @@ -1,6 +1,6 @@ | |||
| 1 | import { Fuse } from '../../../lib.js'; | 1 | import { Fuse } from '../../../lib.js'; |
| 2 | 2 | ||
| 3 | import { characters, eventSource, event_types, generateRaw, getRequestHeaders, main_api, online_status, saveSettingsDebounced, substituteParams, substituteParamsExtended, system_message_types, this_chid } from '../../../script.js'; | 3 | import { characters, eventSource, event_types, generateQuietPrompt, generateRaw, getRequestHeaders, main_api, online_status, saveSettingsDebounced, substituteParams, substituteParamsExtended, system_message_types, this_chid } from '../../../script.js'; |
| 4 | import { dragElement, isMobile } from '../../RossAscends-mods.js'; | 4 | import { dragElement, isMobile } from '../../RossAscends-mods.js'; |
| 5 | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; | 5 | import { getContext, getApiUrl, modules, extension_settings, ModuleWorkerWrapper, doExtrasFetch, renderExtensionTemplateAsync } from '../../extensions.js'; |
| 6 | import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; | 6 | import { loadMovingUIState, performFuzzySearch, power_user } from '../../power-user.js'; |
| @@ -86,6 +86,12 @@ const EXPRESSION_API = { | |||
| 86 | none: 99, | 86 | none: 99, |
| 87 | }; | 87 | }; |
| 88 | 88 | ||
| 89 | /** @enum {string} */ | ||
| 90 | const PROMPT_TYPE = { | ||
| 91 | raw: 'raw', | ||
| 92 | full: 'full', | ||
| 93 | }; | ||
| 94 | |||
| 89 | let expressionsList = null; | 95 | let expressionsList = null; |
| 90 | let lastCharacter = undefined; | 96 | let lastCharacter = undefined; |
| 91 | let lastMessage = null; | 97 | let lastMessage = null; |
| @@ -909,10 +915,6 @@ function sampleClassifyText(text) { | |||
| 909 | * @returns {Promise<string>} Prompt for the LLM API. | 915 | * @returns {Promise<string>} Prompt for the LLM API. |
| 910 | */ | 916 | */ |
| 911 | async function getLlmPrompt(labels) { | 917 | async function getLlmPrompt(labels) { |
| 912 | if (isJsonSchemaSupported()) { | ||
| 913 | return ''; | ||
| 914 | } | ||
| 915 | |||
| 916 | const labelsString = labels.map(x => `"${x}"`).join(', '); | 918 | const labelsString = labels.map(x => `"${x}"`).join(', '); |
| 917 | const prompt = substituteParamsExtended(String(extension_settings.expressions.llmPrompt), { labels: labelsString }); | 919 | const prompt = substituteParamsExtended(String(extension_settings.expressions.llmPrompt), { labels: labelsString }); |
| 918 | return prompt; | 920 | return prompt; |
| @@ -1047,7 +1049,21 @@ export async function getExpressionLabel(text, expressionsApi = extension_settin | |||
| 1047 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); | 1049 | const expressionsList = await getExpressionsList({ filterAvailable: filterAvailable }); |
| 1048 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); | 1050 | const prompt = substituteParamsExtended(customPrompt, { labels: expressionsList }) || await getLlmPrompt(expressionsList); |
| 1049 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); | 1051 | eventSource.once(event_types.TEXT_COMPLETION_SETTINGS_READY, onTextGenSettingsReady); |
| 1050 | const emotionResponse = await generateRaw(text, main_api, false, false, prompt); | 1052 | |
| 1053 | let emotionResponse; | ||
| 1054 | try { | ||
| 1055 | inApiCall = true; | ||
| 1056 | switch (extension_settings.expressions.promptType) { | ||
| 1057 | case PROMPT_TYPE.raw: | ||
| 1058 | emotionResponse = await generateRaw(text, main_api, false, false, prompt); | ||
| 1059 | break; | ||
| 1060 | case PROMPT_TYPE.full: | ||
| 1061 | emotionResponse = await generateQuietPrompt(prompt, false, false); | ||
| 1062 | break; | ||
| 1063 | } | ||
| 1064 | } finally { | ||
| 1065 | inApiCall = false; | ||
| 1066 | } | ||
| 1051 | return parseLlmResponse(emotionResponse, expressionsList); | 1067 | return parseLlmResponse(emotionResponse, expressionsList); |
| 1052 | } | 1068 | } |
| 1053 | // Using WebLLM | 1069 | // Using WebLLM |
| @@ -1702,6 +1718,7 @@ function onExpressionApiChanged() { | |||
| 1702 | if (tempApi) { | 1718 | if (tempApi) { |
| 1703 | extension_settings.expressions.api = Number(tempApi); | 1719 | extension_settings.expressions.api = Number(tempApi); |
| 1704 | $('.expression_llm_prompt_block').toggle([EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)); | 1720 | $('.expression_llm_prompt_block').toggle([EXPRESSION_API.llm, EXPRESSION_API.webllm].includes(extension_settings.expressions.api)); |
| 1721 | $('.expression_prompt_type_block').toggle(extension_settings.expressions.api === EXPRESSION_API.llm); | ||
| 1705 | expressionsList = null; | 1722 | expressionsList = null; |
| 1706 | spriteCache = {}; | 1723 | spriteCache = {}; |
| 1707 | moduleWorker(); | 1724 | moduleWorker(); |
| @@ -2102,6 +2119,11 @@ function migrateSettings() { | |||
| 2102 | extension_settings.expressions.showDefault = false; | 2119 | extension_settings.expressions.showDefault = false; |
| 2103 | saveSettingsDebounced(); | 2120 | saveSettingsDebounced(); |
| 2104 | } | 2121 | } |
| 2122 | |||
| 2123 | if (extension_settings.expressions.promptType === undefined) { | ||
| 2124 | extension_settings.expressions.promptType = PROMPT_TYPE.raw; | ||
| 2125 | saveSettingsDebounced(); | ||
| 2126 | } | ||
| 2105 | } | 2127 | } |
| 2106 | 2128 | ||
| 2107 | (async function () { | 2129 | (async function () { |
| @@ -2169,6 +2191,16 @@ function migrateSettings() { | |||
| 2169 | extension_settings.expressions.llmPrompt = DEFAULT_LLM_PROMPT; | 2191 | extension_settings.expressions.llmPrompt = DEFAULT_LLM_PROMPT; |
| 2170 | saveSettingsDebounced(); | 2192 | saveSettingsDebounced(); |
| 2171 | }); | 2193 | }); |
| 2194 | $('#expression_prompt_raw').on('input', function () { | ||
| 2195 | extension_settings.expressions.promptType = PROMPT_TYPE.raw; | ||
| 2196 | saveSettingsDebounced(); | ||
| 2197 | }); | ||
| 2198 | $('#expression_prompt_full').on('input', function () { | ||
| 2199 | extension_settings.expressions.promptType = PROMPT_TYPE.full; | ||
| 2200 | saveSettingsDebounced(); | ||
| 2201 | }); | ||
| 2202 | $(`input[name="expression_prompt_type"][value="${extension_settings.expressions.promptType}"]`).prop('checked', true); | ||
| 2203 | $('.expression_prompt_type_block').toggle(extension_settings.expressions.api === EXPRESSION_API.llm); | ||
| 2172 | 2204 | ||
| 2173 | $('#expression_custom_add').on('click', onClickExpressionAddCustom); | 2205 | $('#expression_custom_add').on('click', onClickExpressionAddCustom); |
| 2174 | $('#expression_custom_remove').on('click', onClickExpressionRemoveCustom); | 2206 | $('#expression_custom_remove').on('click', onClickExpressionRemoveCustom); |
| @@ -41,7 +41,20 @@ | |||
| 41 | </div> | 41 | </div> |
| 42 | </label> | 42 | </label> |
| 43 | <small data-i18n="Will be used if the API doesn't support JSON schemas or function calling.">Will be used if the API doesn't support JSON schemas or function calling.</small> | 43 | <small data-i18n="Will be used if the API doesn't support JSON schemas or function calling.">Will be used if the API doesn't support JSON schemas or function calling.</small> |
| 44 | <textarea id="expression_llm_prompt" type="text" class="text_pole textarea_compact" rows="2" placeholder="Use {{labels}} special macro."></textarea> | 44 | <textarea id="expression_llm_prompt" type="text" class="text_pole textarea_compact autoSetHeight" rows="2" placeholder="Use {{labels}} special macro."></textarea> |
| 45 | </div> | ||
| 46 | <div class="expression_prompt_type_block flex-container flexFlowColumn"> | ||
| 47 | <div data-i18n="LLM Prompt Strategy" class="title_restorable"> | ||
| 48 | LLM Prompt Strategy | ||
| 49 | </div> | ||
| 50 | <label for="expression_prompt_raw" class="checkbox_label"> | ||
| 51 | <input id="expression_prompt_raw" type="radio" name="expression_prompt_type" value="raw"> | ||
| 52 | <span data-i18n="Limited Context">Limited Context</span> | ||
| 53 | </label> | ||
| 54 | <label for="expression_prompt_full" class="checkbox_label"> | ||
| 55 | <input id="expression_prompt_full" type="radio" name="expression_prompt_type" value="full"> | ||
| 56 | <span data-i18n="Full Context">Full Context</span> | ||
| 57 | </label> | ||
| 45 | </div> | 58 | </div> |
| 46 | <div class="expression_fallback_block m-b-1 m-t-1"> | 59 | <div class="expression_fallback_block m-b-1 m-t-1"> |
| 47 | <label for="expression_fallback" data-i18n="Default / Fallback Expression">Default / Fallback Expression</label> | 60 | <label for="expression_fallback" data-i18n="Default / Fallback Expression">Default / Fallback Expression</label> |