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>
Signed| @@ -8,6 +8,7 @@ import { | |||
| 8 | getGeneratingApi, | 8 | getGeneratingApi, |
| 9 | is_send_press, | 9 | is_send_press, |
| 10 | isStreamingEnabled, | 10 | isStreamingEnabled, |
| 11 | substituteParamsExtended, | ||
| 11 | } from '../script.js'; | 12 | } from '../script.js'; |
| 12 | import { debounce, delay, getStringHash } from './utils.js'; | 13 | import { debounce, delay, getStringHash } from './utils.js'; |
| 13 | import { decodeTextTokens, getTokenizerBestMatch } from './tokenizers.js'; | 14 | import { decodeTextTokens, getTokenizerBestMatch } from './tokenizers.js'; |
| @@ -368,7 +369,7 @@ function onToggleLogprobsPanel() { | |||
| 368 | function createSwipe(messageId, prompt) { | 369 | function createSwipe(messageId, prompt) { |
| 369 | // need to call `cleanUpMessage` on our new prompt, because we were working | 370 | // 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 | 371 | // with raw model output and our new prompt is missing trimming/macro replacements |
| 371 | const cleanedPrompt = cleanUpMessage({ | 372 | let cleanedPrompt = cleanUpMessage({ |
| 372 | getMessage: prompt, | 373 | getMessage: prompt, |
| 373 | isImpersonate: false, | 374 | isImpersonate: false, |
| 374 | isContinue: false, | 375 | isContinue: false, |
| @@ -376,6 +377,46 @@ function createSwipe(messageId, prompt) { | |||
| 376 | }); | 377 | }); |
| 377 | 378 | ||
| 378 | const msg = chat[messageId]; | 379 | 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 | |||
| 379 | const newSwipeInfo = { | 420 | const newSwipeInfo = { |
| 380 | send_date: msg.send_date, | 421 | send_date: msg.send_date, |
| 381 | gen_started: msg.gen_started, | 422 | gen_started: msg.gen_started, |
| @@ -387,8 +428,19 @@ function createSwipe(messageId, prompt) { | |||
| 387 | msg.swipe_info = msg.swipe_info || []; | 428 | msg.swipe_info = msg.swipe_info || []; |
| 388 | 429 | ||
| 389 | // Add our new swipe, then make sure the active swipe is the one just before | 430 | // Add our new swipe, then make sure the active swipe is the one just before |
| 390 | // it. The call to `swipe_right` will switch to it immediately. | 431 | // it. The call to `swipe_right` in addGeneration() will switch to it immediately. |
| 391 | msg.swipes.push(cleanedPrompt); | 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 | ||
| 441 | msg.swipes.push(cleanedPrompt); | ||
| 442 | } | ||
| 443 | |||
| 392 | msg.swipe_info.push(newSwipeInfo); | 444 | msg.swipe_info.push(newSwipeInfo); |
| 393 | msg.swipe_id = Math.max(0, msg.swipes.length - 2); | 445 | msg.swipe_id = Math.max(0, msg.swipes.length - 2); |
| 394 | } | 446 | } |