Skip to content

Commit

Permalink
Update to newer model, add name memory and customization through the …
Browse files Browse the repository at this point in the history
…messages list
  • Loading branch information
Loki-101 authored Mar 3, 2023
1 parent 674e4c2 commit d14d71f
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,48 @@
client = discord.Client(intents=discord.Intents.all())

# Function to generate a response to a message using the OpenAI API
def generate_response(message):
def generate_response(message, identifier):
try:
response = openai.Completion.create(
engine="text-davinci-003",
prompt=(f"{message}\n"),
max_tokens=3000, # Max Tokens in any request for this model is 4097, but the prompt also counts towards this. If you want a larger prompt and smaller response, shrink this value. If you want a shorter prompt but a larger response, increase this value.
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": f"You are a helpful assistant named {bot_name}."},
{"role": "user", "content": message},
{"role": "assistant", "content": ""}
],
max_tokens=2000,
temperature=0.5,
top_p=1
top_p=1,
frequency_penalty=0,
presence_penalty=0,
user=f"{identifier}"
)
except Exception as e:
if debug:
print("Error Occured: ",e)
return e
if debug:
print("API Response: ", json.dumps(response, indent=2))
return response.choices[0].text
return response.choices[0].message.content

# Event listener for when a message is received on Discord
@client.event
async def on_message(message):
if message.author == client.user:
return
lowercase_content = message.content.lower()
if lowercase_content.startswith(f"hey {bot_name}") \
and str(message.channel.id) == str(channel_id):
if lowercase_content.startswith(f"hey {bot_name}"):
print("User: ", f"{message.author.name}, {message.author.id}")
if debug:
print("Original prompt: ", message.content)
message_parts = lowercase_content.split(f"hey {bot_name}")
new_prompt = message_parts[1].lstrip()
response = generate_response(new_prompt)
if response:
await message.channel.send(response)
if debug:
print("Prompt: ", new_prompt)
print("Original Prompt: ", message.content)
# Modify the original prompt
prompt = message.content[len(f"hey {bot_name}"):].lstrip(", ")
if len(prompt) > 0 and prompt[0].isalpha():
prompt = prompt[0].upper() + prompt[1:]
print("Prompt: ", prompt)
# Call generate_response with the modified prompt
response = generate_response(prompt, message.author.id)
# Send the response
await message.channel.send(response)

client.run(discord_bot_token)

0 comments on commit d14d71f

Please sign in to comment.