Skip to content

Commit

Permalink
Patch Updates to Main File
Browse files Browse the repository at this point in the history
- Fixed issue with logging non-standard Unicode characters in username (removed username in logs).
- Resolved issue with logging prompts when prompt is flagged by Moderation API by refactoring log code into multiple functions.
- Added logged moderation prompts to be more clear when flagged by API
  • Loading branch information
Joeya1ds authored Dec 18, 2022
1 parent ae010af commit c18f7ea
Showing 1 changed file with 48 additions and 25 deletions.
73 changes: 48 additions & 25 deletions GPTBot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,31 @@
path = config['LOG_PATH']
openai.api_key = config['OPENAI_API_KEY']


class aclient(discord.Client):

def __init__(self):
super().__init__(intents=discord.Intents.all())
self.synced = False

async def on_ready(self):

await self.wait_until_ready()
await self.change_presence(activity = discord.Activity(
type = discord.ActivityType.listening,

await self.change_presence(activity=discord.Activity(
type=discord.ActivityType.listening,
name='your prompts!'
))

if not self.synced:
await tree.sync()
self.synced = True

print('Logged in as:')
print(f'{self.user.name}, {self.user.id}')
print('Created by Joeyy#4628. The most up-to-date code can be found on github: https://github.com/Joeya1ds/Discord-GPT3-Bot')
print('--------------------------------------------------------------------------------------------------------------------')
print(
'Created by Joeyy#4628. The most up-to-date code can be found on github: https://github.com/Joeya1ds/Discord-GPT3-Bot')
print(
'--------------------------------------------------------------------------------------------------------------------')


client = aclient()
Expand All @@ -41,36 +43,57 @@ async def on_ready(self):

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

username = interaction.user.name
user = interaction.user.id

# Moderation API flagging and response creation
moderate = openai.Moderation.create(input = prompt)
# Moderation API flagging
moderate = openai.Moderation.create(input=prompt)
flagged = moderate['results'][0]['flagged']

# Logging user message functions
def createlog():
if not os.path.exists(f'{path}/{user}'):
print(f'User with ID: {user} does not have a log file created. Creating.')
os.makedirs(f'{path}/{user}')

def userlog():
with open(f'{path}/{user}/{user}.txt', 'a') as f:
f.write(f'{user} wrote: "{prompt}"')

def userlogToken():
with open(f'{path}/{user}/{user}.txt', 'a') as f:
f.write(f' using {openairesponse["usage"]["total_tokens"]} tokens.')

def userlogWarn():
with open(f'{path}/{user}/{user}.txt', 'a') as f:
f.write(f' and was flagged by the OpenAI Moderation API!')

def userlogNL():
with open(f'{path}/{user}/{user}.txt', 'a') as f:
f.write(f'\n')

# Functions for generating and sending bot responses to chat messages
if flagged:
createlog()
userlog()
userlogWarn()
userlogNL()
await interaction.response.send_message('I cannot respond to what you have said, it has been flagged by the Moderation API.')
print(f'User {username} with ID: {user} has had a prompt flagged by the Moderation API. Consider checking logs.')
print(f'User with ID: {user} has had a prompt flagged by the Moderation API. Consider checking logs.')
return

openairesponse = openai.Completion.create(
engine='text-davinci-003',
prompt=prompt,
max_tokens=100,
max_tokens=200,
temperature=0.8,
top_p=0.8,
)

await interaction.response.send_message(openairesponse['choices'][0]['text'])

# Logging User Messages inside text file
if not os.path.exists(f'{path}/{user}'):
print(f'User {username} with ID: {user} does not have a log file created. Creating.')
os.makedirs(f'{path}/{user}')

with open(f'{path}/{user}/{user}.txt', 'a') as f:
f.write(f'User {username} wrote: {prompt} using {openairesponse["usage"]["total_tokens"]} tokens.\n')
createlog()
userlog()
userlogToken()
userlogNL()
await interaction.response.send_message(openairesponse['choices'][0]['text'])


client.run(config['DISCORD_BOT_TOKEN'])

0 comments on commit c18f7ea

Please sign in to comment.