From 312b02c36e05958769f5eaa44f3a113b0aad435f Mon Sep 17 00:00:00 2001 From: maver Date: Thu, 22 Jun 2023 20:22:55 +0200 Subject: [PATCH] Do not count messages with empty prompts --- public/scripts/openai.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/public/scripts/openai.js b/public/scripts/openai.js index 23093fed4..1f93525a8 100644 --- a/public/scripts/openai.js +++ b/public/scripts/openai.js @@ -1164,7 +1164,12 @@ class Message { this.identifier = identifier; this.role = role; this.content = content; - this.tokens = tokenHandler.count(this); + + if (this.content) { + this.tokens = tokenHandler.count({role: this.role, content: this.content}) + } else { + this.tokens = 0; + } } static fromPrompt(prompt) { @@ -1219,8 +1224,8 @@ class ChatCompletion { } setTokenBudget(context, response) { - console.log(`Context size: ${context}`); - console.log(`Response size: ${response}`); + console.log(`Prompt tokens: ${context}`); + console.log(`Completion tokens: ${response}`); this.tokenBudget = context - response; @@ -1238,6 +1243,7 @@ class ChatCompletion { } this.decreaseTokenBudgetBy(collection.getTokens()); + this.log(`Added ${collection.identifier}. Remaining tokens: ${this.tokenBudget}`); return this; @@ -1261,12 +1267,13 @@ class ChatCompletion { else if ('end' === position) this.messages.collection[index].collection.push(message); this.decreaseTokenBudgetBy(message.getTokens()); + this.log(`Inserted ${message.identifier} into ${identifier}. Remaining tokens: ${this.tokenBudget}`); } } canAfford(message) { - return 0 < this.tokenBudget - message.getTokens(); + return 0 <= this.tokenBudget - message.getTokens(); } has(identifier) {