A Discord bot written in Python that uses the assistants API to have conversations with the gpt-4
model.
This bot uses the OpenAI Python Library and discord.py.
It is designed to facilitate interactive learning and engagement through group conversations. It is especially suitable for group learning, as making prompts visible in public threads allows everyone in the channel to learn together.
-
/build
initiates an assistant creation process, with aname
argument. Users can define the assistant's name, description, and instructions in a guided, interactive thread. -
/update
initiates an assistant update process, with aassistant_id
argument. Users can redefine the assistant's description and instructions. -
/chat
starts a public thread where you can select an assistant for the chat. The assistant will generate a reply for every user message in the thread. -
Each Discord thread is linked to an OpenAI thread and an OpenAI assistant. Context window management is handled inside the API.
-
Supports multi-user interaction. The bot can recognize individual users in a thread and generate responses accordingly.
-
You can change the model, the default value is
gpt-4
. Please set it togpt-4-turbo-preview
if you want to usefiles (knowledge retrieval)
orcode interpreter
.
-
Clone the repository
git clone https://github.com/KeioAIConsortium/gpt-discord-bot.git cd gpt-discord-bot
-
Copy
.env.example
to.env
and start filling in the values as detailed below:- Go to https://beta.openai.com/account/api-keys, create a new API key, and fill in
OPENAI_API_KEY
- Create your own Discord application at https://discord.com/developers/applications
- Go to the Bot tab and click "Add Bot"
- Click "Reset Token" and fill in
DISCORD_BOT_TOKEN
- Disable "Public Bot" unless you want your bot to be visible to everyone
- Enable "Message Content Intent" under "Privileged Gateway Intents"
- Click "Reset Token" and fill in
- Go to the OAuth2 tab, copy your "Client ID", and fill in
DISCORD_CLIENT_ID
- Copy the ID the server you want to allow your bot to be used in by right clicking the server icon and clicking "Copy ID". Fill in
ALLOWED_SERVER_IDS
. If you want to allow multiple servers, separate the IDs by "," likeserver_id_1,server_id_2
- Go to https://beta.openai.com/account/api-keys, create a new API key, and fill in
-
Install dependencies and run the bot
pip install -r requirements.txt python -m src.main
You should see an invite URL in the console. Copy and paste it into your browser to add the bot to your server.
Note: make sure you are using Python 3.9+ (check with python --version
)
The bot operates via slash commands. Type /
in a text channel to view available commands.
-
/build
: Initiates an assistant creation process. Users can define the assistant's name, description, and instructions in a guided, interactive thread. Users can also specify tools, such as file retrieval or code interpreter. -
/update
: Initiates an assistant update process. Users can redefine the assistant's description and instructions in a guided, interactive thread. If users do not want to change any of these, they can specify '.' to indicate no change. Users can also change tools and add or remove files. -
/show
: Shows the configuration of the specified assistant. If the content is long (>1,500 characters), the response message will be split. -
/list
: Displays a list of the 20 newest assistants (from the specified offset). If the content is long (>1,500 characters), the response message will be split. -
/delete
: Allows users to delete a specified assistant, with confirmations to prevent accidental deletions. -
/chat
: Starts a conversation in a thread. Each new user message is sent as a separate input to the OpenAI API. Users can select an assistant for the chat.
Note:
In this bot, users are distinguished by inputting their messages in the format username: message
. Therefore, when including custom formats in the system prompt, please keep this in mind and use the format username: ○○: ××
.
Most of this project were inspired by OpenAI's official GPT Discord bot.
This project is currently in development.