Skip to content

Python based ChatGPT bot Integrations (Slack, Discord, CLI)

Notifications You must be signed in to change notification settings

protella/chatgpt-bots

Repository files navigation

ChatGPT Bots

Python based ChatGPT bot integrations

Notice - Focusing on Slackbot over Discord and CLI for now.

The Discord client is a little behind as I've spent the majority of time working on the Slack version. It should still work, but it's missing a lot of the functionality included in the Slack version. The CLI version is very basic as it's only text based. All clients have basic threading support, though individual thread histories are only supported in the Slack client for now.

Description

ChatBot Integrations for Slack, Discord, and the CLI using Python and OpenAPI's ChatGPT platform. This bot is designed around GPT4 and supports GPT4 Vision and Dalle-3.

New Features!

The Slackbot now allows iteration on Dalle-3 images and will also determine if image creation is the appropriate action by using NLP. Talk to it just like you would with the ChatGPT website. Upload (multiple) images and have discussions or conduct analysis on them all in a single conversation (Slack Thread).

Dev Note: I've tested this version for a month or two now and I've had very good success with using NLP to determine whether an image request is intended without explicitly asking for one. I was hesitant to merge this branch into master as it added an entirely new api request for the image gen check as well as a third for generating a behind-the-scenes image prompt that includes the previous conversation history as part of its context (this is how you can iterate on an image). These additional api requests added a bit more latency than felt acceptable, but this has been remedied by OpenAI's launch of the new GPT-4o model which is twice as fast (and 50% cheaper!).

Getting Started

Requires Python 3.10+ as the script takes advantage of the new structural pattern matching (match/case) in this version.

The setup of a Slack or Discord App is out of scope of this README. There's plenty of documentation online detailing these processes.

The Slack event subscriptions and scope are as follows:

Event Name Description Required Scope
app_mention Subscribe to only the message events that mention your app or bot app_mentions:read
message.im A message was posted in a direct message channel im:history

Slack OAuth & Permissions (Scopes):

Scope
app_mentions:read
channels:history
channels:join
chat:write
chat:write.customize
commands
files:read
files:write
im:history
im:read
im:write

Discord OAuth2

image


Install venv module if you don't already have it

python3 -m pip install --user venv

Clone the repository

git clone https://github.com/protella/chatgpt-bots

Create and Activate the Virtual Environment

cd chatgpt-bots
python3 -m venv chatbots
source chatbots/bin/activate

Installing Dependencies:

python3 -m pip install -U -r requirements.txt

  • Note: The included requirements.txt file includes all of the dependencies for all 3 clients in this repo.

Setup .env file

  • Aquire the necessary keys or tokens from the integration you're using. I.e., OpenAI, Slack and Discord tokens. The only required token is the OPENAI_KEY. The others depend on which integration you're using.

  • Create a .env file in the root folder of the repo and populate it with your keys and tokens as follows:

SLACK_BOT_TOKEN = 'YOURTOKENHERE'
SLACK_APP_TOKEN = 'YOURTOKENHERE'
OPENAI_KEY = 'YOURTOKENHERE'
DISCORD_TOKEN = 'YOURTOKENHERE'

Configuration - Bot Tuning

The SYSTEM_PROMPT variable at the top of each script will set the tone for how the bot will respond. Telling it that it is a chatbot and with any specific style of responses will help with more appropriate responses.

Running the bot

Run the py file for your chosen interface, e.g.:

  • python3 discordbot.py
  • python3 slackbot.py
  • python3 cli_bot.py

ToDo:

  • Fix bug w/ thread history rebuilds and Image gen check. Need to compare pre-post restart histories.
  • Work on prompts to minimize the "I can't do this or that" from the model.
  • Cleanup new code around Dalle3 NLP Image gen.
  • Slack threads and thread history are working. Discord is still a shared history.
  • Add command functionality to allow for changing the initial chatbot init phrase
  • Update bot commands to use Slack/Discord's /command functionality rather than old school !commands
  • Track context/history size using the usage stats and pop old items from the history to avoid going over the model's max context size (4k w/ 3.5-turbo but not as much of an issue with GPT4 Turbo) Adjust for different models if necessary. Lower Priority
  • Add support for the bot to recognize individual users within a mult-user conversation.
  • With new Thread History Reloading from Slack, figure out what to do with messages in the Slack history that were not processed due to the bot being busy at the time. They show up in the history during a reload.
  • Fix usage stats function. Decide how/what to track. Global stats or conversation stats, or both?
  • Clean up code, standardize style, move repeated client code to functions and utility modules.
  • Create Slack app manifest file
  • Setup Github workflows and unit tests