Added time to first token

d14b5e51a9715a119100a9f943c480cf3f044d56

bmen25124 <bmen25124@gmail.com>

1 files changed, +10 -3Ignore whitespace
public/script.js+10 -3
@@ -2477,7 +2477,7 @@ export function addOneMessage(mes, { type = 'normal', insertAfter = null, scroll
24772477 timestamp: timestamp,
24782478 extra: mes.extra,
24792479 tokenCount: mes.extra?.token_count ?? 0,
24802480 ...formatGenerationTimer(mes.gen_started, mes.gen_finished, mes.extra?.token_count, mes.time_to_first_token, mes.extra?.reasoning_duration),
24812481 };
24822482
24832483 const renderedMessage = getMessageFromTemplate(params);
@@ -2597,6 +2597,7 @@ export function formatCharacterAvatar(characterAvatar) {
25972597 * @param {Date} gen_started Date when generation was started
25982598 * @param {Date} gen_finished Date when generation was finished
25992599 * @param {number} tokenCount Number of tokens generated (0 if not available)
2600+ * @param {number} timeToFirstToken Time to first token
26002601 * @param {number?} [reasoningDuration=null] Reasoning duration (null if no reasoning was done)
26012602 * @returns {Object} Object containing the formatted timer value and title
26022603 * @example
@@ -2604,7 +2605,7 @@ export function formatCharacterAvatar(characterAvatar) {
26042605 * console.log(timerValue); // 1.2s
26052606 * 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
26062607 */
26072608function formatGenerationTimer(gen_started, gen_finished, tokenCount, timeToFirstToken, reasoningDuration = null) {
26082609 if (!gen_started || !gen_finished) {
26092610 return {};
26102611 }
@@ -2618,6 +2619,7 @@ function formatGenerationTimer(gen_started, gen_finished, tokenCount, reasoningD
26182619 `Generation queued: ${start.format(dateFormat)}`,
26192620 `Reply received: ${finish.format(dateFormat)}`,
26202621 `Time to generate: ${seconds} seconds`,
2622+ timeToFirstToken > 0 ? `Time to first token: ${(timeToFirstToken / 1000).toFixed(1)} seconds` : '',
26212623 reasoningDuration > 0 ? `Time to think: ${reasoningDuration / 1000} seconds` : '',
26222624 tokenCount > 0 ? `Token rate: ${Number(tokenCount / seconds).toFixed(1)} t/s` : '',
26232625 ].filter(x => x).join('\n').trim();
@@ -3158,6 +3160,8 @@ class StreamingProcessor {
31583160 this.abortController = new AbortController();
31593161 this.firstMessageText = '...';
31603162 this.timeStarted = timeStarted;
3163+ this.timeToFirstToken = -1;
3164+ this.createdAt = new Date();
31613165 this.continueMessage = type === 'continue' ? continueMessage : '';
31623166 this.swipes = [];
31633167 /** @type {import('./scripts/logprobs.js').TokenLogprobs[]} */
@@ -3195,6 +3199,8 @@ class StreamingProcessor {
31953199 }
31963200
31973201 async onStartStreaming(text) {
3202+ this.timeToFirstToken = Date.now() - this.createdAt.getTime();
3203+
31983204 if (this.type === 'continue' && this.promptReasoning.prefixReasoning) {
31993205 this.reasoningHandler.initContinue(this.promptReasoning);
32003206 }
@@ -3245,6 +3251,7 @@ class StreamingProcessor {
32453251 const currentTime = new Date();
32463252 chat[messageId]['mes'] = processedText;
32473253 chat[messageId]['gen_started'] = this.timeStarted;
3254+ chat[messageId]['time_to_first_token'] = this.timeToFirstToken;
32483255 chat[messageId]['gen_finished'] = currentTime;
32493256 if (!chat[messageId]['extra']) {
32503257 chat[messageId]['extra'] = {};
@@ -3282,7 +3289,7 @@ class StreamingProcessor {
32823289 this.messageTextDom.innerHTML = formattedText;
32833290 }
32843291
32853292 const timePassed = formatGenerationTimer(this.timeStarted, currentTime, currentTokenCount, this.timeToFirstToken, this.reasoningHandler.getDuration());
32863293 if (this.messageTimerDom instanceof HTMLElement) {
32873294 this.messageTimerDom.textContent = timePassed.timerValue;
32883295 this.messageTimerDom.title = timePassed.timerTitle;