Token counter

This commit is contained in:
2023-10-20 18:33:52 +02:00
committed by migatu
parent b22b9add15
commit 027050a4dd
+27 -14
View File
@@ -40,8 +40,8 @@ import tiktoken
from discord.ext import commands
from spotipy.oauth2 import SpotifyClientCredentials
import yt_dlp
from spotify_dl import youtube as youtube_download
from spotify_dl import spotify
Music_Config = TypedDict(
@@ -592,28 +592,41 @@ 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]["content"], "gpt-4")
tokens = num_tokens_from_string(GPT_SETTINGS[0]["content"], "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]}
tokens += num_tokens_from_string(temp["content"], "gpt-4")
content = "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0]
temp = {"role": "system", "content": content}
tokens += num_tokens_from_string(content, "gpt-4")
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:
history.extend(message_table_muzyka[-20:0])
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo-16k", messages=history
)
algorithm = "gpt-3.5-turbo-16k"
table = message_table_muzyka
token_amount = 16000
else:
history.extend(MESSAGE_TABLE[-20:0])
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo-16k", messages=history
table = MESSAGE_TABLE
algorithm = "gpt-4"
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.debug(history)
logger.info(history)
await asyncio.sleep(10)
result = ""
logger.debug("Odpowiedzi")