Skip to content

Commit

Permalink
Longer prompt issue patch
Browse files Browse the repository at this point in the history
Patched issue where long-form prompts would cause the bot to stall. This was caused due to the discord API expecting a response sooner than the OpenAI API could respond. Added a defer line + 3 second wait to account for this issue.
  • Loading branch information
Joeya1ds authored Dec 18, 2022
1 parent 31e1700 commit 02ef5e1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions GPTBot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import asyncio
import openai
import discord
from discord import app_commands
Expand All @@ -11,6 +12,7 @@
path = config['LOG_PATH']
openai.api_key = config['OPENAI_API_KEY']


class aclient(discord.Client):

def __init__(self):
Expand Down Expand Up @@ -39,10 +41,12 @@ async def on_ready(self):
client = aclient()
tree = app_commands.CommandTree(client)


@tree.command(name='ask', description='Ask the AI bot a question!')
async def ask(interaction: discord.Interaction, prompt: str):

user_id = interaction.user.id
await interaction.response.defer()

# Moderation API flagging
moderate = openai.Moderation.create(input=prompt)
Expand All @@ -56,8 +60,9 @@ async def ask(interaction: discord.Interaction, prompt: str):
logger.append_log(path, user_id, prompt)
logger.append_warning_log(path, user_id)
logger.append_newline_log(path, user_id)

await interaction.response.send_message('I cannot respond to what you have said, it has been flagged by the Moderation API.')

await asyncio.sleep(3)
await interaction.followup.send('I cannot respond to what you have said, it has been flagged by the Moderation API.')
print(f'User with ID: {user_id} has had a prompt flagged by the Moderation API. Consider checking logs.')

return
Expand All @@ -76,7 +81,8 @@ async def ask(interaction: discord.Interaction, prompt: str):
logger.append_token_log(path, user_id, openairesponse["usage"]["total_tokens"])
logger.append_newline_log(path, user_id)

await interaction.response.send_message(openairesponse['choices'][0]['text'])
await asyncio.sleep(3)
await interaction.followup.send(openairesponse['choices'][0]['text'])


client.run(config['DISCORD_BOT_TOKEN'])

0 comments on commit 02ef5e1

Please sign in to comment.