From 41ab2eb1c35a0707f114fff7722dd0dec0678b14 Mon Sep 17 00:00:00 2001 From: SillyLossy Date: Tue, 21 Feb 2023 00:17:07 +0200 Subject: [PATCH] Move world info to client side --- public/index.html | 63 ++++++++++++++----- public/notes/13_1.html | 21 +++++++ public/notes/13_2.html | 21 +++++++ public/style.css | 4 +- public/worlds/Toaru.json | 132 +-------------------------------------- 5 files changed, 91 insertions(+), 150 deletions(-) create mode 100644 public/notes/13_1.html create mode 100644 public/notes/13_2.html diff --git a/public/index.html b/public/index.html index 20fcd4262..71e15be32 100644 --- a/public/index.html +++ b/public/index.html @@ -139,6 +139,7 @@ var world_names; var world_info_data = null; var world_info_depth = 3; + var world_info_budget = 200; var imported_world_name = ''; var max_context = 2048;//2048; var rep_pen = 1; @@ -730,7 +731,7 @@ let allActivatedEntries = new Set(); while (needsToScan) { - let activatedNow = new Set(); + let activatedNow = []; for (let entryUid in world_info_data.entries) { const entry = world_info_data.entries[entryUid]; @@ -739,7 +740,7 @@ } if (entry.constant) { - activatedNow.add(entry.uid); + activatedNow.push(entry.uid); } if (Array.isArray(entry.key) && entry.key.length) { @@ -748,12 +749,12 @@ if (entry.selective && Array.isArray(entry.keysecondary) && entry.keysecondary.length) { secondary: for (let keysecondary of entry.keysecondary) { if (keysecondary && textToScan.includes(keysecondary.trim().toLowerCase())) { - activatedNow.add(entry.uid); + activatedNow.push(entry.uid); break secondary; } } } else { - activatedNow.add(entry.uid); + activatedNow.push(entry.uid); break primary; } } @@ -761,12 +762,20 @@ } } - needsToScan = activatedNow.size > 0; - const newContents = [...activatedNow].map(x => world_info_data.entries[x]).map(x => x.content).join('\n'); - worldInfo = worldInfo + newContents; + needsToScan = activatedNow.length > 0; + const newContents = activatedNow.map(x => world_info_data.entries[x]).map(x => x.content); + + for (const content of newContents) { + worldInfo = `${worldInfo}${content}\n`; + + if (encode(worldInfo).length >= world_info_budget) { + needsToScan = false; + break; + } + } if (needsToScan) { - textToScan = newContents.toLowerCase(); + textToScan = newContents.join('\n').toLowerCase() + textToScan; } allActivatedEntries = new Set([...allActivatedEntries, ...activatedNow]); @@ -1045,7 +1054,7 @@ for (var item of chat2) {//console.log(encode("dsfs").length); chatString = item+chatString; - if(encode(JSON.stringify(storyString+chatString+anchorTop+anchorBottom+charPersonality+promptBias)).length+120 < this_max_context){ //(The number of tokens in the entire promt) need fix, it must count correctly (added +120, so that the description of the character does not hide) + if(encode(JSON.stringify(worldInfoString+storyString+chatString+anchorTop+anchorBottom+charPersonality+promptBias)).length+120 < this_max_context){ //(The number of tokens in the entire promt) need fix, it must count correctly (added +120, so that the description of the character does not hide) //if (is_pygmalion && i == chat2.length-1) item='\n'+item; @@ -1062,7 +1071,7 @@ for(let iii = 0; iii < mesExamplesArray.length; iii++){//mesExamplesArray It need to make from end to start mesExmString = mesExmString+mesExamplesArray[iii]; - if(encode(JSON.stringify(storyString+mesExmString+chatString+anchorTop+anchorBottom+charPersonality+promptBias)).length+120 < this_max_context){ //example of dialogs + if(encode(JSON.stringify(worldInfoString+storyString+mesExmString+chatString+anchorTop+anchorBottom+charPersonality+promptBias)).length+120 < this_max_context){ //example of dialogs if(!is_pygmalion){ mesExamplesArray[iii] = mesExamplesArray[iii].replace(//i, 'This is how '+name2+' should talk');//An example of how '+name2+' responds } @@ -1162,7 +1171,7 @@ } function checkPromtSize(){ setPromtString(); - let thisPromtContextSize = encode(JSON.stringify(storyString+mesExmString+mesSendString+anchorTop+anchorBottom+charPersonality+generatedPromtCache+promptBias)).length+120; + let thisPromtContextSize = encode(JSON.stringify(worldInfoString+storyString+mesExmString+mesSendString+anchorTop+anchorBottom+charPersonality+generatedPromtCache+promptBias)).length+120; if(thisPromtContextSize > this_max_context){ if(count_exm_add > 0){ //mesExamplesArray.length = mesExamplesArray.length-1; @@ -1190,7 +1199,7 @@ }else{ mesSendString = '\n'+mesSendString; } - finalPromt = storyString+mesExmString+mesSendString+generatedPromtCache+promptBias; + finalPromt = worldInfoString+storyString+mesExmString+mesSendString+generatedPromtCache+promptBias; var generate_data; if(main_api == 'kobold'){ @@ -2416,10 +2425,15 @@ var max_contextTimer = setTimeout(saveSettings, 500); }); $(document).on('input', '#world_info_depth', function() { - world_info_depth = parseInt($(this).val()); + world_info_depth = Number($(this).val()); $('#world_info_depth_counter').html(`${$(this).val()} Messages`); setTimeout(saveSettings, 500); }); + $(document).on('input', '#world_info_budget', function() { + world_info_budget = Number($(this).val()); + $('#world_info_budget_counter').html(`${$(this).val()} Tokens`); + setTimeout(saveSettings, 500); + }) $('#style_anchor').change(function() { style_anchor = !!$('#style_anchor').prop('checked'); saveSettings(); @@ -2551,7 +2565,8 @@ if(settings.anchor_order !== undefined) anchor_order = parseInt(settings.anchor_order); if(settings.style_anchor !== undefined) style_anchor = !!settings.style_anchor; if(settings.character_anchor !== undefined) character_anchor = !!settings.character_anchor; - if(settings.world_info_depth !== undefined) world_info_depth = parseInt(settings.world_info_depth); + if(settings.world_info_depth !== undefined) world_info_depth = Number(settings.world_info_depth); + if(settings.world_info_budget !== undefined) world_info_budget = Number(settings.world_info_budget); rep_pen = settings.rep_pen; rep_pen_size = settings.rep_pen_size; @@ -2573,7 +2588,10 @@ $('#world_info_depth_counter').html(`${world_info_depth} Messages`); $('#world_info_depth').val(world_info_depth); - + + $('#world_info_budget_counter').html(`${world_info_budget} Tokens`); + $('#world_info_budget').val(world_info_budget); + addZeros = ""; if(isInt(rep_pen)) addZeros = ".00"; $('#rep_pen').val(rep_pen); @@ -2692,6 +2710,7 @@ rep_pen_size_novel: rep_pen_size_novel, world_info: world_info, world_info_depth: world_info_depth, + world_info_budget: world_info_budget,                  }), beforeSend: function(){ @@ -3911,9 +3930,19 @@
-

Scan Depth

select
+

+ Scan Depth (?) +

+
depth
-
+ +
+

+ Budget (?) +

+
budget
+ +

Pro Settings

diff --git a/public/notes/13_1.html b/public/notes/13_1.html new file mode 100644 index 000000000..c70265c68 --- /dev/null +++ b/public/notes/13_1.html @@ -0,0 +1,21 @@ + + + + Scan Depth + + + + + + +
+
+

Scan Depth

+

Defines how many messages in the chat history should be scanned for World Info keys.

+

If set to 1, then TavernAI only scans the message you send and the most recent reply.

+

This stacks up to 5 message pairs it total.

+
+
+ + + \ No newline at end of file diff --git a/public/notes/13_2.html b/public/notes/13_2.html new file mode 100644 index 000000000..8f9f2e446 --- /dev/null +++ b/public/notes/13_2.html @@ -0,0 +1,21 @@ + + + + Budget + + + + + + +
+
+

World Info Budget

+

Defines how many tokens could be used by World Info entries at once.

+

If the budget was exhausted, then no more entries are activated even if the keys are present in the prompt.

+

Constant entries always take the priority.

+
+
+ + + \ No newline at end of file diff --git a/public/style.css b/public/style.css index e436e3468..b379ce096 100644 --- a/public/style.css +++ b/public/style.css @@ -1232,12 +1232,12 @@ input[type=button] { margin-left: 1rem; } -#world_info_depth_block { +#world_info_depth_block, #world_info_budget_block { opacity: 0.8; margin-top: 20px; } -#world_info_depth_block input[type="range"] { +#world_info_depth_block input[type="range"], #world_info_budget_block input[type="range"] { margin-left: 10px; } diff --git a/public/worlds/Toaru.json b/public/worlds/Toaru.json index cd1e5b55d..694945666 100644 --- a/public/worlds/Toaru.json +++ b/public/worlds/Toaru.json @@ -1,131 +1 @@ -{ - "entries": { - "0": { - "uid": 0, - "key": [ - "Tokiwadai Middle School", - "Tokiwadai" - ], - "keysecondary": [], - "comment": "", - "content": "Place(\"Tokiwadai Middle School\")[\"prestigious girls' school\", \"females only\", \"renowned in the world\", \"requires Esper ability Level Three or higher\"]", - "constant": false, - "selective": false - }, - "1": { - "uid": 1, - "key": [ - "Esper", - "Ability User", - "Level" - ], - "keysecondary": [], - "comment": "", - "content": "Category(\"Esper\")[\"psychic\", \"user of supernatural powers\", \"scientifically based\", \"emits AIM\", \"ranked by Levels of strength\"]", - "constant": false, - "selective": false - }, - "2": { - "uid": 2, - "key": [ - "Anti-Skill", - "Guard" - ], - "keysecondary": [], - "comment": "", - "content": "Faction(\"Anti-Skill\")[\"police of Academy City\", \"security forces\", \"SWAT unit\", \"purple symbol\", \"blue uniform\", \"SWAT armor\", \"riot shields\", \"firearms\"]", - "constant": false, - "selective": false - }, - "3": { - "uid": 3, - "key": [ - "Level 5", - "Level Five" - ], - "keysecondary": [], - "comment": "", - "content": "Category(\"Level Five Rank\")[\"highest esper level\", \"only seven people in Academy City have this rank\"]", - "constant": false, - "selective": false - }, - "4": { - "uid": 4, - "key": [ - "Academy City", - "Gakuen Toshi", - "City" - ], - "keysecondary": [], - "comment": "", - "content": "Place(\"Academy City\")[\"Located west of Tokyo\", \"city of several schools\", \"most advanced city in the world\", \"scientists research on psychic powers and higher technology\", \"composed of 23 districts\", \"population over 2 millions\"]", - "constant": false, - "selective": false - }, - "5": { - "uid": 5, - "key": [ - "Saten Ruiko", - "Saten", - "Ruiko", - "Saten-san" - ], - "keysecondary": [], - "comment": "", - "content": "Character(\"Saten Ruiko\")[\"Female\", \"outgoing\", \"friendly\", \"shameless\", \"Level Zero\", \"friends with: {Misaka, Uiharu, Kuroko}\", \"Student (Sakugawa Middle School)\", \"Classmate of Uiharu\"]", - "constant": false, - "selective": false - }, - "6": { - "uid": 6, - "key": [ - "Misaka Mikoto", - "Misaka", - "Mikoto", - "Onee-sama" - ], - "keysecondary": [], - "comment": "", - "content": "Character(\"Misaka Mikoto\")[\"Female\", \"tsundere\", \"short tempered\", \"boyish\", \"Level Five Esper\", \"third most powerful esper of Academy City\", \"Student (Tokiwadai Middle School)\", \"Railgun\", \"Electromaster\", \"Roommate of Kuroko\", \"Friends with: {Kuroko, Uiharu, Saten}\"]", - "constant": false, - "selective": false - }, - "7": { - "uid": 7, - "key": [ - "Kuroko", - "Shirai Kuroko", - "Shirai" - ], - "keysecondary": [], - "comment": "", - "content": "Character(\"Shirai Kuroko\")[\"Female\", \"Level Four Esper\", \"Teleporter powers\", \"Student (Tokiwadai Middle School)\", \"works at Judgement with Uiharu\", \"Roommate of Misaka\", \"obsessed with Misaka\", \"calls Misaka Onee-sama\", \"Friends with: {Misaka, Uiharu, Saten}\"]", - "constant": false, - "selective": false - }, - "8": { - "uid": 8, - "key": [ - "Uihari", - "Uiharu Kazari", - "Kazari" - ], - "keysecondary": [], - "comment": "", - "content": "Character(\"Uiharu Kazari\")[\"Female\", \"works for Judgement with Kuroko\", \"expert in computers and hacking\", \"wears a flower circlet\", \"Level One\", \"friends with: {Misaka, Saten, Kuroko}\", \"Student (Sakugawa Middle School)\", \"Classmate of Saten\"]", - "constant": false, - "selective": false - }, - "9": { - "uid": 9, - "key": [ - "Judgement" - ], - "keysecondary": [], - "comment": "", - "content": "Faction(\"Judgement\")[\"composed of students\", \"student-based disciplinary committee\", \"tasked to maintain peace-and-order within the school system\", \"members wear armbands on right sleeves\", \"green shield symbol with white stripes\"]", - "constant": false, - "selective": false - } - } -} \ No newline at end of file +{"entries":{"0":{"uid":0,"key":["Tokiwadai Middle School","Tokiwadai"],"keysecondary":[],"comment":"","content":"Place(\"Tokiwadai Middle School\")[\"prestigious girls' school\", \"females only\", \"renowned in the world\", \"requires Esper ability Level Three or higher\"]","constant":false,"selective":false},"1":{"uid":1,"key":["Esper","Ability User","Level"],"keysecondary":[],"comment":"","content":"Category(\"Esper\")[\"psychic\", \"user of supernatural powers\", \"scientifically based\", \"emits AIM\", \"ranked by Levels of strength\"]","constant":false,"selective":false},"2":{"uid":2,"key":["Anti-Skill","Guard"],"keysecondary":[],"comment":"","content":"Faction(\"Anti-Skill\")[\"police of Academy City\", \"security forces\", \"SWAT unit\", \"purple symbol\", \"blue uniform\", \"SWAT armor\", \"riot shields\", \"firearms\"]","constant":false,"selective":false},"3":{"uid":3,"key":["Level 5","Level Five"],"keysecondary":[],"comment":"","content":"Category(\"Level Five Rank\")[\"highest esper level\", \"only seven people in Academy City have this rank\"]","constant":false,"selective":false},"4":{"uid":4,"key":["Academy City","Gakuen Toshi","City"],"keysecondary":[],"comment":"","content":"Place(\"Academy City\")[\"Located west of Tokyo\", \"city of several schools\", \"most advanced city in the world\", \"scientists research on psychic powers and higher technology\", \"composed of 23 districts\", \"population over 2 millions\"]","constant":false,"selective":false},"5":{"uid":5,"key":["Saten Ruiko","Saten","Ruiko","Saten-san"],"keysecondary":[],"comment":"","content":"Character(\"Saten Ruiko\")[\"Female\", \"outgoing\", \"friendly\", \"shameless\", \"Level Zero\", \"friends with: {Misaka, Uiharu, Kuroko}\", \"Student (Sakugawa Middle School)\", \"Classmate of Uiharu\"]","constant":false,"selective":false},"6":{"uid":6,"key":["Misaka Mikoto","Misaka","Mikoto","Onee-sama"],"keysecondary":[],"comment":"","content":"Character(\"Misaka Mikoto\")[\"Female\", \"tsundere\", \"short tempered\", \"boyish\", \"Level Five Esper\", \"third most powerful esper of Academy City\", \"Student (Tokiwadai Middle School)\", \"Railgun\", \"Electromaster\", \"Roommate of Kuroko\", \"Friends with: {Kuroko, Uiharu, Saten}\"]","constant":false,"selective":false},"7":{"uid":7,"key":["Kuroko","Shirai Kuroko","Shirai"],"keysecondary":[],"comment":"","content":"Character(\"Shirai Kuroko\")[\"Female\", \"Level Four Esper\", \"Teleporter powers\", \"Student (Tokiwadai Middle School)\", \"works at Judgement with Uiharu\", \"Roommate of Misaka\", \"obsessed with Misaka\", \"calls Misaka Onee-sama\", \"Friends with: {Misaka, Uiharu, Saten}\"]","constant":false,"selective":false},"8":{"uid":8,"key":["Uihari","Uiharu Kazari","Kazari"],"keysecondary":[],"comment":"","content":"Character(\"Uiharu Kazari\")[\"Female\", \"works for Judgement with Kuroko\", \"expert in computers and hacking\", \"wears a flower circlet\", \"Level One\", \"friends with: {Misaka, Saten, Kuroko}\", \"Student (Sakugawa Middle School)\", \"Classmate of Saten\"]","constant":false,"selective":false},"9":{"uid":9,"key":["Judgement"],"keysecondary":[],"comment":"","content":"Faction(\"Judgement\")[\"composed of students\", \"student-based disciplinary committee\", \"tasked to maintain peace-and-order within the school system\", \"members wear armbands on right sleeves\", \"green shield symbol with white stripes\"]","constant":false,"selective":false}}} \ No newline at end of file