Skip to content

Commit

Permalink
Format Python code with psf/black push
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions authored and github-actions committed Jan 14, 2023
1 parent 5d9e78f commit ab362b9
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 125 deletions.
44 changes: 20 additions & 24 deletions cogs/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
ALLOWED_GUILDS = EnvService.get_allowed_guilds()



class Commands(discord.Cog, name="Commands"):
def __init__(
self,
Expand Down Expand Up @@ -97,7 +96,6 @@ async def settings(
async def local_size(self, ctx: discord.ApplicationContext):
await self.image_draw_cog.local_size_command(ctx)


@add_to_group("system")
@discord.slash_command(
name="clear-local",
Expand All @@ -108,7 +106,6 @@ async def local_size(self, ctx: discord.ApplicationContext):
async def clear_local(self, ctx: discord.ApplicationContext):
await self.image_draw_cog.clear_local_command(ctx)


@add_to_group("system")
@discord.slash_command(
name="usage",
Expand All @@ -119,7 +116,6 @@ async def clear_local(self, ctx: discord.ApplicationContext):
async def usage(self, ctx: discord.ApplicationContext):
await self.converser_cog.usage_command(ctx)


@add_to_group("system")
@discord.slash_command(
name="set-usage",
Expand All @@ -134,7 +130,6 @@ async def usage(self, ctx: discord.ApplicationContext):
async def set_usage(self, ctx: discord.ApplicationContext, usage_amount: float):
await self.converser_cog.set_usage_command(ctx, usage_amount)


@add_to_group("system")
@discord.slash_command(
name="delete-conversation-threads",
Expand Down Expand Up @@ -163,7 +158,6 @@ async def delete_all_conversation_threads(self, ctx: discord.ApplicationContext)
async def moderations_test(self, ctx: discord.ApplicationContext, prompt: str):
await self.moderations_cog.moderations_test_command(ctx, prompt)


@add_to_group("mod")
@discord.slash_command(
name="set",
Expand Down Expand Up @@ -241,8 +235,9 @@ async def ask(
frequency_penalty: float,
presence_penalty: float,
):
await self.converser_cog.ask_command(ctx, prompt, temperature, top_p, frequency_penalty, presence_penalty)

await self.converser_cog.ask_command(
ctx, prompt, temperature, top_p, frequency_penalty, presence_penalty
)

@add_to_group("gpt")
@discord.slash_command(
Expand All @@ -251,10 +246,15 @@ async def ask(
guild_ids=ALLOWED_GUILDS,
)
@discord.option(
name="instruction", description="How you want GPT3 to edit the text", required=True
name="instruction",
description="How you want GPT3 to edit the text",
required=True,
)
@discord.option(
name="input", description="The text you want to edit, can be empty", required=False, default=""
name="input",
description="The text you want to edit, can be empty",
required=False,
default="",
)
@discord.option(
name="temperature",
Expand All @@ -273,10 +273,7 @@ async def ask(
max_value=1,
)
@discord.option(
name="codex",
description="Enable codex version",
required=False,
default=False
name="codex", description="Enable codex version", required=False, default=False
)
@discord.guild_only()
async def edit(
Expand All @@ -288,8 +285,9 @@ async def edit(
top_p: float,
codex: bool,
):
await self.converser_cog.edit_command(ctx, instruction, input, temperature, top_p, codex)

await self.converser_cog.edit_command(
ctx, instruction, input, temperature, top_p, codex
)

@add_to_group("gpt")
@discord.slash_command(
Expand Down Expand Up @@ -329,8 +327,9 @@ async def converse(
private: bool,
minimal: bool,
):
await self.converser_cog.converse_command(ctx, opener, opener_file, private, minimal)

await self.converser_cog.converse_command(
ctx, opener, opener_file, private, minimal
)

@add_to_group("gpt")
@discord.slash_command(
Expand All @@ -356,7 +355,6 @@ async def end(self, ctx: discord.ApplicationContext):
async def draw(self, ctx: discord.ApplicationContext, prompt: str):
await self.image_draw_cog.draw_command(ctx, prompt)


@add_to_group("dalle")
@discord.slash_command(
name="optimize",
Expand All @@ -375,23 +373,21 @@ async def optimize(self, ctx: discord.ApplicationContext, prompt: str):
"""

@discord.slash_command(
name="private-test",
description="Private thread for testing. Only visible to you and server admins.",
guild_ids=ALLOWED_GUILDS,
name="private-test",
description="Private thread for testing. Only visible to you and server admins.",
guild_ids=ALLOWED_GUILDS,
)
@discord.guild_only()
async def private_test(self, ctx: discord.ApplicationContext):
await self.converser_cog.private_test_command(ctx)


@discord.slash_command(
name="help", description="Get help for GPT3Discord", guild_ids=ALLOWED_GUILDS
)
@discord.guild_only()
async def help(self, ctx: discord.ApplicationContext):
await self.converser_cog.help_command(ctx)


@discord.slash_command(
name="setup",
description="Setup your API key for use with GPT3Discord",
Expand Down
9 changes: 4 additions & 5 deletions cogs/image_service_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ def __init__(
print("Draw service initialized")
self.redo_users = {}


async def draw_command(self, ctx: discord.ApplicationContext, prompt: str):
user_api_key = None
if USER_INPUT_API_KEYS:
user_api_key = await TextService.get_user_api_key(ctx.user.id, ctx, USER_KEY_DB)
user_api_key = await TextService.get_user_api_key(
ctx.user.id, ctx, USER_KEY_DB
)
if not user_api_key:
return

Expand All @@ -58,8 +59,7 @@ async def draw_command(self, ctx: discord.ApplicationContext, prompt: str):
try:
asyncio.ensure_future(
ImageService.encapsulated_send(
self,
user.id, prompt, ctx, custom_api_key=user_api_key
self, user.id, prompt, ctx, custom_api_key=user_api_key
)
)

Expand Down Expand Up @@ -98,4 +98,3 @@ async def clear_local_command(self, ctx):
print(e)

await ctx.respond("Local images cleared.")

8 changes: 6 additions & 2 deletions cogs/moderations_service_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
print("Failed to retrieve the General and Moderations DB")
raise e


class ModerationsService(discord.Cog, name="ModerationsService"):
def __init__(
self,
Expand All @@ -32,6 +33,7 @@ def __init__(
self.moderation_enabled_guilds = []
self.moderation_tasks = {}
self.moderations_launched = []

@discord.Cog.listener()
async def on_ready(self):
# Check moderation service for each guild
Expand Down Expand Up @@ -81,6 +83,7 @@ async def check_and_launch_moderations(self, guild_id, alert_channel_override=No
return moderations_channel

return None

async def moderations_command(
self, ctx: discord.ApplicationContext, status: str, alert_channel_id: str
):
Expand Down Expand Up @@ -118,9 +121,10 @@ async def moderations_command(
Moderation.moderations_launched.remove(ctx.guild_id)
await ctx.respond("Moderations service disabled")

async def moderations_test_command(self, ctx: discord.ApplicationContext, prompt: str):
async def moderations_test_command(
self, ctx: discord.ApplicationContext, prompt: str
):
await ctx.defer()
response = await self.model.send_moderations_request(prompt)
await ctx.respond(response["results"][0]["category_scores"])
await ctx.send_followup(response["results"][0]["flagged"])

54 changes: 30 additions & 24 deletions cogs/text_service_cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ def __init__(
self.conversation_threads = {}
self.summarize = self.model.summarize_conversations


# Pinecone data
self.pinecone_service = pinecone_service

Expand Down Expand Up @@ -157,7 +156,6 @@ def __init__(
self.message_queue = message_queue
self.conversation_thread_owners = {}


async def load_file(self, file, ctx):
try:
async with aiofiles.open(file, "r") as f:
Expand Down Expand Up @@ -212,7 +210,6 @@ async def on_ready(self):
)
print(f"Commands synced")


# TODO: add extra condition to check if multi is enabled for the thread, stated in conversation_threads
def check_conversing(self, user_id, channel_id, message_content, multi=None):
cond1 = channel_id in self.conversation_threads
Expand Down Expand Up @@ -298,7 +295,6 @@ async def end_conversation(
traceback.print_exc()
pass


async def send_settings_text(self, ctx):
embed = discord.Embed(
title="GPT3Bot Settings",
Expand Down Expand Up @@ -386,10 +382,12 @@ async def paginate_and_send(self, response_text, ctx):

async def paginate_embed(self, response_text, codex, prompt=None, instruction=None):

if codex: #clean codex input
if codex: # clean codex input
response_text = response_text.replace("```", "")
response_text = response_text.replace(f"***Prompt: {prompt}***\n", "")
response_text = response_text.replace(f"***Instruction: {instruction}***\n\n", "")
response_text = response_text.replace(
f"***Instruction: {instruction}***\n\n", ""
)

response_text = [
response_text[i : i + self.EMBED_CUTOFF]
Expand All @@ -400,12 +398,20 @@ async def paginate_embed(self, response_text, codex, prompt=None, instruction=No
# Send each chunk as a message
for count, chunk in enumerate(response_text, start=1):
if not first:
page = discord.Embed(title=f"Page {count}", description=chunk if not codex else f"***Prompt:{prompt}***\n***Instruction:{instruction:}***\n```python\n{chunk}\n```")
page = discord.Embed(
title=f"Page {count}",
description=chunk
if not codex
else f"***Prompt:{prompt}***\n***Instruction:{instruction:}***\n```python\n{chunk}\n```",
)
first = True
else:
page = discord.Embed(title=f"Page {count}", description=chunk if not codex else f"```python\n{chunk}\n```")
page = discord.Embed(
title=f"Page {count}",
description=chunk if not codex else f"```python\n{chunk}\n```",
)
pages.append(page)

return pages

async def queue_debug_message(self, debug_message, debug_channel):
Expand Down Expand Up @@ -511,11 +517,10 @@ async def on_message_edit(self, before, after):
).timestamp()
await Moderation.moderation_queues[after.guild.id].put(
Moderation(after, timestamp)
) # TODO Don't proceed if message was deleted!
) # TODO Don't proceed if message was deleted!

await TextService.process_conversation_edit(self, after, original_message)


@discord.Cog.listener()
async def on_message(self, message):
if message.author == self.bot.user:
Expand All @@ -534,11 +539,12 @@ async def on_message(self, message):
).timestamp()
await Moderation.moderation_queues[message.guild.id].put(
Moderation(message, timestamp)
) # TODO Don't proceed to conversation processing if the message is deleted by moderations.

) # TODO Don't proceed to conversation processing if the message is deleted by moderations.

# Process the message if the user is in a conversation
if await TextService.process_conversation_message(self, message, USER_INPUT_API_KEYS, USER_KEY_DB):
if await TextService.process_conversation_message(
self, message, USER_INPUT_API_KEYS, USER_KEY_DB
):
original_message[message.author.id] = message.id

def cleanse_response(self, response_text):
Expand All @@ -548,7 +554,9 @@ def cleanse_response(self, response_text):
response_text = response_text.replace("<|endofstatement|>", "")
return response_text

def remove_awaiting(self, author_id, channel_id, from_ask_command, from_edit_command):
def remove_awaiting(
self, author_id, channel_id, from_ask_command, from_edit_command
):
if author_id in self.awaiting_responses:
self.awaiting_responses.remove(author_id)
if not from_ask_command and not from_edit_command:
Expand All @@ -569,7 +577,7 @@ async def mention_to_username(self, ctx, message):
pass
return message

# COMMANDS
# COMMANDS

async def help_command(self, ctx):
await ctx.defer()
Expand Down Expand Up @@ -623,8 +631,9 @@ async def help_command(self, ctx):
embed.add_field(name="/help", value="See this help text", inline=False)
await ctx.respond(embed=embed)


async def set_usage_command(self, ctx: discord.ApplicationContext, usage_amount: float):
async def set_usage_command(
self, ctx: discord.ApplicationContext, usage_amount: float
):
await ctx.defer()

# Attempt to convert the input usage value into a float
Expand All @@ -636,8 +645,9 @@ async def set_usage_command(self, ctx: discord.ApplicationContext, usage_amount:
await ctx.respond("The usage value must be a valid float.")
return


async def delete_all_conversation_threads_command(self, ctx: discord.ApplicationContext):
async def delete_all_conversation_threads_command(
self, ctx: discord.ApplicationContext
):
await ctx.defer()

for guild in self.bot.guilds:
Expand All @@ -650,7 +660,6 @@ async def delete_all_conversation_threads_command(self, ctx: discord.Application
pass
await ctx.respond("All conversation threads have been deleted.")


async def usage_command(self, ctx):
await ctx.defer()
embed = discord.Embed(
Expand All @@ -669,7 +678,6 @@ async def usage_command(self, ctx):
)
await ctx.respond(embed=embed)


async def ask_command(
self,
ctx: discord.ApplicationContext,
Expand Down Expand Up @@ -908,7 +916,6 @@ async def converse_command(
if thread.id in self.awaiting_thread_responses:
self.awaiting_thread_responses.remove(thread.id)


async def end_command(self, ctx: discord.ApplicationContext):
await ctx.defer(ephemeral=True)
user_id = ctx.user.id
Expand Down Expand Up @@ -964,4 +971,3 @@ async def settings_command(

# Otherwise, process the settings change
await self.process_settings(ctx, parameter, value)

1 change: 0 additions & 1 deletion gpt3discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ async def main():
)
)


apply_multicog(bot)

await bot.start(os.getenv("DISCORD_TOKEN"))
Expand Down
Loading

0 comments on commit ab362b9

Please sign in to comment.