From b261aa6df43cf4ae0b83b1e07dce1937a5107a52 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 12 Jan 2023 12:42:50 +0000 Subject: [PATCH] Add codex to the edit command --- README.md | 2 +- cogs/gpt_3_commands_and_converser.py | 51 ++++++++++++++++++++-------- models/openai_model.py | 14 +++----- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index f4cff45..d2f7193 100644 --- a/README.md +++ b/README.md @@ -70,7 +70,7 @@ These commands are grouped, so each group has a prefix but you can easily tab co `/gpt ask ` Ask the GPT3 Davinci 003 model a question. Optional overrides available -`/gpt edit ` Use the bot to edit text using the given instructions for how to do it, currently an alpha openai feature so results might vary +`/gpt edit ` Use the bot to edit text using the given instructions for how to do it, currently an alpha openai feature so results might vary. Codex uses a model trained on code. Editing is currently free `/gpt converse` - Start a conversation with the bot, like ChatGPT diff --git a/cogs/gpt_3_commands_and_converser.py b/cogs/gpt_3_commands_and_converser.py index f4b1ea9..e9da996 100644 --- a/cogs/gpt_3_commands_and_converser.py +++ b/cogs/gpt_3_commands_and_converser.py @@ -859,6 +859,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): from_g_command=False, instruction=None, from_edit_command=False, + codex=False, custom_api_key=None, edited_request=False, redo_request=False, @@ -867,7 +868,10 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): from_context = isinstance(ctx, discord.ApplicationContext) - tokens = self.usage_service.count_tokens(new_prompt) + 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) try: @@ -1037,10 +1041,12 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): # Send the request to the model if from_edit_command: response = await self.model.send_edit_request( - new_prompt, - instruction, - temp_override, - top_p_override, + input=new_prompt, + instruction=instruction, + temp_override=temp_override, + top_p_override=top_p_override, + codex=codex, + custom_api_key=custom_api_key, ) else: response = await self.model.send_request( @@ -1056,10 +1062,17 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): # Clean the request response response_text = self.cleanse_response(str(response["choices"][0]["text"])) - if from_g_command or from_edit_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}" + 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}" + elif from_edit_command: + if codex: + response_text = response_text.strip() + response_text = f"Prompt:{prompt}\nInstruction:{instruction}\n\n```\n{response_text}\n```" + else: + response_text = response_text.strip() + response_text = f"Prompt:{prompt}\nInstruction:{instruction}\n\n{response_text}\n" # If gpt3 tries writing a user mention try to replace it with their name response_text = await self.mention_to_username(ctx, response_text) @@ -1278,10 +1291,10 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): guild_ids=ALLOWED_GUILDS, ) @discord.option( - name="input", description="The text you want to edit", required=True + name="instruction", description="How you want GPT3 to edit the text", required=True ) @discord.option( - name="instruction", description="How you want GPT3 to edit the text", required=True + name="input", description="The text you want to edit, can be empty", required=False, default="" ) @discord.option( name="temperature", @@ -1299,17 +1312,24 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): min_value=0, max_value=1, ) + @discord.option( + name="codex", description="Enable codex version", required=False + ) @discord.guild_only() async def edit( self, ctx: discord.ApplicationContext, - prompt: str, instruction: str, + input: str, temperature: float, top_p: float, + codex: bool, ): user = ctx.user - prompt = await self.mention_to_username(ctx, prompt.strip()) + + 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: @@ -1321,12 +1341,13 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): await self.encapsulated_send( user.id, - prompt, - ctx, + prompt=input, + ctx=ctx, temp_override=temperature, top_p_override=top_p, instruction=instruction, from_edit_command=True, + codex=codex, custom_api_key=user_api_key, ) diff --git a/models/openai_model.py b/models/openai_model.py index 1a660fe..cd9752a 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -26,6 +26,7 @@ class Models: CURIE = "text-curie-001" EMBEDDINGS = "text-embedding-ada-002" EDIT = "text-davinci-edit-001" + CODE_EDIT = "code-davinci-edit-001" class ImageSize: @@ -374,14 +375,9 @@ class Model: traceback.print_exc() return - async def send_edit_request(self, text, instruction, temp_override=None, top_p_override=None, 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(text) < self.prompt_min_length: - raise ValueError( - "Prompt must be greater than 8 characters, it is currently " - + str(len(text)) - ) if len(instruction) < self.prompt_min_length: raise ValueError( "Instruction must be greater than 8 characters, it is currently " @@ -389,15 +385,15 @@ class Model: ) - print(f"The text about to be edited is [{text}] with instructions [{instruction}]") + print(f"The text about to be edited is [{input}] with instructions [{instruction}]") print( f"Overrides -> temp:{temp_override}, top_p:{top_p_override}" ) async with aiohttp.ClientSession() as session: payload = { - "model": Models.EDIT, - "input": text, + "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