Merge branch 'staging' into char-shallow

This commit is contained in:
Cohee
2025-03-06 20:55:22 +02:00
19 changed files with 1077 additions and 32 deletions

View File

@@ -2475,7 +2475,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
timestamp: timestamp,
extra: mes.extra,
tokenCount: mes.extra?.token_count ?? 0,
...formatGenerationTimer(mes.gen_started, mes.gen_finished, mes.extra?.token_count, mes.extra?.reasoning_duration),
...formatGenerationTimer(mes.gen_started, mes.gen_finished, mes.extra?.token_count, mes.extra?.reasoning_duration, mes.extra?.time_to_first_token),
};
const renderedMessage = getMessageFromTemplate(params);
@@ -2596,13 +2596,14 @@ export function formatCharacterAvatar(characterAvatar) {
* @param {Date} gen_finished Date when generation was finished
* @param {number} tokenCount Number of tokens generated (0 if not available)
* @param {number?} [reasoningDuration=null] Reasoning duration (null if no reasoning was done)
* @param {number?} [timeToFirstToken=null] Time to first token
* @returns {Object} Object containing the formatted timer value and title
* @example
* const { timerValue, timerTitle } = formatGenerationTimer(gen_started, gen_finished, tokenCount);
* console.log(timerValue); // 1.2s
* console.log(timerTitle); // Generation queued: 12:34:56 7 Jan 2021\nReply received: 12:34:57 7 Jan 2021\nTime to generate: 1.2 seconds\nToken rate: 5 t/s
*/
function formatGenerationTimer(gen_started, gen_finished, tokenCount, reasoningDuration = null) {
function formatGenerationTimer(gen_started, gen_finished, tokenCount, reasoningDuration = null, timeToFirstToken = null) {
if (!gen_started || !gen_finished) {
return {};
}
@@ -2616,8 +2617,9 @@ function formatGenerationTimer(gen_started, gen_finished, tokenCount, reasoningD
`Generation queued: ${start.format(dateFormat)}`,
`Reply received: ${finish.format(dateFormat)}`,
`Time to generate: ${seconds} seconds`,
timeToFirstToken ? `Time to first token: ${timeToFirstToken / 1000} seconds` : '',
reasoningDuration > 0 ? `Time to think: ${reasoningDuration / 1000} seconds` : '',
tokenCount > 0 ? `Token rate: ${Number(tokenCount / seconds).toFixed(1)} t/s` : '',
tokenCount > 0 ? `Token rate: ${Number(tokenCount / seconds).toFixed(3)} t/s` : '',
].filter(x => x).join('\n').trim();
if (isNaN(seconds) || seconds < 0) {
@@ -3156,6 +3158,9 @@ class StreamingProcessor {
this.abortController = new AbortController();
this.firstMessageText = '...';
this.timeStarted = timeStarted;
/** @type {number?} */
this.timeToFirstToken = null;
this.createdAt = new Date();
this.continueMessage = type === 'continue' ? continueMessage : '';
this.swipes = [];
/** @type {import('./scripts/logprobs.js').TokenLogprobs[]} */
@@ -3247,6 +3252,7 @@ class StreamingProcessor {
if (!chat[messageId]['extra']) {
chat[messageId]['extra'] = {};
}
chat[messageId]['extra']['time_to_first_token'] = this.timeToFirstToken;
// Update reasoning
await this.reasoningHandler.process(messageId, mesChanged);
@@ -3264,7 +3270,12 @@ class StreamingProcessor {
if ((this.type == 'swipe' || this.type === 'continue') && Array.isArray(chat[messageId]['swipes'])) {
chat[messageId]['swipes'][chat[messageId]['swipe_id']] = processedText;
chat[messageId]['swipe_info'][chat[messageId]['swipe_id']] = { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) };
chat[messageId]['swipe_info'][chat[messageId]['swipe_id']] = {
'send_date': chat[messageId]['send_date'],
'gen_started': chat[messageId]['gen_started'],
'gen_finished': chat[messageId]['gen_finished'],
'extra': JSON.parse(JSON.stringify(chat[messageId]['extra']))
};
}
const formattedText = messageFormatting(
@@ -3280,7 +3291,7 @@ class StreamingProcessor {
this.messageTextDom.innerHTML = formattedText;
}
const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount, this.reasoningHandler.getDuration());
const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount, this.reasoningHandler.getDuration(), this.timeToFirstToken);
if (this.messageTimerDom instanceof HTMLElement) {
this.messageTimerDom.textContent = timePassed.timerValue;
this.messageTimerDom.title = timePassed.timerTitle;
@@ -3355,7 +3366,12 @@ class StreamingProcessor {
if (this.type !== 'swipe' && this.type !== 'impersonate') {
if (Array.isArray(chat[messageId]['swipes']) && chat[messageId]['swipes'].length === 1 && chat[messageId]['swipe_id'] === 0) {
chat[messageId]['swipes'][0] = chat[messageId]['mes'];
chat[messageId]['swipe_info'][0] = { 'send_date': chat[messageId]['send_date'], 'gen_started': chat[messageId]['gen_started'], 'gen_finished': chat[messageId]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])) };
chat[messageId]['swipe_info'][0] = {
'send_date': chat[messageId]['send_date'],
'gen_started': chat[messageId]['gen_started'],
'gen_finished': chat[messageId]['gen_finished'],
'extra': JSON.parse(JSON.stringify(chat[messageId]['extra'])),
};
}
}
}
@@ -3389,7 +3405,11 @@ class StreamingProcessor {
const sw = new Stopwatch(1000 / power_user.streaming_fps);
const timestamps = [];
for await (const { text, swipes, logprobs, toolCalls, state } of this.generator()) {
timestamps.push(Date.now());
const now = Date.now();
timestamps.push(now);
if (!this.timeToFirstToken) {
this.timeToFirstToken = now - this.createdAt.getTime();
}
if (this.isStopped || this.abortController.signal.aborted) {
return this.result;
}
@@ -8865,7 +8885,12 @@ const swipe_right = () => {
chat[chat.length - 1]['swipes'] = []; // empty the array
chat[chat.length - 1]['swipe_info'] = [];
chat[chat.length - 1]['swipes'][0] = chat[chat.length - 1]['mes']; //assign swipe array with last message from chat
chat[chat.length - 1]['swipe_info'][0] = { 'send_date': chat[chat.length - 1]['send_date'], 'gen_started': chat[chat.length - 1]['gen_started'], 'gen_finished': chat[chat.length - 1]['gen_finished'], 'extra': JSON.parse(JSON.stringify(chat[chat.length - 1]['extra'])) };
chat[chat.length - 1]['swipe_info'][0] = {
'send_date': chat[chat.length - 1]['send_date'],
'gen_started': chat[chat.length - 1]['gen_started'],
'gen_finished': chat[chat.length - 1]['gen_finished'],
'extra': JSON.parse(JSON.stringify(chat[chat.length - 1]['extra'])),
};
//assign swipe info array with last message from chat
}
if (chat.length === 1 && chat[0]['swipe_id'] !== undefined && chat[0]['swipe_id'] === chat[0]['swipes'].length - 1) { // if swipe_right is called on the last alternate greeting, loop back around