Kaveen Kumarasinghe 2 years ago
commit 57973cf606

1
.gitignore vendored

@ -5,6 +5,7 @@ __pycache__
#user files
.env
.vscode
*.sqlite
bot.pid
usage.txt
/dalleimages

@ -172,7 +172,7 @@ The Moderations service still uses the main API key defined in the `.env` file.
[**GPT3Discord Guides**](https://github.com/Kav-K/GPT3Discord/tree/main/detailed_guides)
If you follow the link above, you will now get to detailed step-by-step guides that will help you to install and set up your GPT3Discord bot quickly and easily. If you still run into problems or have suggestions for improving the guides, you can join the [**Discord-Server**](https://discord.gg/WvAHXDMS7Q) and we try will help you. Keep in mind that the maintainers are volunteers and will try to help you on their schedule.
If you follow the link above, you will now get to detailed step-by-step guides that will help you to install and set up your GPT3Discord bot quickly and easily. If you still run into problems or have suggestions for improving the guides, you can join the [**Discord-Server**](https://discord.gg/WvAHXDMS7Q) and we will try to help you. Keep in mind that the maintainers are volunteers and will try to help you on their schedule.
*The number and content of the guides is constantly adapted to current requirements.*

@ -621,7 +621,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
ctx = self.redo_users[after.author.id].ctx
await response_message.edit(content="Redoing prompt 🔄...")
edited_content = after.content
edited_content = await self.mention_to_username(after, after.content)
if after.channel.id in self.conversation_threads:
# Remove the last two elements from the history array and add the new <username>: prompt
@ -713,7 +713,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
if not user_api_key:
return
prompt = content
prompt = await self.mention_to_username(message, content)
await self.check_conversation_limit(message)
@ -795,6 +795,20 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
response_text = response_text.replace("<|endofstatement|>", "")
return response_text
async def mention_to_username(self, ctx, message):
if not discord.utils.raw_mentions(message):
return message
else:
for mention in discord.utils.raw_mentions(message):
try:
user = await discord.utils.get_or_fetch(
ctx.guild, "member", mention
)
message = message.replace(f"<@{str(mention)}>", user.display_name)
except:
pass
return message
# ctx can be of type AppContext(interaction) or Message
async def encapsulated_send(
self,
@ -997,12 +1011,8 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
response_text = response_text.strip()
response_text = f"***{prompt}***\n\n{response_text}"
# If GPT3 tries to ping somebody, don't let it happen
if re.search(r"<@!?\d+>|<@&\d+>|<#\d+>", str(response_text)):
message = "I'm sorry, I can't mention users, roles, or channels."
await ctx.send_followup(message) if from_context else await ctx.reply(
message
)
# If gpt3 tries writing a user mention try to replace it with their name
response_text = await self.mention_to_username(ctx, response_text)
# If the user is conversing, add the GPT response to their conversation history.
if (
@ -1047,9 +1057,12 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
custom_api_key=custom_api_key,
)
# Cleanse
# Cleanse again
response_text = self.cleanse_response(response_text)
# escape any other mentions like @here or @everyone
response_text = discord.utils.escape_mentions(response_text)
# If we don't have a response message, we are not doing a redo, send as a new message(s)
if not response_message:
if len(response_text) > self.TEXT_CUTOFF:
@ -1186,7 +1199,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
presence_penalty: float,
):
user = ctx.user
prompt = prompt.strip()
prompt = await self.mention_to_username(ctx, prompt.strip())
user_api_key = None
if USER_INPUT_API_KEYS:
@ -1267,6 +1280,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"):
)
return
if opener:
opener = await self.mention_to_username(ctx, opener)
if not opener and not opener_file:
user_id_normalized = user.id
else:

@ -74,7 +74,8 @@ class ImgPromptOptimizer(discord.Cog, name="ImgPromptOptimizer"):
user = ctx.user
final_prompt = self.OPTIMIZER_PRETEXT
final_prompt += prompt
# replace mentions with nicknames for the prompt
final_prompt += await self.converser_cog.replace_mention(ctx, prompt)
# If the prompt doesn't end in a period, terminate it.
if not final_prompt.endswith("."):
@ -100,12 +101,8 @@ class ImgPromptOptimizer(discord.Cog, name="ImgPromptOptimizer"):
# also relatively cost-effective
response_text = response["choices"][0]["text"]
if re.search(r"<@!?\d+>|<@&\d+>|<#\d+>", response_text):
await ctx.respond(
"I'm sorry, I can't mention users, roles, or channels."
)
return
# escape any mentions
response_text = discord.utils.escape_mentions(response_text)
# If the response_message is > 75 words, concatenate to the last 70th word
# TODO Temporary workaround until prompt is adjusted to make the optimized prompts shorter.

Loading…
Cancel
Save