Creating Instagram Reels coding tutorials automatically with OpenAI's GPT

After creating a Node.js script to automatically post Instagram Reels and one to automatically generate thumbnails for a Instagram Reels, what comes next? Automatically generating the content of the video, of course!

In this post I’ll show you how to use OpenAI’s GPT-3.5 to generate the content of a coding tutorial video. I’ll use the same Node.js script I used in the previous posts, plus a create-react-app that will be used to type in the code from the generated tutorial.

AI is taking over
AI is taking over

Prompting engineeering

The first question I had to ask myself was: how can I prompt GPT-3.5 to generate a coding tutorial? I mean, I can’t just ask it to generate a coding tutorial, right? I need to give it some context, some information, something to work with.

Future is here
Future is here

In this new era of working with language models, it’s weird that if you want the model to do someting, you just have to ask for it. So, I started by creating a list of things I wanted to be in the video, like:

  • A title
  • A description
  • The code itself
  • A list of ideas I’ve used before
  • A list of things to avoid

And then I started to think about how I could use this information to prompt GPT-3.5 to generate a coding tutorial. I came up with the following prompt:

Take a deep breath, relax, and think analytically about the problem you’re trying to solve. I have a JSON file for a script for a video coding tutorial for Instagram Reels, which looks like this: <JSON_EXAMPLE_HERE> Please use this template to generate a new script for a video coding tutorial for Instagram Reels. Avoid using the following ideas: <USED_IDEAS> And avoid the following topics: <AVOID_TOPICS> Make sure to only answer me with a JSON file with the same structure as the one I gave you and nothing else.

After crafting this prompt, I tested it a few time on the ChatGPT, and it was looking good, but as expected, sometimes the output wasn’t a valid JSON format, and that’s when I’ve learned about function calling.

Function calling

Function calling is a new feature of the OpenAI API that allows you to call a function from the prompt, and it helps it to make sure the output from GPT is what you expect.

In my case, I created a function called generateTutorialViaOpenAi that receives the JSON file with the script for the video and returns a new JSON file with the script for the video.

https://gist.github.com/blopa/1b9d2acf8a69cee4b25642476d9a8af0

As you can see, the schema object is the key here, making sure the output from GPT is what I expect.

Generating the video

Now that I have a JSON file generated on demand by OpenAI, I need to somehow convert it into a video, and this is where the Puppeteer.js script comes into play.

I’m not going to get into too much detail here because this is actually the most complex part, and it deserves its own post one day, but in a nutshell, I’m using Puppeteer.js to open a headless Chrome browser, load a React app, and using react-ace for the coding highlighting, then with Puppeteer.js I’m typing away the code and taking a screenshot of the page and saving it as a PNG file by using the ‘Page.screencastFrame’.

https://gist.github.com/blopa/f3df1e6b6f27cf82d6177c8c65ccaf8b

After that, I’m using ffmpeg to convert the PNG files into a video.

https://gist.github.com/blopa/3526eb15ea065b5e4848504f81abd080

Putting it all together

Now that I have all the pieces, I just need to put them together. I created a new Node.js script that will:

  • Generate a new JSON file with the script for the video
  • Generate a new video with the script for the video
  • Generate a new thumbnail for the video
  • Post the video to Instagram Reels
https://gist.github.com/blopa/fc06e367d9753f818b56a02bb55b1a96

The actual code I’m using is a little bit more complex, but this is the gist of it.

Proof of concept

Of course after doing all this work I had to test it, so I created a new Instagram account that will post a new tutorial every day, and always avoid existing ideas and topics, well, at least in theory…

… but the reality ended up being that the generated tutorials are not that good, and I’m not sure if it’s because of the prompt I’m using, or because of the model itself, but I’m sure it’s a combination of both. For some reason it keeps generating the same tutorials, always related to coloring things using CSS.

I have been running this account for 3 months now, and it got only 38 followers as of today and 466 accounts reached in the past 30 days, so I’m not sure if it’s worth it to keep it running, even though it’s fully automated and the total cost so far was only U$1.00.

I think the best approach here is a hybrid one, where I generate my own scripts and use my Node.js code to generate the video and post it to Instagram Reels, which already automates a lot of the work.

Conclusion

I’m really excited about this new era of working with language models. I’m sure we’ll see a lot of cool stuff being created in the next few years.

Even though this project didn’t work as expected, I learned a lot about OpenAI’s API and how to use it, and I’m sure I’ll be using it in the future.

I hope you enjoyed this post, and don’t forget to check how to automatically post Instagram Reels using Node.js and how to automatically generate thumbnails for your videos with Node.js.