#Beginner-Friendly ChatGPT Discord Bot

110 messages ยท Page 1 of 1 (latest)

tough trout
#

Hey all,

I'm sharing the code for a simple, rudimentary Discord bot that you can ask questions to and get answers from for the GPT-3.5 and GPT-4 models.


I've been using my own Discord bot for months now, but I never put it out in public, mainly because it's rudimentary and bad. I decided to change that at 2am in the morning.

I cleaned up my code slightly, added comments to all of my code (with the help of a certain something), and turned it more or less into a nice learning resource for how to create a Discord bot, and how to utilize an API. It serves as a nice starting point for you, or other people, to turn this bot into something greater.

I personally use it to get away with asking GPT-4 things while in lectures, so it doesn't look like I'm blatantly using ChatGPT to the kid sitting right next to me.

It doesn't support back-and-forth chatting right now, simply because I never found the need for that- I'll implement it later, it's a quick and easy fix. I'll also implement other features as necessary, if anyone actually ends up using this repo.

You will need your own Discord bot token and OpenAI API key. If you don't have access to GPT-4, attempting to ask it a question will throw an error.

https://github.com/applebaconsoda123/chatgpt-discord-bot

GitHub

Python script for interacting with OpenAI's GPT-3.5 and GPT-4 models via a Discord bot. - GitHub - applebaconsoda123/chatgpt-discord-bot: Python script for interacting with OpenAI's...

#

(Rudimentary) ChatGPT Discord Bot

sudden cosmos
#

hi, does the command have limited input length?

#

like, if i do want to paste a full code of a file, would it work as an input? 1k chars let's say

tough trout
#

this is a limitation on the user side of discord, not OpenAI

#

there's lots of stupid easy ways to get around this, I just haven't found that necessary for my own use case since I just use a vscode extension

tough trout
#

just made some changes

  • If you upload a file, it'll get the plaintext from the file and add that on to the end of whatever input you send in the Discord message (you'll still have to tag the bot and write '/input' first)
    • If you upload multiple files, it'll only get the plaintext from the very first file
  • More commands, more presets
  • Better logger (idk why it wasn't even good in the first place)

todo

  • Add and revise comments
  • Overhaul code to make it more efficient/cleaner (the code is slowly becoming a guide on how to make your code as inefficient as possible and as unreadable as possible to your group team members)
sudden cosmos
#

cannot it just read and reply in 1 channel?

#

also congrats for the newly added features

#

also what I've seen by looking at some other bots is the "persona" feature. it has an initial system message, for example the one in the screenshot below. and the user can add more personas from the code. it would be cool if it can also read from text files to also be able to just reply in a channel or just with a tag instead of /input and /system..

#

so, this would be a predefined system message so it has a QA Analyst persona, and that persona could be selected. would this be hard ot make? ๐Ÿ˜„

tough trout
sudden cosmos
#

oh... okay

#

cannot wait to use it

sudden cosmos
#

will it take long to implement them?

tough trout
sudden cosmos
#

oh cool

tough trout
#

technically the 'persona' feature is already on the main branch but it's broken LOL

sudden cosmos
#

rlly want to use it xD

tough trout
#

no worries, that's fair

sudden cosmos
#

please ping me when you add those, especially reading from txt files

tough trout
#

๐Ÿ‘

#

actually I do need a quick clarification, what do you mean by text files? it can read basic plaintext from a text file atm

#

do you mean support for more than one file in one msg?

vernal kindle
#

Hallo

#

How are you today?

tough trout
#

hey, I'm doing well! what's up

tough trout
#

@sudden cosmos
I didn't thoroughly test the code but I spent an hour coding the changes up and moving everything into new files for better organization, very badly named files though

#

Note: You can change current preset personas in config.py.

Changes:

  • You can switch between (preset) personas now. /programmer_assistant, /eli5_assistant, /scientific_assistant, /general_assistant
  • You can upload multiple files now. Should support most file types, e.g. PDFs, Word Docs, (very un-professional implementation for doing so, c-p'd my old code from three years back). Note: corrupted files will raise errors that are not caught and not sent to the user in Discord, it'll just seem like the bot isn't responding
  • No longer need to use /system and /input; use /set_system_text for custom system text. To message with the bot, just tag the bot and send your message

To-do, short term:

  • Add more try/except blocks to keep user in the loop on any errors. There are currently big gaping gaps in the logging side of things
  • requirements.py is funky, I forgot to run pipreqs but I'm too lazy
  • add my actual project files in (i.e. yaml) instead of having caveman-like project (it's like having a working car, but you're missing all the extras like windows, cosmetics, center console...)
  • add ability to edit preset personas in discord
  • add ability to create your own preset personas in discord
  • add option to switch between having to tag bot vs bot replying to any message sent in a channel, tag or not
  • add comments to make code beginner-friendly
  • a few other things I can't remember
#

example of multiple files being uploaded:

#

Beginner-Friendly ChatGPT Discord Bot

sudden cosmos
#

also how do i set up the variables likes the discord bot token?

tough trout
#

You can also hardcode the values if you want by just setting the value as a string and c-p your discord token in, but that's not good practice for application security

sudden cosmos
#

i have a vps..

#

idk

#

running ubuntu

#

how should i hard-code them? where exactly

tough trout
sudden cosmos
#

made it

#

but the bot has shortened responses, why is that

dense mantle
#

got this error

sudden cosmos
#

mine works fine now

tough trout
# sudden cosmos but the bot has shortened responses, why is that

Shortened responses is because the system text specifically asks the bot to "be concise". I've found that telling it to "be concise" is the most effective way to save money in the long run since 99% of users will not be a corporation or startup, but rather someone who wants ease of use or to implement it in their own server using their very own money

tough trout
dense mantle
#

same error

USERNAME: J๐‘’๐‘’#8848
SYSTEM: You're a highly capable assistant trained to help users with every possible task. 
    - Be concise.
INPUT: how to cook

Warning: gpt-4 may change over time. Returning num tokens assuming gpt-4-0314.
2023-05-08 04:28:53 ERROR    ChatGPT_Discord_Bot -> module 'openai' has no attribute 'ChatCompletion'
2023-05-08 04:28:54 DEBUG    ChatGPT_Discord_Bot -> Deleted 'GPT-4 is thinking' message.
tough trout
dense mantle
tough trout
#

Do pip uninstall openai then reinstall with pip install openai

#

The version needs to be 0.27 or higher

dense mantle
#

its 26.5 again

dense mantle
tough trout
#

this is a general python issue afaik

#

oh woops I'm dumb. activate the venv first (source /home/runner/chatgpt-discord-bot/venv/bin/activate), uninstall it, then reinstall it again. make sure to specify version with openai==0.27.6 just to be sure

dense mantle
#

It now workss. I change /set_model to 3.5

#

Thankss sir @tough trout ๐Ÿซถ

tough trout
sudden cosmos
#

also /help is not working

#

and.. does chat history consume more?

tough trout
sudden cosmos
#

ping me when u do please

#

currently im very pleased by what you accomplished with the bot. congrats! it s something that works seamlessly

#

and also, the adding of persona is very handy (inside discord)

tough trout
#

I'll ping you when I make an update

sudden cosmos
#

thanks!

#

and

#

would it be possible to make an "updater" sort of?

#

like, if i just replace my files with ur new updated files

#

personas are lost and stuff

#

maybe they should be in another file

#

it s like inserting a memory card with songs xD

#

and i highly suggest you do a command for that, like /persona because if i want 30 personas imma have 30 commands right? ๐Ÿ˜„

#

and i'm not sure i want 30 commands

tough trout
# sudden cosmos personas are lost and stuff

if you're using git to get updates, you can git fetch, then git merge, it'll prompt you if there's conflicts, from there you can resolve the issues
I can see if there's a more user-friendly way to accomplish that though

tough trout
sudden cosmos
#

yeah, i get ur point

#

well, i guess the only thing that can be affected are personas, while updating, so storing them in an external file (i could even in a database) would be better i guess

tough trout
sudden cosmos
#

hmm, so the users will still be able to add personas, yeah

tough trout
#

๐Ÿ‘ i'll work on it! will lyk

sudden cosmos
#

Also, how could we keep track of number of tokens in a channel/category? could this be possible?

subtle sparrow
#

Pyhton

tough trout
#

@sudden cosmos I can make a command for that so it has an option to tell you in a separate channel

#

btw, how's this look?

#

c-p'd code from my other projects so it's not done, but if it looks better than typing in each system command then I'll work on it more

sudden cosmos
#

it s perfect

tough trout
#

thoughts? could also create a command to edit preset messages. this command is to create a new one

dense mantle
dense mantle
#

how to lock bot in specific channel? like it will reply only in that channel

green reef
dense mantle
#

gpt-4 gives error not working

tough trout
#

the next update is already finished but I'm in the middle of moving places, plus the code looks horrid so I'm trying to clean it up a tiny bit

tough trout
#

Hallo, can you tell me where I can found a translation programm from French to English? Thank you in advance!

tough trout
#

Thank you, dinkD! I was,told there,is an IA programm for translating a book that I wrote in French and would like to translate into English. Excuse me, but I am a 71 years old lady who is not a geek... Thank you for your kind help.

dense mantle
tough trout
# tough trout Thank you, dinkD! I was,told there,is an IA programm for translating a book that...

It's possible! It may likely be much easier for you to directly use it through OpenAI's own website.

The basic free version for everyone is this link: https://chat.openai.com

If you find that to be slow (or it doesn't work well), use your account with OpenAI, set it from "Complete" to "Chat" at the right side of the screen, and set the "Maximum length" at the right side of the screen to 2048. https://platform.openai.com/playground?mode=chat

tough trout
low vine
#

Awesome program! I'll take a look at it later and if I can add anything, I'll send a PR