How to integrate OpenAI GPT3 with a Databases - Crash Course

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video I'm going to explore how to connect gpt3 to a database why because the true strength of AI is in how it handles data let me start off with the project plan I get hundreds if not thousands of comments every day and every week it's hard for me to even view them all let alone to respond to them sometimes I get positive feedback sometimes there are ones that I actually have to reply back to and sometimes I even get malicious ones where a bot is trying to impersonate me I want to use open AI to help me sift through all these comments and as well Mark the ones that should be marked as spam right now YouTube and Google aren't doing a very good job at this so it's time for me to take it into my own hands so what I'm going to do as part of this video is structure it in three parts in the first part I'm going to create a real database one that I can use and connect up to GPT the second part will actually be connecting it up creating some apis to create communication to and from the database to open Ai and in the third and final part I'll take a look at some of the really cool things that openai can do for my day data so let me start here on the back end of the YouTube channel there you can jump onto content and you can view your videos as well as how many comments they have you can even click through to a video and view all the comments that video has received the reality of the situation is that it's actually an endless Scroll of comments that I can never really seem to get through and then somewhere in the middle of all of these are spam comments like this one here where there's a bot trying to impersonate my avatar as well as my name trying to force people to DM them probably trying to exploit something I have to mark these as unwanted spam as well as report them to YouTube as impersonation so what I'm gonna do is try and download all my comments I'm gonna use the Google API to do this by jumping on the Google Cloud platform luckily they have their own YouTube data API version 3 which I'll use and I'll simply enable it which is something you can do if you press the button just over here next I'll need an API key I'll head over to manage and head over to credentials and generate a new one I'll make sure this is private and I won't share this with anyone especially the entire internet I'm going to open up vs code and create a new file called index.js now it's time to do some programming caution ahead I do go through this quite quickly but if you get stuck at any point or if you need to backtrack feel free to do so next I'll install Google apis by running npm install Google apis with that done I'm going to require it in the project passing in const Google equals require Google apis I just want to access YouTube so I'll pass in const YouTube equals google.youtube passing in the version as well as my API key with that done I can now call the YouTube API and this specific method I want to call is the one that gets the comments so I'm going to pass in comment threads I want to pull this out as a list so I'll add in dot list and finally I just need to add a few parameters which includes a part snippet as well as the video URL I'll do some quick error handling and if there are no errors then I'll console log out the data packet now let me get the unique ID for the video and that's just the ID that you find at the top of the URL whenever you're viewing a YouTube video I'll paste that in and then I'll run node and check if it actually worked and it did here's my response including all the items and objects which I assume are the comments let me see if I can spit this out in the file I'm going to import FS from the fs library then I'm going to pass in data dot data dot items that's where the object has all the comments then I'm going to convert this into a stringified Json which I want to write to a file called comments.json and then I'm going to log it out and say the file has been saved running node.js creates the file here comments.json and I can see it's been saved let's have a look at it now opening up the file actually showcases a lot of information and most of this is actually kind of hard to read I'm going to make it more beautiful by running a shortcut called beautify it's an extension in vs code this cleans up the file adding an indentation and with a vs code it also adds the colors in as well so now it's quite easy to read the very first comment is my own me trying to promote my own little products here a GPT starter kit and open AI template there were a few other comments all of these weren't very useful because I wasn't sure what people were trying to say but I did stumble across at this one over here which is a perfect example of a question that I could reply to so now let me see if I can put these into a database so that I have to constantly call the API in order to do any functionality I'm gonna use a single store database which is also real-time unified and uses distributed SQL there are also today's sponsor if you sign up with them you get 500 worth of free credit and they're also very easy to use especially their cloud system I'll be able to create a database in just a few minutes on AWS and the good thing is it's scalable so if I want to increase it I can always do so at a later date and that's it single store is now deploying my database to this workspace and it's complete now I can start using it inside of my code to access single store you need to have MySQL to installed I'm gonna do this right now installing it via npm then in the single store admin dashboard I want to head to workspaces and create a new database I'm simply going to call it single store with that done I want to connect up this database to vs code so I'm going to create a new file called db.js then if I head back to the documentation I can see that there's already a script to connect it to it through node.js so I'm going to copy paste that in and then all I have to do is simply replace the values over here at the very top the first is the URL for the workspace so you can head over to actions and click connect directly select your app and then copy the URL here for the single store instance this I'll paste into host the next is the username and the password the username is admin and the password is preset but since I don't know what it is I'm just going to reset it using a new strongly generated password and finally the name of the database which I just called single store earlier I can try to run this up in node passing in nodetv.js and I can see that a successful connection to single store has been made back here in single store the database if I view it currently has no tables no views no functions nothing it's completely empty so I'm going to create a couple of tables and some data so that I can get it ready to populate with my YouTube comments so let me create this I'm going to head over to the SQL editor and I'm going to create a new table using SQL format and here it is it'll Auto increment as well as capture things like the comment ID commenter and the comment itself I'll also have some rows for gbt whether I should flag or respond to comments the only thing left now is to run this command which I'll do and this will create the new table inside of the database if I head over to single store and select comments there it is it's time to put this together and populate it using the YouTube API I headed to the documentation there was a function here to create an operation this operation was to insert data into a table rather than the sample I added this to be a comment and to insert into my comments table I then also use the different types of values here such as commenter ID comment GPT flag and I added in some question marks which will be populated by an object I'll pass in which will include all these values I also also opted at the attribute for connection to be the same as the one that I'm using in my main function and finally I'm going to call this execute command so I'm going to paste this in after we establish a connection to the database and I'm going to update the connection as well as the content itself to be a comment and this comment will be a Json object with some sample data just for testing right now if I run the command nodeb.js I can see I've connected to single store and I've also inserted the row if I jump into single store and jump into the database I can see that the sample data shows the row right there now I'm going to combine the database as well as the YouTube API for the most part I can copy paste most of the syntax across to the index.js file but because we're using import statements I'm going to update the syntax here not to use requires but to use Imports instead next I'm gonna copy over the connection method so let me copy across the variables here that I have to connect to single store I'll also copy across the function to insert a data into to my single store table and finally I'll copy over the main function here which is where most of the interaction is happening my plan is to build out most of the YouTube API inside of the main function but in order to do this I need to turn this YouTube function into an async promise that I can use and await because currently the Google apis does not provide that so to do that I'll simply create a new async function called get YouTube comments there is some extra code here to write to a comments.json file I won't be using this anymore since I'll be writing to the database so I'll remove it and I might cut out this function and create a new promise inside and This Promise will resolve once I get the data back from the YouTube list I can copy this function out and reuse it anywhere and I'm going to be using it in my main function I'm going to create a new variable here called comments and I'm going to await the results that come back from calling the YouTube API then I'll create a for Loop I'll Loop through all the comments based on how many is in that array and for each one I'll populate the database inserting that data into single store right now I still have my sample data in here so I'll pass it in comment with the I for the index from the array passing in things like the ID as well as the commenter and the comment itself GitHub copilot is good as it fills out the Syntax for this so I'll just let it autocomplete but if I wanted to I could jump into the Json file to have a look at the correct nesting for each one of these variables and that's it now I can run the index.js file and I should see it start to populate all the comments straight into the database now I'm going to connect up open Ai and GPT I'll head to the developer section of openai's website and the first thing I need to do is install their package which allows me to access their models I'll install it here in console by running npm install open AI next I need an API key you'll need your own account I'm just going to log in with mine that I created earlier and then I'm going to head over to them menu on the top right and select view API keys I'll generate a new secret key this is something you want to remain private then I'm going to paste this into a new file where I want to create a connection to openai I'll call this file ai.js here I'll paste in the key and the other item I also need is the organization ID which you can find here under settings and then I'll create a connection by copying over the syntax that open AI have as a starter template to be able to connect it to their API I'll paste in the organization ID as well as the API key here in the configuration next I'll jump into open ai's documentation under API references and create completion I'll copy out this code which essentially I can plug straight into my AI file it basically queries DaVinci and then I'll console log out the response here and test this out in terminal here it should say this is a test and the response is this is indeed a test with this API connection to open AI config figured I can now put it into the database file essentially calling out different database entries then have openai access them process them and then update the database I'll start doing this on the db.js file what I'm going to do is copy out this main function that I previously had and create a new one but it's going to be slightly different and this time I'm going to call it update database using GPT this time we're going to read the database and then use openai to make changes I'm going to call it down here and I'm going to cancel out at the other content which I won't be using I'll comment out main.js and jump to the ai.js file to copy out the open AI configuration I'll go to the top of the file and import this configuration so it can be initialized and I'll cut out the response and I'll place that inside of my new function I'll use these shortly but for the time being I need to read from my data database and in order to do that I'm going to jump back into single store copy over the documentation for a reading rows from the database and plug this in as an async function that I can call I'll make a few small changes first instead of just selecting a few different attributes I'm going to select a star so it gets all the information I'll pull this from my comments table and I won't have a where section I'll just have everything pulled in for the time being I'll return all the rows not just the first one and I won't pass in an ID finally I'll update the connection method using the syntax iron labeled here a single store connection I can now call this function I'm going to save it to a variable called comments I'll run const comments equals await and read n and then pass in the connection method then I'll create a console log so that I can see exactly what comments are coming out moving forward I want to create a for Loop here I'm going to Loop through the comments and I want to call the API from open a AI each time I do this will be the essence of connecting the database to open AI I've created a custom prompt in order to define whether or not comments should be responded to so here is my custom prompt first I've defined the prompt the context is the following AI tool helps YouTubers identify whether a comment should be replied to or not I've given an example of when it shouldn't and I've given an example of when it should finally here is where we put in the real content here I'm passing in the variables for my commenter and the comment itself and finishing off with a should I reply this gets console logged out as a text I'm also going to define a stop prompt this means that the AI will stop the completion whenever a line break user comment or should reply text appears in the response so let me open up terminal and run node db.js and looks like there's an error I guess I am human and can make mistakes I have not updated the connection method here to be called single store connection let me run it again and here it is I've got my my database entries and I've got openai now telling me whether or not I should respond to these comments now it's time to update the database I'll add an ifscript if the response is yes then I'll later call a database update query I'll also trim the text because sometimes there's leading spaces now to update the database I'm going to head to the documentation there is a small script here to create an update it's basic SQL code and I don't need all of it to be honest I just need this one single line here where a connection is established then a command is run to update a certain parameter in the database and then we check which ID we're updating against so what will happen here is I'll update the table comments and I'll set a respond to equal to one where the ID of the comment is equal to the ID that we're currently on on the for Loop this of course only works if openai responds with a yes let me make sure that this database object is called single store database space and not connection because that way we might throw an error and finally our console log out that the database has in fact been updated when this query is run and that's all let me run this in the terminal I'll run node db.js here I've got a couple of no responses and here for the yes I can see that the database is being updated I can head to single store now and have a look at my entries there's still zeros but if I was to refresh the page I would see that they have now been updated and the ones that I should respond to are marked with a number one so let me see if I can view these changes I'm going to copy this function for updating the database but create a new one this will just be to read the database I'll delete most of the content that I had in here before and I'll just console log out the comments and call this function read database and call it let me test this out I'll pass in node DB and here are all my database entries if I scroll up I can have a look at some of them such as this one here where someone's asking me for cooperation I can respond to that where as this one here might be something I can flag as spam later on and this is the essence of how you would connect a GPT up to a database you could use this in any way you see fit giving GPT some short-term memory or analyzing different types of information like reworking this prompt to identify things that look like spam and then Mark the flag as one whenever it is and taking it one step further you could turn this into a Cron job that is automated so that it's constantly populating the database and updating those entries another step forward could be creating a user interface so that I could interact with this information and then respond to it accordingly but that could take hours and it could be for another video this was mainly to Showcase how openai connects to a database and uses it finally I'll upload all these files to GitHub so if you want to try this out yourself the link will be in the description below
Info
Channel: Adrian Twarog
Views: 182,063
Rating: undefined out of 5
Keywords: openai, gpt, gpt3, database, db, nodejs, ai, open ai, singlestore, crash course, tutorial, openai api, gpt3 api, gpt3 database, gpt data, gpt database, sql, api, gpt api, website, developer, programming, coding, javascript, node js
Id: N4nX_rTwKx4
Channel Id: undefined
Length: 16min 48sec (1008 seconds)
Published: Fri Apr 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.