From 1a9a831277c4197ce1b27d9f51c754598df1cb24 Mon Sep 17 00:00:00 2001 From: Webifi Date: Fri, 9 Jun 2023 03:40:30 -0500 Subject: [PATCH] Only count prompts if valid response received --- src/lib/ChatCompletionResponse.svelte | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/lib/ChatCompletionResponse.svelte b/src/lib/ChatCompletionResponse.svelte index 5add032..dc76e3c 100644 --- a/src/lib/ChatCompletionResponse.svelte +++ b/src/lib/ChatCompletionResponse.svelte @@ -56,6 +56,7 @@ export class ChatCompletionResponse { } updateFromSyncResponse (response: Response) { + this.setModel(response.model) response.choices.forEach((choice, i) => { const exitingMessage = this.messages[i] const message = exitingMessage || choice.message @@ -81,7 +82,6 @@ export class ChatCompletionResponse { message.finish_reason = choice.finish_reason message.role = choice.message.role message.model = response.model - this.setModel(response.model) this.messages[i] = message if (this.opts.autoAddMessages) addMessage(this.chat.id, message) }) @@ -91,6 +91,7 @@ export class ChatCompletionResponse { updateFromAsyncResponse (response: Response) { let completionTokenCount = 0 + this.setModel(response.model) response.choices.forEach((choice, i) => { const message = this.messages[i] || { role: 'assistant', @@ -108,7 +109,6 @@ export class ChatCompletionResponse { } completionTokenCount += encode(message.content).length message.model = response.model - this.setModel(response.model) message.finish_reason = choice.finish_reason message.streaming = choice.finish_reason === null && !this.finished this.messages[i] = message @@ -186,14 +186,11 @@ export class ChatCompletionResponse { subtractRunningTotal(this.chat.id, this.offsetTotals, model) } updateRunningTotal(this.chat.id, message.usage as Usage, model) - } else { + } else if (this.model) { // If no messages it's probably because of an error or user initiated abort. - // We could miss counting the cost of the prompts sent. - // To deal with this accurately, we'd need to figure out how far the request - // made it before ending, and that may not be practical or possible to do reliably. - // For now, to error on the side of caution, we'll just count the prompts we - // sent / attempted to send. This will over-count in many error cases, - // and may under-count in others. + // this.model is set when we received a valid response. If we've made it that + // far, we'll assume we've been charged for the prompts sent. + // This could under-count in some cases. const usage:Usage = { prompt_tokens: this.promptTokenCount, completion_tokens: 0, // We have no idea if there are any to count