Add codex to the edit command

Rene Teigen 1 year ago
parent 4efb82a033
commit b261aa6df4

@ -70,7 +70,7 @@ These commands are grouped, so each group has a prefix but you can easily tab co
`/gpt ask <prompt> <temp> <top_p> <frequency penalty> <presence penalty>` Ask the GPT3 Davinci 003 model a question. Optional overrides available
`/gpt edit <text> <instruction> <temp> <top_p>` 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 <instruction> <input> <temp> <top_p> <codex>` 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

@ -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,
)

@ -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

Loading…
Cancel
Save