From 6029b65e2b290c639bcde5ebe8e4e6ff359a7047 Mon Sep 17 00:00:00 2001 From: Migatu Date: Sat, 21 Oct 2023 00:04:32 +0200 Subject: [PATCH] Fix --- bot.py | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/bot.py b/bot.py index 864c70a..a2b27eb 100644 --- a/bot.py +++ b/bot.py @@ -337,11 +337,13 @@ async def on_message(message): def num_tokens_from_string(message, model): """ - The function `num_tokens_from_string` returns the number of tokens in a given string. + The function takes a string message and a model as input and returns the number of tokens in the + message according to the given model. - :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. + :param message: A string containing the message or text from which you want to count the number of + tokens + :param model: The model parameter refers to a language model or tokenizer that can be used to + tokenize the input string. It could be a pre-trained model or a custom tokenizer """ tokens_per_message = 3 tokens_per_name = 1 @@ -489,7 +491,6 @@ async def play(ctx, zamawial=None, arg=None): async def check(): """Funkcja sprawdzająca czy grać następny kawałek.""" while True: - # TODO: Tu dołożyć skanowanie rozmiaru pliku logowania oraz plików pamięci i przenoszenie ich na większy dysk. # trunk-ignore(codespell/misspelled) # TODO: dlaczego sie tu wypierdala if client.voice_clients: @@ -557,6 +558,7 @@ async def check(): await asyncio.sleep(60) +# trunk-ignore(pylint/R0914) # trunk-ignore(pylint/R0913) # trunk-ignore(pylint/R0915) async def handle_response(prompt, vykidailo, bartender, history, username, music): @@ -602,17 +604,17 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music json.dump(file_data, file_memory, indent=4) history = [] history.append(GPT_SETTINGS[0]) - chat_GPT_tokens = num_tokens_from_string(GPT_SETTINGS[0], "gpt-4") + chat_gpt_config_request_size = num_tokens_from_string(GPT_SETTINGS[0], "gpt-4") for (slowo, reakcja) in word_reactions.items(): if not reakcja[3]: content = "Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja[0] temp = {"role": "system", "content": content} - chat_GPT_tokens += num_tokens_from_string(temp, "gpt-4") + chat_gpt_config_request_size += num_tokens_from_string(temp, "gpt-4") history.append(temp) final_prompt = username + ":" + prompt - logger.error("Liczba tokenów przed dodaniem historii %s", chat_GPT_tokens) + logger.info("Rozmiar zapytania przed dodaniem historii %s", chat_gpt_config_request_size) if music: algorithm = "gpt-3.5-turbo-16k" table = message_table_muzyka @@ -622,27 +624,28 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music algorithm = "gpt-4" token_amount = 8000 - prompt_tokens = num_tokens_from_string({"role": "user", "content": final_prompt}, "gpt-4") + prompt_gpt_request_size = num_tokens_from_string({"role": "user", "content": final_prompt}, "gpt-4") temptable = [] for i in reversed(table): temp_token = num_tokens_from_string(i, "gpt-4") - logger.error("Liczba tokenow %s prompt token %s temp token %s", chat_GPT_tokens, prompt_tokens, temp_token) - if chat_GPT_tokens < token_amount + prompt_tokens + temp_token: + logger.info("Rozmiar zapytania %s prompt %s temp %s", chat_gpt_config_request_size, prompt_gpt_request_size, temp_token) + if chat_gpt_config_request_size < token_amount + prompt_gpt_request_size + temp_token: temptable.insert(1, i) - chat_GPT_tokens += temp_token + chat_gpt_config_request_size += temp_token history.extend(temptable) temp = {"role": "user", "content": final_prompt} history.append(temp) - logger.error("Liczba tokenów wyslana %s", chat_GPT_tokens) + logger.info("Rozmiar zapytania po wyslaniu %s", chat_gpt_config_request_size) response = await openai.ChatCompletion.acreate( model=algorithm, messages=history ) logger.info("Historia wysłana:") logger.info(history) - await asyncio.sleep(10) + await asyncio.sleep(15) result = "" logger.debug("Odpowiedzi") + logger.info(response) logger.debug(response.choices) for choice in response.choices: result += choice.message.content