This commit is contained in:
2023-10-21 00:04:32 +02:00
parent 6d3e74f758
commit 6029b65e2b
+17 -14
View File
@@ -337,11 +337,13 @@ async def on_message(message):
def num_tokens_from_string(message, model): 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 :param message: A string containing the message or text from which you want to count the number of
tokens for tokens
:return: the number of tokens in the given string. :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_message = 3
tokens_per_name = 1 tokens_per_name = 1
@@ -489,7 +491,6 @@ async def play(ctx, zamawial=None, arg=None):
async def check(): async def check():
"""Funkcja sprawdzająca czy grać następny kawałek.""" """Funkcja sprawdzająca czy grać następny kawałek."""
while True: 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) # trunk-ignore(codespell/misspelled)
# TODO: dlaczego sie tu wypierdala # TODO: dlaczego sie tu wypierdala
if client.voice_clients: if client.voice_clients:
@@ -557,6 +558,7 @@ async def check():
await asyncio.sleep(60) await asyncio.sleep(60)
# trunk-ignore(pylint/R0914)
# trunk-ignore(pylint/R0913) # trunk-ignore(pylint/R0913)
# trunk-ignore(pylint/R0915) # trunk-ignore(pylint/R0915)
async def handle_response(prompt, vykidailo, bartender, history, username, music): 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) json.dump(file_data, file_memory, indent=4)
history = [] history = []
history.append(GPT_SETTINGS[0]) 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(): for (slowo, reakcja) in word_reactions.items():
if not reakcja[3]: if not reakcja[3]:
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]
temp = {"role": "system", "content": content} 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) history.append(temp)
final_prompt = username + ":" + prompt 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: if music:
algorithm = "gpt-3.5-turbo-16k" algorithm = "gpt-3.5-turbo-16k"
table = message_table_muzyka table = message_table_muzyka
@@ -622,27 +624,28 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
algorithm = "gpt-4" algorithm = "gpt-4"
token_amount = 8000 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 = [] temptable = []
for i in reversed(table): for i in reversed(table):
temp_token = num_tokens_from_string(i, "gpt-4") 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) logger.info("Rozmiar zapytania %s prompt %s temp %s", chat_gpt_config_request_size, prompt_gpt_request_size, temp_token)
if chat_GPT_tokens < token_amount + prompt_tokens + temp_token: if chat_gpt_config_request_size < token_amount + prompt_gpt_request_size + temp_token:
temptable.insert(1, i) temptable.insert(1, i)
chat_GPT_tokens += temp_token chat_gpt_config_request_size += temp_token
history.extend(temptable) history.extend(temptable)
temp = {"role": "user", "content": final_prompt} temp = {"role": "user", "content": final_prompt}
history.append(temp) 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( response = await openai.ChatCompletion.acreate(
model=algorithm, messages=history model=algorithm, messages=history
) )
logger.info("Historia wysłana:") logger.info("Historia wysłana:")
logger.info(history) logger.info(history)
await asyncio.sleep(10) await asyncio.sleep(15)
result = "" result = ""
logger.debug("Odpowiedzi") logger.debug("Odpowiedzi")
logger.info(response)
logger.debug(response.choices) logger.debug(response.choices)
for choice in response.choices: for choice in response.choices:
result += choice.message.content result += choice.message.content