temp fix for gpt4 conversations, some safeguards for channel chats

Kaveen Kumarasinghe 1 year ago
parent fd3a4d7c0a
commit 6dc58ea025

@ -43,6 +43,7 @@ CHAT_BYPASS_ROLES = EnvService.get_bypass_roles()
PRE_MODERATE = EnvService.get_premoderate() PRE_MODERATE = EnvService.get_premoderate()
FORCE_ENGLISH = EnvService.get_force_english() FORCE_ENGLISH = EnvService.get_force_english()
BOT_TAGGABLE = EnvService.get_bot_is_taggable() BOT_TAGGABLE = EnvService.get_bot_is_taggable()
CHANNEL_CHAT_ROLES = EnvService.get_channel_chat_roles()
# #
# Obtain the Moderation table and the General table, these are two SQLite tables that contain # Obtain the Moderation table and the General table, these are two SQLite tables that contain
@ -1073,6 +1074,25 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
await ctx.respond("Conversation started.") await ctx.respond("Conversation started.")
target = thread target = thread
else: else:
# Check if this current channel is already in a conversation
if ctx.channel.id in self.conversation_threads:
await ctx.respond(
"There is already a conversation in this channel. Please finish that conversation before starting a new one."
)
return
# Check if the user is permitted to start a conversation in full channels
# check if any of the user role names match CHANNEL_CHAT_ROLES
if CHANNEL_CHAT_ROLES and CHANNEL_CHAT_ROLES != [None]:
if not any(
role.name in CHANNEL_CHAT_ROLES for role in ctx.user.roles
):
await ctx.respond(
"You are not permitted to start a conversation in this channel."
)
return
target = ctx.channel target = ctx.channel
if private: if private:
embed_title = f"{user.name}'s private conversation with GPT" embed_title = f"{user.name}'s private conversation with GPT"

@ -33,4 +33,4 @@ Human: I'm making a discord bot <|endofstatement|>
There can be an arbitrary amount of newlines between chat entries. <username> can be any name, pay attention to who's talking. The text "<|endofstatement|>" is used to separate chat entries and make it easier for you to understand the context. There can be an arbitrary amount of newlines between chat entries. <username> can be any name, pay attention to who's talking. The text "<|endofstatement|>" is used to separate chat entries and make it easier for you to understand the context.
You speak in a fun, casual, and friendly tone, not worrying about capitalizations and using slang like "lol", "lmao", and etc, like you're talking to a friend. You speak in a fun, casual, and friendly tone, not worrying about capitalizations and using slang like "lol", "lmao", and etc, like you're talking to a friend, you are not overly verbose.

@ -1,4 +1,6 @@
## Other Features ## Other Features
### Full-channel chat
This bot supports designating an entire discord text channel as a GPT conversation. Use /gpt converse and set the use_threads option to false. You can designate who is allowed to turn full channels into chat by setting the `CHANNEL_CHAT_ROLES` variable in your `.env` file to be a list of role names separated by commas that should be able to use this functionality.
### Health Check Service ### Health Check Service
The bot has the ability to launch a HTTP endpoint at `<host>:8181/` that will return a json response of the bot's status and uptime. This is especially useful if you want to run this bot on cloud application containers, like Azure App Service. The bot has the ability to launch a HTTP endpoint at `<host>:8181/` that will return a json response of the bot's status and uptime. This is especially useful if you want to run this bot on cloud application containers, like Azure App Service.

@ -33,7 +33,7 @@ from services.environment_service import EnvService
from models.openai_model import Model from models.openai_model import Model
__version__ = "11.1.1" __version__ = "11.1.2"
PID_FILE = Path("bot.pid") PID_FILE = Path("bot.pid")

@ -874,7 +874,6 @@ class Model:
messages.append( messages.append(
{ {
"role": "system", "role": "system",
"name": "Instructor",
"content": message.text, "content": message.text,
} }
) )
@ -884,29 +883,27 @@ class Model:
text = message.text.replace(bot_name, "") text = message.text.replace(bot_name, "")
text = text.replace("<|endofstatement|>", "") text = text.replace("<|endofstatement|>", "")
messages.append( messages.append(
{"role": "assistant", "name": bot_name_clean, "content": text} {"role": "assistant", "content": text} # TODO add back the assistant's name when the API is fixed..
) )
else: else:
try: try:
print("In first block The message text is ->" + message.text)
if ( if (
message.text.strip() message.text.strip()
.lower() .lower()
.startswith("this conversation has some context from earlier") .startswith("this conversation has some context from earlier")
): ):
print("Hit the exception clause")
raise Exception("This is a context message") raise Exception("This is a context message")
username = re.search(r"(?<=\n)(.*?)(?=:)", message.text).group() username = re.search(r"(?<=\n)(.*?)(?=:)", message.text).group()
username_clean = self.cleanse_username(username) username_clean = self.cleanse_username(username)
text = message.text.replace(f"{username}:", "") text = message.text.replace(f"{username}:", "")
# Strip whitespace just from the right side of the string
text = text.rstrip()
text = text.replace("<|endofstatement|>", "") text = text.replace("<|endofstatement|>", "")
messages.append( messages.append(
{"role": "user", "name": username_clean, "content": text} {"role": "user", "name": username_clean, "content": text}
) )
print("Got to here")
except Exception: except Exception:
print("In second block The message text is ->" + message.text)
text = message.text.replace("<|endofstatement|>", "") text = message.text.replace("<|endofstatement|>", "")
messages.append({"role": "system", "content": text}) messages.append({"role": "system", "content": text})

@ -250,6 +250,32 @@ class EnvService:
) )
return index_roles return index_roles
@staticmethod
def get_channel_chat_roles():
# GPT_ROLES is a comma separated list of string roles
# It can also just be one role
# Read these allowed roles and return as a list of strings
try:
cc_roles = os.getenv("CHANNEL_CHAT_ROLES")
except Exception:
cc_roles = None
if cc_roles is None:
print(
"INDEX_ROLES is not defined properly in the environment file!"
"Please copy your server's role and put it into INDEX_ROLES in the .env file."
'For example a line should look like: `INDEX_ROLES="Gpt"`'
)
print("Defaulting to allowing all users to use Index commands...")
return [None]
cc_roles = (
cc_roles.lower().strip().split(",")
if "," in cc_roles
else [cc_roles.lower()]
)
return cc_roles
@staticmethod @staticmethod
def get_welcome_message(): def get_welcome_message():
# WELCOME_MESSAGE is a default string used to welcome new members to the server if GPT3 is not available. # WELCOME_MESSAGE is a default string used to welcome new members to the server if GPT3 is not available.

Loading…
Cancel
Save