WIP stat counting

This commit is contained in:
BlipRanger
2023-07-01 14:15:18 -04:00
parent 3353fe572c
commit f2cde4d40a
5 changed files with 74 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
import { humanizedDateTime, favsToHotswap, getMessageTimeStamp, dragElement, isMobile } from "./scripts/RossAscends-mods.js"; import { humanizedDateTime, favsToHotswap, getMessageTimeStamp, dragElement, isMobile, AA_CountCharTime } from "./scripts/RossAscends-mods.js";
import { encode } from "../scripts/gpt-2-3-tokenizer/mod.js"; import { encode } from "../scripts/gpt-2-3-tokenizer/mod.js";
import { GPT3BrowserTokenizer } from "../scripts/gpt-3-tokenizer/gpt3-tokenizer.js"; import { GPT3BrowserTokenizer } from "../scripts/gpt-3-tokenizer/gpt3-tokenizer.js";
import { import {
@@ -872,8 +872,11 @@ async function getCharacters() {
if (this_chid != undefined && this_chid != "invalid-safety-id") { if (this_chid != undefined && this_chid != "invalid-safety-id") {
$("#avatar_url_pole").val(characters[this_chid].avatar); $("#avatar_url_pole").val(characters[this_chid].avatar);
} }
console.log(characters);
await getGroups(); await getGroups();
await printCharacters(); await printCharacters();
await AA_CountCharTime(this_chid);
} }
} }
@@ -3824,12 +3827,18 @@ async function getChat() {
chat.push(...response); chat.push(...response);
chat_create_date = chat[0]['create_date']; chat_create_date = chat[0]['create_date'];
chat_metadata = chat[0]['chat_metadata'] ?? {}; chat_metadata = chat[0]['chat_metadata'] ?? {};
console.log(chat_metadata);
chat.shift(); chat.shift();
} else { } else {
chat_create_date = humanizedDateTime(); chat_create_date = humanizedDateTime();
} }
await getChatResult(); await getChatResult();
await saveChat(); await saveChat();
console.log('Chat loaded');
//loop through chats and get both chat[messageId]['gen_started'] and chat[messageId]['gen_finished']
//add up the difference of all of them and print it in seconds
let totalTime = 0;
setTimeout(function () { setTimeout(function () {
$('#send_textarea').click(); $('#send_textarea').click();
$('#send_textarea').focus(); $('#send_textarea').focus();

View File

@@ -121,6 +121,37 @@ waitForElement("#floatingPrompt", 10000).then(function () {
console.log("floating prompt box not loaded yet"); console.log("floating prompt box not loaded yet");
}); });
//humanize character generation time
export function humanizeGenTime(character) {
//convert time_spent to humanized format of "_ Hours, _ Minutes, _ Seconds" from milliseconds
let time_spent = character.total_gen_time
time_spent = Math.floor(time_spent / 1000);
let seconds = time_spent % 60;
time_spent = Math.floor(time_spent / 60);
let minutes = time_spent % 60;
time_spent = Math.floor(time_spent / 60);
let hours = time_spent % 24;
time_spent = Math.floor(time_spent / 24);
let days = time_spent;
time_spent = "";
if (days > 0) { time_spent += `${days} Days, `; }
if (hours > 0) { time_spent += `${hours} Hours, `; }
if (minutes > 0) { time_spent += `${minutes} Minutes, `; }
time_spent += `${seconds} Seconds`;
return time_spent;
}
export function AA_CountCharTime(chid){
console.log("AA_CountCharTime");
if (chid !== undefined && chid !== "invalid-safety-id") { // if we are counting a valid pre-saved char
let selected_character = characters[chid];
let timeStirng = humanizeGenTime(selected_character);
console.log("Time spent generating: " + timeStirng)
//append to #result_info html
$("#result_info").append(`<div class="result_info_item"><span class="result_info_item_title"></span><small class="result_info_item_value">${timeStirng}</small></div>`);
}
}
// Device detection // Device detection
export const deviceInfo = await getDeviceInfo(); export const deviceInfo = await getDeviceInfo();
@@ -286,10 +317,11 @@ export function RA_CountCharTokens() {
// if neither, probably safety char or some error in loading // if neither, probably safety char or some error in loading
} else { console.debug("RA_TC -- no valid char found, closing."); } } else { console.debug("RA_TC -- no valid char found, closing."); }
} }
$("#result_info").html(`<small>${count_tokens} Tokens dumb (${perm_tokens} Permanent)</small>`);
// display the counted tokens // display the counted tokens
const tokenLimit = Math.max(((main_api !== 'openai' ? max_context : oai_settings.openai_max_context) / 2), 1024); const tokenLimit = Math.max(((main_api !== 'openai' ? max_context : oai_settings.openai_max_context) / 2), 1024);
if (count_tokens < tokenLimit && perm_tokens < tokenLimit) { if (count_tokens < tokenLimit && perm_tokens < tokenLimit) {
$("#result_info").html(`<small>${count_tokens} Tokens (${perm_tokens} Permanent)</small>`);
} else { } else {
$("#result_info").html(` $("#result_info").html(`
<div class="flex-container alignitemscenter"> <div class="flex-container alignitemscenter">

View File

@@ -6,7 +6,7 @@ import {
isDataURL, isDataURL,
createThumbnail, createThumbnail,
} from './utils.js'; } from './utils.js';
import { RA_CountCharTokens, humanizedDateTime } from "./RossAscends-mods.js"; import { RA_CountCharTokens, humanizedDateTime, AA_CountCharTime } from "./RossAscends-mods.js";
import { sortCharactersList, sortGroupMembers } from './power-user.js'; import { sortCharactersList, sortGroupMembers } from './power-user.js';
import { import {
@@ -1223,6 +1223,7 @@ function openCharacterDefinition(characterSelect) {
select_selected_character(chid); select_selected_character(chid);
// Gentle nudge to recalculate tokens // Gentle nudge to recalculate tokens
RA_CountCharTokens(); RA_CountCharTokens();
AA_CountCharTime(chid);
// Do a little tomfoolery to spoof the tag selector // Do a little tomfoolery to spoof the tag selector
applyTagsOnCharacterSelect.call(characterSelect); applyTagsOnCharacterSelect.call(characterSelect);
} }

View File

@@ -19,6 +19,7 @@ import {
setCharacterId setCharacterId
} from "../script.js"; } from "../script.js";
import { favsToHotswap, isMobile, initMovingUI } from "./RossAscends-mods.js"; import { favsToHotswap, isMobile, initMovingUI } from "./RossAscends-mods.js";
import { getContext } from "./extensions.js";
import { import {
groups, groups,
resetSelectedGroup, resetSelectedGroup,
@@ -653,7 +654,7 @@ function loadPowerUserSettings(settings, data) {
switchWaifuMode(); switchWaifuMode();
loadMovingUIState(); loadMovingUIState();
//console.log(power_user) console.log(power_user);
} }
function loadMovingUIState() { function loadMovingUIState() {
@@ -830,6 +831,8 @@ const compareFunc = (first, second) => {
}; };
function sortCharactersList() { function sortCharactersList() {
console.log(characters);
console.log(getContext());
const arr1 = groups.map(x => ({ const arr1 = groups.map(x => ({
item: x, item: x,
id: x.id, id: x.id,

View File

@@ -1148,6 +1148,31 @@ app.post("/getcharacters", jsonParser, function (request, response) {
if (Array.isArray(chats) && chats.length) { if (Array.isArray(chats) && chats.length) {
for (const chat of chats) { for (const chat of chats) {
//open the jsonl file and read the lines
//extract the gen_started and gen_finished from each line, calculating the difference and adding it to a running total
let filepath = path.join(char_dir, chat);
let file = fs.readFileSync(filepath, 'utf8');
let lines = file.split('\n');
let totalGenTime = 0;
for(let line of lines) {
if(line.length) {
let json = JSON.parse(line);
if(json.gen_started && json.gen_finished) {
//convert json.get_started and json.gen_finished to Date objects
//calculate the difference between the two and add it to totalGenTime
let gen_started = new Date(json.gen_started);
let gen_finished = new Date(json.gen_finished);
let gen_time = gen_finished - gen_started;
totalGenTime += gen_time;
}
}
}
if(totalGenTime) {
console.log(`Total gen time for ${item} is ${totalGenTime}`)
characters[i]['total_gen_time'] = totalGenTime;
}
const chatStat = fs.statSync(path.join(char_dir, chat)); const chatStat = fs.statSync(path.join(char_dir, chat));
chat_size += chatStat.size; chat_size += chatStat.size;
date_last_chat = Math.max(date_last_chat, chatStat.mtimeMs); date_last_chat = Math.max(date_last_chat, chatStat.mtimeMs);