From b22b9add15c008275db3dc98513a95c172741ede Mon Sep 17 00:00:00 2001 From: Migatu Date: Thu, 19 Oct 2023 13:45:11 +0200 Subject: [PATCH] Gpt fixes FIx Fix Fix Fix. Another. ! Fix Tiktoken Fix Fix --- bot.py | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index d305868..483179a 100644 --- a/bot.py +++ b/bot.py @@ -36,13 +36,14 @@ import pdf2image import PyPDF2 import requests import spotipy +import tiktoken from discord.ext import commands from spotipy.oauth2 import SpotifyClientCredentials -import yt_dlp -from spotify_dl import spotify from spotify_dl import youtube as youtube_download +from spotify_dl import spotify + Music_Config = TypedDict( "Music_Config", { @@ -94,7 +95,7 @@ if platform in ("linux", "linux2"): else: LOGFILE = "/home/pi/Conjurer/discord.log" MEMORY_FIVE_SIARA = "/home/pi/Conjurer/pamiec.json" - SYSTEM_GPT_SETTINGS = "/home/pi/Conjurer/conjurer/system_gpt_settings.json" + SYSTEM_GPT_SETTINGS = "/home/pi/Conjurer/system_gpt_settings.json" MEMORY_FIVE_MUZYKA = "/home/pi/Conjurer/pamiec_muzyki.json" MUSIC_FOLDER = "/home/pi/RetroPie/mp3/" SETTINGS_FILE = "/home/pi/Conjurer/settings.json" @@ -334,6 +335,18 @@ async def on_message(message): # *=========================================== Define Functions +def num_tokens_from_string(string, model): + """ + The function `num_tokens_from_string` returns the number of tokens in a given string. + + :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. + """ + chat_gpt_encoding = tiktoken.encoding_for_model(model) + num_tokens = len(chat_gpt_encoding.encode(string)) + return num_tokens + async def connect(ctx, arg=None): """ @@ -579,19 +592,25 @@ 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") for (slowo, reakcja) in word_reactions.items(): - temp = {"role : system", "content : Kiedy słyszysz " + slowo + " to reagujesz lub dzieje się to " + reakcja} + 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") + history.append(temp) + logger.error("Liczba tokenów %s", tokens) if music: - history.extend(message_table_muzyka) + history.extend(message_table_muzyka[-20:0]) response = await openai.ChatCompletion.acreate( model="gpt-3.5-turbo-16k", messages=history ) else: - history.extend(MESSAGE_TABLE) + history.extend(MESSAGE_TABLE[-20:0]) response = await openai.ChatCompletion.acreate( - model="gpt-4-32k", messages=history + model="gpt-3.5-turbo-16k", messages=history ) logger.info("Historia wysłana:") logger.debug(history)