Move world info to client side

This commit is contained in:
SillyLossy
2023-02-21 00:17:07 +02:00
parent 53bfce557f
commit c17e4c321c
5 changed files with 91 additions and 150 deletions

View File

@@ -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='<START>\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(/<START>/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 = '<START>\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;
@@ -2574,6 +2589,9 @@
$('#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 @@
</select>
<input id="world_info_edit_button" type="button" value="Details">
<div id="world_info_depth_block">
<h4>Scan Depth </h4><h5 id="world_info_depth_counter">select</h5>
<h4>
Scan Depth (<a href="/notes/13_1.html" target="_blank">?</a>)
</h4>
<h5 id="world_info_depth_counter">depth</h5>
<input type="range" id="world_info_depth" name="volume" min="1" max="5" step="1">
</div>
</div>
<div id="world_info_budget_block">
<h4>
Budget (<a href="/notes/13_2.html" target="_blank">?</a>)
</h4>
<h5 id="world_info_budget_counter">budget</h5>
<input type="range" id="world_info_budget" name="volume" min="100" max="500" step="1">
</div>
<hr>
<div style="opacity:0.80">
<h3 style='margin-bottom: 10px;'>Pro Settings</h3>

21
public/notes/13_1.html Normal file
View File

@@ -0,0 +1,21 @@
<html>
<head>
<title>Scan Depth</title>
<link rel="stylesheet" href="/css/notes.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="main">
<div id="content">
<h2>Scan Depth</h2>
<h4>Defines how many messages in the chat history should be scanned for World Info keys.</h4>
<p>If set to 1, then TavernAI only scans the message you send and the most recent reply.</p>
<p>This stacks up to 5 message pairs it total.</p>
</div>
</div>
</body>
</html>

21
public/notes/13_2.html Normal file
View File

@@ -0,0 +1,21 @@
<html>
<head>
<title>Budget</title>
<link rel="stylesheet" href="/css/notes.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="main">
<div id="content">
<h2>World Info Budget</h2>
<h4>Defines how many tokens could be used by World Info entries at once.</h4>
<p>If the budget was exhausted, then no more entries are activated even if the keys are present in the prompt.</p>
<p>Constant entries always take the priority.</p>
</div>
</div>
</body>
</html>

View File

@@ -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;
}

View File

@@ -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
}
}
}
{"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}}}