diff --git a/bot.py b/bot.py index 2bbc757..3eaffbc 100644 --- a/bot.py +++ b/bot.py @@ -36,6 +36,7 @@ import pdf2image import PyPDF2 import requests import spotipy +import tiktoken from discord.ext import commands from spotipy.oauth2 import SpotifyClientCredentials @@ -334,6 +335,18 @@ async def on_message(message): # *=========================================== Define Functions +def num_tokens_from_string(string, model): + """ + The function `num_tokens_from_string` returns the number of tokens in a given string. + + :param string: The `string` parameter is a string of text that you want to count the number of + tokens for + :return: the number of tokens in the given string. + """ + chat_gpt_encoding = tiktoken.encoding_for_model(model) + num_tokens = len(chat_gpt_encoding.encode(string)) + return num_tokens + async def connect(ctx, arg=None): """ @@ -579,11 +592,16 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music # convert back to json. json.dump(file_data, file_memory, indent=4) history = [] + tokens = 0 history.append(GPT_SETTINGS[0]) + tokens += num_tokens_from_string(GPT_SETTINGS[0][1], "gpt-4") for (slowo, reakcja) in word_reactions.items(): if not reakcja[3]: - temp = {"role : system", "content : Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]} + temp = {"role": "system", "content": "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]} + tokens += num_tokens_from_string(temp["content"], "gpt-4") + history.append(temp) + logger.error("Liczba tokenów %s", tokens) if music: history.extend(message_table_muzyka[-20:0]) response = await openai.ChatCompletion.acreate(