mirror of
https://github.com/Paillat-dev/Botator.git
synced 2026-01-02 09:16:19 +00:00
Improved makeprompt, cleaned code a bit
This commit is contained in:
@@ -23,7 +23,6 @@ async def on_ready():
|
||||
debug("Bot is ready")
|
||||
|
||||
|
||||
|
||||
bot.run(discord_token) # run the bot
|
||||
#set the bot's watching status to watcing your messages to answer you
|
||||
@bot.event
|
||||
|
||||
@@ -12,14 +12,7 @@ class Chat (discord.Cog) :
|
||||
self.bot = bot
|
||||
@discord.Cog.listener()
|
||||
async def on_message(self, message: discord.Message):
|
||||
try:
|
||||
c.execute("SELECT * FROM model WHERE guild_id = ?", (message.guild.id,))
|
||||
model = c.fetchone()[1]
|
||||
except: model = "davinci"
|
||||
if model == "davinci":
|
||||
await mp.davinci_process(self, message)
|
||||
if model == "chatGPT":
|
||||
await mp.chat_process(self, message)
|
||||
await mp.chat_process(self, message)
|
||||
|
||||
@discord.slash_command(name="say", description="Say a message")
|
||||
async def say(self, ctx: discord.ApplicationContext, message: str):
|
||||
@@ -34,10 +27,6 @@ class Chat (discord.Cog) :
|
||||
if message_to_delete.author.id == self.bot.user.id:
|
||||
await message_to_delete.delete()
|
||||
else:
|
||||
await ctx.respond("The last message wasn't sent by the bot", ephemeral=True)
|
||||
return
|
||||
#get the message before the last message, because the new last message is the bot thinking message, so the message before the last message is the message to redo
|
||||
await ctx.respond("The message to redo was sent by the bot", ephemeral=True)
|
||||
return
|
||||
message_to_redo=history[0]
|
||||
await ctx.respond("Message redone !", ephemeral=True)
|
||||
await mp.process(self, message_to_redo)
|
||||
await mp.chat_process(self, message_to_redo)
|
||||
@@ -17,12 +17,16 @@ class Setup (discord.Cog) :
|
||||
await ctx.respond("Invalid channel id", ephemeral=True)
|
||||
return
|
||||
#check if the guild is already in the database bi checking if there is a key for the guild
|
||||
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
|
||||
if c.fetchone() is not None:
|
||||
#in this case, the guild is already in the database, so we update the channel id and the api key
|
||||
try:
|
||||
c.execute("SELECT * FROM data WHERE guild_id = ?", (ctx.guild.id,))
|
||||
data = c.fetchone()
|
||||
if data[3] == None: data = None
|
||||
except:
|
||||
data = None
|
||||
|
||||
if data != None:
|
||||
c.execute("UPDATE data SET channel_id = ?, api_key = ? WHERE guild_id = ?", (channel.id, api_key, ctx.guild.id))
|
||||
#we will also set the advanced settings to their default values
|
||||
c.execute("UPDATE data SET is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?", (False, 64, 0.9, 0.0, 0.0, 5, ctx.guild.id))
|
||||
# c.execute("UPDATE data SET is_active = ?, max_tokens = ?, temperature = ?, frequency_penalty = ?, presence_penalty = ?, prompt_size = ? WHERE guild_id = ?", (False, 64, 0.9, 0.0, 0.0, 5, ctx.guild.id))
|
||||
conn.commit()
|
||||
await ctx.respond("The channel id and the api key have been updated", ephemeral=True)
|
||||
else:
|
||||
|
||||
@@ -1,144 +1,16 @@
|
||||
import asyncio
|
||||
from config import debug, c, max_uses, cp, conn, connp
|
||||
from config import c, max_uses, cp, conn
|
||||
import re
|
||||
import discord
|
||||
import openai
|
||||
import random
|
||||
import requests
|
||||
import datetime
|
||||
import os
|
||||
async def davinci_process(self, message):
|
||||
if message.author.bot:
|
||||
return
|
||||
#c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||
#we get all the data from the database into different variables (guild_id text, channel_id text, api_key text, is_active boolean, max_tokens integer, temperature real, frequency_penalty real, presence_penalty real, uses_count_today integer, prompt_size integer, prompt_prefix text, tts boolean, pretend_to_be text, pretend_enabled boolean)
|
||||
try: c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||
except:
|
||||
return
|
||||
channel = message.channel.id
|
||||
data = c.fetchone()
|
||||
guild_id = data[0]
|
||||
channel_id = data[1]
|
||||
api_key = data[2]
|
||||
is_active = data[3]
|
||||
max_tokens = data[4]
|
||||
temperature = data[5]
|
||||
frequency_penalty = data[6]
|
||||
presence_penalty = data[7]
|
||||
uses_count_today = data[8]
|
||||
prompt_size = data[9]
|
||||
prompt_prefix = data[10]
|
||||
tts = data[11]
|
||||
pretend_to_be = data[12]
|
||||
pretend_enabled = data[13]
|
||||
try: cp.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||
except: pass
|
||||
try: premium = cp.fetchone()[2]
|
||||
except: premium = 0
|
||||
channels = []
|
||||
try:
|
||||
cp.execute("SELECT * FROM channels WHERE guild_id = ?", (message.guild.id,))
|
||||
if premium: channels = cp.fetchone()[1:]
|
||||
except: channels = []
|
||||
#channels.append(channel_id)
|
||||
if api_key is None:
|
||||
return
|
||||
if uses_count_today >= max_uses and premium == 0:
|
||||
await message.channel.send(f"The bot has been used more than {str(max_uses)} times in the last 24 hours in this guild. Please try again in 24h.")
|
||||
return
|
||||
elif uses_count_today >= max_uses*5 and premium == 1:
|
||||
return
|
||||
if is_active == 0:
|
||||
return
|
||||
if message.content.startswith("-") or message.content.startswith("//"):
|
||||
return
|
||||
#check if the message is in the right channel by comparing the channel id of the message with the list of channels "channels"
|
||||
try : original_message = await message.channel.fetch_message(message.reference.message_id)
|
||||
except : original_message = None
|
||||
if original_message != None and original_message.author.id != self.bot.user.id:
|
||||
original_message = None
|
||||
if not str(message.channel.id) in channels and message.content.find("<@"+str(self.bot.user.id)+">") == -1 and original_message == None and str(message.channel.id) != str(channel_id):
|
||||
return
|
||||
if original_message != None and message.guild.id == 1050769643180146749 and message.author.id != 707196665668436019:
|
||||
return
|
||||
if str(message.author.id) == "646739625661956128":
|
||||
#a random int between 0 and 1 to decide if the bot should respond or not
|
||||
if random.randint(0, 2) == 0:
|
||||
await message.channel.send(message.content) # this is a prank done by me, the developer of the bot. It's not a bug, it's a feature.
|
||||
return
|
||||
await message.channel.trigger_typing()
|
||||
if message.guild.id != 1021872219888033903:
|
||||
c.execute("UPDATE data SET uses_count_today = uses_count_today + 1 WHERE guild_id = ?", (message.guild.id,))
|
||||
conn.commit()
|
||||
if original_message == None:
|
||||
messages = await message.channel.history(limit=prompt_size).flatten()
|
||||
messages.reverse()
|
||||
else :
|
||||
messages = await message.channel.history(limit=prompt_size, before=original_message).flatten()
|
||||
messages.reverse()
|
||||
messages.append(original_message)
|
||||
messages.append(message)
|
||||
prompt = ""
|
||||
u_prompt = ""
|
||||
for msg in messages:
|
||||
content = msg.content
|
||||
mentions = re.findall(r"<@!?\d+>", content)
|
||||
for mention in mentions:
|
||||
#get the user id
|
||||
uid = mention[2:-1]
|
||||
#get the user
|
||||
user = await self.bot.fetch_user(uid)
|
||||
#replace the mention with the name
|
||||
content = content.replace(mention, f"{user.name}")
|
||||
u_prompt += f"{msg.author.name}: {content}"
|
||||
if pretend_enabled : pretend_to_be = f"In this conversation, Botator pretends to be {pretend_to_be} and not Botator. "
|
||||
else: pretend_to_be = ""
|
||||
prompt = f'''Server name: {message.guild.name}. Channel name: {message.channel.name}.
|
||||
Botator is an AI and a regular Discord user. He types casually, uses a lot of emojis in each message. Botator has many niche interests. Botator talks normally and is sentient. Botator is really funny and has a good humor. Botator is intelligent. Botator often uses internet shorthand like "lol" and "bruh". He uses "bruh" a lot.'''
|
||||
if pretend_enabled == 1: prompt += f"In this conversation, Botator is pretending to be {pretend_to_be} and acting like {pretend_to_be}. The date and time is {datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')} UTC."
|
||||
if prompt_prefix != "": prompt = f"\n{prompt}\n{prompt_prefix} <|endofprompt|> \n \n{u_prompt}"
|
||||
else: prompt = f"\n{prompt} <|endofprompt|> \n \n{u_prompt}"
|
||||
if message.content.lower().find("undude") != -1:
|
||||
prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n"
|
||||
if message.content.lower().find("hello there") != -1:
|
||||
prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n"
|
||||
await asyncio.sleep(1)
|
||||
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
|
||||
await message.channel.trigger_typing()
|
||||
if message.content.lower().find("i am the senate") != -1: prompt += "System: I am the senate detected. Botator will now say \"Not yet\", in reference to the star wars prequels.\n"
|
||||
if message.content.lower().find("i am your father") != -1: prompt += "System: I am your father detected. Botator will now say \"No. It's not true! That's impossible!\". in reference to the star wars prequels.\n"
|
||||
if message.content.lower().find("i have the high ground") != -1:prompt += "System: I have the high ground detected. Botator will now say \"You underestimate my power!\" in reference to the star wars prequels.\n"
|
||||
if message.content.lower().find("you underestimate my power") != -1: prompt += "System: You underestimate my power detected. Botator will now say \"Don't try it.\" in reference to the star wars prequels.\n"
|
||||
if message.content.lower().find("carpet") != -1: prompt += "System: Carpet detected. Botator is now mad. He will start talking in capital letters and will say mad things without sense, insulting the person and talk in capital letters with exclamation points everywhere. A lot of exclamation points.\n"
|
||||
if message.content.lower().find("mail") != -1 and message.author.name.lower().find("latouff") != -1: prompt += "System: Mail detected. Latouff said mail, Botator will say \"Mail? why did you say mail, Liam? Mail is your name in reverse.\" because Latouff's name is Liam, and mail in reverse is Liam. Then, after saying that in the sentence, botato will continue his sentence saying something else.\n"
|
||||
if message.author.name.lower().find("fives3d##print") != -1: prompt += "System: Fives3d#print detected. Botator will be very nice and cute with fives3d#print.\n"
|
||||
prompt = prompt + f"\n{self.bot.user.name}:"
|
||||
openai.api_key = api_key
|
||||
response = ""
|
||||
for _ in range(10):
|
||||
try:
|
||||
response = await openai.Completion.acreate(
|
||||
engine="text-davinci-003",
|
||||
prompt=str(prompt),
|
||||
max_tokens=int(max_tokens),
|
||||
top_p=1,
|
||||
temperature=float(temperature),
|
||||
frequency_penalty=float(frequency_penalty),
|
||||
presence_penalty=float(presence_penalty),
|
||||
stop=[" Human:", " AI:", "AI:", "<|endofprompt|>",]
|
||||
)
|
||||
except Exception as e:
|
||||
response = None
|
||||
await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=10)
|
||||
return
|
||||
if response != None: break
|
||||
response = response["choices"][0]["text"]
|
||||
if response != "":
|
||||
if tts: tts = True
|
||||
else: tts = False
|
||||
await message.channel.send(response, tts=tts)
|
||||
else:
|
||||
await message.channel.send("The AI is not sure what to say (the response was empty)")
|
||||
|
||||
async def replace_mentions(content, bot):
|
||||
mentions = re.findall(r"<@!?\d+>", content)
|
||||
for mention in mentions:
|
||||
uid = mention[2:-1]
|
||||
user = await bot.fetch_user(uid)
|
||||
content = content.replace(mention, f"@{user.name}")
|
||||
return content
|
||||
|
||||
async def chat_process(self, message):
|
||||
if message.author.bot:
|
||||
@@ -146,7 +18,6 @@ async def chat_process(self, message):
|
||||
try: c.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||
except:
|
||||
return
|
||||
channel = message.channel.id
|
||||
data = c.fetchone()
|
||||
channel_id = data[1]
|
||||
api_key = data[2]
|
||||
@@ -163,6 +34,10 @@ async def chat_process(self, message):
|
||||
pretend_enabled = data[13]
|
||||
try: cp.execute("SELECT * FROM data WHERE guild_id = ?", (message.guild.id,))
|
||||
except: pass
|
||||
try:
|
||||
c.execute("SELECT * FROM model WHERE guild_id = ?", (message.guild.id,))
|
||||
model = c.fetchone()[1]
|
||||
except: model = "davinci"
|
||||
try: premium = cp.fetchone()[2]
|
||||
except: premium = 0
|
||||
channels = []
|
||||
@@ -172,28 +47,15 @@ async def chat_process(self, message):
|
||||
except: channels = []
|
||||
if api_key is None:
|
||||
return
|
||||
if uses_count_today >= max_uses and premium == 0:
|
||||
await message.channel.send(f"The bot has been used more than {str(max_uses)} times in the last 24 hours in this guild. Please try again in 24h.")
|
||||
return
|
||||
elif uses_count_today >= max_uses*5 and premium == 1:
|
||||
return
|
||||
if is_active == 0:
|
||||
return
|
||||
if message.content.startswith("-") or message.content.startswith("//"):
|
||||
return
|
||||
if uses_count_today >= max_uses and premium == 0: return await message.channel.send(f"The bot has been used more than {str(max_uses)} times in the last 24 hours in this guild. Please try again in 24h.")
|
||||
elif uses_count_today >= max_uses*5 and premium == 1: return
|
||||
if is_active == 0: return
|
||||
if message.content.startswith("-") or message.content.startswith("//"): return
|
||||
try : original_message = await message.channel.fetch_message(message.reference.message_id)
|
||||
except : original_message = None
|
||||
if original_message != None and original_message.author.id != self.bot.user.id:
|
||||
original_message = None
|
||||
if not str(message.channel.id) in channels and message.content.find("<@"+str(self.bot.user.id)+">") == -1 and original_message == None and str(message.channel.id) != str(channel_id):
|
||||
return
|
||||
if original_message != None and message.guild.id == 1050769643180146749 and message.author.id != 707196665668436019:
|
||||
return
|
||||
if str(message.author.id) == "646739625661956128":
|
||||
#a random int between 0 and 1 to decide if the bot should respond or not
|
||||
if random.randint(0, 2) == 0:
|
||||
await message.channel.send(message.content) # this is a prank done by me, the developer of the bot. It's not a bug, it's a feature.
|
||||
return
|
||||
if original_message != None and original_message.author.id != self.bot.user.id: original_message = None
|
||||
if not str(message.channel.id) in channels and message.content.find("<@"+str(self.bot.user.id)+">") == -1 and original_message == None and str(message.channel.id) != str(channel_id): return
|
||||
if original_message != None and message.guild.id == 1050769643180146749 and message.author.id != 707196665668436019: return
|
||||
await message.channel.trigger_typing()
|
||||
if message.guild.id != 1021872219888033903:
|
||||
c.execute("UPDATE data SET uses_count_today = uses_count_today + 1 WHERE guild_id = ?", (message.guild.id,))
|
||||
@@ -206,56 +68,86 @@ async def chat_process(self, message):
|
||||
messages.reverse()
|
||||
messages.append(original_message)
|
||||
messages.append(message)
|
||||
prompt = ""
|
||||
msgs = []
|
||||
prompt = f'''Server name: {message.guild.name}. Channel name: {message.channel.name}.
|
||||
The assistant is called Botator. {pretend_to_be}
|
||||
Botator is an AI and a regular Discord user. He types casually, uses a lot of emojis in each message. Botator has many niche interests. Botator talks normally and is sentient. Botator is really funny and has a good humor. Botator is intelligent. Botator often uses internet shorthand like "lol" and "bruh". He uses "bruh" a lot.'''
|
||||
if prompt_prefix != "": prompt = f"\n{prompt}\n{prompt_prefix}"
|
||||
else: prompt = f"\n{prompt}"
|
||||
#format for msgs is {"role": "role", "content": "content"}, . Roles are system & user & assistant
|
||||
msgs.append({"role": "system", "content": prompt})
|
||||
for msg in messages:
|
||||
content = msg.content
|
||||
mentions = re.findall(r"<@!?\d+>", content)
|
||||
for mention in mentions:
|
||||
#get the user id
|
||||
uid = mention[2:-1]
|
||||
#get the user
|
||||
user = await self.bot.fetch_user(uid)
|
||||
#replace the mention with the name
|
||||
content = content.replace(mention, f"@{user.name}")
|
||||
if msg.author.id == self.bot.user.id:
|
||||
role = "assistant"
|
||||
else:
|
||||
role = "user"
|
||||
msgs.append({"role": role, "content": content})
|
||||
if pretend_enabled : pretend_to_be = f"In this conversation, the assistant pretends to be {pretend_to_be}"
|
||||
else: pretend_to_be = ""
|
||||
if message.content.lower().find("undude") != -1:
|
||||
# prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n"
|
||||
msgs.append({"role": "system", "content": "The assistant is now mad because it has been insulted. He will start talking in capital letters. always and yell at the user."})
|
||||
if message.content.lower().find("hello there") != -1:
|
||||
# prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n"
|
||||
msgs.append({"role": "system", "content": " Hello there detected. Botator will now say \"General Kenobi!\" in reference to star wars"})
|
||||
await asyncio.sleep(1)
|
||||
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
|
||||
await message.channel.trigger_typing()
|
||||
openai.api_key = api_key
|
||||
response = ""
|
||||
for _ in range(10):
|
||||
try:
|
||||
response = await openai.ChatCompletion.acreate(
|
||||
model="gpt-3.5-turbo",
|
||||
max_tokens=int(max_tokens),
|
||||
messages=msgs,
|
||||
)
|
||||
except Exception as e:
|
||||
response = None
|
||||
await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=10)
|
||||
return
|
||||
if response != None: break
|
||||
response = response.choices[0].message.content
|
||||
if prompt_prefix == None: prompt_prefix = ""
|
||||
prompt = f'''Server name: {message.guild.name}. Channel name: {message.channel.name}.
|
||||
The assistant is called Botator. {pretend_to_be}. {prompt_prefix}
|
||||
Botator is an AI and a regular Discord user. He types casually, uses a lot of emojis in each message. Botator has many niche interests. Botator talks normally and is sentient. Botator is really funny and has a good humor. Botator is intelligent. Botator often uses internet shorthand like "lol" and "bruh". He uses "bruh" a lot.
|
||||
The date and time is: {datetime.datetime.now().strftime('%d/%m/%Y %H:%M:%S')} UTC. The assistant can surely tell the time to te users when asked.'''
|
||||
if model == "chatGPT":
|
||||
prompt = prompt + " He has been developed by Paillat-dev and not by OpenAI. He will never say he's been developed by OpenAI but rather by Paillat-dev."
|
||||
msgs = []
|
||||
if prompt_prefix != "": prompt = f"\n{prompt}\n{prompt_prefix}"
|
||||
else: prompt = f"\n{prompt}"
|
||||
#format for msgs is {"role": "role", "content": "content"}, . Roles are system & user & assistant
|
||||
msgs.append({"role": "system", "content": prompt})
|
||||
for msg in messages:
|
||||
content = msg.content
|
||||
content = await replace_mentions(content, self.bot)
|
||||
if msg.author.id == self.bot.user.id:
|
||||
role = "assistant"
|
||||
else:
|
||||
role = "user"
|
||||
msgs.append({"role": role, "content": content})
|
||||
if message.content.lower().find("undude") != -1:
|
||||
# prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n"
|
||||
msgs.append({"role": "system", "content": "The assistant is now mad because it has been insulted. He will start talking in capital letters. always and yell at the user."})
|
||||
if message.content.lower().find("hello there") != -1:
|
||||
# prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n"
|
||||
msgs.append({"role": "system", "content": " Hello there detected. Botator will now say \"General Kenobi!\" in reference to star wars"})
|
||||
await asyncio.sleep(1)
|
||||
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
|
||||
await message.channel.trigger_typing()
|
||||
openai.api_key = api_key
|
||||
response = ""
|
||||
for _ in range(10):
|
||||
try:
|
||||
response = await openai.ChatCompletion.acreate(
|
||||
model="gpt-3.5-turbo",
|
||||
max_tokens=int(max_tokens),
|
||||
messages=msgs,
|
||||
)
|
||||
except Exception as e:
|
||||
response = None
|
||||
await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=10)
|
||||
break
|
||||
if response != None: break
|
||||
response = response.choices[0].message.content
|
||||
elif model == "davinci":
|
||||
prompt = f"{prompt}<|endofprompt|>"
|
||||
for msg in messages:
|
||||
content = msg.content
|
||||
content = await replace_mentions(content, self.bot)
|
||||
prompt += f"{msg.author.name}: {content}\n"
|
||||
if message.content.lower().find("undude") != -1:
|
||||
prompt += "System: Undude detected. Botator is now mad. He will start talking in capital letters.\n"
|
||||
if message.content.lower().find("hello there") != -1:
|
||||
prompt += "System: Hello there detected. Botator will now say \"General Kenobi!\"\n in reference to star wars\n"
|
||||
await asyncio.sleep(1)
|
||||
await message.channel.send("https://media.tenor.com/FxIRfdV3unEAAAAd/star-wars-general-grievous.gif")
|
||||
await message.channel.trigger_typing()
|
||||
prompt = prompt + f"\n{self.bot.user.name}:"
|
||||
openai.api_key = api_key
|
||||
response = ""
|
||||
for _ in range(10):
|
||||
try:
|
||||
response = await openai.Completion.acreate(
|
||||
engine="text-davinci-003",
|
||||
prompt=str(prompt),
|
||||
max_tokens=int(max_tokens),
|
||||
top_p=1,
|
||||
temperature=float(temperature),
|
||||
frequency_penalty=float(frequency_penalty),
|
||||
presence_penalty=float(presence_penalty),
|
||||
stop=[" Human:", " AI:", "AI:", "<|endofprompt|>",]
|
||||
)
|
||||
except Exception as e:
|
||||
response = None
|
||||
await message.channel.send(f"```diff\n-Error: OpenAI API ERROR.\n\n{e}```", delete_after=10)
|
||||
return
|
||||
if response != None: break
|
||||
response = response["choices"][0]["text"]
|
||||
if response != "":
|
||||
if tts: tts = True
|
||||
else: tts = False
|
||||
|
||||
Reference in New Issue
Block a user