Easily fine-tune an LLM using Replicate’s API

fofrai · Beginner ·🧠 Large Language Models ·3y ago

Key Takeaways

Fine-tuning a large language model using Replicate's API, specifically fine-tuning Flan-T5 for generating image prompts for Mid-Journey.

Full Transcript

welcome to foffer AI the channel where we explore the world of artificial intelligence and its latest tools and techniques fine-tuning a large language model sounds complicated but if you use the replica API it's really easy I am going to show you just how easy it is so here I am I'm on replicate.com and I'm going to create a model the model is a holding place for our fine-tuned large language model once the training has completed so I'm going to make an image model that is based on my mid-journey image prompts so hopefully at the end of it we'll get a model that can give me image prompts and help me craft image prompts to make better mid-journey images so I'm going to call it image prompts I'm going to make it public so that other people can play with this as well so now I've created my holding model next we need to choose which large language model we want to fine tune and train replica currently has three models that we can train the first one you probably know about which is larma 7B and there are two others there's flan T5 which is a Google language model and there's GPT j6b so 6B means it's trained on six billion parameters and this model is by a Luther AI the task that we want to complete the image prompter we want to provide an example such as a cottage by a river and then we want the model the fine-tuned large language model to produce an output which is a cottage by a river 8K detailed beautiful sun morning Misty light dapple light really you want the model to embellish it with that in mind I think that flan T5 is a good model choice flan T5 is really good at discrete tasks like classification and summarization on each model page there are details about the model itself and its usage so it's quite good at question answering sentiment analysis and classification you can also see here that it has a fine tuning section and it tells us what parameters we need to pass in when we are tuning the model now this looks quite complex really the only parameter we are going to care about is the training data and so when we train any of the models that are on replicate we need to use training data that is in this format so a Json l or a Json list where each Json object is made up of two properties the first one being the prompt and the second one being completion so we need to create a set of training data where our prompts are things like a cottage by a river and our completions are Cottage by a river comma and then all the keywords and all the embellishments that we want we create a large set of training data we pass it into the fine tune API we train the model and then we get back a model that can give us image pumps for other types of things so I've gone away and prepared my training data so this is my Json L file it's prompts.jsonl and it is my collection of mid-journey prompts since I started using mid journey and the way that I've structured this training data and we'll see if it works or not is the prompt part of the training is the first part of the mid Journey prompt so I typically separate my prompts with commas so it's going to take everything up until the first comma as the main subject of the image and everything after that comma as the Embellishments so if you look at the highlighted examples we've got our prompts being beautiful Japanese River and landscape with pink flowers and our completions include that text but then they have all of the extra keywords afterwards like vibrant stage backdrops light pink and teal Japanese traditional blurred imagery cherry blossoms and so on so what we're hoping to get from our trained language model is the sense of how to do this so if I ask for an image of a river it should be able to then give me a prompt that that is embellished and has the keywords and makes a good picture on mid-journey I'm going to kick off the training on replicate using the replicate API using JavaScript and the replicate npm package so here I'm importing the replicate package and I'm setting it up here with a replica API token which I've set in my environment here I've got each of the llms that can be trained and their most recent version numbers we've chosen to go with flan and this is the version number for flan so I'm choosing flan here and that sets the llm and version variables we'll pass in a training data URL so I'll upload that Json L file somewhere and then we'll pass it in when we run train then we call the replica API we call replicate trainings create and then this is the name of the model that we're training and this is the version and then this is the destination so this is where we want our fine tune version to end up which is the image prompts holding model we saw at the start and our input which is our training data pointing at our training data URL now when you run this script when you run main it will kick off the training the training can take as long as it needs to so depending on the size of your data it could be 20 minutes could be an hour it could be a few hours depends how big a training job it is but right after this is completed as soon as the training is kicked off we will console log out a URL here which includes the training ID so we can track that progress of the training on replicate.com so I have uploaded my training data and now I'm going to kick off the training so I'm just going to run nodetrain.js so train.js is the file we're looking at before and with any luck there we go our training has started so if we go to this URL so let's just copy and paste this so this is our training that is in progress now in the logs here you can probably see the actual complexity the replicate has abstracted away you can see it downloading weights and um doing the actual training and going through the training data and then tensorizing the output and that's all stuff you don't really need to worry about because you get your end result and then you can use it what we can see here is the progress so we can see that the training is going through our training data and we can see it sort of 0.15 so 15 through our training data we've only got one Epoch so that means going through the training data just the once so when this gets to 100 I would expect the training to finish shortly after that and we'll see how long it takes um after it's gone through the training data it then has to tensorize the outputs and then shortly after we'll get our model now that our training is finished the image prompts model that we set up at the start has are fine-tuned flan T5 model and if we put in a prompt we can see the output and this is this looks like it's kind of doing what we want so he's put in image a Japanese River and the output is prompt Japanese River Morning Light Morning Mist with an aspect ratio that's quite exciting if we compare that to the original flan model if we asked for a Japanese River what we get back is a small stream in Japan which is very different so our training has clearly done something let's try it again so let's say a Cottage By the Sea caused by the Sea beautiful summer evening group of family enjoying dinner good people are not friends painting of a beautiful scene with a lot of color that seems to be okay let's just run it again beautiful morning light shining from the windows warm Ray of sun shining on the roof a woman in a pink Blazer and a skirt well that seems like a pretty good prompt I'm going to run that in mid journey and see what we get [Music] thanks for watching I hope you found that interesting and if you like the video don't forget to like And subscribe thanks very much

Original Description

Fine tune models like Llama, GPT-J and Flan-T5 using the Replicate fine tuning API. Replicate documentation: https://replicate.com/docs/guides/fine-tune-a-language-model The "image-prompts" model shown in this video: https://replicate.com/fofr/image-prompts The javascript code repo for starting a training: https://github.com/fofr/replicate-llm-training
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

This video teaches how to fine-tune a large language model using Replicate's API, specifically for generating image prompts for Mid-Journey. The speaker demonstrates how to create a model, choose a large language model, prepare training data, and train the model using JavaScript and the Replicate npm package.

Key Takeaways
  1. Create a model on Replicate
  2. Choose a large language model to fine-tune
  3. Prepare training data in JSONL format
  4. Train the model using JavaScript and the Replicate npm package
  5. Monitor the training progress on Replicate
  6. Test the fine-tuned model with prompts
💡 Fine-tuning a large language model can be made easier using Replicate's API, which abstracts away the complexity of the training process.

Related Reads

Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →