I created this simple Python program to allow me to more easily interact with the powerful OpenAI API, within a Discord server. Once you've setup a discord bot and an have obtained an OpenAI API key, this is incredibly easy to do.
- Direct communication with OpenAI text model within discord server
- Logging of all communications and tokens used in SQLite database
Below is a very simple example of how this can be achieved. For the full example, see docs/simple_test_client.py
:
@client.event
async def on_message(message):
# Prevents the bot from responding to itself (and creating an infinite loop of self-responding messages)
if message.author == client.user:
return
# Send a request to the OpenAI API for a response from the API, based on the received discord message
completion = openai.Completion.create(
engine=model_engine,
prompt=message.content,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5,
)
# get the first generated response. since we specified n=1, there is only one anyways
response = completion.choices[0].text
# send the AI's response on the same channel as the message was sent
await message.channel.send(response)
client.run(discord_token)
- Setup a discord bot and invite it to your server
- Setup an OpenAI account and obtain an API key
- Add the required variables in a file called
.env
. Use.env.example
as a template. - Run with or without docker (see "Running on AWS with Docker and Docker Compose" and "Running without Docker")
-
Create new AWS EC2 instance, connect to it using SSH, etc
-
Install docker and docker compose
-
Clone the project into a
discord-bot
folder -
Create a
data
folder in thediscort-bot
folder -
Give full write permissions for this directory. Otherwise the container cannot use its database in this folder:
sudo chmod a+w data
-
Start the containers:
docker compose up --build -d
I haven't tested this, but it should work. Note that this will be less secure.
- Ensure Python and PIP are installed.
- Install requirements:
python3 -m pip install -r requirements.txt
- Run the discord server:
python3 src/openai_discord_bot.py
write me a 5000 word report about Insert Topic Here. Use markdown-style formatting with headings for major sections and sub-sections. Include at least 5 major sections and at least 3 sub-sections within each major section. There should be an abstract section, an introduction section, and a conclusion section as well.
- Log number of tokens used in message and response separately
- Allow for usage of slash
/
commands from within discord server e.g./ask what is the linux kernel?
- Allow for OpenAI image model (and possibly other models) to be used as well e.g.
/createimage panda riding bicyle
- Better error handling. For example, if the OpenAI API sends an error response, the script is unable to respond properly. It should
send an error message to the
ERROR_CHANNEL_NAME
channel (as defined in.env
), but instead it causes the code to output an error (possibly due to synchronous code running slowly within async block?).