function setPromtString(){
                    mesSendString = '';
                    mesExmString = '';
                    for(let j = 0; j < count_exm_add; j++){
                        mesExmString+=mesExamplesArray[j];
                    }
                    for(let j = 0; j < mesSend.length; j++){
                        mesSendString+=mesSend[j]['mes'];
                        if(mesSend[j]['image_for_recognition'] !== undefined){
                            imageRecognitionBudgetTokens += 85;
                        }
                        if(mesSend[j]['system_prompt'] !== undefined){
                            if(mesSend[j]['system_prompt'].length > 0)
                            mesSendString+=mesSend[j]['system_prompt']+'\n';
                        }
                        if(mesSend[j]['jailbreak_prompt'] !== undefined){
                            if(mesSend[j]['jailbreak_prompt'].length > 0)
                            mesSendString+=mesSend[j]['jailbreak_prompt']+'\n';
                        }
                        if((type === 'force_name2' || isNeedToAddName2AfterLastMessage) && j === mesSend.length-1){
                            mesSendString+= name2+': ';
                        }
                    }
                }
Base replace***
            if(mesExamples !== undefined){
                if(mesExamples.length > 0){
                    if(is_pyg){
                        mesExamples = mesExamples.replace(/{{user}}:\s/gi, 'You: ');
                        mesExamples = mesExamples.replace(/<USER>:\s/gi, 'You: ');
                    }
                    mesExamples = mesExamples.replace(/You:\s/gi, name1+": ");
                    mesExamples = mesExamples.replace(/{{user}}/gi, name1);
                    mesExamples = mesExamples.replace(/{{char}}/gi, name2);
                    mesExamples = mesExamples.replace(/<USER>/gi, name1);
                    mesExamples = mesExamples.replace(/<BOT>/gi, name2);
                    //mesExamples = mesExamples.replaceAll('<START>', '[An example of how '+name2+' responds]');
                    let blocks = mesExamples.split(/<START>/gi);
                    mesExamplesArray = blocks.slice(1).map(block => `<START>\n${block.trim()}\n`);
                }
            }
            if(charDescription !== undefined){
                if(charDescription.length > 0){
                    charDescription = charDescription.replace(/{{user}}/gi, name1);
                    charDescription = charDescription.replace(/{{char}}/gi, name2);
                    charDescription = charDescription.replace(/<USER>/gi, name1);
                    charDescription = charDescription.replace(/<BOT>/gi, name2);
                }
            }
            if(charPersonality !== undefined){
                if(charPersonality.length > 0){
                    charPersonality = charPersonality.replace(/{{user}}/gi, name1);
                    charPersonality = charPersonality.replace(/{{char}}/gi, name2);
                    charPersonality = charPersonality.replace(/<USER>/gi, name1);
                    charPersonality = charPersonality.replace(/<BOT>/gi, name2);
                }
            }
            if(Scenario !== undefined){
                if(Scenario.length > 0){
                    Scenario = Scenario.replace(/{{user}}/gi, name1);
                    Scenario = Scenario.replace(/{{char}}/gi, name2);
                    Scenario = Scenario.replace(/<USER>/gi, name1);
                    Scenario = Scenario.replace(/<BOT>/gi, name2);
                }
            }
            if(is_pyg){
                if(charDescription.length > 0){
                    storyString = name2+"'s Persona: "+charDescription+"\n";
                }
                if(charPersonality.length > 0){
                    storyString+= 'Personality: '+charPersonality+'\n';
                }
                if(Scenario.length > 0){
                    storyString+= 'Scenario: '+Scenario+'\n';
                }
            }else{
                if(charDescription !== undefined){
                    if(charPersonality.length > 0){
                        charPersonality = name2+"'s personality: "+charPersonality;//"["+name2+"'s personality: "+charPersonality+"]";
                    }
                }
                if(charDescription !== undefined){
                    if($.trim(charDescription).length > 0){
                        if(charDescription.slice(-1) !== ']' || charDescription.substr(0,1) !== '['){
                            //charDescription = '['+charDescription+']';
                        }
                        storyString+=charDescription+'\n';
                    }
                }
                if(count_view_mes < topAnchorDepth){
                    storyString+=charPersonality+'\n';
                }
            }
            if(main_api == 'kobold' || main_api == 'webui') {
                if(prepend.length) {
                    storyString = prepend.join("\n") + "\n" + storyString;
                }
                if(append.length) {
                    storyString = storyString + append.join("\n") + "\n";
                }
                storyString = storyString.replace(/\n+/g, "\n");
            }
            if(SystemPrompt.system_depth >= SystemPrompt.system_depth_max){
                let sp_string = "";
                sp_string = SystemPrompt.system_prompt.replace(/{{user}}/gi, name1) //System prompt
                                .replace(/{{char}}/gi, name2)
                                .replace(/<USER>/gi, name1)
                                .replace(/<BOT>/gi, name2);
                storyString = sp_string+'\n'+storyString;
            }
            var count_exm_add = 0;
            var chat2 = [];
            var j = 0;
            for(var i = chat.length-1; i >= 0; i--){
                if(j == 0){
                    chat[j]['mes'] = chat[j]['mes'].replace(/{{user}}/gi, name1);
                    chat[j]['mes'] = chat[j]['mes'].replace(/{{char}}/gi, name2);
                    chat[j]['mes'] = chat[j]['mes'].replace(/<USER>/gi, name1);
                    chat[j]['mes'] = chat[j]['mes'].replace(/<BOT>/gi, name2);
                }
                let this_mes_ch_name = '';
                if(chat[j]['is_user']){
                    this_mes_ch_name = name1;
                }else{
                    if(!is_room)
                        this_mes_ch_name = name2;
                    else
                        this_mes_ch_name = Characters.id[chat[j]['chid']].name;
                }
                chat2[i] = {};
                if(chat[j]['is_name']){
                    chat2[i]['mes'] = this_mes_ch_name+': '+chat[j]['mes']+'\n';
                }else{
                    chat2[i]['mes'] = chat[j]['mes']+'\n';
                }
                if(chat[j]['image_for_recognition'] !== undefined && isModelHaveImageRecognition()){
                    chat2[i]['image_for_recognition'] = chat[j]['image_for_recognition'];
                }
                j++;
            }
            //chat2 = chat2.reverse();
            var this_max_context = 4096;
            if(main_api == 'kobold') this_max_context = max_context;
            if(main_api == 'webui') this_max_context = max_context_webui;
            if(main_api == 'horde') this_max_context = max_context;
            if(main_api == 'novel'){
                if(novel_tier === 1){
                    this_max_context = 1024;
                }else{
                    this_max_context = 2048-60;//fix for fat tokens 
                    if(model_novel === 'krake-v2'){
                        this_max_context-=160;
                    }
                    if(model_novel === 'clio-v1' || model_novel === 'kayra-v1'){
                        this_max_context = 8000;//FunkEngine Fixing for what I suspect may be our failure to include JBs/prompts in token counts here as well, specific with kayra-vi 2023-11-30 @Zando in discord.
                        this_max_context-=160;//fix for fat tokens 
                    }
                }
            }
            if(main_api == 'openai' || main_api == 'proxy') this_max_context = max_context_openai;
            if(main_api == 'claude') this_max_context = 100000;
            var i = 0;
            let mesExmString = '';
            count_exm_add = 0;
            if(keep_dialog_examples){
                for(let iii = 0; iii < mesExamplesArray.length; iii++){
                    mesExmString = mesExmString+mesExamplesArray[iii];
                    if(!is_pyg){
                        // mesExamplesArray[iii] = mesExamplesArray[iii].replace(/<START>/i, 'This is how '+name2+' should talk');//An example of how '+name2+' responds
                    }
                    count_exm_add++;
                }
            }
            if(type == 'swipe'){
                chat2.shift();
            }
            var isNeedToAddName2AfterLastMessage = false;
            runGenerate = function(cycleGenerationPromt = ''){
                generatedPromtCache+=cycleGenerationPromt;
                if(generatedPromtCache.length == 0){
                    chatString = "";
                    arrMes = arrMes.reverse();
                    var is_add_personality = false;

                    if (inject && inject.length && arrMes.length) {
                        let thisInject = {
                            mes: inject
                        };
                        arrMes.splice(arrMes.length - 1, 0, thisInject);
                    }
                    arrMes.forEach(function(item, i, arr) {//For added anchors and others
                        if((i >= arrMes.length-1 && $.trim(item['mes']).substr(0, (name1+":").length) != name1+":" && (main_api !== 'openai' && main_api !== 'proxy')) || 
                                (i >= arrMes.length-1 && $.trim(item['mes']).substr(0, (name1+":").length) != name1+":" && (main_api === 'openai' || main_api === 'proxy') && SystemPrompt.jailbreak_prompt.length === 0)){
                            if(textareaText == ""){
                                item['mes'] = item['mes'].substr(0,item['mes'].length-1);
                            }
                        }
                        if(i === arrMes.length-topAnchorDepth && count_view_mes>=topAnchorDepth && !is_add_personality){
                            is_add_personality = true;
                            //chatString = chatString.substr(0,chatString.length-1);
                            //anchorAndPersonality = "[Genre: roleplay chat][Tone: very long messages with descriptions]";
                            if((anchorTop != "" || charPersonality != "") && !is_pyg){
                                if(anchorTop != "") charPersonality+=' ';
                                item['mes']+="["+charPersonality+anchorTop+']\n';
                            }
                        }
                        if(i >= arrMes.length-1 && count_view_mes>8 && $.trim(item['mes']).substr(0, (name1+":").length) == name1+":" && !is_pyg){//For add anchor in end
                            item['mes'] = item['mes'].substr(0,item['mes'].length-1);
                            //chatString+=postAnchor+"\n";//"[Writing style: very long messages]\n";
                            item['mes'] =item['mes']+ anchorBottom+"\n";
                        }
                        /*
                        if(!free_char_name_mode && !((main_api === 'openai' || main_api === 'proxy') && isOpenAIChatModel())){
                            if(i >= arrMes.length-1 && $.trim(item['mes']).substr(0, (name1+":").length) == name1+":"){//for add name2 when user sent
                                item['mes'] =item['mes']+name2+":";
                            }
                            if(i >= arrMes.length-1 && $.trim(item['mes']).substr(0, (name1+":").length) != name1+":"){//for add name2 when continue
                                if(textareaText == ""){
                                    item['mes'] =item['mes']+'\n'+name2+":";
                                }
                            }
                        }