Fine-tuning GPT from Scratch in 15 Minutes with JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this video we're going to look at how to fine tune a GPT model with node.js and JavaScript and we're going to do it in just a few minutes actually so this is going to be somewhat of a quick tutorial but it's something that I think it's valuable because when I first started looking into gpt3 and chat gbt and all this stuff I realized that there are some limitations around you know new information but also a lot of Niche information so if you want to train your model on your documentation or your API or anything that really isn't part of that data set this is how you add it and I thought it would be very complicated but in reality it wasn't that complicated so I'm hoping to break this down and make it super easy for you to understand and this is really going to be following along also with my blog post called supercharger GPT model on my sub stack it's natter.substack.com if you want to follow along all of the code is there the only prerequisites that you're going to need are going to be to have Python and node.js installed as well as an open AI API key which you can get at open AI platform.openai.com down here you'll see API keys and with that API key you should be ready to go so with that being said I'm going to get started writing some code by creating an empty directory and we're going to start writing some JavaScript so I'm going to create a directory called GPT app or something like that and then here we're going to say npm init Dash Dash Y which is going to initialize a new um package.json then we're going to open this up in our text editor and this is going to be where we write all of our code for our app and in the package.json we're going to make one small modification here we're going to set the type as module so we want to use es modules and that will allow us to do Imports so with that being said we want to go ahead and set our open AI API key now I already have this set as an environment variable and you can set it yourself by just typing this in it's your terminal and then setting the key and then during that session you will have it available to you or you can set it somewhere more permanent like in a bash RC or a zh zshrc file or something like that so the first thing we want to do now is install the open AI JavaScript package and that will give us the apis to talk to the actual service and then you also need to install the open AI CLI and you do that with Pip which you should have if you have python installed and then you should be able to type in open Ai and see that you have all the stuff available to you and the and you also are going to be installing this openai data lib which is going to be something we use for data transformations so we're done with that setup part now we want to look at how we're going to train the data now the way that you do this is that you have this prompt and then you have a completion The Prompt is like the question and the completion is the answer or the prompt would be you know the Declaration of the information and the the completion would be the response for that or however you want to think about that and a good way for you to figure out like what some of this might look like is just actually go here to GPT and just ask it that question so I asked it that and I was like um what are some good examples of training data and Json L format and Json L is basically the format which we need to have it in and we'll talk about that more later but it's kind of like a Json type of format but you kind of separate each individual piece of data in a new line so um this actually gave me a really good response if you want to kind of understand this a little better and this is kind of what that data model will look like where we save this and this is going to be our training data so for uh for us you know you can do whatever you want but for this example I was looking at training GPT for the lens protocol documentation so I kind of have a lot of uh example like responses and information there that that's what I'm going to be using so your data could be whatever you want but the training data I'm going to be using for this tutorial is going to be that and in addition to the tutorial here I also have a GitHub repository that I that is LinkedIn into this blog post but I'll also link it to this video that has the training data that we're going to probably use and I will go ahead and just take a look at that this is that data.json l so uh having that data is going to be what you need to kind of get ready for the next steps and the the file would basically look like this where you just have like an object with a key and a value for prompt and a key and a value for completion and then each individual new line would kind of have a new set of data now it's recommended to have like 500 or more pieces of data to get a good actual like machine learning AI type of like you know thing going on here with good information coming back with mine I only have like 18 lines but you're going to notice that even with 18 lines at least it kind of works and it's good for this tutorial but I don't I didn't have enough time to kind of do a lot more information yet so just keep in mind that we're going to get some response back but it's not going to be perfect but if we did actually add that 500 lines or more of information it would work a lot better and if you look around I even saw a GitHub repo that someone created that allows you to kind of like feed an entire GitHub repo into a tool that spits out Json l so look around you'll see some cool stuff that's out there so um what I'll do is I will just copy everything here and I will create a file called data.json l and I will go ahead and just paste all this there and now we have our data okay so with that we can go ahead and save that and now we have the next step where we're going to actually use this open AI tool to format this data a little bit even better because there's a few nuances between the way it is now versus the way that openai likes it to be and we don't really have to to worry about doing that ourselves manually we can just go to our command line and say data.jsonl with this open AI tools fine-tuned prepare data and I'll just accept all these prompts and what you'll see when this is done is going to be this data data prepared.jsonl so we have all of that prepared and this is going to be the file that we upload for our training data so we have our training data ready to go now we want to have the API to interact with it so I'm going to create this API interface at api.js so I'm going to create a file called api.js and I'll go ahead and paste this code here and we're going to import the configuration and open AI API apis from openai we're going to reference the API key in a in a variable by getting the process.emv.openai key we're going to create a configuration variable calling new configuration passing in the API key and then we're going to call a new instance of openai API passing in that configuration and we're just exporting this openai variable so we can import it into our all the files we're actually calling the service so here we're just like declaring the API and then we can use this API variable to interact so now that we have the AI the API ready to go the first thing we need to do is upload the file so I'm going to create a file called uploadfile.js and here I'm going to have the open AI API we're going to import the file system and in this upload function we're going to call openai.create file and we're going to call uh we're going to pass in the data prepared.jsonl which is right here and we're going to pass in fine tune as the second argument and then we're going to log out this file ID which is going to be very important for us because we're going to need to use that file ID to train our DaVinci model which is going to be kind of the base model we start off with so let's go ahead and try this out and see if it works to do that I can just call node upload file.js and there we go we have our file ID and this is the file ID we need to reference in the next step so I'm going to actually create like a new window so we don't lose that file ID and I'm going to go back to the tutorial and we're going to create the fine tune and to do that we're going to create a file called create fine tune.js and here we're going to you know import our API again and the API call that we're going to make here is a open AI dot create fine tune and here we need to pass in the file ID which is going to be this and then we're going to pass in as a second argument at the model we're starting with now we're just starting with the basic DaVinci model but you can continue to train existing models that you've custom created so when we when we train this we're going to get our new model back you could then pass that model in here to continue training it so remember you're just starting off with one you can continue to add uh and modify those in the future as well and that's really it so once we create this fine tune then it will actually start you know the process of fine-tuning with that file so I can call now this node create fine tune and we now see that we're creating this fine tune with the status of pinning and what we need to do at this point is kind of like check on the status of this because we want to wait until this has been done and then we want to actually call a completion which is essentially us testing it out so to to check the status we can call the list fine tunes API so I'm going to create a new file called list find tunes.js and this is going to call openai that list fine tune so pretty straightforward and then we're just going to log that data out and this will be helping us in two ways first of all it will tell us when the status is completed and then once that status is completed completed it will actually give us the fine-tuned model ID which is what we're going to need to actually call the completion so with that being said we'll go back to our tutorial and get our last file and the last file that we're going to be using is the create completion so I'm going to create a new file called creates completion.js and this is going to call openai Dot create completion and then here we're going to pass in the model name this is going to be our custom model name we don't know what this is yet because our our model is not completely is still pending it's not finished so training yet and then the prompt is going to be you know what we're asking and again I told you I'm training this on lens protocol docs this could be whatever your prompt it needs to be maybe something different and then you can also pass and optionally the max token so I'm using 200 to get like a decent size response back and then we'll just log out the response there so before I can call that we need to go back here and we're going to call list fine tunes again and we need to wait here a few moments until this status is complete all right now let's check it out and see if it's completed so we'll run the list fine tunes and if it is completed we should see the status succeeded with the fine tune model ID so we're going to copy this fine tune model ID and we're going to go here to create completion and we're going to change this to our model save the file and then we're going to go ahead and run this by calling node create completion and this should come back with the response from the open AI API and there we go we actually have a response that is somewhat you know decent it says lens protocol is a database protocol based on it's actually not that great but we also didn't feed it a ton of data so let's try it again and just out of curiosity see what this response looks like it's any better so here we have an effort to map the world's connected lens the data Unleashed will be limited not that great but at least it's somewhat having to do with uh with lens and I've seen you know good and bad comeback with this small amount of data but um you know this is in a similar vein of what it does but I know for sure if you add more data it's going to you know obviously get better results but anyway but I guess that's about it if you want to follow along uh definitely check out the written tutorial as well as the GitHub repo this has everything you need to kind of get started in fact you don't even have to write this from scratch you can just clone this and run it as long as as long as you've set your open AI API key it should be all good to go so that's it from this video thanks for checking it out if you like this be sure to subscribe and like and all that stuff thanks
Info
Channel: Nader Dabit
Views: 34,053
Rating: undefined out of 5
Keywords:
Id: Sb7U32kXMB0
Channel Id: undefined
Length: 14min 41sec (881 seconds)
Published: Fri Mar 24 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.