diff --git a/aiserver.py b/aiserver.py index dec57f01..4bf12ab4 100644 --- a/aiserver.py +++ b/aiserver.py @@ -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 #==================================================================# diff --git a/koboldai_settings.py b/koboldai_settings.py index 01e917e6..e57bf319 100644 --- a/koboldai_settings.py +++ b/koboldai_settings.py @@ -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])] diff --git a/static/koboldai.css b/static/koboldai.css index 168d6b92..8b7951ea 100644 --- a/static/koboldai.css +++ b/static/koboldai.css @@ -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 { diff --git a/static/koboldai.js b/static/koboldai.js index 8064ec7e..f5d4dba7 100644 --- a/static/koboldai.js +++ b/static/koboldai.js @@ -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("
", 'keyboard_return'); @@ -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; - } - } -} \ No newline at end of file +//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; +// } +// } +//} \ No newline at end of file diff --git a/templates/index_new.html b/templates/index_new.html index bbfd1b87..661daf66 100644 --- a/templates/index_new.html +++ b/templates/index_new.html @@ -9,7 +9,7 @@ // debug_info.push({msg: message, url: url, line: line}); }); - + @@ -89,7 +89,7 @@ + onkeyup="update_token_lengths()">
diff --git a/templates/popups.html b/templates/popups.html index 5e1927c1..14afdbfa 100644 --- a/templates/popups.html +++ b/templates/popups.html @@ -181,9 +181,10 @@ Memory Author's Note Action + Submit - close + close diff --git a/templates/story flyout.html b/templates/story flyout.html index 313aeb47..4f7f90ef 100644 --- a/templates/story flyout.html +++ b/templates/story flyout.html @@ -80,7 +80,7 @@