[0:00] Today, you'll learn how to fine tune LLMs in Python for use in Ollama. [0:04] I'll walk you through it [0:05] step by step, give you all the code and show you how to test it out. [0:09] So let's go ahead and get started. [0:11] Now first I want to discuss what fine tuning is [0:14] and when you should actually do it. [0:15] So fine tuning is taking a pre-trained language model and teaching it [0:19] to be better at your specific task. [0:21] So think of this like hiring [0:22] an experienced chef and training them on your restaurant's particular recipes. [0:26] Rather than teaching someone to cook completely from scratch. [0:30] So here's how it works. [0:31] Instead of training a model from zero, you start with something like GPT [0:35] or Claude that already understands human language. [0:38] Then you feed these examples of your specific use cases. [0:41] So maybe customer service conversations, legal documents or medical records. [0:46] The model then adjust its existing [0:48] knowledge to excel at your specific domain using those examples. [0:52] Now this is completely different from parameter tuning, which is just adjusting [0:55] settings like temperature or topic to change how the model performs. [0:59] Parameter tuning is like adjusting your car's radio. [1:02] Whereas fine tuning [1:03] is like teaching your car to drive in a completely different neighborhood. [1:06] Now, when should you fine tune? [1:08] Well, there's three main scenarios. [1:10] First is when you need consistent formatting or style. [1:13] The prompting alone can't achieve. [1:15] So something like you want to output a particular Json model or something, [1:18] or you want the model to write in a very particular format. [1:21] Second, you have a lot of domain specific data [1:24] that the model just hasn't seen before. [1:26] So things like advanced medical records or customer service logs [1:30] or information that I just wouldn't naturally know because it's very specific [1:34] to what it is that you're doing. [1:35] And then third, when you need to reduce costs [1:37] by using a smaller, specialized model instead of a really massive, larger one. [1:42] Now, the key advantage to fine tuning, rather than training from scratch, [1:46] is that you need way less data and compute power to do this. [1:49] Instead of millions of examples and months of training, [1:52] you might need thousands or even hundreds of examples [1:55] and maybe minutes to train or hours to train, not weeks. [1:59] But here's the catch when you do fine tune, you can make these models worse. [2:03] At general tasks, but you will make them better at yours. [2:06] So if you do fine tune [2:07] one of these models, keep in mind that it will get worse at general tasks, [2:11] at least typically, but be much better at what you're fine tuning it to do. [2:15] Anyways, with that information out of the way, [2:16] I want to show you how to do this fine tuning. [2:19] So let's go over to the computer and I'm going to show you how to set this [2:21] up in Python with some example data that I have some on the computer now. [2:26] And the first step that we need to complete [2:28] if we want to fine tune a model, is we need to gather the data [2:31] that we're going to fine tune that model with. [2:33] If this is the most important step, if you have bad data, [2:36] you're going to have a poorly fine tuned model. [2:38] So make sure that you take your time here and gather the correct data. [2:42] Now for this video I've just used AI to generate a pretty simple data set [2:45] which demonstrates HTML extraction. [2:49] So you can see that I have some HTML like a div, an h2 tag, [2:52] a price tag, whatever, etc.. [2:54] Okay, there's a bunch of information kind of inside of here, [2:56] and what I'm expecting the model to give me is a nicely formatted output [3:01] that tells me the name, price, category and manufacturer of this particular data. [3:05] Now, this is very specific because I just want to extract data [3:08] from these types of tags. [3:10] However, you could do anything as advanced as you want here. [3:13] And you can see that I have 500 examples in this Json file [3:17] where I have some sample input and some sample output. [3:20] Now this can be literally anything [3:22] that you want, because all you're doing to fine tune the model is [3:25] you're giving it example prompts and then example outputs of what [3:28] the result or the answer from the LMS should be based on that prompt. [3:32] So you can use customer data again medical reports files whatever. [3:36] In this case just simple HTML extraction. [3:38] So you can see kind of how it works. [3:40] And I can compare it easily in this video. [3:42] If you want this specific data set [3:44] I'll leave a link to download it in the description. [3:46] Okay. [3:47] Now for this video [3:48] we're gonna use something called onslaught for doing our fine training. [3:51] This is open source. [3:52] It's free to use, and it's just extremely good [3:55] and very fast at fine training these models. [3:58] All right. Now what I'm going to do [3:59] is I'm going to go over to this fine training notebook that I've already set up [4:03] for the purpose of this video. [4:04] I'm not going to write all of the code from scratch. [4:06] I'm just going to leave a link to this notebook [4:08] in the description that you can download, [4:10] or that you can connect to in Google Collaboratory, [4:12] and then you can use your own data and start fine tuning your own models. [4:16] But before we get into that, here's a quick idea that's worth stealing. [4:20] If you're already fine tuning LMS or building tools with alarm. [4:23] Chances are you're solving real problems for real users. [4:27] Maybe it's a niche SAS, [4:28] an AI agent, or a quick side hustle for early stage businesses. [4:32] Now imagine giving those users a fully functional website, a storefront, [4:36] booking system, portfolio, whatever it is, without ever leaving your app. [4:40] No templates, no drag and drop builder to maintain just one API call. [4:45] Well, that's what ten web's AI Website Builder API does. [4:49] It's not a template engine or a static generator. [4:52] It's a real API that spins up fully edible WordPress based websites, [4:56] structure content design, images, e-commerce, etc. [5:00] all in under 60s. [5:02] With one API call, you can generate a complete website. [5:06] Your users never leave your dashboard. [5:08] You get full white label control, so you're branding your domain, [5:12] your customer relationship, and ten web handles the hosting, WordPress [5:16] management, and technical infrastructure while you focus on your core product. [5:20] This is how ten web powers websites with over 2 million users. [5:23] And now you can do the same thing without building anything. [5:27] Whether your user is launching a restaurant coaching business [5:30] or online store, the site comes complete with hosting, security, [5:34] mobile optimization, and full e-commerce capabilities, all powered by WordPress. [5:39] The platform that runs 62% of the web. [5:42] If you're ready to turn your next big idea into reality, [5:44] then visit my link in the description and book it strategy. [5:47] Call with the ten web team to see how they can help transform your business. [5:51] Okay, so how does the fine tuning work? [5:54] Well, you can fine tune this locally on your own computer [5:57] by running all of the code that you see right here [5:59] that you can download from the link in the description. [6:02] But unless you have a really powerful GPU, something like a 48, 40, 90, something [6:06] even more powerful than that, then this is going to take a very long time. [6:10] So for most of you, I suggest that you use something called Google Collaboratory. [6:13] This is a free online [6:15] code editor environment provided by Google that allows you to connect and use super [6:18] high end GPUs so you can do all of your training in Google Collaboratory. [6:22] We can then take the model that we train here, [6:25] and we can download it to our own computer and run it in Alana, which is exactly [6:29] what I'm going to show you how to do. [6:30] So first step here is open up this notebook again. [6:32] I'll leave a link in the description. [6:33] If you're doing this on Google Colab and connect to a run times, [6:37] you should see a button here that says connect. [6:39] We're going to press that [6:40] and we should connect to a T4 instance runtime, which is a T4 GPU. [6:45] And if you're not sure, you can press on this right here [6:47] and it's going to show you what you have available for [6:49] system Ram, GPU, Ram, and disk, [6:51] and it will show you the runtime you're connected to. [6:53] If that didn't work, you can go here and you can go to change runtime type. [6:57] And then you can make sure that you select T4 GPU [7:00] and that you're running in Python three. Okay. [7:03] So now that we're connected to this runtime, [7:05] we can start running the various cells that I have inside of this notebook. [7:08] Now, because we're going to train this [7:10] on our own custom data, we need to bring in this file. [7:12] So what I've done is I have this Json file and I'm going to open it up here. [7:16] But in order to open it in [7:17] Google Collaboratory I need to upload it to Google Laboratory. [7:20] So I'm going to press on this file button here on the left hand side. [7:23] And I'm going to go to this little upload button where it says upload. [7:26] And I'm going to select this file again. [7:28] You can download this from the link in the description okay. [7:31] So once this file is uploaded you should see it here. [7:35] And what I'm doing is I'm simply opening this file and loading it [7:38] as a Json file by providing the name Json Extraction data set 500. [7:42] Now you can use any type of data that you want. [7:44] Again, just make sure you get it in this format [7:47] where you have some input and then some output. [7:49] Okay. [7:49] And the output should actually be a string. [7:51] I'm going to convert this [7:52] to a string in one second, because right now it's a Json object. [7:56] All right. [7:56] So now that we have that I'm going to run this. [7:58] And you can see that I just printed out the first example. [8:00] And I'm successfully loading in this file that I've uploaded. [8:03] So I'll go ahead and close that window. [8:04] Next we need to install the various dependencies. [8:06] In my case I'm in Google Colab. [8:08] So I'm going to run exclamation mark pip install for now. [8:11] I'll remove the uninstall command. [8:12] I just had that in case I had something installed before it. [8:15] Okay, this is going to take probably a minute or two to download and install. [8:19] Once it's done, I'll be right back. It will continue. All right. [8:21] So that install has finished here. [8:23] And now we're going to [8:23] move on to the next step here which is related to the GPU check. [8:27] However, as I says here, the following packages were previously imported. [8:30] In this runtime, [8:30] you must restart the runtime in order to use the newly installed versions. [8:34] So I'm just going to press on the restart session button here [8:37] just to get this reloaded. [8:38] And once this restarting thing is done, okay, we're good to go. [8:42] We can move on to the next cell. [8:44] And if you are running this locally, [8:45] the install might look a little bit different [8:47] and you are going to need to have Cuda installed on your system [8:50] and be using an invariant GPU [8:51] in order for this to work, or at least for to work quickly. [8:54] That's why I like using Google Colab, [8:56] because everything is already set up for us. [8:57] So I'm going to go ahead and press run here. [8:59] I'm just going to run this cell and check [9:01] if I have a GPU available and if Cuda is installed, [9:04] I should get two trues here or sorry, a true and then a GPU value. [9:08] So Cuda is available and I'm using the Tesla T4 GPU [9:12] in this Google Colab instance or runtime, which is exactly what I want. [9:16] Okay, so now we can move on [9:17] and we can start actually setting up our fine tuning training. [9:20] Now here we need to pick the model that we want to fine tune. [9:24] Now I'm going to pick a really small model, [9:25] because I want to do this in a small amount of time, [9:27] and I want this to take days or weeks to train. [9:30] And the model that I'm using here is the 53 mini model. [9:33] Okay. [9:33] You can look up this model [9:34] if you want to see more information about it, [9:36] but you can fine tune any model that you want. [9:38] That's open source. [9:39] So for the model name here, you can put any sloth model. [9:42] You can go to the onslaught documentation [9:44] to find all the models that are available. [9:46] There are pretty much any open source model [9:47] you could do here like Lama 3.1 for example, Mistral Pixel, etc. [9:52] you can put them all here. [9:53] Then we're going to set the sequence length. [9:54] Now in this case I'm just going with 2048 tokens. [9:57] This is the maximum number of tokens [9:59] that the model can handle as input for the dtype or the data type. [10:02] Don't worry about that. We'll leave it as none. [10:04] And this just means that we're going to auto detect what the data type is. [10:07] Now what we need to do here is just load the model that we're trying to fine tune. [10:10] So that's why I put the model name here. [10:12] And I'm getting the model [10:13] and the tokenizer from the fast language model from Unsworth. [10:16] And then I'm [10:16] loading this pre-trained model which is v3, giving the maximum sequence [10:20] length load in four bit is equal to true because this is a four bit model okay. [10:25] So I'm going to go ahead and press on run. [10:27] That's going to load the model for us. [10:28] It will need to download it if it's not already here. [10:31] So that can take a second. [10:32] And you might see some stuff like this where Unsworth is patching our computer. [10:36] Don't worry, that is totally normal. And once that is finished, [10:38] I'll be right back and we'll move on to the next step. [10:41] All right, so the model is now downloaded here. [10:43] Again, I picked a pretty small one just for this video. [10:45] If you pick a larger one, this can take a much longer amount of time. [10:48] And next what we're going to do is we're just going to preprocess our data. [10:51] So the day that I have remember comes in this format [10:53] where we have some input and then we have some output. [10:56] Now what we need to do is we need to put this in just one single [10:59] string that we can send to the model when we're fine tuning it. [11:03] So what we're going to do is we're just going to use this format prompt function [11:06] that I wrote here, where we're going to have an input. [11:08] That input is going to be equal to whatever the input is from our data set. [11:12] So you'll likely have to change this function to work with [11:14] whatever type of data it is that you have. [11:16] Then I have a new line character, then I have the output. [11:18] And what I've done here for the output is I've gone and I've grabbed this [11:22] Json object, I've converted it into text. [11:25] So that's what Json dump is doing. [11:27] I'm taking a Json object and converting this to a string. [11:30] So I'm doing that for my output okay. [11:32] And then I put this end of text tag here [11:34] so that the model knows okay this is the end of the text. [11:37] That's it. [11:38] There's many different ways that you can format the prompt, [11:40] but this is what I want the prompt to look like where we have this input. [11:43] And then this is the expected output okay. [11:46] I then have my format of data where I essentially just run [11:48] through all of the items [11:49] that I have in my file and call this format prompt function on them. [11:53] And then I convert this into a data set. [11:55] Now if you try to run this, you might get an error. [11:57] That's just because this first line up here, [12:00] we may need to rerun where we're loading in the file [12:02] because we reset the runtime. [12:04] So if we scroll down here now after rerunning [12:06] that we can re-execute this cell. [12:08] And we should be good to go. [12:09] And we now generate this data set that just because we need this data set object [12:12] when we're running this trainer okay. [12:14] So now what we're going to do is we're going to use [12:16] something called the Lora adapters. [12:18] Now I'm not going to go into too much depth on exactly what this is doing. [12:21] But this line right here is essentially going to add the kind of layers [12:25] that we need to our lemma in order to actually do this fine tuning. [12:29] So again this can get very complicated. [12:30] You don't need to understand all of the parameters that are here. [12:33] And if you do want to mess with them you are going to have to look these up. [12:35] Or maybe use an L1 to explain them to you, [12:37] because there's a lot going on and it's not enough time for me. [12:39] Explain this in this video if I want to make it short. [12:41] So in fact, if we want more information on this [12:43] or in Colab, I'll just highlight this. [12:45] Right click on it, press explain code. [12:48] This is going to use Gemini to explain the code to us. [12:50] And then we can actually just see what the explanation is. [12:52] And that's going to be more accurate than me walking through everything. [12:55] So says the selected code applies the Lora low rank adoption [12:58] method to the language model. [12:59] Using this, here's a breakdown of the parameters [13:02] and then it tells you exactly what these are. [13:04] Okay, so use Gemini within here [13:05] if you want to know about all the parameters. [13:07] Either way, we're going to go ahead and run this for you to create this new model. [13:10] Now by kind of adding these adapters to it, [13:12] which then will actually allow us to perform the fine tuning, [13:15] which we'll do now. [13:15] So you can see 1.32 pounds layers with 32 shaved layers. [13:19] Don't worry too much about that if it means nothing to you. [13:21] Okay, so now that we've learned in the model, we're going to set up the trainer. [13:24] Now the trainer is actually the thing that's going to perform the fine [13:27] tuning for us people much smarter than all of us have written all of this code. [13:31] So all we have to do is simply use it. [13:33] So again, you can read through all the parameters [13:34] if you want, but what I'm using here is the SFT trainer. [13:38] Importantly, I pass my model I pass the tokenizer for the model. [13:41] This is actually going [13:42] to convert our string into the token so it can be understood by the model. [13:46] I pass my data set which I created earlier. [13:48] The field for my data set is text. [13:51] That's important because if we go look at the data set you can see that is it. [13:55] Here we created the data set with this dictionary which is text. [13:59] And then it has all of the values okay. [14:01] And then we have a few other things in here that you can look at. [14:03] For example the maximum sequence length [14:05] that needs to match with what we had before. [14:07] And then all of these training arguments. [14:10] Again, I'm just going to leave [14:11] these kind of all default, but you can adjust them if you like. [14:14] All right. [14:15] So now we're going to run this. [14:16] We're going to initialize the trainer. [14:18] And now that that is created what we're going to do [14:20] is actually train the model. [14:22] Now this step is going [14:23] to take a different amount of time based on how many examples you have [14:26] and the different settings that you configure here. [14:28] The more examples you have, the longer this will take, but the better performance [14:32] you will get. [14:33] The larger the base model is that you're using. [14:35] Then again, the longer this will take. [14:37] In my case, I'm using a very small model [14:39] with a very small amount of examples, so my performance won't be great, [14:43] but this should train in maybe a few minutes, not hours or weeks. [14:47] All right, so we are training now [14:49] and you can watch this window when you kind of see the progress. [14:51] So once this is finished I'll be right back. [14:53] And then I'll show you how [14:54] we can actually test this model, make sure that it's working [14:57] and then download it and use it with Alama. [14:59] All right. So the training step is finished here. [15:01] Just as an FYI that took about ten minutes to run. [15:04] So the next step is we're going to set up the model for inference [15:07] just so we can test it and make sure it works before we download it. [15:10] So in order to do that, pretty much all you have to do [15:12] here is modify these messages to be the messages that you want to test. [15:15] So you can see I have some raw which is the user. [15:18] And then I have content which is the prompt I want to test. [15:20] You can place multiple messages here. [15:22] And you can see that this message is just similar to one of the examples where [15:25] I'm doing this kind of HTML text extraction. [15:28] You don't need to worry too much about these lines right here. [15:31] We're just kind of putting the input and output into a format [15:33] where we're able to actually send it to this model, [15:36] at least in the inference mode, and then we can go ahead and press on run. [15:40] That will just take a second. It will run the inference. [15:42] And then you can see that we get the output. [15:44] So we have user right which said extract the information. [15:47] And then if we go over here you can see we have the assistant. [15:50] And then we get the output in the format that we specified. [15:53] Okay. So we can see this is indeed working. [15:55] And we can test this with multiple other messages [15:57] to make sure it is working to our liking before we go ahead and download it. [16:01] Okay. [16:01] So now that we've assumed all right this is good. [16:04] It's working. [16:04] We've tested a few times in Google Colab. [16:07] What we want to do is download this model so we can actually load it into a llama, [16:11] and then start using it more permanently on our own computer or our local machine. [16:15] And this step does take a while. [16:17] So bear with me here. [16:18] What we're going to do is run this where we say model dot save pre-trained gov. [16:22] Gov is the model format that a llama understands. [16:26] So that's why we're downloading it in this format. [16:28] So I'm going to go ahead and press run. [16:29] It is now going to essentially create this output file for us. [16:33] Download it to Google Colab. [16:35] And then after that we need to manually download this ourself [16:38] so we can save it to our computer. [16:40] Now this can take a long time. [16:42] It can take ten minutes 15 minutes 20 minutes. [16:44] So just bear with it. [16:45] Be patient. [16:46] And once it's done you can run this cell. [16:49] This cell will then download it to your own computer. [16:52] Then read and move on to the next step [16:54] where I show you how to actually load this into a llama on your local machine. [16:58] So once this is done executing and you've ran that final cell, [17:01] you should have a file downloaded to your downloads [17:04] folder that's named something like this Unsworth IQ for Kcmg. You. [17:10] If you're just looking for some file that ends in.gov, [17:13] this is going to be a pretty large file and it will take a long time to download. [17:17] In my case, both those steps took about 25 minutes to run, [17:20] so just keep that in mind. [17:22] This will take a while, especially if you have a slow [17:24] internet connection when you're downloading the file. [17:26] Okay, now that we have the file, what we need to do is add this to a llama. [17:31] So we need to create something called a model file. [17:34] So what I'm going to do is I'm going to open up my terminal. [17:36] I'm just going to do this all from terminal commands [17:37] to make our life a little bit easier. [17:39] And I'm going to open this up okay. [17:41] I'm going to zoom in a little bit and we're just going to test [17:43] and make sure that a llama is installed on the system. [17:46] So run the llama command, [17:48] make sure you've got that downloaded and installed for the next steps to work. [17:52] Okay, from here I'm going to go to my downloads folder. [17:55] So CD into downloads and from the downloads folder I know that [17:58] I have this Unsworth kind of for underscore k underscore m file. [18:04] So what I'm going to do is I'm just going to make a new directory. [18:06] So I'm going to say mkdir Alamut dash test. [18:09] Okay. [18:10] If we go to downloads now we can see this folder is created. [18:13] I'm just going to drag this file into the folder here. [18:15] So now it's inside of Olamide test okay [18:18] I'm now going to go inside of Olamide test. [18:20] And I'm just doing this to kind of organize things. [18:22] But you don't need to follow this whole setup here. [18:24] And what we're going to do now [18:25] is we're going to make something called a model file. [18:27] Now a model file defines a custom configuration for a model [18:31] that you want to run in a llama. [18:33] So we need to make one of those. [18:34] Now to do that we can make a new file so we can say something like touch. [18:37] And then we're going to call this model file okay. [18:39] Just exactly like this with the capital M that's going to make a new file for us. [18:44] If we ls we can see it's here. [18:46] Now to edit this we can do nano model file like that. [18:50] And then I'm going to paste in what the model file should look like okay. [18:54] So it's going to say from dot slash. [18:56] And then this needs to be the name of your file to the name of your model. [19:00] So the local file that we had is called this. [19:02] So I'm just going to copy the name [19:03] by going to rename copy and then pasting it here. [19:06] So we're going to say from dot slash Unsworth dot Q4 underscore [19:10] underscore mgg us again this is kind of the base model. [19:13] So we're saying from this file right here which is in our local directory [19:17] where this model file exists, then we can specify some different parameters. [19:21] So we can do the top the temperature the parameter stop. [19:24] So user end of text. [19:26] Because that's the way that our model works. [19:28] And then we have a template. [19:29] So the template is going to be user prompt and then assistant. [19:33] And then we just have a system message. [19:35] So if we want to tell the model to do something specific [19:37] we can in this case we just say you are a helpful I assistant. [19:41] So this is a very simple model file. [19:43] This is how we're going to be able [19:44] to load in this particular model and use it in a llama. [19:47] So now we are going to save. [19:49] So I'm going to hit Ctrl x and then press Y to save the model file. [19:53] Go ahead and press enter. [19:54] And now you can see if we open up the model file again. [19:56] So nano model file. [19:59] It looks like this. Again we can get out of that. [20:01] So now we have a model file and we have the model downloaded. [20:05] What we need to do is add this to a llama. [20:07] So in order to add this to a llama we're going to type a llama and then create. [20:11] And then we're going to give the model a name. [20:12] So I'm going to say this is an HTML model okay. [20:17] And we're going to do dash f and then model file. [20:20] What this is going to do [20:21] is add a new model configuration two llama for us using the model file. [20:24] So let's go ahead and press enter. That's going to take a second. [20:27] And now if we want to see if this is here we can type a llama list. [20:30] And we should see that we get the HTML model that was just created. [20:34] So now if we want to run the model we can just type a llama run. [20:38] And then the name of this which is the HTML model. [20:41] It's going to take a second to load up. [20:42] And then we can paste any message inside of here. [20:44] And we should see it working. [20:46] So let's go into our data set. [20:47] Let's just copy one of the ones that we have just to keep things simple. [20:50] So let's copy this right here. [20:53] Go back and paste it okay [20:55] I just cleared the screen here so it's a bit easier to see okay. [20:58] Then you can see after I put the prompt here and press enter. [21:00] This is the result that we got. [21:02] Now this isn't always going to work. So I can put this a few times. [21:04] And you'll see now look like it gives me a different output. [21:07] That's because obviously we had a small data set we're using, [21:10] you know a pretty poor model. [21:11] Not one of the massive models, [21:13] but generally it is working in the way that we wanted it to work. [21:16] And more examples pass in. [21:17] This will give us a better output either way. [21:20] This is a now custom fine tuned model loaded in a llama that we can run [21:24] locally on our own computer. [21:25] We could connect to it from Python or something and use it in our application. [21:29] And that is going to wrap up this video. [21:31] Now I know this is not going to be perfect. [21:33] You do need to experiment with this. Modify some of the parameters. [21:36] But I wanted to show you how to do fine tuning, talk about what that actually is [21:40] and give you a step by step guide. [21:42] So hopefully this can get you up and running as fast as possible. [21:45] All of the assets for this [21:46] video will be linked in the description in case you need them. [21:49] And with that said, I look forward to seeing you in another video.