Intermediate commits (oldest → newest):
- Mention conjurer
- Fixes + upgrade GPT model
- Test heartbeat fix
- Nope. It did not happen
- GPT fixes
- Gpt fixes
- Token counter
- Token amount change
- Fix. Last.
- Fix. Was joking.
- Update fabryczka
This commit is contained in:
2025-10-30 16:58:52 +01:00
parent 75032b67d0
commit 3a843abccf
7 changed files with 195 additions and 130 deletions
+102 -25
View File
@@ -35,12 +35,13 @@ 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",
@@ -72,7 +73,6 @@ MUZYKA_MOJEGO_LUDU_HISTORIA = 1500
MUZYKA_MOJEGO_LUDU_SLOWA_KLUCZOWE = 15
MUZYKA_MOJEGO_LUDU_PLAJLISTA = 30
# *=========================================== Platform Specific Predefines
if platform in ("linux", "linux2"):
@@ -80,6 +80,7 @@ if platform in ("linux", "linux2"):
if "microsoft-standard" in uname().release:
LOGFILE = "/home/mtuszowski/conjurer/discord.log"
MEMORY_FIVE_SIARA = "/home/mtuszowski/conjurer/pamiec.json"
SYSTEM_GPT_SETTINGS = "/home/mtuszowski/conjurer/system_gpt_settings.json"
MEMORY_FIVE_MUZYKA = "/home/mtuszowski/conjurer/pamiec_muzyki.json"
MUSIC_FOLDER = "/mnt/g/Muzyka/"
SETTINGS_FILE = "/home/mtuszowski/conjurer/settings.json"
@@ -93,6 +94,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/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"
@@ -107,6 +109,7 @@ if platform in ("linux", "linux2"):
elif platform == "win32":
LOGFILE = "discord.log"
MEMORY_FIVE_SIARA = "pamiec.json"
SYSTEM_GPT_SETTINGS = "system_gpt_settings.json"
MEMORY_FIVE_MUZYKA = "pamiec_muzyki.json"
MUSIC_FOLDER = "G:\\Muzyka\\"
SETTINGS_FILE = "settings.json"
@@ -167,6 +170,10 @@ for mp3_item in Path.glob(Path(MUSIC_FOLDER), "**/*.mp3"):
if platform == "win32":
temp_music_file = temp_music_file.replace("/", "\\")
music_file_list.append(temp_music_file)
with open(SYSTEM_GPT_SETTINGS, "r+", encoding=ENCODING) as temp_settings_file:
# First we load existing data into a dict.
GPT_SETTINGS = json.load(temp_settings_file)
with open(MEMORY_FIVE_SIARA, "r+", encoding=ENCODING) as temp_memory_file:
# First we load existing data into a dict.
MESSAGE_TABLE = json.load(temp_memory_file)
@@ -176,6 +183,7 @@ with open(MEMORY_FIVE_MUZYKA, "r+", encoding=ENCODING) as temp_music_memory_file
with open(SETTINGS_FILE, "r", encoding=ENCODING) as f_settings_file:
data = json.load(f_settings_file)
word_reactions = data["word_reactions"]
cyclic_words = data["cyclic_words"]
historia_fabryczki = data["fabryczka"]
for key in word_reactions:
word_reactions[key][2] = datetime.now()
@@ -270,26 +278,33 @@ async def on_message(message):
word_reactions[word][2] = datetime.now()
# TODO: drobne literówki, mentiony, spacja przed dwukropkiem. napraw.
kondziu_mentioned = False
for mention in message.mentions:
if mention == client.user:
kondziu_mentioned = True
if "conjurer:" in message.content:
if kondziu_mentioned or "conjurer:" in message.content:
async with channel.typing():
logger.debug("Procedura chatu")
message.content = message.content.replace("conjurer: ", "")
prompt = message.content
if message.author.nick:
username = message.author.nick
else:
username = message.author.name
vykidailo = False
bartender = False
if kondziu_mentioned:
prompt = message.clean_content
else:
prompt = message.content
for role in message.author.roles:
if role.name == "Vykidailo":
vykidailo = True
if role.name == "Bartender":
bartender = True
global MESSAGE_TABLE # pylint: disable=global-statement
result, MESSAGE_TABLE = await handle_response(
prompt, vykidailo, bartender, MESSAGE_TABLE, username, False
)
@@ -320,6 +335,29 @@ async def on_message(message):
# *=========================================== Define Functions
def num_tokens_from_string(message, model):
"""
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 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 = 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
async def connect(ctx, arg=None):
"""
@@ -452,8 +490,16 @@ async def play(ctx, zamawial=None, arg=None):
async def check():
"""Funkcja sprawdzająca czy grać następny kawałek."""
last_spontaneous_call = datetime.now()
while True:
# TODO: Tu dołożyć skanowanie rozmiaru pliku logowania oraz plików pamięci i przenoszenie ich na większy dysk.
tdelta = datetime.now() - last_spontaneous_call
tdelta = tdelta.total_seconds()
if tdelta > 34000:
channel = client.get_channel(1062047571557744721)
last_spontaneous_call = datetime.now()
with channel.typing():
message = get_random_cyclic_message()
await channel.send(message)
# trunk-ignore(codespell/misspelled)
# TODO: dlaczego sie tu wypierdala
if client.voice_clients:
@@ -521,7 +567,19 @@ async def check():
await asyncio.sleep(60)
async def get_random_cyclic_message():
"""
The function `get_random_cyclic_message` returns a random cyclic message from a list of cyclic
words.
:return: a random cyclic message from the list `cyclic_words`.
"""
messnum = random.randint(0, len(cyclic_words)-1)
return cyclic_words[messnum][0]
# trunk-ignore(pylint/R0914)
# trunk-ignore(pylint/R0913)
# trunk-ignore(pylint/R0915)
async def handle_response(prompt, vykidailo, bartender, history, username, music):
"""
Handle responses by appending them to a history, use OpenAI to
@@ -564,30 +622,49 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
# convert back to json.
json.dump(file_data, file_memory, indent=4)
history = []
# * append bo pierwszy index
# * extend bo 20 ostatnich
history.append(GPT_SETTINGS[0])
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_config_request_size += num_tokens_from_string(temp, "gpt-4")
history.append(temp)
final_prompt = username + ":" + prompt
logger.info("Rozmiar zapytania przed dodaniem historii %s", chat_gpt_config_request_size)
if music:
history.append(message_table_muzyka[0])
history.extend(message_table_muzyka[-15:])
response = await openai.ChatCompletion.acreate(
model="gpt-4-32k", messages=history
)
algorithm = "gpt-3.5-turbo-16k"
table = message_table_muzyka
token_amount = 10700
else:
history.append(MESSAGE_TABLE[0])
history.extend(MESSAGE_TABLE[-15:])
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo-16k", messages=history
)
table = MESSAGE_TABLE
algorithm = "gpt-4"
token_amount = 7000
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.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)
logger.info("Historia wysłana:")
logger.debug(history)
response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo", messages=history
)
await asyncio.sleep(10)
model=algorithm, messages=history
)
logger.info("Historia wysłana:")
logger.info(history)
await asyncio.sleep(15)
result = ""
logger.debug("Odpowiedzi")
logger.info(response)
logger.debug(response.choices)
for choice in response.choices:
result += choice.message.content