mirror of
https://github.com/migatu/conjurer.git
synced 2026-07-17 07:12:09 +00:00
Token amount change
Last (hopefully) fix Fix+1 Fix Fix Fix Test Fix tokens Fix
This commit is contained in:
@@ -335,16 +335,27 @@ async def on_message(message):
|
|||||||
|
|
||||||
# *=========================================== Define Functions
|
# *=========================================== 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
|
: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_name = 1
|
||||||
chat_gpt_encoding = tiktoken.encoding_for_model(model)
|
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
|
return num_tokens
|
||||||
|
|
||||||
|
|
||||||
@@ -480,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:
|
||||||
@@ -548,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):
|
||||||
@@ -593,43 +604,48 @@ 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])
|
||||||
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():
|
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}
|
||||||
tokens += num_tokens_from_string(content, "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", 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
|
||||||
token_amount = 16000
|
token_amount = 15700
|
||||||
else:
|
else:
|
||||||
table = MESSAGE_TABLE
|
table = MESSAGE_TABLE
|
||||||
algorithm = "gpt-4"
|
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):
|
for i in reversed(table):
|
||||||
temp_token = num_tokens_from_string(i["content"], "gpt-4")
|
temp_token = num_tokens_from_string(i, "gpt-4")
|
||||||
if 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)
|
||||||
history.append(i)
|
if chat_gpt_config_request_size < token_amount + prompt_gpt_request_size + temp_token:
|
||||||
tokens += temp_token
|
temptable.insert(1, i)
|
||||||
|
chat_gpt_config_request_size += temp_token
|
||||||
|
history.extend(temptable)
|
||||||
temp = {"role": "user", "content": final_prompt}
|
temp = {"role": "user", "content": final_prompt}
|
||||||
history.append(temp)
|
history.append(temp)
|
||||||
|
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.error("Liczba tokenów przed dodaniem historii %s", tokens)
|
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user