From 754cdc4d58a23958ff95dfa7c09868c9a639c602 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Ra=C4=8Dinsk=C3=BD?= Date: Sun, 28 Apr 2024 14:09:10 +0200 Subject: [PATCH] refactor: prompt itemization split to multiple functions --- public/script.js | 59 +++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/public/script.js b/public/script.js index f9faf08b0..6f395b5cc 100644 --- a/public/script.js +++ b/public/script.js @@ -293,6 +293,9 @@ export { isOdd, countOccurrences, renderTemplate, + itemizedPrompts, + findItemizedPromptSet, + itemizedParams, }; /** @@ -4601,29 +4604,7 @@ async function DupeChar() { } } -async function promptItemize(itemizedPrompts, requestedMesId) { - console.log('PROMPT ITEMIZE ENTERED'); - var incomingMesId = Number(requestedMesId); - console.debug(`looking for MesId ${incomingMesId}`); - var thisPromptSet = undefined; - - for (var i = 0; i < itemizedPrompts.length; i++) { - console.log(`looking for ${incomingMesId} vs ${itemizedPrompts[i].mesId}`); - if (itemizedPrompts[i].mesId === incomingMesId) { - console.log(`found matching mesID ${i}`); - thisPromptSet = i; - PromptArrayItemForRawPromptDisplay = i; - console.log(`wanting to raw display of ArrayItem: ${PromptArrayItemForRawPromptDisplay} which is mesID ${incomingMesId}`); - console.log(itemizedPrompts[thisPromptSet]); - } - } - - if (thisPromptSet === undefined) { - console.log(`couldnt find the right mesId. looked for ${incomingMesId}`); - console.log(itemizedPrompts); - return null; - } - +async function itemizedParams(itemizedPrompts, thisPromptSet) { const params = { charDescriptionTokens: await getTokenCountAsync(itemizedPrompts[thisPromptSet].charDescription), charPersonalityTokens: await getTokenCountAsync(itemizedPrompts[thisPromptSet].charPersonality), @@ -4722,6 +4703,38 @@ async function promptItemize(itemizedPrompts, requestedMesId) { params.allAnchorsTokensPercentage = ((params.allAnchorsTokens / (params.totalTokensInPrompt)) * 100).toFixed(2); params.selectedTokenizer = getFriendlyTokenizerName(params.this_main_api).tokenizerName; } + return params; +} + +function findItemizedPromptSet(itemizedPrompts, incomingMesId) { + var thisPromptSet = undefined; + + for (var i = 0; i < itemizedPrompts.length; i++) { + console.log(`looking for ${incomingMesId} vs ${itemizedPrompts[i].mesId}`); + if (itemizedPrompts[i].mesId === incomingMesId) { + console.log(`found matching mesID ${i}`); + thisPromptSet = i; + PromptArrayItemForRawPromptDisplay = i; + console.log(`wanting to raw display of ArrayItem: ${PromptArrayItemForRawPromptDisplay} which is mesID ${incomingMesId}`); + console.log(itemizedPrompts[thisPromptSet]); + } + } + return thisPromptSet; +} + +async function promptItemize(itemizedPrompts, requestedMesId) { + console.log('PROMPT ITEMIZE ENTERED'); + var incomingMesId = Number(requestedMesId); + console.debug(`looking for MesId ${incomingMesId}`); + var thisPromptSet = findItemizedPromptSet(itemizedPrompts, incomingMesId); + + if (thisPromptSet === undefined) { + console.log(`couldnt find the right mesId. looked for ${incomingMesId}`); + console.log(itemizedPrompts); + return null; + } + + const params = await itemizedParams(itemizedPrompts, thisPromptSet); if (params.this_main_api == 'openai') { const template = await renderTemplateAsync('itemizationChat', params);