From 99bb6200aa554feb092c27faa25700b585142a5c Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sat, 14 Jan 2023 09:36:34 +0000 Subject: [PATCH] Format Python code with psf/black push --- cogs/draw_image_generation.py | 18 +++++- cogs/gpt_3_commands_and_converser.py | 95 ++++++++++++++++++++-------- cogs/image_prompt_optimizer.py | 7 +- models/openai_model.py | 21 ++++-- 4 files changed, 106 insertions(+), 35 deletions(-) diff --git a/cogs/draw_image_generation.py b/cogs/draw_image_generation.py index e822af7..ef62fb7 100644 --- a/cogs/draw_image_generation.py +++ b/cogs/draw_image_generation.py @@ -123,7 +123,14 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"): if from_context: result_message = await ctx.fetch_message(result_message.id) - redo_users[user_id] = RedoUser(prompt=prompt, message=ctx, ctx=ctx, response=response_message, instruction=None, codex=False) + redo_users[user_id] = RedoUser( + prompt=prompt, + message=ctx, + ctx=ctx, + response=response_message, + instruction=None, + codex=False, + ) else: if not vary: # Editing case @@ -177,7 +184,14 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"): ) ) - redo_users[user_id] = RedoUser(prompt=prompt, message=ctx, ctx=ctx, response=response_message, instruction=None, codex=False) + redo_users[user_id] = RedoUser( + prompt=prompt, + message=ctx, + ctx=ctx, + response=response_message, + instruction=None, + codex=False, + ) self.converser_cog.users_to_interactions[user_id].append( response_message.id diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index b251861..db69848 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -889,14 +889,20 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): edited_request=False, redo_request=False, ): - new_prompt = prompt + "\nGPTie: " if not from_g_command and not from_edit_command else prompt + new_prompt = ( + prompt + "\nGPTie: " + if not from_g_command and not from_edit_command + else prompt + ) from_context = isinstance(ctx, discord.ApplicationContext) if not instruction: - tokens = self.usage_service.count_tokens(new_prompt) - else: - tokens = self.usage_service.count_tokens(new_prompt) + self.usage_service.count_tokens(instruction) + tokens = self.usage_service.count_tokens(new_prompt) + else: + tokens = self.usage_service.count_tokens( + new_prompt + ) + self.usage_service.count_tokens(instruction) try: @@ -1089,9 +1095,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): response_text = self.cleanse_response(str(response["choices"][0]["text"])) if from_g_command: - # Append the prompt to the beginning of the response, in italics, then a new line - response_text = response_text.strip() - response_text = f"***{prompt}***\n\n{response_text}" + # Append the prompt to the beginning of the response, in italics, then a new line + response_text = response_text.strip() + response_text = f"***{prompt}***\n\n{response_text}" elif from_edit_command: if codex: response_text = response_text.strip() @@ -1164,21 +1170,34 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): response_message = await ctx.reply( response_text, view=ConversationView( - ctx, self, ctx.channel.id, model, custom_api_key=custom_api_key + ctx, + self, + ctx.channel.id, + model, + custom_api_key=custom_api_key, ), ) elif from_edit_command: response_message = await ctx.respond( response_text, view=ConversationView( - ctx, self, ctx.channel.id, model, from_edit_command, custom_api_key=custom_api_key + ctx, + self, + ctx.channel.id, + model, + from_edit_command, + custom_api_key=custom_api_key, ), ) - else: + else: response_message = await ctx.respond( response_text, view=ConversationView( - ctx, self, ctx.channel.id, model, custom_api_key=custom_api_key + ctx, + self, + ctx.channel.id, + model, + custom_api_key=custom_api_key, ), ) @@ -1190,7 +1209,12 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ) self.redo_users[ctx.author.id] = RedoUser( - prompt=new_prompt, instruction=instruction, ctx=ctx, message=ctx, response=actual_response_message, codex=codex + prompt=new_prompt, + instruction=instruction, + ctx=ctx, + message=ctx, + response=actual_response_message, + codex=codex, ) self.redo_users[ctx.author.id].add_interaction( actual_response_message.id @@ -1219,7 +1243,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): await ctx.send_followup(message) else: await ctx.reply(message) - self.remove_awaiting(ctx.author.id, ctx.channel.id, from_g_command, from_edit_command) + self.remove_awaiting( + ctx.author.id, ctx.channel.id, from_g_command, from_edit_command + ) # Error catching for OpenAI model value errors except ValueError as e: @@ -1227,7 +1253,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): await ctx.send_followup(e) else: await ctx.reply(e) - self.remove_awaiting(ctx.author.id, ctx.channel.id, from_g_command, from_edit_command) + self.remove_awaiting( + ctx.author.id, ctx.channel.id, from_g_command, from_edit_command + ) # General catch case for everything except Exception: @@ -1236,7 +1264,9 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): await ctx.send_followup(message) if from_context else await ctx.reply( message ) - self.remove_awaiting(ctx.author.id, ctx.channel.id, from_g_command, from_edit_command) + self.remove_awaiting( + ctx.author.id, ctx.channel.id, from_g_command, from_edit_command + ) traceback.print_exc() try: @@ -1318,6 +1348,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): from_g_command=True, custom_api_key=user_api_key, ) + @add_to_group("gpt") @discord.slash_command( name="edit", @@ -1325,10 +1356,15 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): 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", @@ -1347,10 +1383,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): 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( @@ -1367,7 +1400,6 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): input = await self.mention_to_username(ctx, input.strip()) instruction = await self.mention_to_username(ctx, instruction.strip()) - user_api_key = None if USER_INPUT_API_KEYS: user_api_key = await GPT3ComCon.get_user_api_key(user.id, ctx) @@ -1795,7 +1827,15 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): class ConversationView(discord.ui.View): - def __init__(self, ctx, converser_cog, id, model, from_edit_command=False, custom_api_key=None): + def __init__( + self, + ctx, + converser_cog, + id, + model, + from_edit_command=False, + custom_api_key=None, + ): super().__init__(timeout=3600) # 1 hour interval to redo. self.converser_cog = converser_cog self.ctx = ctx @@ -1803,7 +1843,12 @@ class ConversationView(discord.ui.View): self.from_edit_command = from_edit_command self.custom_api_key = custom_api_key self.add_item( - RedoButton(self.converser_cog, model, from_edit_command, custom_api_key=self.custom_api_key) + RedoButton( + self.converser_cog, + model, + from_edit_command, + custom_api_key=self.custom_api_key, + ) ) if id in self.converser_cog.conversation_threads: @@ -1890,7 +1935,7 @@ class RedoButton(discord.ui.Button["ConversationView"]): codex=codex, custom_api_key=self.custom_api_key, redo_request=True, - from_edit_command=self.from_edit_command + from_edit_command=self.from_edit_command, ) else: await interaction.response.send_message( diff --git a/cogs/image_prompt_optimizer.py b/cogs/image_prompt_optimizer.py index 5255137..37e9019 100644 --- a/cogs/image_prompt_optimizer.py +++ b/cogs/image_prompt_optimizer.py @@ -124,7 +124,12 @@ class ImgPromptOptimizer(discord.Cog, name="ImgPromptOptimizer"): ) self.converser_cog.redo_users[user.id] = RedoUser( - prompt=final_prompt, message=ctx, ctx=ctx, response=response_message, instruction=None, codex=False + prompt=final_prompt, + message=ctx, + ctx=ctx, + response=response_message, + instruction=None, + codex=False, ) self.converser_cog.redo_users[user.id].add_interaction(response_message.id) await response_message.edit( diff --git a/models/openai_model.py b/models/openai_model.py index e850be0..59b1d9d 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -399,28 +399,35 @@ class Model: max_tries=6, on_backoff=backoff_handler, ) - async def send_edit_request(self, instruction, input=None, temp_override=None, top_p_override=None, codex=False, custom_api_key=None): - + async def send_edit_request( + self, + instruction, + input=None, + temp_override=None, + top_p_override=None, + codex=False, + custom_api_key=None, + ): + # Validate that all the parameters are in a good state before we send the request if len(instruction) < self.prompt_min_length: raise ValueError( "Instruction must be greater than 8 characters, it is currently " + str(len(instruction)) ) - - print(f"The text about to be edited is [{input}] with instructions [{instruction}] codex [{codex}]") print( - f"Overrides -> temp:{temp_override}, top_p:{top_p_override}" + f"The text about to be edited is [{input}] with instructions [{instruction}] codex [{codex}]" ) - + print(f"Overrides -> temp:{temp_override}, top_p:{top_p_override}") + async with aiohttp.ClientSession(raise_for_status=True) as session: payload = { "model": Models.EDIT if codex is False else Models.CODE_EDIT, "input": "" if input is None else input, "instruction": instruction, "temperature": self.temp if temp_override is None else temp_override, - "top_p": self.top_p if top_p_override is None else top_p_override + "top_p": self.top_p if top_p_override is None else top_p_override, } headers = { "Content-Type": "application/json",