From 829704105909be602991d0fe1150941e718437ab Mon Sep 17 00:00:00 2001 From: Webifi Date: Sun, 28 May 2023 20:32:22 -0500 Subject: [PATCH] More accurate token estimates --- src/lib/Chat.svelte | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib/Chat.svelte b/src/lib/Chat.svelte index b8048f4..f67459f 100644 --- a/src/lib/Chat.svelte +++ b/src/lib/Chat.svelte @@ -160,9 +160,13 @@ // Get an estimate of the total prompt size we're sending const promptTokenCount:number = filtered.reduce((a, m) => { - a += encode(m.content).length + 8 // + 8, always seems to under count by around 8 + // Not sure how OpenAI formats it, but this seems to get close to the right counts + // Sure would be nice to know + a += encode('## ' + m.role + " ##:\r\n\r\n" + m.content + "\r\n\r\n\r\n").length return a - }, 0) + }, 0) + 3 + + // console.log('Estimated',promptTokenCount,'prompt token for this request') if (chatSettings.useSummarization && !withSummary && !doingSummary &&