Token counter

This commit is contained in:
2023-10-20 18:33:52 +02:00
parent 66953992f1
commit daea2e30b7
+27 -14
View File
@@ -40,8 +40,8 @@ import tiktoken
from discord.ext import commands from discord.ext import commands
from spotipy.oauth2 import SpotifyClientCredentials from spotipy.oauth2 import SpotifyClientCredentials
import yt_dlp
from spotify_dl import youtube as youtube_download from spotify_dl import youtube as youtube_download
from spotify_dl import spotify from spotify_dl import spotify
Music_Config = TypedDict( Music_Config = TypedDict(
@@ -592,28 +592,41 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
# convert back to json. # convert back to json.
json.dump(file_data, file_memory, indent=4) json.dump(file_data, file_memory, indent=4)
history = [] history = []
tokens = 0
history.append(GPT_SETTINGS[0]) history.append(GPT_SETTINGS[0])
tokens += num_tokens_from_string(GPT_SETTINGS[0]["content"], "gpt-4") tokens = num_tokens_from_string(GPT_SETTINGS[0]["content"], "gpt-4")
for (slowo, reakcja) in word_reactions.items(): for (slowo, reakcja) in word_reactions.items():
if not reakcja[3]: if not reakcja[3]:
temp = {"role": "system", "content": "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]} content = "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]
tokens += num_tokens_from_string(temp["content"], "gpt-4") temp = {"role": "system", "content": content}
tokens += num_tokens_from_string(content, "gpt-4")
history.append(temp) history.append(temp)
logger.error("Liczba tokenów %s", tokens) final_prompt = username + ":" + prompt
logger.error("Liczba tokenów przed dodaniem historii %s", tokens)
if music: if music:
history.extend(message_table_muzyka[-20:0]) algorithm = "gpt-3.5-turbo-16k"
response = await openai.ChatCompletion.acreate( table = message_table_muzyka
model="gpt-3.5-turbo-16k", messages=history token_amount = 16000
)
else: else:
history.extend(MESSAGE_TABLE[-20:0]) table = MESSAGE_TABLE
response = await openai.ChatCompletion.acreate( algorithm = "gpt-4"
model="gpt-3.5-turbo-16k", messages=history token_amount = 80000
prompt_tokens = num_tokens_from_string(final_prompt, "gpt-4")
for i in reversed(table):
temp_token = num_tokens_from_string(i["content"], "gpt-4")
if tokens < token_amount + prompt_tokens + temp_token:
history.append(i)
tokens += temp_token
temp = {"role": "user", "content": final_prompt}
history.append(temp)
response = await openai.ChatCompletion.acreate(
model=algorithm, messages=history
) )
logger.error("Liczba tokenów przed dodaniem historii %s", tokens)
logger.info("Historia wysłana:") logger.info("Historia wysłana:")
logger.debug(history) logger.info(history)
await asyncio.sleep(10) await asyncio.sleep(10)
result = "" result = ""
logger.debug("Odpowiedzi") logger.debug("Odpowiedzi")