Token amount change

Last (hopefully) fix

Fix+1

Fix

Fix

Fix

Test

Fix tokens

Fix
This commit is contained in:
2023-10-20 18:45:03 +02:00
committed by migatu
parent 027050a4dd
commit fda2733600
+36 -20
View File
@@ -335,16 +335,27 @@ async def on_message(message):
# *=========================================== Define Functions
def num_tokens_from_string(string, 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
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
chat_gpt_encoding = tiktoken.encoding_for_model(model)
num_tokens = len(chat_gpt_encoding.encode(string))
num_tokens = 0
num_tokens += tokens_per_message
for keys, values in message.items():
num_tokens += len(chat_gpt_encoding.encode(values))
if keys == "role":
num_tokens += tokens_per_name
num_tokens += 3 # every reply is primed with <|start|>assistant<|message|>
return num_tokens
@@ -480,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:
@@ -548,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):
@@ -593,43 +604,48 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
json.dump(file_data, file_memory, indent=4)
history = []
history.append(GPT_SETTINGS[0])
tokens = num_tokens_from_string(GPT_SETTINGS[0]["content"], "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}
tokens += num_tokens_from_string(content, "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", 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
token_amount = 16000
token_amount = 15700
else:
table = MESSAGE_TABLE
algorithm = "gpt-4"
token_amount = 80000
token_amount = 7700
prompt_tokens = num_tokens_from_string(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["content"], "gpt-4")
if tokens < token_amount + prompt_tokens + temp_token:
history.append(i)
tokens += temp_token
temp_token = num_tokens_from_string(i, "gpt-4")
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_config_request_size += temp_token
history.extend(temptable)
temp = {"role": "user", "content": final_prompt}
history.append(temp)
logger.info("Rozmiar zapytania po wyslaniu %s", chat_gpt_config_request_size)
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(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