How to integrate OpenAI GPT3 with a Databases - Crash Course
Connecting GPT-3 to a Database
In this video, the speaker explores how to connect GPT-3 to a database. The goal is to use AI to handle data and sift through comments on YouTube videos.
Creating a Real Database
- The speaker receives hundreds of comments every day and wants to use open AI to help sift through them.
- The plan is structured into three parts: creating a real database, connecting it up, and exploring what openai can do for the data.
- To start, the speaker navigates to the back end of their YouTube channel where they can view videos and comments.
- They want to download all their comments using the Google API by enabling the YouTube Data API version 3.
Connecting Up the Database
- After generating an API key, they create a new file called index.js in VS Code.
- They install Google APIs by running npm install Google apis and require it in the project passing in const Google equals require Google apis.
- They access YouTube by passing in const YouTube equals google.youtube with version and API key parameters.
- They call the YouTube API method that gets comments by passing in comment threads as well as part snippet and video URL parameters.
Exploring OpenAI's Capabilities
- After converting data into stringified JSON format, they write it into a file called comments.json using FS from fs library.
- Running node.js creates the file here comments.json which showcases information that is hard to read.
- Using an extension called beautify cleans up the file by adding indentation and colors.
Setting up Single Store Database
In this section, the speaker sets up a single store database and connects it to VS Code.
Creating a Single Store Database
- The speaker creates a single store database using AWS.
- The database is scalable and uses distributed SQL.
Connecting to Single Store
- To access Single Store, MySQL needs to be installed.
- The speaker installs MySQL via npm.
- A new file called db.js is created to connect the database to VS Code.
- A script from the documentation is used to connect the database through node.js.
Populating the Database
- The speaker creates a table in the SQL editor with rows for comment ID, commenter, comment itself, and GPT flag.
- A command is run to create the new table inside of the database.
- An execute command is used to insert data into the table using a JSON object with sample data for testing purposes.
Combining Database and YouTube API
In this section, the speaker combines the single store database with YouTube API.
Updating Syntax
- Import statements are used instead of requires due to using ES6 syntax.
Copying Over Functions
- Connection method variables are copied over from db.js file.
- Function for inserting data into single store table is copied over from db.js file.
- Main function is copied over from index.js file.
Creating an Async Function to Get YouTube Comments
In this section, the speaker creates a new async function called "get YouTube comments" and removes some extra code that writes to a comments.json file. They create a new promise inside the function that resolves once they get data back from the YouTube list API. The speaker then populates the database by inserting data into single store.
Creating an Async Function for Getting YouTube Comments
- Create a new async function called "get YouTube comments".
- Remove extra code that writes to a comments.json file.
- Create a new promise inside the function that resolves once data is received from the YouTube list API.
- Populate the database by inserting data into single store.
Connecting OpenAI and GPT
In this section, the speaker connects OpenAI and GPT. They install OpenAI's package, generate an API key, create a connection to OpenAI in ai.js file, and use openai to process database entries.
Installing OpenAI Package and Generating API Key
- Install OpenAI's package using npm install open AI command.
- Generate an API key by logging in with your account on OpenAI's website.
- Head over to them menu on the top right and select view API keys.
- Generate a new secret key which should remain private.
Creating Connection to OpenAI in ai.js File
- Paste generated API key into ai.js file along with organization ID under settings.
- Import configuration at top of file so it can be initialized.
Using OpenAI to Process Database Entries
- Copy out main function from db.js file and create another one called "update database using GPT".
- Read from the database and use OpenAI to make changes.
- Call function in db.js file.
Reading Rows from Database
In this section, the speaker reads rows from the database by copying over documentation for reading rows from single store. They make a few small changes such as selecting all information instead of just a few attributes and returning all rows instead of just the first one.
Reading Rows from Database
- Copy over documentation for reading rows from single store.
- Select all information instead of just a few attributes.
- Return all rows instead of just the first one.
Using OpenAI to Connect to a Database
In this section, the speaker explains how to use OpenAI to connect to a database and update it based on responses from the AI tool.
Defining Custom Prompts
- The speaker defines a custom prompt for an AI tool that helps YouTubers identify whether a comment should be replied to or not.
- The speaker passes in variables for commenter and comment and finishes off with "should I reply", which gets console logged out as text.
- The speaker defines a stop prompt that means the AI will stop completion whenever a line break user comment or should reply text appears in the response.
Updating the Database
- The speaker updates the database by adding an if script. If the response is yes, then they call a database update query. They also trim the text because sometimes there are leading spaces.
- To update the database, the speaker heads to documentation where there is SQL code for creating an update. They only need one single line where a connection is established, then run command to update certain parameters in the database and check which ID they're updating against.
- The speaker updates table comments and sets respond equal to one where ID of comment equals current ID on for loop. This only works if OpenAI responds with yes.
- When this query runs, console logs out that the database has been updated.
Reading from Database
- The speaker creates another function just to read from the database. They console log out the comments and call this function read database.
Conclusion
- The speaker explains how to connect GPT up to a database, giving it short-term memory or analyzing different types of information. They suggest turning it into a Cron job that is automated so that it's constantly populating the database and updating those entries.
- Finally, the speaker uploads all these files to GitHub for others to try out themselves.