As we enter the age of artificial intelligence, everything seems to be accelerating at an unprecedented rate. Generative this, generative that – data overload everywhere. I’m pretty sure you’re already mentally exhausted from all this information, so let’s get straight to it, okay?
What I want to do:
YouTube video → Information in bite-sized chunks → Learn ✨ + Share on social media 🐦
…That’s it…
This AI race is all about speed. If we can use AI to accelerate our learning, why not? So share this – whether with your family, friends or followers or even as an influencer.
The results:
Repo:
Disclaimer:
This tutorial only works for YouTube videos with English subtitles and is not applicable to YouTube Shorts.
The process
0. Previous knowledge:
Before you begin, it is important to have some basic knowledge of how LangChain.js, Prompt Engineering, and OpenAI Models work.
1. Tools Required:
In my case, I use Yarn to start developing. In order to quickly test the script, I created a quick MVP using the CLI to try out the project.
2. Select name for CLI
For quick clarity and to have a catchy name, I named it “yt2tweets,” which essentially means → “YouTube to Tweets.”
3. Desired output
$ yt2tweets ” # Result: # Tweet 1: Introduction … 🧵👇 (1/X) # Tweet 2: … 🧵 (2/X) # Tweet 3: … 🧵 (3/X) # Tweet 4: … 🧵 (4/X) # Tweet 5: Conclusion … 🧵 (5/X)
Ideally, we need to provide the transcript as context for the AI model, in our case GPT-4o-mini, so that it can understand the context and summarize the input in the output format we specify.
4. Fast design
Here lies the secret ingredient ✨ to make things work: Prompt engineering is a necessary core competency to get the job done.
To customize how the CLI converts YouTube videos to Twitter/X threads, follow the simple and straightforward 3-step setup I’ve defined. You can adjust the tone, length and style to suit your needs. To guide the AI, follow the paste block below for a smooth configuration process.
Identity and purpose
Define the role and goals of AI with the Identity and Purpose block. Define its role and goals to ensure it generates content that meets your needs and desired outcomes.
Procedure
Define the step-by-step actions you want the AI to take, ensuring a clear and structured approach to creating your content.
Output formats
Specify the formats in which the AI should deliver content.
Example like below:
import { ChatPromptTemplate } from ‘@langchain/core/prompts’; const prompt = ChatPromptTemplate.fromMessages([ { role: ‘system’, content: ` # IDENTITY AND PURPOSE {identity} # STEPS {steps} # OUTPUT INSTRUCTIONS {formats} # INPUT INPUT: {input} `, }, ]);
In {input} I enter the entire transcript so that GPT can do the summarization.
A reference on how I added my prompt, with an example below, can be found here:
5. Put everything together
Finally, for it to run you need to have @langchain/openai installed and have your OpenAI API key ready. Once everything is sorted out, you can initiate the model and start submitting prompts and feeds to the AI to get a response.
import { ChatOpenAI } from ‘@langchain/openai’; // Instantiate Model const llm = new ChatOpenAI({ modelName: ‘gpt-4o-mini’, temperature: 0.7, // > result?.content’); // Tweet 1: Introduction … 🧵👇 (1/X) …
6. Package it as CLI
For convenience, I exported the function as a CLI so that I can easily use it in the future.
To achieve this I used the following:
- Commander – to enable CLI for NPM package BIN
- Ora – Elegant terminal spinner
- Chalk – Terminal String Styling
Below is a code snippet (full code at the end):
import { Command } from ‘commander’; import chalk from ‘chalk’; import ora from ‘ora’; const spinner = ora(‘Loading…’); // Initialize the command line interface const program = new Command(); // Command to convert a YouTube URL program .argument(”) .description(‘Turn YouTube Videos into Twitter Threads with AI’) .action(async url => { const apiKey = readApiKey(); // Read the saved API key // … spinner.start(); await convertYt2Tweets(url, apiKey); // … });
Repository link (full code)
Diploma
I hope again that this project helps you accelerate your learning process and digest YouTube content or share it with your friends, family and followers.
If you prefer to access the UI-enabled project, I have created a UI for the same project. The link can be found below: