cleanUpMessages() now uses object args. Adding option to trim names.
| @@ -3315,11 +3315,23 @@ class StreamingProcessor { | |||
| 3315 | 3315 | ||
| 3316 | if (!isImpersonate && !isContinue && Array.isArray(this.swipes) && this.swipes.length > 0) { | 3316 | if (!isImpersonate && !isContinue && Array.isArray(this.swipes) && this.swipes.length > 0) { |
| 3317 | for (let i = 0; i < this.swipes.length; i++) { | 3317 | for (let i = 0; i < this.swipes.length; i++) { |
| 3318 | this.swipes[i] = cleanUpMessage(this.swipes[i], false, false, true, this.stoppingStrings); | 3318 | this.swipes[i] = cleanUpMessage({ |
| 3319 | getMessage: this.swipes[i], | ||
| 3320 | isImpersonate: false, | ||
| 3321 | isContinue: false, | ||
| 3322 | displayIncompleteSentences: true, | ||
| 3323 | stoppingStrings: this.stoppingStrings | ||
| 3324 | }); | ||
| 3319 | } | 3325 | } |
| 3320 | } | 3326 | } |
| 3321 | 3327 | ||
| 3322 | let processedText = cleanUpMessage(text, isImpersonate, isContinue, !isFinal, this.stoppingStrings); | 3328 | let processedText = cleanUpMessage({ |
| 3329 | getMessage: text, | ||
| 3330 | isImpersonate: isImpersonate, | ||
| 3331 | isContinue: isContinue, | ||
| 3332 | displayIncompleteSentences: !isFinal, | ||
| 3333 | stoppingStrings: this.stoppingStrings | ||
| 3334 | }); | ||
| 3323 | 3335 | ||
| 3324 | const charsToBalance = ['*', '"', '```']; | 3336 | const charsToBalance = ['*', '"', '```']; |
| 3325 | for (const char of charsToBalance) { | 3337 | for (const char of charsToBalance) { |
| @@ -3643,7 +3655,13 @@ export async function generateRaw(prompt, api, instructOverride, quietToLoud, sy | |||
| 3643 | } | 3655 | } |
| 3644 | 3656 | ||
| 3645 | // format result, exclude user prompt bias | 3657 | // format result, exclude user prompt bias |
| 3646 | const message = cleanUpMessage(extractMessageFromData(data), false, false, true, null, false); | 3658 | const message = cleanUpMessage({ |
| 3659 | getMessage: extractMessageFromData(data), | ||
| 3660 | isImpersonate: false, | ||
| 3661 | isContinue: false, | ||
| 3662 | displayIncompleteSentences: true, | ||
| 3663 | includeUserPromptBias: false | ||
| 3664 | }); | ||
| 3647 | 3665 | ||
| 3648 | if (!message) { | 3666 | if (!message) { |
| 3649 | throw new Error('No message generated'); | 3667 | throw new Error('No message generated'); |
| @@ -4844,7 +4862,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4844 | 4862 | ||
| 4845 | hideSwipeButtons(); | 4863 | hideSwipeButtons(); |
| 4846 | let getMessage = await streamingProcessor.generate(); | 4864 | let getMessage = await streamingProcessor.generate(); |
| 4847 | let messageChunk = cleanUpMessage(getMessage, isImpersonate, isContinue, false); | 4865 | let messageChunk = cleanUpMessage({ |
| 4866 | getMessage: getMessage, | ||
| 4867 | isImpersonate: isImpersonate, | ||
| 4868 | isContinue: isContinue, | ||
| 4869 | displayIncompleteSentences: false | ||
| 4870 | }); | ||
| 4848 | 4871 | ||
| 4849 | if (isContinue) { | 4872 | if (isContinue) { |
| 4850 | getMessage = continue_mag + getMessage; | 4873 | getMessage = continue_mag + getMessage; |
| @@ -4928,7 +4951,14 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4928 | 4951 | ||
| 4929 | const swipes = extractMultiSwipes(data, type); | 4952 | const swipes = extractMultiSwipes(data, type); |
| 4930 | 4953 | ||
| 4931 | messageChunk = cleanUpMessage(getMessage, isImpersonate, isContinue, false); | 4954 | messageChunk = cleanUpMessage({ |
| 4955 | getMessage: getMessage, | ||
| 4956 | isImpersonate: isImpersonate, | ||
| 4957 | isContinue: isContinue, | ||
| 4958 | displayIncompleteSentences: false | ||
| 4959 | }); | ||
| 4960 | |||
| 4961 | |||
| 4932 | reasoning = getRegexedString(reasoning, regex_placement.REASONING); | 4962 | reasoning = getRegexedString(reasoning, regex_placement.REASONING); |
| 4933 | 4963 | ||
| 4934 | if (power_user.trim_spaces) { | 4964 | if (power_user.trim_spaces) { |
| @@ -4942,7 +4972,12 @@ export async function Generate(type, { automatic_trigger, force_name2, quiet_pro | |||
| 4942 | 4972 | ||
| 4943 | //Formating | 4973 | //Formating |
| 4944 | const displayIncomplete = type === 'quiet' && !quietToLoud; | 4974 | const displayIncomplete = type === 'quiet' && !quietToLoud; |
| 4945 | getMessage = cleanUpMessage(getMessage, isImpersonate, isContinue, displayIncomplete); | 4975 | getMessage = cleanUpMessage({ |
| 4976 | getMessage: getMessage, | ||
| 4977 | isImpersonate: isImpersonate, | ||
| 4978 | isContinue: isContinue, | ||
| 4979 | displayIncompleteSentences: displayIncomplete | ||
| 4980 | }); | ||
| 4946 | 4981 | ||
| 4947 | if (isImpersonate) { | 4982 | if (isImpersonate) { |
| 4948 | $('#send_textarea').val(getMessage)[0].dispatchEvent(new Event('input', { bubbles: true })); | 4983 | $('#send_textarea').val(getMessage)[0].dispatchEvent(new Event('input', { bubbles: true })); |
| @@ -5908,7 +5943,13 @@ function extractMultiSwipes(data, type) { | |||
| 5908 | 5943 | ||
| 5909 | for (let i = 1; i < data.choices.length; i++) { | 5944 | for (let i = 1; i < data.choices.length; i++) { |
| 5910 | const text = data?.choices[i]?.message?.content ?? data?.choices[i]?.text ?? ''; | 5945 | const text = data?.choices[i]?.message?.content ?? data?.choices[i]?.text ?? ''; |
| 5911 | const cleanedText = cleanUpMessage(text, false, false, false); | 5946 | const cleanedText = cleanUpMessage({ |
| 5947 | getMessage: text, | ||
| 5948 | isImpersonate: false, | ||
| 5949 | isContinue: false, | ||
| 5950 | displayIncompleteSentences: false | ||
| 5951 | }); | ||
| 5952 | |||
| 5912 | swipes.push(cleanedText); | 5953 | swipes.push(cleanedText); |
| 5913 | } | 5954 | } |
| 5914 | } | 5955 | } |
| @@ -5916,7 +5957,25 @@ function extractMultiSwipes(data, type) { | |||
| 5916 | return swipes; | 5957 | return swipes; |
| 5917 | } | 5958 | } |
| 5918 | 5959 | ||
| 5919 | export function cleanUpMessage(getMessage, isImpersonate, isContinue, displayIncompleteSentences = false, stoppingStrings = null, includeUserPromptBias = true) { | 5960 | /** |
| 5961 | * Formats a message according to user settings | ||
| 5962 | * @param {object} [options] - Additional options. | ||
| 5963 | * @param {string} [options.getMessage] The message to clean up | ||
| 5964 | * @param {boolean} [options.isImpersonate] Whether this is an impersonated message | ||
| 5965 | * @param {boolean} [options.isContinue] Whether this is a continued message | ||
| 5966 | * @param {boolean} [options.displayIncompleteSentences] Whether to *not* trim incomplete sentences. | ||
| 5967 | * @param {array} [options.stoppingStrings] Array of stopping strings. | ||
| 5968 | * @param {boolean} [options.includeUserPromptBias] Whether to permit prepending the user prompt bias at the beginning. | ||
| 5969 | @param {boolean} [options.trimNames] Whether to allow trimming "{{char}}:" or "{{user}}:" from the beginning. | ||
| 5970 | * | ||
| 5971 | * @returns {string} The formatted message | ||
| 5972 | */ | ||
| 5973 | export function cleanUpMessage({ getMessage, isImpersonate, isContinue, displayIncompleteSentences = false, stoppingStrings = null, includeUserPromptBias = true, trimNames = true} = {}) { | ||
| 5974 | if (arguments.length > 0 && typeof arguments[0] !== 'object') { | ||
| 5975 | console.trace('cleanUpMessage called with positional arguments. Please use an object instead.'); | ||
| 5976 | [getMessage, isImpersonate, isContinue, displayIncompleteSentences, stoppingStrings, includeUserPromptBias] = arguments; | ||
| 5977 | } | ||
| 5978 | |||
| 5920 | if (!getMessage) { | 5979 | if (!getMessage) { |
| 5921 | return ''; | 5980 | return ''; |
| 5922 | } | 5981 | } |
| @@ -5961,25 +6020,26 @@ export function cleanUpMessage(getMessage, isImpersonate, isContinue, displayInc | |||
| 5961 | // "trailing whitespace on newlines\nevery line of the string\nsample text" | 6020 | // "trailing whitespace on newlines\nevery line of the string\nsample text" |
| 5962 | getMessage = getMessage.replace(/[^\S\r\n]+$/gm, ''); | 6021 | getMessage = getMessage.replace(/[^\S\r\n]+$/gm, ''); |
| 5963 | 6022 | ||
| 5964 | let nameToTrim = isImpersonate ? name2 : name1; | 6023 | // Trim instances of "{{name}}:" from the start of the message+ |
| 5965 | 6024 | if (trimNames) { | |
| 5966 | if (isImpersonate) { | 6025 | let nameToTrim = isImpersonate ? name2 : name1; |
| 5967 | nameToTrim = power_user.allow_name2_display ? '' : name2; | 6026 | if (isImpersonate) { |
| 5968 | } | 6027 | nameToTrim = power_user.allow_name2_display ? '' : name2; |
| 5969 | else { | 6028 | } else { |
| 5970 | nameToTrim = power_user.allow_name1_display ? '' : name1; | 6029 | nameToTrim = power_user.allow_name1_display ? '' : name1; |
| 5971 | } | 6030 | } |
| 5972 | |||
| 5973 | // get text from after the name (and colon) to the end of the string | ||
| 5974 | if (nameToTrim && getMessage.indexOf(`${nameToTrim}:`) === 0) { | ||
| 5975 | getMessage = getMessage.substring(nameToTrim.length+1); | ||
| 5976 | } | ||
| 5977 | 6031 | ||
| 5978 | // account for case where the name is after a newline | ||
| 5979 | let startIndex = getMessage.indexOf(`\n${nameToTrim}:`) | ||
| 5980 | if (nameToTrim && startIndex >= 0) { | ||
| 5981 | // get text from after the name (and colon) to the end of the string | 6032 | // get text from after the name (and colon) to the end of the string |
| 5982 | getMessage = getMessage.substring(startIndex+nameToTrim.length+2); | 6033 | if (nameToTrim && getMessage.indexOf(`${nameToTrim}:`) === 0) { |
| 6034 | getMessage = getMessage.substring(nameToTrim.length + 1); | ||
| 6035 | } | ||
| 6036 | |||
| 6037 | // account for case where the name is after a newline | ||
| 6038 | let startIndex = getMessage.indexOf(`\n${nameToTrim}:`) | ||
| 6039 | if (nameToTrim && startIndex >= 0) { | ||
| 6040 | getMessage = getMessage.substring(startIndex + nameToTrim.length + 2); | ||
| 6041 | } | ||
| 6042 | getMessage.trimStart(); | ||
| 5983 | } | 6043 | } |
| 5984 | 6044 | ||
| 5985 | if (getMessage.indexOf('<|endoftext|>') != -1) { | 6045 | if (getMessage.indexOf('<|endoftext|>') != -1) { |
| @@ -6041,13 +6101,15 @@ export function cleanUpMessage(getMessage, isImpersonate, isContinue, displayInc | |||
| 6041 | getMessage = fixMarkdown(getMessage, false); | 6101 | getMessage = fixMarkdown(getMessage, false); |
| 6042 | } | 6102 | } |
| 6043 | 6103 | ||
| 6044 | const nameToTrim2 = isImpersonate | 6104 | if (trimNames) { |
| 6045 | ? (!power_user.allow_name1_display ? name1 : '') | 6105 | const nameToTrim2 = isImpersonate |
| 6046 | : (!power_user.allow_name2_display ? name2 : ''); | 6106 | ? (!power_user.allow_name1_display ? name1 : '') |
| 6107 | : (!power_user.allow_name2_display ? name2 : ''); | ||
| 6047 | 6108 | ||
| 6048 | if (nameToTrim2 && getMessage.startsWith(nameToTrim2 + ':')) { | 6109 | if (nameToTrim2 && getMessage.startsWith(nameToTrim2 + ':')) { |
| 6049 | getMessage = getMessage.replace(nameToTrim2 + ':', ''); | 6110 | getMessage = getMessage.replace(nameToTrim2 + ':', ''); |
| 6050 | getMessage = getMessage.trimStart(); | 6111 | getMessage = getMessage.trimStart(); |
| 6112 | } | ||
| 6051 | } | 6113 | } |
| 6052 | 6114 | ||
| 6053 | if (isImpersonate) { | 6115 | if (isImpersonate) { |
| @@ -368,7 +368,12 @@ function onToggleLogprobsPanel() { | |||
| 368 | function createSwipe(messageId, prompt) { | 368 | function createSwipe(messageId, prompt) { |
| 369 | // need to call `cleanUpMessage` on our new prompt, because we were working | 369 | // need to call `cleanUpMessage` on our new prompt, because we were working |
| 370 | // with raw model output and our new prompt is missing trimming/macro replacements | 370 | // with raw model output and our new prompt is missing trimming/macro replacements |
| 371 | const cleanedPrompt = cleanUpMessage(prompt, false, false, true); | 371 | const cleanedPrompt = cleanUpMessage({ |
| 372 | getMessage: prompt, | ||
| 373 | isImpersonate: false, | ||
| 374 | isContinue: false, | ||
| 375 | displayIncompleteSentences: true | ||
| 376 | }); | ||
| 372 | 377 | ||
| 373 | const msg = chat[messageId]; | 378 | const msg = chat[messageId]; |
| 374 | const newSwipeInfo = { | 379 | const newSwipeInfo = { |