Gpt fixes

FIx

Fix

Fix

Fix. Another.

!

Fix

Tiktoken

Fix

Fix
This commit is contained in:
2023-10-19 13:45:11 +02:00
committed by migatu
parent 1fd5b8cfac
commit b22b9add15
+26 -7
View File
@@ -36,13 +36,14 @@ import pdf2image
import PyPDF2 import PyPDF2
import requests import requests
import spotipy import spotipy
import tiktoken
from discord.ext import commands from discord.ext import commands
from spotipy.oauth2 import SpotifyClientCredentials 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 youtube as youtube_download
from spotify_dl import spotify
Music_Config = TypedDict( Music_Config = TypedDict(
"Music_Config", "Music_Config",
{ {
@@ -94,7 +95,7 @@ if platform in ("linux", "linux2"):
else: else:
LOGFILE = "/home/pi/Conjurer/discord.log" LOGFILE = "/home/pi/Conjurer/discord.log"
MEMORY_FIVE_SIARA = "/home/pi/Conjurer/pamiec.json" 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" MEMORY_FIVE_MUZYKA = "/home/pi/Conjurer/pamiec_muzyki.json"
MUSIC_FOLDER = "/home/pi/RetroPie/mp3/" MUSIC_FOLDER = "/home/pi/RetroPie/mp3/"
SETTINGS_FILE = "/home/pi/Conjurer/settings.json" SETTINGS_FILE = "/home/pi/Conjurer/settings.json"
@@ -334,6 +335,18 @@ async def on_message(message):
# *=========================================== Define Functions # *=========================================== 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): async def connect(ctx, arg=None):
""" """
@@ -579,19 +592,25 @@ async def handle_response(prompt, vykidailo, bartender, history, username, music
# convert back to json. # convert back to json.
json.dump(file_data, file_memory, indent=4) json.dump(file_data, file_memory, indent=4)
history = [] history = []
tokens = 0
history.append(GPT_SETTINGS[0]) history.append(GPT_SETTINGS[0])
tokens += num_tokens_from_string(GPT_SETTINGS[0]["content"], "gpt-4")
for (slowo, reakcja) in word_reactions.items(): 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: if music:
history.extend(message_table_muzyka) history.extend(message_table_muzyka[-20:0])
response = await openai.ChatCompletion.acreate( response = await openai.ChatCompletion.acreate(
model="gpt-3.5-turbo-16k", messages=history model="gpt-3.5-turbo-16k", messages=history
) )
else: else:
history.extend(MESSAGE_TABLE) history.extend(MESSAGE_TABLE[-20:0])
response = await openai.ChatCompletion.acreate( 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.info("Historia wysłana:")
logger.debug(history) logger.debug(history)