How to Query Any YouTube Video Using LangChain

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
ain't nobody will watch a video and then you don't remember the thing that you saw in some podcast a few years ago and you're like ah I wish I could find that information however it's like hidden away in hours and hours of potential podcasts what if you could ask questions to specific YouTube videos to just query for the information you're looking for so in this video we're gonna learn how to ask questions and query any YouTube video using link Chang let's get started okay so I have my vs code open here on my left and what we're going to be doing is first we're going to start by importing Arc bars so Argo parse is going to be used to create the CLI tool that allows me to use this as something that I can just call from the terminal then I'm going to import two things from link chain first the YouTube loader uh YouTube we're gonna import the YouTube loader so that we can load a YouTube video so we're gonna come here from the document loaders and we're going to import YouTube loader and then we're gonna also import the vectorization part so we're going to import the vector index creator so from linkchang.indexes import vector store index Creator perfect so now we're going to be doing is uh let me just improve here okay now we're going to set up a few functions we're going to set up one function to extract the new tube ID from a YouTube url okay we're also going to set up another function that's going to load and vectorize the information that we get from the YouTube video so YouTube url ID add video info equal to false so this is going to allow us to do all the loading and vectorization separately from the queries so we're gonna we're going to load some URL from a YouTube video then we're going to load and vectorize the information from the video then we can start asking as many questions as we want also going to set up the function to query any YouTube video with query index and that function will take in the index which will be created in this function load and vectorize and it's going to take in the query from the user and now let's set up a main function that will run our app so the main function is going to be doing all the heavy lifting in the sense of it's going to be it's going to have all the structure that we need to set up our app and GitHub copilot is already getting ahead of itself but let's go line by line so the first thing we're going to have is the parser so we're going to use rcars dot argument parser and description in a description and on the description we're going to say okay so query a YouTube video yeah query YouTube video sure why not and then yeah we're gonna add an argument to this parser so add argument perfect and the name of that argument is going to be minus URL and it's going to be of the type string yeah type string and a little help description in case we want to know something and we don't remember we should input and it will say the U to URL from the video so D URL from the YouTube video okay perfect now there you have that we're gonna just say Okay so args equal parser dot parse arcs which will allow us to parse all the arguments in this case that there's only one argument and that's going to be the the URL from the YouTube video then what we're going to do is we're going to capture the ID of that URL so YouTube url ID equal extract extract um YouTube ID right and that will take in the URL we're also going to give the index and then we're gonna go load and vectorize YouTube url ID and that will load the YouTube video and then vectorize the information using chroma so that we can so that we can start asking questions and querying that Index right then we're going to set up the input so we're going to say okay so the first question so what is your question right so it's going to ask the question and we're going to set up in barensis enter which uh to exit yeah enter enter which to exit okay okay guys so now we're gonna set up a while loop so that we can ask multiple questions right not only just uh one question to the video we're going to say something like response equal the query index and then we're going to give the index and the query and then we're going to say print answer and then we're gonna say the response so we're gonna give the response that we got from querying the index and finally we're gonna ask again and I'm just gonna copy this yeah I'm just gonna copy this query what is your question exactly so now we're going to say okay so if query equals quit uh or query equals Q we're gonna break that Loop and by breaking that loop we're out we're we're not asking questions anymore and I'm just gonna say enter quit or shoot whoop enter quit or Cube here so that we can you know properly ask and properly give the information on how to exit the app once you've asked all the queries that you want I was also thinking about maybe uh okay so how about this um if we query yeah we could we could add a save option to this thing but let's let's leave it like like that for now yeah let's leave it like that for now and that may be something that I can improve later okay so now I can just set up the main Loop call the main function the only thing that we have to write these three functions yet right so for the extracted tube ID what we're going to be doing is we uh the way that we load the videos to Lane chain is not through the URL itself but it's through an ID so the way to re remove get that ID is like this so let's say I got some video like um let's say this one from hubberman let's see uh I guess eight hour sleep is the worst okay all right so fast okay so how learn skills faster from huberman that's a relatively big video and the way that we're getting the ID is this is the actual ID right and this is the information that we want the thing that comes after The V equal so this is the ID that we want and to get that what we're going to be doing is we're just going to say okay return YouTube url dot split YouTube url dot split oh wait okay return YouTube url dot split and then I will split on the vehicle bit okay I'm gonna write it like this yeah and then we're gonna get the last we're gonna get the last part because this is gonna return the list and then we're gonna get the last bit which is going to be the actual ID from the YouTube url so that's what we're going to be returning that function and we can test it real quick here we can say okay uh say I have an URL right here right so I guess the URL is this and then I can say print extract YouTube ID and I give the URL and I just run this to test and let's see what happens boom we get the YouTube url ID which is this thing here perfect so this thing is working great now let's do the rest so load and vectorize is going to be just loading using the amazing lag Library Lang chain so we're just gonna say loader equal YouTube loader Dot from YouTube URL and then we're gonna give the YouTube url ID and we're gonna say add video info honestly we're always going to say add video info equals false and I'm going to remove this parameter from here so that yeah because we we don't we're always going to set this to false we're going to load the YouTube the YouTube video from the URL and then we're gonna say docs equals loader download and from there we're going to say index equals Vector story index Creator and now will be using this index from the documents that we loaded by loading the YouTube video so we're going to say return index Dot from documents and we're going to give the docs and now we have a perfect function so load of vectorize is going to do all the heavy actual heavy lifting of loading the documents and vectorizing the information and then the query index part is the easy part we're just going to say response is equal to wait response is equal to index.query and then we're going to give the query and then we're going to return the response I'm just gonna say like return index response like this right it's fire I mean yeah beautiful so now I think we have everything that we need Let's test out our little app so we're gonna save we're going to remove this we're going to say we're going to run the main function it's important to know that to run this you would need to run a pip install link chain and uh chroma DB and open AI so you have to have these three packages installed I'm gonna put them in a requirements file in the repo that I'm going to create for this project uh uh yeah so you will need bip install Lang Chang chroma DB and if I'm not mistaken open AI but you might not need open AI or do you not sure Okay cool so now let's test our app so I'm gonna run ask YouTube and I have to give a URL to this video let's give this URL how to learn skills faster that's a topic that I'm really interested in so minus URL so now we're running our main function so it's creating the chroma database and now what is your question what are five behavior protocols to learn faster I'm just gonna ask the the obvious question how to learn skills faster that's an obvious question for this particular video there we go designated block of time to form repetition is the skill try to perform the maximum repetition they can do safely per unit time yeah and I've seen that video so that makes sense make errors within the same session use a physical mental visualization to help with the learning consolidation of the practice nice what are some important biological factors involved in learning new skills learning skills involves performing many repetitions releasing dopamine when something's performed correctly making lots of Errors to get the correct performance all right releasing dopamine that makes that makes sense for the video so let's now say Q to exit so now we exit and let's ask and not let's see another video so let's see something like uh machine learning tutorial python something random just just to test okay so we got a we got this tutorial here machine learning with python Jupiter shortcuts okay so perfect let's use this example I'm going to use this example so I'm going to run the same thing so ask YouTube and I'm going to say minus URL as this guy and I'm going to ask something about the YouTuber the the Jupiter shortcuts and let's see what happens okay so what is your question uh tell me some Jupiter shortcuts let's see and now it takes a few seconds some useful shortcuts are pressing escape key to switch between edit mode and command mode pressing command control to add remove combo pressing A or B that's all correct and perfect so yeah our tool is working perfectly and that is how you ask any question to YouTube using link chain if you like this video don't forget to like And subscribe and see you next time guys cheers
Info
Channel: Automata Learning Lab
Views: 1,901
Rating: undefined out of 5
Keywords:
Id: cY-0TRj-teI
Channel Id: undefined
Length: 14min 46sec (886 seconds)
Published: Wed May 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.