"Squash system messages" is back

59ba22fa9e5cb1c0a9f58935408017d0bf05a52c

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

4 files changed, +68 -1Ignore whitespace
default/content/presets/openai/Default.json+1 -0
@@ -231,6 +231,7 @@
231 "assistant_prefill": "",231 "assistant_prefill": "",
232 "assistant_impersonation": "",232 "assistant_impersonation": "",
233 "use_sysprompt": false,233 "use_sysprompt": false,
234 "squash_system_messages": false,
234 "media_inlining": true,235 "media_inlining": true,
235 "bypass_status_check": false,236 "bypass_status_check": false,
236 "continue_prefill": false,237 "continue_prefill": false,
default/content/settings.json+3 -1
@@ -631,6 +631,8 @@
631 "show_external_models": false,631 "show_external_models": false,
632 "proxy_password": "",632 "proxy_password": "",
633 "assistant_prefill": "",633 "assistant_prefill": "",
634 "assistant_impersonation": ""634 "assistant_impersonation": "",
635 "use_sysprompt": false,
636 "squash_system_messages": false
635 }637 }
636}638}
public/index.html+13 -0
@@ -1930,6 +1930,19 @@
1930 </span>1930 </span>
1931 </div>1931 </div>
1932 </div>1932 </div>
1933 <div class="range-block">
1934 <label for="squash_system_messages" title="Squash system messages" data-i18n="[title]Squash system messages" class="checkbox_label widthFreeExpand">
1935 <input id="squash_system_messages" type="checkbox" />
1936 <span data-i18n="Squash system messages">
1937 Squash system messages
1938 </span>
1939 </label>
1940 <div class="toggle-description justifyLeft">
1941 <span data-i18n="Combines consecutive system messages into one (excluding example dialogues). May improve coherence for some models.">
1942 Combines consecutive system messages into one (excluding example dialogues). May improve coherence for some models.
1943 </span>
1944 </div>
1945 </div>
1933 <div class="range-block" data-source="claude,makersuite,vertexai">1946 <div class="range-block" data-source="claude,makersuite,vertexai">
1934 <label for="use_sysprompt" class="checkbox_label widthFreeExpand">1947 <label for="use_sysprompt" class="checkbox_label widthFreeExpand">
1935 <input id="use_sysprompt" type="checkbox" />1948 <input id="use_sysprompt" type="checkbox" />
public/scripts/openai.js+51 -0
@@ -334,6 +334,7 @@ export const settingsToUpdate = {
334 vertexai_auth_mode: ['#vertexai_auth_mode', 'vertexai_auth_mode', false, true],334 vertexai_auth_mode: ['#vertexai_auth_mode', 'vertexai_auth_mode', false, true],
335 vertexai_region: ['#vertexai_region', 'vertexai_region', false, true],335 vertexai_region: ['#vertexai_region', 'vertexai_region', false, true],
336 vertexai_express_project_id: ['#vertexai_express_project_id', 'vertexai_express_project_id', false, true],336 vertexai_express_project_id: ['#vertexai_express_project_id', 'vertexai_express_project_id', false, true],
337 squash_system_messages: ['#squash_system_messages', 'squash_system_messages', true, false],
337 media_inlining: ['#openai_media_inlining', 'media_inlining', true, false],338 media_inlining: ['#openai_media_inlining', 'media_inlining', true, false],
338 inline_image_quality: ['#openai_inline_image_quality', 'inline_image_quality', false, false],339 inline_image_quality: ['#openai_inline_image_quality', 'inline_image_quality', false, false],
339 continue_prefill: ['#continue_prefill', 'continue_prefill', true, false],340 continue_prefill: ['#continue_prefill', 'continue_prefill', true, false],
@@ -430,6 +431,7 @@ const default_settings = {
430 vertexai_auth_mode: 'express',431 vertexai_auth_mode: 'express',
431 vertexai_region: 'us-central1',432 vertexai_region: 'us-central1',
432 vertexai_express_project_id: '',433 vertexai_express_project_id: '',
434 squash_system_messages: false,
433 media_inlining: true,435 media_inlining: true,
434 inline_image_quality: 'auto',436 inline_image_quality: 'auto',
435 bypass_status_check: false,437 bypass_status_check: false,
@@ -1448,6 +1450,10 @@ export async function prepareOpenAIMessages({
1448 // Pass chat completion to prompt manager for inspection1450 // Pass chat completion to prompt manager for inspection
1449 promptManager.setChatCompletion(chatCompletion);1451 promptManager.setChatCompletion(chatCompletion);
14501452
1453 if (oai_settings.squash_system_messages && dryRun == false) {
1454 await chatCompletion.squashSystemMessages();
1455 }
1456
1451 // All information is up-to-date, render.1457 // All information is up-to-date, render.
1452 if (false === dryRun) promptManager.render(false);1458 if (false === dryRun) promptManager.render(false);
1453 }1459 }
@@ -3275,6 +3281,46 @@ class MessageCollection {
3275 */3281 */
3276export class ChatCompletion {3282export class ChatCompletion {
3277 /**3283 /**
3284 * Combines consecutive system messages into one if they have no name attached.
3285 * @returns {Promise<void>}
3286 */
3287 async squashSystemMessages() {
3288 const excludeList = ['newMainChat', 'newChat', 'groupNudge'];
3289 this.messages.collection = this.messages.flatten();
3290
3291 let lastMessage = null;
3292 let squashedMessages = [];
3293
3294 for (let message of this.messages.collection) {
3295 // Force exclude empty messages
3296 if (message.role === 'system' && !message.content) {
3297 continue;
3298 }
3299
3300 const shouldSquash = (message) => {
3301 return !excludeList.includes(message.identifier) && message.role === 'system' && !message.name;
3302 };
3303
3304 if (shouldSquash(message)) {
3305 if (lastMessage && shouldSquash(lastMessage)) {
3306 lastMessage.content += '\n' + message.content;
3307 lastMessage.tokens = await tokenHandler.countAsync({ role: lastMessage.role, content: lastMessage.content });
3308 }
3309 else {
3310 squashedMessages.push(message);
3311 lastMessage = message;
3312 }
3313 }
3314 else {
3315 squashedMessages.push(message);
3316 lastMessage = message;
3317 }
3318 }
3319
3320 this.messages.collection = squashedMessages;
3321 }
3322
3323 /**
3278 * Initializes a new instance of ChatCompletion.3324 * Initializes a new instance of ChatCompletion.
3279 * Sets up the initial token budget and a new message collection.3325 * Sets up the initial token budget and a new message collection.
3280 */3326 */
@@ -6112,6 +6158,11 @@ export function initOpenAI() {
6112 saveSettingsDebounced();6158 saveSettingsDebounced();
6113 });6159 });
61146160
6161 $('#squash_system_messages').on('input', function () {
6162 oai_settings.squash_system_messages = !!$(this).prop('checked');
6163 saveSettingsDebounced();
6164 });
6165
6115 $('#openai_media_inlining').on('input', function () {6166 $('#openai_media_inlining').on('input', function () {
6116 oai_settings.media_inlining = !!$(this).prop('checked');6167 oai_settings.media_inlining = !!$(this).prop('checked');
6117 updateFeatureSupportFlags();6168 updateFeatureSupportFlags();