mirror of
https://github.com/KoboldAI/KoboldAI-Client.git
synced 2025-06-05 21:59:24 +02:00
Context viewer update
Context bar update in AI class update
This commit is contained in:
@@ -9083,6 +9083,14 @@ def UI_2_get_next_100_actions(data):
|
||||
logger.debug("data_to_send length: {}".format(len(data_to_send)))
|
||||
emit("var_changed", {"classname": "story", "name": "actions", "old_value": None, "value":data_to_send})
|
||||
|
||||
#==================================================================#
|
||||
# Get next 100 actions for infinate scroll
|
||||
#==================================================================#
|
||||
@socketio.on("update_tokens")
|
||||
@logger.catch
|
||||
def UI_2_update_tokens(data):
|
||||
ignore = koboldai_vars.calc_ai_text(submitted_text=data)
|
||||
|
||||
#==================================================================#
|
||||
# Test
|
||||
#==================================================================#
|
||||
|
@@ -226,7 +226,7 @@ class koboldai_vars(object):
|
||||
text += wi_text
|
||||
|
||||
|
||||
action_text_split = self.actions.to_sentences()
|
||||
action_text_split = self.actions.to_sentences(submitted_text=submitted_text)
|
||||
|
||||
|
||||
#Add prompt lenght/text if we're set to always use prompt
|
||||
@@ -298,14 +298,17 @@ class koboldai_vars(object):
|
||||
break;
|
||||
if len(action_text_split) - i - 1 == self.andepth and self.authornote != "":
|
||||
game_text = "{}{}".format(authors_note_final, game_text)
|
||||
game_context.insert(0, {"type": "authors_note", "text": authors_note_final, "tokens": authornote_length})
|
||||
game_context.insert(0, {"type": "authors_note", "text": authors_note_final, "tokens": self.authornote_length})
|
||||
length = 0 if self.tokenizer is None else len(self.tokenizer.encode(action_text_split[i][0]))
|
||||
if length+used_tokens <= token_budget and not used_all_tokens:
|
||||
used_tokens += length
|
||||
selected_text = action_text_split[i][0]
|
||||
action_text_split[i][3] = True
|
||||
game_text = "{}{}".format(selected_text, game_text)
|
||||
game_context.insert(0, {"type": "action", "text": selected_text, "tokens": length})
|
||||
if action_text_split[i][1] == [self.actions.action_count+1]:
|
||||
game_context.insert(0, {"type": "submit", "text": selected_text, "tokens": length, "action_ids": action_text_split[i][1]})
|
||||
else:
|
||||
game_context.insert(0, {"type": "action", "text": selected_text, "tokens": length, "action_ids": action_text_split[i][1]})
|
||||
for action in action_text_split[i][1]:
|
||||
if action >= 0:
|
||||
self.actions.set_action_in_ai(action)
|
||||
@@ -1261,6 +1264,7 @@ class KoboldStoryRegister(object):
|
||||
self.set_game_saved()
|
||||
|
||||
def set_action_in_ai(self, action_id, used=True):
|
||||
return
|
||||
if action_id in self.actions:
|
||||
if 'In AI Input' in self.actions[action_id]:
|
||||
old = self.actions[action_id]['In AI Input']
|
||||
@@ -1465,7 +1469,7 @@ class KoboldStoryRegister(object):
|
||||
self.actions[action_id]["Options"][option_number]['Probabilities'].append(probabilities)
|
||||
process_variable_changes(self.socketio, "story", 'actions', {"id": action_id, 'action': self.actions[action_id]}, None)
|
||||
|
||||
def to_sentences(self):
|
||||
def to_sentences(self, submitted_text=""):
|
||||
#start_time = time.time()
|
||||
#we're going to split our actions by sentence for better context. We'll add in which actions the sentence covers. Prompt will be added at a -1 ID
|
||||
actions = {i: self.actions[i]['Selected Text'] for i in self.actions}
|
||||
@@ -1473,8 +1477,10 @@ class KoboldStoryRegister(object):
|
||||
actions[-1] = ""
|
||||
else:
|
||||
actions[-1] = self.story_settings.prompt
|
||||
if submitted_text != "":
|
||||
actions[self.action_count+1] = submitted_text
|
||||
action_text = self.__str__()
|
||||
action_text = "{}{}".format("" if self.story_settings is None else self.story_settings.prompt, action_text)
|
||||
action_text = "{}{}{}".format("" if self.story_settings is None else self.story_settings.prompt, action_text, submitted_text)
|
||||
###########action_text_split = [sentence, actions used in sentence, token length, included in AI context]################
|
||||
action_text_split = [[x, [], 0, False] for x in re.findall(".*?[.!?]\s+", action_text)]
|
||||
#The above line can trim out the last sentence if it's incomplete. Let's check for that and add it back in
|
||||
@@ -1483,7 +1489,6 @@ class KoboldStoryRegister(object):
|
||||
#The last action shouldn't have the extra space from the sentence splitting, so let's remove it
|
||||
if len(action_text_split) == 0:
|
||||
return []
|
||||
action_text_split[-1][0] = action_text_split[-1][0][:-1]
|
||||
|
||||
Action_Position = [-1, len(actions[-1])] #First element is the action item, second is how much text is left
|
||||
Sentence_Position = [0, len(action_text_split[0][0])]
|
||||
|
@@ -1877,6 +1877,7 @@ body {
|
||||
.context-memory {background-color: var(--context_colors_memory);}
|
||||
.context-an {background-color: var(--context_colors_authors_notes);}
|
||||
.context-action {background-color: var(--context_colors_game_text);}
|
||||
.context-submit {background-color: var(--context_colors_submit);}
|
||||
|
||||
/* File Drag Indicator */
|
||||
#file-upload-notice {
|
||||
|
@@ -574,15 +574,15 @@ function var_changed(data) {
|
||||
actions_data[action.id] = action.action;
|
||||
do_story_text_updates(action);
|
||||
create_options(action);
|
||||
do_story_text_length_updates(action);
|
||||
//do_story_text_length_updates(action);
|
||||
if ('Probabilities' in action.action) {
|
||||
do_probabilities(action);
|
||||
}
|
||||
if (action.action['In AI Input']) {
|
||||
document.getElementById('Selected Text Chunk '+action.id).classList.add("within_max_length");
|
||||
} else {
|
||||
document.getElementById('Selected Text Chunk '+action.id).classList.remove("within_max_length");
|
||||
}
|
||||
//if (action.action['In AI Input']) {
|
||||
// document.getElementById('Selected Text Chunk '+action.id).classList.add("within_max_length");
|
||||
//} else {
|
||||
// document.getElementById('Selected Text Chunk '+action.id).classList.remove("within_max_length");
|
||||
//}
|
||||
}
|
||||
}
|
||||
if (seen_action) {
|
||||
@@ -2610,16 +2610,9 @@ function autoResize(element) {
|
||||
element.style.height = element.scrollHeight + 'px';
|
||||
}
|
||||
|
||||
function token_length(text) {
|
||||
if (typeof encode === 'function') {
|
||||
return encode(text).length;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function calc_token_usage(soft_prompt_tokens, memory_tokens, authors_notes_tokens, prompt_tokens, game_text_tokens, world_info_tokens) {
|
||||
submit_tokens = token_length(document.getElementById("input_text").value);
|
||||
function calc_token_usage(soft_prompt_tokens, memory_tokens, authors_notes_tokens, prompt_tokens, game_text_tokens, world_info_tokens, submit_tokens) {
|
||||
//submit_tokens = token_length(document.getElementById("input_text").value);
|
||||
total_tokens = parseInt(document.getElementById('model_max_length_cur').value);
|
||||
|
||||
unused_tokens = total_tokens - memory_tokens - authors_notes_tokens - world_info_tokens - prompt_tokens - game_text_tokens - submit_tokens;
|
||||
@@ -2876,6 +2869,7 @@ function update_bias_slider_value(slider) {
|
||||
}
|
||||
|
||||
function update_context(data) {
|
||||
console.log(data);
|
||||
$(".context-block").remove();
|
||||
|
||||
memory_tokens = 0;
|
||||
@@ -2884,6 +2878,12 @@ function update_context(data) {
|
||||
game_text_tokens = 0;
|
||||
world_info_tokens = 0;
|
||||
soft_prompt_tokens = 0;
|
||||
submit_tokens = 0;
|
||||
|
||||
//clear out within_max_length class
|
||||
for (action of document.getElementsByClassName("within_max_length")) {
|
||||
action.classList.remove("within_max_length");
|
||||
}
|
||||
|
||||
for (const entry of data) {
|
||||
//console.log(entry);
|
||||
@@ -2893,13 +2893,15 @@ function update_context(data) {
|
||||
world_info: "wi",
|
||||
memory: "memory",
|
||||
authors_note: "an",
|
||||
action: "action"
|
||||
action: "action",
|
||||
submit: 'submit'
|
||||
}[entry.type]);
|
||||
|
||||
let el = document.createElement("span");
|
||||
el.classList.add("context-block");
|
||||
el.classList.add(contextClass);
|
||||
el.innerText = entry.text;
|
||||
el.title = entry.tokens + " tokens";
|
||||
|
||||
el.innerHTML = el.innerHTML.replaceAll("<br>", '<span class="material-icons-outlined context-symbol">keyboard_return</span>');
|
||||
|
||||
@@ -2908,19 +2910,37 @@ function update_context(data) {
|
||||
switch (entry.type) {
|
||||
case 'soft_prompt':
|
||||
soft_prompt_tokens = entry.tokens;
|
||||
break;
|
||||
case 'prompt':
|
||||
prompt_tokens = entry.tokens;
|
||||
break;
|
||||
case 'world_info':
|
||||
world_info_tokens += entry.tokens;
|
||||
break;
|
||||
case 'memory':
|
||||
memory_tokens = entry.tokens;
|
||||
break;
|
||||
case 'authors_note':
|
||||
authors_notes_tokens = entry.tokens;
|
||||
break;
|
||||
case 'action':
|
||||
game_text_tokens += entry.tokens;
|
||||
if ('action_ids' in entry) {
|
||||
for (action_id of entry.action_ids) {
|
||||
if (document.getElementById('Selected Text Chunk '+action_id)) {
|
||||
document.getElementById('Selected Text Chunk '+action_id).classList.add("within_max_length");
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'submit':
|
||||
console.log(entry);
|
||||
submit_tokens = entry.tokens;
|
||||
break;
|
||||
}
|
||||
calc_token_usage(soft_prompt_tokens, memory_tokens, authors_notes_tokens, prompt_tokens, game_text_tokens, world_info_tokens);
|
||||
}
|
||||
console.log("Submit Tokens: "+submit_tokens);
|
||||
calc_token_usage(soft_prompt_tokens, memory_tokens, authors_notes_tokens, prompt_tokens, game_text_tokens, world_info_tokens, submit_tokens);
|
||||
|
||||
|
||||
}
|
||||
@@ -3409,131 +3429,7 @@ function highlight_world_info_text_in_chunk(action_id, wi) {
|
||||
|
||||
function update_token_lengths() {
|
||||
clearTimeout(calc_token_usage_timeout);
|
||||
calc_token_usage_timeout = setTimeout(calc_token_usage, 200);
|
||||
return
|
||||
max_token_length = parseInt(document.getElementById("model_max_length_cur").value);
|
||||
included_world_info = [];
|
||||
//clear out the world info included tags
|
||||
for (item of document.getElementsByClassName("world_info_included")) {
|
||||
item.classList.remove("world_info_included");
|
||||
}
|
||||
//clear out the text tags
|
||||
for (item of document.getElementsByClassName("within_max_length")) {
|
||||
item.classList.remove("within_max_length");
|
||||
}
|
||||
|
||||
//figure out memory length
|
||||
if ((document.getElementById("memory").getAttribute("story_memory_length") == null) || (document.getElementById("memory").getAttribute("story_memory_length") == "")) {
|
||||
memory_length = 0;
|
||||
} else {
|
||||
memory_length = parseInt(document.getElementById("memory").getAttribute("story_memory_length"));
|
||||
}
|
||||
//figure out and tag the length of all the constant world infos
|
||||
for (uid in world_info_data) {
|
||||
if (world_info_data[uid].constant) {
|
||||
if (world_info_data[uid].token_length != null) {
|
||||
memory_length += world_info_data[uid].token_length;
|
||||
included_world_info.push(uid);
|
||||
document.getElementById("world_info_"+uid).classList.add("world_info_included");
|
||||
}
|
||||
}
|
||||
}
|
||||
//Figure out author's notes length
|
||||
if ((document.getElementById("authors_notes").getAttribute("story_authornote_length") == null) || (document.getElementById("authors_notes").getAttribute("story_authornote_length") == "")) {
|
||||
authors_notes = 0;
|
||||
} else {
|
||||
authors_notes = parseInt(document.getElementById("authors_notes").getAttribute("story_authornote_length"));
|
||||
}
|
||||
//figure out prompt length
|
||||
if ((document.getElementById("story_prompt").getAttribute("story_prompt_length") == null) || (document.getElementById("story_prompt").getAttribute("story_prompt_length") == "")) {
|
||||
prompt_length = 0;
|
||||
} else {
|
||||
prompt_length = parseInt(document.getElementById("story_prompt").getAttribute("story_prompt_length"));
|
||||
}
|
||||
|
||||
//prompt is truncated at 512 tokens
|
||||
if (prompt_length > 512) {
|
||||
prompt_length = 512;
|
||||
}
|
||||
|
||||
//used token length
|
||||
token_length = memory_length + authors_notes;
|
||||
|
||||
//add in the prompt length if it's set to always add, otherwise add it later
|
||||
always_prompt = document.getElementById("story_useprompt").value == "true";
|
||||
if (always_prompt) {
|
||||
token_length += prompt_length
|
||||
document.getElementById("story_prompt").classList.add("within_max_length");
|
||||
uids = document.getElementById("story_prompt").getAttribute("world_info_uids")
|
||||
for (uid of uids?uids.split(','):[]) {
|
||||
if (!(included_world_info.includes(uid))) {
|
||||
token_length += world_info_data[uid].token_length;
|
||||
included_world_info.push(uid);
|
||||
document.getElementById("world_info_"+uid).classList.add("world_info_included");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
document.getElementById("story_prompt").classList.remove("within_max_length");
|
||||
}
|
||||
//figure out how many chunks we have
|
||||
max_chunk = -1;
|
||||
for (item of document.getElementById("Selected Text").childNodes) {
|
||||
if (item.id != undefined) {
|
||||
if (item.id != "story_prompt") {
|
||||
chunk_num = parseInt(item.id.replace("Selected Text Chunk ", ""));
|
||||
if (chunk_num > max_chunk) {
|
||||
max_chunk = chunk_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//go backwards through the text chunks and tag them if we still have space
|
||||
passed_token_limit = false;
|
||||
for (var chunk=max_chunk;chunk >= 0;chunk--) {
|
||||
if (document.getElementById("Selected Text Chunk "+chunk).getAttribute("token_length") == null) {
|
||||
current_chunk_length = 999999999999;
|
||||
} else {
|
||||
current_chunk_length = parseInt(document.getElementById("Selected Text Chunk "+chunk).getAttribute("token_length"));
|
||||
}
|
||||
if ((current_chunk_length != 0) && (token_length+current_chunk_length < max_token_length)&& (!(passed_token_limit))) {
|
||||
token_length += current_chunk_length;
|
||||
document.getElementById("Selected Text Chunk "+chunk).classList.add("within_max_length");
|
||||
uids = document.getElementById("Selected Text Chunk "+chunk).getAttribute("world_info_uids")
|
||||
for (uid of uids?uids.split(','):[]) {
|
||||
if (!(included_world_info.includes(uid))) {
|
||||
token_length += world_info_data[uid].token_length;
|
||||
included_world_info.push(uid);
|
||||
document.getElementById("world_info_"+uid).classList.add("world_info_included");
|
||||
}
|
||||
}
|
||||
} else if (!(passed_token_limit) && (current_chunk_length != 0)) {
|
||||
passed_token_limit = true;
|
||||
document.getElementById("Selected Text Chunk "+chunk).classList.remove("within_max_length");
|
||||
} else {
|
||||
document.getElementById("Selected Text Chunk "+chunk).classList.remove("within_max_length");
|
||||
}
|
||||
}
|
||||
|
||||
//if we don't always add prompts
|
||||
if ((!always_prompt) && (token_length+prompt_length < max_token_length)) {
|
||||
token_length += prompt_length
|
||||
document.getElementById("story_prompt").classList.add("within_max_length");
|
||||
uids = document.getElementById("story_prompt").getAttribute("world_info_uids")
|
||||
for (uid of uids?uids.split(','):[]) {
|
||||
if (!(included_world_info.includes(uid))) {
|
||||
token_length += world_info_data[uid].token_length;
|
||||
included_world_info.push(uid);
|
||||
document.getElementById("world_info_"+uid).classList.add("world_info_included");
|
||||
}
|
||||
}
|
||||
} else if (!always_prompt) {
|
||||
document.getElementById("story_prompt").classList.remove("within_max_length");
|
||||
}
|
||||
//Add token count to used_token_length tags
|
||||
for (item of document.getElementsByClassName("used_token_length")) {
|
||||
item.textContent = "Used Tokens: " + token_length;
|
||||
}
|
||||
calc_token_usage_timeout = setTimeout(function() {socket.emit("update_tokens", document.getElementById("input_text").value);}, 500);
|
||||
}
|
||||
|
||||
String.prototype.toHHMMSS = function () {
|
||||
@@ -4558,14 +4454,6 @@ for (const tweakContainer of document.getElementsByClassName("tweak-container"))
|
||||
|
||||
process_cookies();
|
||||
|
||||
$("#context-viewer-close").click(function() {
|
||||
$el("#context-viewer-container").classList.add("hidden");
|
||||
});
|
||||
|
||||
$(".token_breakdown").click(function() {
|
||||
$el("#context-viewer-container").classList.remove("hidden");
|
||||
});
|
||||
|
||||
/* -- Drag and Drop -- */
|
||||
(function() {
|
||||
let lastTarget = null;
|
||||
@@ -4831,14 +4719,14 @@ document.addEventListener("keydown", function(event) {
|
||||
});
|
||||
|
||||
//function to load more actions if nessisary
|
||||
document.getElementById("Selected Text").onscroll = function(){
|
||||
//TOP
|
||||
if ((scroll_trigger_element != undefined) && (scroll_trigger_element != null) && (first_scroll_occurred == true)) {
|
||||
if(scroll_trigger_element.getBoundingClientRect().bottom >= 0){
|
||||
console.log("Scrolling action: "+scroll_trigger_element.getAttribute("chunk"));
|
||||
console.log("sending emit");
|
||||
socket.emit("get_next_100_actions", parseInt(scroll_trigger_element.getAttribute("chunk")));
|
||||
scroll_trigger_element == undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
//document.getElementById("Selected Text").onscroll = function(){
|
||||
// //TOP
|
||||
// if ((scroll_trigger_element != undefined) && (scroll_trigger_element != null) && (first_scroll_occurred == true)) {
|
||||
// if(scroll_trigger_element.getBoundingClientRect().bottom >= 0){
|
||||
// console.log("Scrolling action: "+scroll_trigger_element.getAttribute("chunk"));
|
||||
// console.log("sending emit");
|
||||
// socket.emit("get_next_100_actions", parseInt(scroll_trigger_element.getAttribute("chunk")));
|
||||
// scroll_trigger_element == undefined;
|
||||
// }
|
||||
// }
|
||||
//}
|
@@ -9,7 +9,7 @@
|
||||
// debug_info.push({msg: message, url: url, line: line});
|
||||
});
|
||||
</script>
|
||||
<script type="module">import {encode, decode} from "./static/tokenizer.js";window.encode = encode;window.decode = decode;</script>
|
||||
<!---<script type="module">import {encode, decode} from "./static/tokenizer.js";window.encode = encode;window.decode = decode;</script>--->
|
||||
<link href="static/open-iconic/css/open-iconic.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="static/bootstrap-toggle.min.css">
|
||||
<link rel="stylesheet" href="static/bootstrap.min.css">
|
||||
@@ -89,7 +89,7 @@
|
||||
<textarea autocomplete="off" row=5 id="input_text" placeholder="Enter Prompt Here (shift+enter for new line)" oninput='if (this.value != "") {
|
||||
document.getElementById("themetext").value = "";
|
||||
}'
|
||||
onkeyup="calc_token_usage()"></textarea>
|
||||
onkeyup="update_token_lengths()"></textarea>
|
||||
<div class="statusbar_outer hidden var_sync_alt_system_aibusy" id="status_bar" onclick="socket.emit('abort','');">
|
||||
<div class="statusbar_inner" style="width:0%" onclick="socket.emit('abort','');">0%</div>
|
||||
</div><br>
|
||||
|
@@ -181,9 +181,10 @@
|
||||
<span class="noselect context-block-example context-memory">Memory</span>
|
||||
<span class="noselect context-block-example context-an">Author's Note</span>
|
||||
<span class="noselect context-block-example context-action">Action</span>
|
||||
<span class="noselect context-block-example context-submit">Submit</span>
|
||||
</div>
|
||||
</div>
|
||||
<span id="context-viewer-close" class="material-icons-outlined">close</span>
|
||||
<span id="context-viewer-close" class="material-icons-outlined" onclick='$el("#context-viewer-container").classList.add("hidden");'>close</span>
|
||||
</div>
|
||||
</div>
|
||||
<span class="help_text">
|
||||
|
@@ -80,7 +80,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div id="token-breakdown-container" class="settings_footer" style="padding-top: 10px;">
|
||||
<div class="token_breakdown">
|
||||
<div class="token_breakdown" onclick='socket.emit("update_tokens", document.getElementById("input_text").value);$el("#context-viewer-container").classList.remove("hidden");'>
|
||||
<div id="soft_prompt_tokens" style="width:0%; background-color: var(--context_colors_soft_prompt);"></div>
|
||||
<div id="memory_tokens" style="width:40%; background-color: var(--context_colors_memory);"></div>
|
||||
<div id="authors_notes_tokens" style="width:10%; background-color: var(--context_colors_authors_notes);"></div>
|
||||
|
Reference in New Issue
Block a user