From c5383882791887d41ecbbedd9d0e32dc1ed251d2 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 21 Jul 2024 21:01:54 +0200 Subject: [PATCH] added countTokensUsingAPI method --- js/api/openai.js | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/js/api/openai.js b/js/api/openai.js index f6477beb..40c99d01 100644 --- a/js/api/openai.js +++ b/js/api/openai.js @@ -63,17 +63,21 @@ export class OpenAI { }), }); - // if (!response.ok) { - // const errorDetail = await response.text(); - // let err_msg = "[ThunderAI] OpenAI API request failed: " + response.status + " " + response.statusText + ", Detail: " + errorDetail; - // console.log(err_msg); - // throw new Error(err_msg); - // } - - // const responseData = await response.json(); - // return responseData.choices[0].message.content; - return response; } + async countTokensUsingAPI(model, text) { + const response = await fetch('https://api.openai.com/v1/engines/'+model+'/tokenizer', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': 'Bearer ' + this.apiKey + }, + body: JSON.stringify({ text }) + }); + + const data = await response.json(); + return data.token_count; + } + } \ No newline at end of file