sdadszx111

Untitled

Apr 6th, 2023
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.37 KB | None | 0 0
  1. Hey how's it going everyone? So OpenAI recently released the model they use for chat GPT as an API service. It's called GPT 3.5 Turbo and the best part about this new model is that it's 10 times cheaper than the DaVinci model which we have previously used to create a similar chatbot. Don't worry though you don't have to pay anything in order to start using this API. In this video I'm going to show you how to make use of this new model in Node.js to create a Discord chatbot like this. If you guys want to see a live demo of how this chatbot works, I have it set up and open for everyone in the chat gpt channel of my Discord server. Before we start make sure you have Nodejs installed because we're writing server-side JavaScript and I'm going to be using Visual Studio Code as my code editor. Of course because we're writing JavaScript code make sure you know at least the basics just so you're not left scratching your head. Anyway, let's get started by first creating our Discord bot account. So head over to the Discord Developer Portal which you can find at [discord.com](http://discord.com/) slash developers slash applications. From here, go ahead and create a new application. I'm going to call my application GPT 3.5 and make sure to accept the terms of service and click create. Once you have your application created, go to the bot section on the left hand side of your screen and click add bot. Then click on yes do it. This should create your bot account. From here you can go ahead and give your bot an avatar and change its username. However, I'm just going to leave this the way it is. Next scroll down and I'm going to disable public bot. Turning off this option means only I can invite this bot. This is your personal preference however. Next scroll down further and enable all three of these intense options. These options will make sure your bot is receiving all the required information from the Discord API. Now we're going to be inviting this bot to our server. So make sure you have a server ready for testing your new Discord bot. To invite your bot head over to the OAuth 2 section on the left hand side of your screen and click on URL generator. Under scopes go ahead and choose bot and if you're going to be adding slash commands to your bot make sure you also enable application.commands. Now scroll down and select the permissions you want your bot to have. Of course I only recommend giving it the permission to send messages because if it does have enough permissions then people can potentially abuse your bot and make it ping everyone. This has happened in my server once but luckily it was my friend who caught on to it and notified me. Anyway copy the link that you get down here and what I like to do is head over to discord and paste the link then click on this and invite the bot directly from here. You'll notice now the bot has joined your server and if you click on it you'll notice that it's offline. Don't worry we'll work on that in just a moment. With your bot added to your server we can go ahead and deal with our OpenAI account. So now head over to [platform.openai.com](http://platform.openai.com/) and from here first of all go ahead and sign into your account. Of course go ahead and register if you haven't already and after being logged in click on your account and click on manage account. Here you'll notice we have a free trial with $18 worth of free credit. Now this may not seem like a lot to you but this was already a decent bid for a free trial with the previous DaVinci model. You'll notice when the new model was announced I switched to it and my usage dropped drastically even though my bot handles a couple hundred messages every day. So it's safe to say that $18 worth of free credit is pretty good and it should be enough for most applications. We're going to come back to this and our Discord developer portal in just a moment. So go ahead and minimize your web browser. Now go ahead and open up your favorite code editor. I'm using Visual Studio Code which I mentioned at the start of this video. And once you're here go ahead and open up a folder which you can do by clicking on File and Open Folder. I'm going to create a new folder on my desktop and I'm going to call this GPT 3.5. Go ahead and click Open. Once you have opened up your folder in VS Code, Go ahead and open up the terminal which you can do by clicking on View and clicking on Terminal. But I will be using this shortcut throughout this video. Now let's initialize a new npm project by typing npm init dash y. We'll now install a few packages. So type out npm install discord.js openai and .env. The installation will take some time depending on your internet speed. Once it is downloaded, go ahead and create a new file in the root directory of your project called .env. Here we're going to store all our environment variables. The first thing we're going to add is token, which is going to be our Discord's bot token. So head back to the Discord developer portal, go back to the bot section on the left hand side of your screen, scroll up and click on reset token. Once you reset your token, go ahead and copy this, then head back to Visual Studio Code and paste it over here. We're also going to create another variable called API key and this will be our API key from OpenAI. So let's go back to our browser and go back to OpenAI. From here go to API keys and click on create new secret key. Now go ahead and copy this key and back in your code editor go ahead and paste this in the .env file. These values are important information so make sure you don't share it with anybody else. After this video is done I'm going to go ahead and reset both of them. We also have to create one last variable called channel underscore id and we're going to set this to the id of the channel where we want our AI bot to work. So I'm going to go back to discord and let's say I want my bots to work in this channel. So I'm going to right click this and click copy id. Head back to our code editor and I'm going to paste the id over here. So save your file and you can close out of both the files that we just opened and now in your root directory go ahead and create a new file called index.js. So the first thing that we're going to do is we're going to import .env slash config using require. After this we're going to import some stuff from Discord.js. The first thing that we need to import is client and the second thing is intents bit field. Now I'm going to go ahead and define a variable called client and I should mention that some people like to call this bot. The choice is yours to be honest. And we're going to set this equals to a new instance of the client class. Now pass in an object and the first property is going to be intents and this is going to be an array. We're going to pass in first of all intents bitfield.flags.guilds. This will give us access to the guilds information that our bot is in. Guilds by the way in discord development means server. Anyway we can go ahead and duplicate this now and we can repeat this for guild messages and this will also give us more information on every single message sent inside a server. We can duplicate this one more time. and we're going to use this for message content. And as the name suggests this will give us access to the message content itself. We can now go ahead and add an event listener which will be triggered every time the bot comes online. So let's say client.on and the event name is ready. So add a function over here and inside this function we're going to go ahead and console.log and say the bot is online. After this make sure to log into the bot using client.login. And if you remember our token is actually stored inside the .env. So to use that we're going to do process.env.token. However, this is not going to work if you did not import .env.config. So let's save this file and this code should work. So open up your terminal and type node index.js. In the terminal we get the bot is online and if we go back to Discord our bot is actually online. So now let's go back and listen for messages. So back in our code I'm going to add another event listener. So I'm going to say client.on and the other event is message.create and this will give us access to the message object as the parameter. And what we can do is we can go ahead and console.log this message object and see what we get. I'm going to save this file and restart my bot using ctrl-c and then using node-index.js again. Once our bot comes online I can go back to Discord. And here I'm going to send a message saying let's say ping. Now if I go back to VS Code and you'll notice we get access to the message object itself. If we scroll a little bit up you're going to see we get the message content saying ping. So now we know what property to access. So let's go ahead and close out of this terminal and what we're going to do now is we're going to configure our OpenAI. So let's go ahead and first import OpenAI using require. And from OpenAI I'm going to import first of all configuration. And the next thing is OpenAI API. Now right before the message create event listener I'm going to define configuration using const configuration and I'm going to set this to a new configuration class and inside I'm going to pass in an object and the thing that we're going to pass in is the API key. If you remember our API key is stored as API underscore key. So let's go ahead and copy that and inside index.js we can say process.env.api underscore key. Now we can use this configuration to communicate with the OpenAI API. So let's go ahead and define a variable called OpenAI and we're going to set this to a new instance of the OpenAI API class and we're going to pass in the configuration. Now we can use this variable to communicate with OpenAI and generate a response. However before we generate a response we need to first run some checks. So I'm going to say if message author bot return. This basically means if the message author was a bot then we're going to completely return from the function and I'm going to do the same if the message channel id does not equal to this channel id. So let's say if [message.channel.id](http://message.channel.id/) does not equal to and then we're going to pass in process.env.channel underscore id and if that's the case we can return again. And finally I'm going to add a way for people to be able to chat in that channel normally. without having to trigger the bot every time. We can do that by using some sort of prefix. So let's say if message.content.startsWith() and over here I'm going to add an exclamation point. Of course you can add whatever you want but if a message starts with an exclamation point in this channel then we're going to completely ignore that message. Now let's go ahead and define a conversation. So I'm going to say let conversation log and this is going to be an array of objects. The first object is going to be the initial context that we want to give our bot. So the role is going to be system and the content is going to be whatever you want to explain to your bot. So I'm going to say you are a friendly chatbot and if you guys want your bot to be a little bit more sarcastic like my bot, then you can basically tell it to be sarcastic over here. Now what I'm going to do is I'm going to push the content of this message into this conversation log. So I'm going to say conversation log. dot push and I'm going to push an object. The first property is going to be the role which in this case is user and the content is of course going to be message dot content. Now before we send an API request to OpenAI let's make our bot pretend that it's actually typing. So I'm going to say await message dot channel dot send typing and this is a method and because we're using await our function has to be asynchronous. Now after we send a typing status we can go ahead and define results which is going to be await openai dot create chat completion and in here we're going to pass in an object and the first property is going to be the model and as I mentioned before the model is gpt 3.5 dash turbo. The next property that we need to pass is messages and this is an array of messages. If you remember we created conversation log so let's go ahead and pass it in over here. Finally we can go ahead and reply to this message using message.reply and we're going to pass in the result.data.choices and because choices is an array let's go ahead and get the first element and we're going to get the message from it. Now go ahead and save your file and if we now run our bot using node index.js Once the bot is online let's go to discord and test our bot out. I'm gonna try saying hey how are you? And our bot is going to first pretend to be typing and once the response was generated our bot is going to reply with it. And it does seem to be working perfectly fine. Now what we can do is we can go ahead and make it hold conversations with people. So the way we can do that is we can fetch previous messages based on the person who sent the latest message. Back in our code what I'm going to do is I'm going to remove the send typing. Actually I'm going to cut it and I'm going to go ahead and put it right after defining conversation log. And then after that what I'm going to do is I'm going to define a variable called previous messages and we're going to set this to await message.channel.messages.fetch and we're going to pass in an object with a limit of 15. this means our bot is going to fetch 15 of the previous messages to add to this conversation log. Now we're going to go ahead and reverse these previous messages because they are sorted in latest to oldest order. So I'm going to go ahead and say previous messages.reverse and we can now loop through all of these messages and push them to the conversation log. So what I'm going to do is I'm going to say previous messages for each and we're going to refer to each of the messages as msg. basically short for message. And as we did here before, if the message content starts with an exclamation point, then we want to ignore that completely. So let's go ahead and add that line of code. Now what we want to do is we want our bot to ignore other bots but not itself. So the way we can do that is we can say if message author id is not equal to client user id and message author is a bot. If that is the case, we can return. Finally, because we're trying to hold only a single conversation with one person, we can say if message author ID does not equal to message author ID, then we can return as well. So if a new person is trying to send a message, we're not going to be using anybody else's user ID in this conversation log. However, you can feel free to of course modify this to your liking. But this is all the conditions that we have. So what I'm going to do is I'm going to go ahead and push this to conversation log using conversation log dot push and then we're going to push it as an array. The role is going to be user and the content is going to be message dot content. And outside of this for each method we can go ahead and remove the previous conversation log push method. Now I can go ahead and save this file and restart our bot. Now I'm going to go back to discord and ask what was my previous question. So it did correctly answer by saying hey how are you was my previous question which it was in this case. So anyway I really hope you guys enjoyed this video if it helped you out make sure to give it a like. If you guys are having any sort of problems be sure to join my discord server and ask your questions in the discord.js channel. Anyway thanks for watching and I'm gonna see you guys in the next one.
Advertisement
Add Comment
Please, Sign In to add comment