working fix for logprob reroll with autoparsed reasoning (#3998) * working fix for logprob reroll with autoparsed reasoning * fix prefix being added all the time * Code clean-up --------- Co-authored-by: Cohee <18619528+Cohee1207@users.noreply.github.com>

8d7648ccf7c4294239b9501cfb1cd1cf9c6871a8

RossAscends <124905043+RossAscends@users.noreply.github.com>

Signed
1 files changed, +54 -2Showing whitespace changes
public/scripts/logprobs.js+54 -2
@@ -8,6 +8,7 @@ import {
88 getGeneratingApi,
99 is_send_press,
1010 isStreamingEnabled,
11+ substituteParamsExtended,
1112} from '../script.js';
1213import { debounce, delay, getStringHash } from './utils.js';
1314import { decodeTextTokens, getTokenizerBestMatch } from './tokenizers.js';
@@ -368,7 +369,7 @@ function onToggleLogprobsPanel() {
368369function createSwipe(messageId, prompt) {
369370 // need to call `cleanUpMessage` on our new prompt, because we were working
370371 // with raw model output and our new prompt is missing trimming/macro replacements
371372 constlet cleanedPrompt = cleanUpMessage({
372373 getMessage: prompt,
373374 isImpersonate: false,
374375 isContinue: false,
@@ -376,6 +377,46 @@ function createSwipe(messageId, prompt) {
376377 });
377378
378379 const msg = chat[messageId];
380+
381+ const reasoningPrefix = substituteParamsExtended(power_user.reasoning.prefix);
382+ const reasoningSuffix = substituteParamsExtended(power_user.reasoning.suffix);
383+ const isReasoningAutoParsed = power_user.reasoning.auto_parse;
384+ const msgHasParsedReasoning = msg.extra?.reasoning?.length > 0;
385+ let shouldRerollReasoning = false;
386+
387+ //if we have pre-existing reasoning and are currently autoparsing
388+ if (isReasoningAutoParsed && msgHasParsedReasoning) {
389+ console.debug('saw autoparse on with reasoning in message');
390+ //but the reroll prompt does not include the end of reasoning
391+ if (cleanedPrompt.includes(reasoningPrefix) && !cleanedPrompt.includes(reasoningSuffix)) {
392+ //we need to send the results to the reasoning block
393+ //this will involve the ReasoningHandler from reasoning.js
394+ console.debug('..with start tag but no end tag... reroll reasoning');
395+ shouldRerollReasoning = true;
396+ }
397+
398+ let hasReasoningPrefix = cleanedPrompt.includes(reasoningPrefix);
399+ let hasReasoningSuffix = cleanedPrompt.includes(reasoningSuffix);
400+
401+ //..with both the start and end think tags
402+ //OR
403+ //..with only the end think tag (implying prefilled think start)
404+ if (hasReasoningPrefix && hasReasoningSuffix) {
405+ //we need to send the results to the response block without reasoning attached
406+ console.debug('...incl. end tag...rerolling response');
407+ const endOfThink = cleanedPrompt.indexOf(reasoningSuffix) + reasoningSuffix.length;
408+ cleanedPrompt = cleanedPrompt.substring(endOfThink);
409+ }
410+
411+ //if cleanedprompt includes the think prefix, but no suffix..
412+ if (hasReasoningPrefix && !hasReasoningSuffix) {
413+ console.debug('..no end tag...rerolling reasoning, so removing prefix');
414+ cleanedPrompt = cleanedPrompt.replace(reasoningPrefix, '');
415+ }
416+ }
417+
418+ console.debug('cleanedPrompt: ', cleanedPrompt);
419+
379420 const newSwipeInfo = {
380421 send_date: msg.send_date,
381422 gen_started: msg.gen_started,
@@ -387,8 +428,19 @@ function createSwipe(messageId, prompt) {
387428 msg.swipe_info = msg.swipe_info || [];
388429
389430 // Add our new swipe, then make sure the active swipe is the one just before
390431 // it. The call to `swipe_right` in addGeneration() will switch to it immediately.
432+
433+ //if we determined that we need to reroll from reasoning
434+ if (shouldRerollReasoning) {
435+ //cleaned prompt goes into reasoning
436+ newSwipeInfo.extra.reasoning = cleanedPrompt;
437+ //mes_text becomes empty, causing the reasoning handler to parse the reasoning first
438+ msg.swipes.push('');
439+ } else {
440+ //otherwise just add the cleaned prompt to the message and continue
391441 msg.swipes.push(cleanedPrompt);
442+ }
443+
392444 msg.swipe_info.push(newSwipeInfo);
393445 msg.swipe_id = Math.max(0, msg.swipes.length - 2);
394446}