Skip to content

Commit b2e8da7

Browse files
committed
Improve response stats accuracy
Start the timer when the request is sent, not when the first chunk is received.
1 parent 0458d9d commit b2e8da7

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

llamafile/server/www/chatbot.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function onWheel(e) {
172172
disableAutoScroll = true;
173173
}
174174

175-
async function handleChatStream(response) {
175+
async function handleChatStream(response, stats) {
176176
const reader = response.body.getReader();
177177
const decoder = new TextDecoder();
178178
let buffer = "";
@@ -185,13 +185,6 @@ async function handleChatStream(response) {
185185
streamingMessageContent = [];
186186
const prefillStatus = document.getElementById('prefill-status');
187187
const progressBar = prefillStatus.querySelector('.progress-bar');
188-
const stats = {
189-
startTime: Date.now(), // Timestamp when the request started
190-
firstContentTime: null, // Timestamp when the first content was received
191-
endTime: null, // Timestamp when the response was fully received
192-
promptTokenCount: 0, // Number of tokens in the prompt
193-
reponseTokenCount: 0 // Number of tokens in the response
194-
};
195188

196189
try {
197190
while (true) {
@@ -322,6 +315,13 @@ async function sendMessage() {
322315

323316
const settings = loadSettings();
324317
try {
318+
const stats = {
319+
startTime: Date.now(), // Timestamp when the request started
320+
firstContentTime: null, // Timestamp when the first content was received
321+
endTime: null, // Timestamp when the response was fully received
322+
promptTokenCount: 0, // Number of tokens in the prompt
323+
reponseTokenCount: 0 // Number of tokens in the response
324+
};
325325
const response = await fetch("/v1/chat/completions", {
326326
method: "POST",
327327
headers: {
@@ -343,7 +343,7 @@ async function sendMessage() {
343343
signal: abortController.signal
344344
});
345345
if (response.ok) {
346-
await handleChatStream(response);
346+
await handleChatStream(response, stats);
347347
const lastMessage = streamingMessageContent.join("");
348348
if (lastMessage)
349349
chatHistory.push({ role: "assistant", content: lastMessage });

0 commit comments

Comments
 (0)