From 5de0fd37da49cceff2eefea564c6fe71c3b904cf Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 30 Jan 2023 20:30:28 +0000 Subject: [PATCH 1/8] Conversation command now has a model option Added model autocomplete --- cogs/commands.py | 9 +++++++++ cogs/text_service_cog.py | 4 +++- models/autocomplete_model.py | 11 +++++++++++ models/openai_model.py | 4 +++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/cogs/commands.py b/cogs/commands.py index ef48595..f28c4bf 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -422,6 +422,13 @@ class Commands(discord.Cog, name="Commands"): required=False, default=False, ) + @discord.option( + name="model", + description="Which model to use with the bot", + required=False, + default=False, + autocomplete=Settings_autocompleter.get_models, + ) @discord.option( name="temperature", description="Higher values means the model will take more risks", @@ -462,6 +469,7 @@ class Commands(discord.Cog, name="Commands"): opener_file: str, private: bool, minimal: bool, + model: str, temperature: float, top_p: float, frequency_penalty: float, @@ -473,6 +481,7 @@ class Commands(discord.Cog, name="Commands"): opener_file, private, minimal, + model, temperature, top_p, frequency_penalty, diff --git a/cogs/text_service_cog.py b/cogs/text_service_cog.py index 88fb3bb..b79d36a 100644 --- a/cogs/text_service_cog.py +++ b/cogs/text_service_cog.py @@ -801,6 +801,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): opener_file: str, private: bool, minimal: bool, + model: str, temperature: float, top_p: float, frequency_penalty: float, @@ -814,6 +815,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): opener_file (str): A .txt or .json file which is appended before the opener private (bool): If the thread should be private minimal (bool): If a minimal starter should be used + model (str): The openai model that should be used temperature (float): Sets the temperature override top_p (float): Sets the top p override frequency_penalty (float): Sets the frequency penalty override @@ -866,7 +868,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ) self.conversation_threads[thread.id] = Thread(thread.id) - self.conversation_threads[thread.id].model = self.model.model + self.conversation_threads[thread.id].model = self.model.model if not model else model # Set the overrides for the conversation self.conversation_threads[thread.id].set_overrides( diff --git a/models/autocomplete_model.py b/models/autocomplete_model.py index 8525cb8..c89acfd 100644 --- a/models/autocomplete_model.py +++ b/models/autocomplete_model.py @@ -86,6 +86,17 @@ class Settings_autocompleter: await ctx.interaction.response.defer() # defer so the autocomplete in int values doesn't error but rather just says not found return [] + async def get_models( + ctx: discord.AutocompleteContext, + ): + """Gets all models""" + return [ + value + for value in Models.TEXT_MODELS + if value.startswith(ctx.value.lower()) + ] + + async def get_value_moderations( ctx: discord.AutocompleteContext, ): # Behaves a bit weird if you go back and edit the parameter without typing in a new command diff --git a/models/openai_model.py b/models/openai_model.py index 03c5d28..da66744 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -44,6 +44,7 @@ class Override: class Models: # Text models DAVINCI = "text-davinci-003" + DAVINCI_FT = "davinci:ft-personal-2023-01-28-08-02-00" CURIE = "text-curie-001" BABBAGE = "text-babbage-001" ADA = "text-ada-001" @@ -60,7 +61,7 @@ class Models: CODE_EDIT = "code-davinci-edit-001" # Model collections - TEXT_MODELS = [DAVINCI, CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] + TEXT_MODELS = [DAVINCI, DAVINCI_FT,CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] EDIT_MODELS = [EDIT, CODE_EDIT] DEFAULT = DAVINCI @@ -69,6 +70,7 @@ class Models: # Tokens Mapping TOKEN_MAPPING = { "text-davinci-003": 4024, + "davinci:ft-personal-2023-01-28-08-02-00": 4042, "text-curie-001": 2024, "text-babbage-001": 2024, "text-ada-001": 2024, From 02b2bb97d87bd2e01508eabeee3568803698dbe6 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 30 Jan 2023 21:31:51 +0000 Subject: [PATCH 2/8] Change backoff handling to be more verbose --- models/openai_model.py | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/models/openai_model.py b/models/openai_model.py index da66744..760cecc 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -578,11 +578,16 @@ class Model: self._prompt_min_length = value SETTINGS_DB["prompt_min_length"] = value - def backoff_handler(details): + def backoff_handler_http(details): print( f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | " f"{details['exception'].status}: {details['exception'].message}" ) + def backoff_handler_request(details): + print( + f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | " + f"{details['exception'].args}" + ) async def valid_text_request(self, response): try: @@ -600,7 +605,7 @@ class Model: factor=3, base=5, max_tries=4, - on_backoff=backoff_handler, + on_backoff=backoff_handler_http, ) async def send_embedding_request(self, text, custom_api_key=None): async with aiohttp.ClientSession(raise_for_status=True) as session: @@ -626,11 +631,11 @@ class Model: @backoff.on_exception( backoff.expo, - aiohttp.ClientResponseError, + ValueError, factor=3, base=5, - max_tries=6, - on_backoff=backoff_handler, + max_tries=4, + on_backoff=backoff_handler_request, ) async def send_edit_request( self, @@ -679,7 +684,7 @@ class Model: factor=3, base=5, max_tries=6, - on_backoff=backoff_handler, + on_backoff=backoff_handler_http, ) async def send_moderations_request(self, text): # Use aiohttp to send the above request: @@ -698,11 +703,11 @@ class Model: @backoff.on_exception( backoff.expo, - aiohttp.ClientResponseError, + ValueError, factor=3, base=5, max_tries=4, - on_backoff=backoff_handler, + on_backoff=backoff_handler_request, ) async def send_summary_request(self, prompt, custom_api_key=None): """ @@ -749,11 +754,11 @@ class Model: @backoff.on_exception( backoff.expo, - aiohttp.ClientResponseError, + ValueError, factor=3, base=5, max_tries=4, - on_backoff=backoff_handler, + on_backoff=backoff_handler_request, ) async def send_request( self, @@ -783,14 +788,14 @@ class Model: f"Overrides -> temp:{temp_override}, top_p:{top_p_override} frequency:{frequency_penalty_override}, presence:{presence_penalty_override}" ) - async with aiohttp.ClientSession(raise_for_status=True) as session: + async with aiohttp.ClientSession(raise_for_status=False) as session: payload = { "model": self.model if model is None else model, "prompt": prompt, "stop": "" if stop is None else stop, "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, - "max_tokens": self.max_tokens - tokens + "max_tokens": self.max_tokens if not model else Models.get_max_tokens(model) - tokens if not max_tokens_override else max_tokens_override, "presence_penalty": self.presence_penalty @@ -844,7 +849,7 @@ class Model: factor=3, base=5, max_tries=4, - on_backoff=backoff_handler, + on_backoff=backoff_handler_http, ) async def send_image_request( self, ctx, prompt, vary=None, custom_api_key=None From 95dd054f1cf9a4f3dd87433db19fa303887bd524 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 30 Jan 2023 21:32:56 +0000 Subject: [PATCH 3/8] Remove finetune stuff --- models/openai_model.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/models/openai_model.py b/models/openai_model.py index 760cecc..24ebcc6 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -44,7 +44,6 @@ class Override: class Models: # Text models DAVINCI = "text-davinci-003" - DAVINCI_FT = "davinci:ft-personal-2023-01-28-08-02-00" CURIE = "text-curie-001" BABBAGE = "text-babbage-001" ADA = "text-ada-001" @@ -61,7 +60,7 @@ class Models: CODE_EDIT = "code-davinci-edit-001" # Model collections - TEXT_MODELS = [DAVINCI, DAVINCI_FT,CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] + TEXT_MODELS = [DAVINCI, CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] EDIT_MODELS = [EDIT, CODE_EDIT] DEFAULT = DAVINCI @@ -70,7 +69,6 @@ class Models: # Tokens Mapping TOKEN_MAPPING = { "text-davinci-003": 4024, - "davinci:ft-personal-2023-01-28-08-02-00": 4042, "text-curie-001": 2024, "text-babbage-001": 2024, "text-ada-001": 2024, From 71125ce65da4ce62eb468b7ee76ef9457c1c7b93 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 30 Jan 2023 21:50:18 +0000 Subject: [PATCH 4/8] Add ephemeral option to ask and edit --- cogs/commands.py | 16 ++++++++++++++-- cogs/text_service_cog.py | 6 ++++-- models/openai_model.py | 6 +++--- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cogs/commands.py b/cogs/commands.py index f28c4bf..093d5a5 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -301,6 +301,11 @@ class Commands(discord.Cog, name="Commands"): @discord.option( name="prompt", description="The prompt to send to GPT3", required=True ) + @discord.option( + name="ephemeral", + description="Will only be visible to you", + required=False + ) @discord.option( name="temperature", description="Higher values means the model will take more risks", @@ -334,13 +339,14 @@ class Commands(discord.Cog, name="Commands"): self, ctx: discord.ApplicationContext, prompt: str, + ephemeral: bool, temperature: float, top_p: float, frequency_penalty: float, presence_penalty: float, ): await self.converser_cog.ask_command( - ctx, prompt, temperature, top_p, frequency_penalty, presence_penalty + ctx, prompt, ephemeral, temperature, top_p, frequency_penalty, presence_penalty ) @add_to_group("gpt") @@ -360,6 +366,11 @@ class Commands(discord.Cog, name="Commands"): required=False, default="", ) + @discord.option( + name="ephemeral", + description="Will only be visible to you", + required=False + ) @discord.option( name="temperature", description="Higher values means the model will take more risks", @@ -385,12 +396,13 @@ class Commands(discord.Cog, name="Commands"): ctx: discord.ApplicationContext, instruction: str, text: str, + ephemeral: bool, temperature: float, top_p: float, codex: bool, ): await self.converser_cog.edit_command( - ctx, instruction, text, temperature, top_p, codex + ctx, instruction, text, ephemeral, temperature, top_p, codex ) @add_to_group("gpt") diff --git a/cogs/text_service_cog.py b/cogs/text_service_cog.py index b79d36a..2e01125 100644 --- a/cogs/text_service_cog.py +++ b/cogs/text_service_cog.py @@ -693,6 +693,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): self, ctx: discord.ApplicationContext, prompt: str, + ephemeral: bool, temperature: float, top_p: float, frequency_penalty: float, @@ -720,7 +721,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): if not user_api_key: return - await ctx.defer() + await ctx.defer(ephemeral=ephemeral) overrides = Override(temperature, top_p, frequency_penalty, presence_penalty) @@ -741,6 +742,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ctx: discord.ApplicationContext, instruction: str, text: str, + ephemeral: bool, temperature: float, top_p: float, codex: bool, @@ -766,7 +768,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): if not user_api_key: return - await ctx.defer() + await ctx.defer(ephemeral=ephemeral) overrides = Override(temperature, top_p, 0, 0) diff --git a/models/openai_model.py b/models/openai_model.py index 24ebcc6..338df39 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -584,7 +584,7 @@ class Model: def backoff_handler_request(details): print( f"Backing off {details['wait']:0.1f} seconds after {details['tries']} tries calling function {details['target']} | " - f"{details['exception'].args}" + f"{details['exception'].args[0]}" ) async def valid_text_request(self, response): @@ -657,7 +657,7 @@ class Model: ) print(f"Overrides -> temp:{temp_override}, top_p:{top_p_override}") - async with aiohttp.ClientSession(raise_for_status=True) as session: + async with aiohttp.ClientSession(raise_for_status=False) as session: payload = { "model": Models.EDIT if codex is False else Models.CODE_EDIT, "input": "" if text is None else text, @@ -724,7 +724,7 @@ class Model: tokens = self.usage_service.count_tokens(summary_request_text) - async with aiohttp.ClientSession(raise_for_status=True) as session: + async with aiohttp.ClientSession(raise_for_status=False) as session: payload = { "model": Models.DAVINCI, "prompt": summary_request_text, From 669e3f0191ccda5de97a8eb0f600dc30c7ef8366 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Mon, 30 Jan 2023 22:53:20 +0000 Subject: [PATCH 5/8] Changed how max tokens are fetched lowered from 4024 to 2024 if max tokens can't be gotten --- models/openai_model.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/models/openai_model.py b/models/openai_model.py index 338df39..45e97b4 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -45,6 +45,7 @@ class Models: # Text models DAVINCI = "text-davinci-003" CURIE = "text-curie-001" + BABBAGES = "babbage" BABBAGE = "text-babbage-001" ADA = "text-ada-001" @@ -60,7 +61,7 @@ class Models: CODE_EDIT = "code-davinci-edit-001" # Model collections - TEXT_MODELS = [DAVINCI, CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] + TEXT_MODELS = [DAVINCI, CURIE, BABBAGES, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] EDIT_MODELS = [EDIT, CODE_EDIT] DEFAULT = DAVINCI @@ -78,7 +79,7 @@ class Models: @staticmethod def get_max_tokens(model: str) -> int: - return Models.TOKEN_MAPPING.get(model, 4024) + return Models.TOKEN_MAPPING.get(model, 2024) class ImageSize: @@ -781,6 +782,10 @@ class Model: f"Prompt must be greater than {self.prompt_min_length} characters, it is currently: {len(prompt)} characters" ) + if not max_tokens_override: + if model: + max_tokens_override = Models.get_max_tokens(model) - tokens + print(f"The prompt about to be sent is {prompt}") print( f"Overrides -> temp:{temp_override}, top_p:{top_p_override} frequency:{frequency_penalty_override}, presence:{presence_penalty_override}" @@ -793,8 +798,8 @@ class Model: "stop": "" if stop is None else stop, "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, - "max_tokens": self.max_tokens if not model else Models.get_max_tokens(model) - tokens - if not max_tokens_override + "max_tokens": self.max_tokens - tokens + if max_tokens_override is None else max_tokens_override, "presence_penalty": self.presence_penalty if presence_penalty_override is None From f48deab320e79bfb56da866d10f66c561eb8bb71 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Tue, 31 Jan 2023 10:01:49 +0000 Subject: [PATCH 6/8] Forgot to remove extra models --- models/openai_model.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/models/openai_model.py b/models/openai_model.py index 45e97b4..ead92ca 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -45,7 +45,6 @@ class Models: # Text models DAVINCI = "text-davinci-003" CURIE = "text-curie-001" - BABBAGES = "babbage" BABBAGE = "text-babbage-001" ADA = "text-ada-001" @@ -61,7 +60,7 @@ class Models: CODE_EDIT = "code-davinci-edit-001" # Model collections - TEXT_MODELS = [DAVINCI, CURIE, BABBAGES, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] + TEXT_MODELS = [DAVINCI, CURIE, BABBAGE, ADA, CODE_DAVINCI, CODE_CUSHMAN] EDIT_MODELS = [EDIT, CODE_EDIT] DEFAULT = DAVINCI From 5be9971b26d2859123cbb5cefc7daa06f5250c69 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Wed, 1 Feb 2023 15:29:45 +0000 Subject: [PATCH 7/8] Set ephemeral option to be named private --- cogs/commands.py | 12 ++++++------ cogs/text_service_cog.py | 8 ++++---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/cogs/commands.py b/cogs/commands.py index 093d5a5..2e43229 100644 --- a/cogs/commands.py +++ b/cogs/commands.py @@ -302,7 +302,7 @@ class Commands(discord.Cog, name="Commands"): name="prompt", description="The prompt to send to GPT3", required=True ) @discord.option( - name="ephemeral", + name="private", description="Will only be visible to you", required=False ) @@ -339,14 +339,14 @@ class Commands(discord.Cog, name="Commands"): self, ctx: discord.ApplicationContext, prompt: str, - ephemeral: bool, + private: bool, temperature: float, top_p: float, frequency_penalty: float, presence_penalty: float, ): await self.converser_cog.ask_command( - ctx, prompt, ephemeral, temperature, top_p, frequency_penalty, presence_penalty + ctx, prompt, private, temperature, top_p, frequency_penalty, presence_penalty ) @add_to_group("gpt") @@ -367,7 +367,7 @@ class Commands(discord.Cog, name="Commands"): default="", ) @discord.option( - name="ephemeral", + name="private", description="Will only be visible to you", required=False ) @@ -396,13 +396,13 @@ class Commands(discord.Cog, name="Commands"): ctx: discord.ApplicationContext, instruction: str, text: str, - ephemeral: bool, + private: bool, temperature: float, top_p: float, codex: bool, ): await self.converser_cog.edit_command( - ctx, instruction, text, ephemeral, temperature, top_p, codex + ctx, instruction, text, private, temperature, top_p, codex ) @add_to_group("gpt") diff --git a/cogs/text_service_cog.py b/cogs/text_service_cog.py index 2e01125..8f01b84 100644 --- a/cogs/text_service_cog.py +++ b/cogs/text_service_cog.py @@ -693,7 +693,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): self, ctx: discord.ApplicationContext, prompt: str, - ephemeral: bool, + private: bool, temperature: float, top_p: float, frequency_penalty: float, @@ -721,7 +721,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): if not user_api_key: return - await ctx.defer(ephemeral=ephemeral) + await ctx.defer(ephemeral=private) overrides = Override(temperature, top_p, frequency_penalty, presence_penalty) @@ -742,7 +742,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): ctx: discord.ApplicationContext, instruction: str, text: str, - ephemeral: bool, + private: bool, temperature: float, top_p: float, codex: bool, @@ -768,7 +768,7 @@ class GPT3ComCon(discord.Cog, name="GPT3ComCon"): if not user_api_key: return - await ctx.defer(ephemeral=ephemeral) + await ctx.defer(ephemeral=private) overrides = Override(temperature, top_p, 0, 0) From 945d3db7de5c726beb7c4a2a4b96830c9b14e8c2 Mon Sep 17 00:00:00 2001 From: Kaveen Kumarasinghe Date: Fri, 3 Feb 2023 00:16:35 -0500 Subject: [PATCH 8/8] bump version --- gpt3discord.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gpt3discord.py b/gpt3discord.py index 77356d9..9db8afb 100644 --- a/gpt3discord.py +++ b/gpt3discord.py @@ -30,7 +30,7 @@ from services.environment_service import EnvService from models.openai_model import Model -__version__ = "9.0.1" +__version__ = "9.1" PID_FILE = Path("bot.pid")