Fix for chat completion prompts

This commit is contained in:
Cohee 2024-07-07 10:15:53 +03:00
parent 014ef0cc4f
commit 792c077267
2 changed files with 24 additions and 24 deletions

View File

@ -45,6 +45,7 @@ module.exports = {
Readability: 'readonly',
isProbablyReaderable: 'readonly',
ePub: 'readonly',
diff_match_patch: 'readonly',
},
},
],

View File

@ -4927,35 +4927,39 @@ async function promptItemize(itemizedPrompts, requestedMesId) {
}
const params = await itemizedParams(itemizedPrompts, thisPromptSet);
const flatten = (rawPrompt) => Array.isArray(rawPrompt) ? rawPrompt.map(x => x.content).join('\n') : rawPrompt;
const template = params.this_main_api == 'openai'
? await renderTemplateAsync('itemizationChat', params)
: await renderTemplateAsync('itemizationText', params);
const popup = new Popup(template, POPUP_TYPE.TEXT);
/** @type {HTMLElement} */
const diffPrevPrompt = popup.dlg.querySelector('#diffPrevPrompt');
if (priorPromptArrayItemForRawPromptDisplay) {
popup.dlg.querySelector('#diffPrevPrompt').style.display='';
popup.dlg.querySelector('#diffPrevPrompt').addEventListener('click', function () {
var dmp = new diff_match_patch();
var text1 = itemizedPrompts[priorPromptArrayItemForRawPromptDisplay].rawPrompt;
var text2 = itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt;
diffPrevPrompt.style.display = '';
diffPrevPrompt.addEventListener('click', function () {
const dmp = new diff_match_patch();
const text1 = flatten(itemizedPrompts[priorPromptArrayItemForRawPromptDisplay].rawPrompt);
const text2 = flatten(itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt);
dmp.Diff_Timeout = 2.0;
var d = dmp.diff_main(text1, text2);
var ds = dmp.diff_prettyHtml(d);
const d = dmp.diff_main(text1, text2);
let ds = dmp.diff_prettyHtml(d);
// make it readable
ds = ds.replaceAll("background:#e6ffe6;", "background:#b9f3b9; color:black;");
ds = ds.replaceAll("background:#ffe6e6;", "background:#f5b4b4; color:black;");
ds = ds.replaceAll("¶", "");
ds = "<div style='display:block;'>" + ds + "</div>";
ds = ds.replaceAll('background:#e6ffe6;', 'background:#b9f3b9; color:black;');
ds = ds.replaceAll('background:#ffe6e6;', 'background:#f5b4b4; color:black;');
ds = ds.replaceAll('&para;', '');
const container = document.createElement('div');
container.innerHTML = DOMPurify.sanitize(ds);
const rawPromptWrapper = document.getElementById('rawPromptWrapper');
rawPromptWrapper.innerHTML = ds;
rawPromptWrapper.replaceChildren(container);
$('#rawPromptPopup').slideToggle();
});
} else {
popup.dlg.querySelector('#diffPrevPrompt').style.display='none';
diffPrevPrompt.style.display = 'none';
}
popup.dlg.querySelector('#copyPromptToClipboard').addEventListener('click', function () {
let rawPrompt = itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt;
@ -4975,16 +4979,11 @@ async function promptItemize(itemizedPrompts, requestedMesId) {
console.log(itemizedPrompts);
console.log(itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt);
let rawPrompt = itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt;
let rawPromptValues = rawPrompt;
if (Array.isArray(rawPrompt)) {
rawPromptValues = rawPrompt.map(x => x.content).join('\n');
}
const rawPrompt = flatten(itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt);
//let DisplayStringifiedPrompt = JSON.stringify(itemizedPrompts[PromptArrayItemForRawPromptDisplay].rawPrompt).replace(/\n+/g, '<br>');
const rawPromptWrapper = document.getElementById('rawPromptWrapper');
rawPromptWrapper.innerText = rawPromptValues;
rawPromptWrapper.innerText = rawPrompt;
$('#rawPromptPopup').slideToggle();
});