Skip to main content
Version: v1.0 (Raw Transcripts)

Transcript: RAG Applied: Search Youtube videos with Redis vector database

Version 1: Source Transcript

This document represents the raw lecture transcript recorded from the video lecture. For architectural study notes and system design documentation, see Version 2.

Full Transcript​

YouTube is widely used but the search functionality is notoriously underwhelming if you try to search YouTube to find specific answers to a question you're lucky if the recommended videos have anything to do with the question and you're left having to click through them to figure it out what you need is a way to summarize a collection of YouTube videos so you can look them up later when a question is asked this way you can both answer the question and provide relevant videos related to the question this is possible today using Ai and reddis with just a few lines of code the process is as follows first you take a list of videos and you use an llm to Summarize each video it helps to ask for example questions so that you can more easily match the videos later next you use the llm to generate Vector embeddings for each video summary and store the vectors in a vector database like redis now when someone asks a question you use Vector similarity to look up relevant videos in your vector database then you take the list of videos and pass them to the llm to generate the answer finally you serve the answer back to the user in theory this sounds great in practice there are some tricks you'll want to understand let's look at how you can do this in code we're going to be taking video transcripts and using them to ask an llm for a summary and Sample questions to do this we need to come up with a very detailed prompt for the llm we need to ask it to specifically summarize videos and generate sample questions here we're importing ing Lang chain. JS and then we're writing our summary prompt template we're going to supply the transcript text to the llm and in response we want a summary and example questions we'll use this to generate a prompt template with Lang chain next we need to import all our dependencies including the Lang chain dependencies for summarization open AI embeddings open AI chat and reddis Vector store we'll also need to import the reddis client and the summary prompt we just created now we need to create a summarization model using chat open AI we'll also need to create an embeddings model using open AI embeddings we'll store the embeddings in reddis using reddis Vector store R Vector store lets you specify options for indexing and searching and automatically creates an index for you with our summary model and our prompt we can create a summarization chain using Lang chain this will be used to summarize videos the last thing we need to do here is write a function that will take in video documents with metadata like ID link title description and thumbnail then it will iterate over each video and use the summarization chain to summarize the video content finally it will add all the documents to reddis using the reddis vector store let's see what these summaries look like in reddis Insight I've gone through the process of populating a reddis database already so I already have video summaries in reddis with an index set up using the index filter in reddis Insight you can easily get to the Keys associated with an index looking at one of the videos you can see the summary content the vector embedding and the metadata now we're almost ready to start answering questions but we're missing the critical piece of looking up relevant videos and calling the llm to retrieve the answer here's how that works first we need to build a semantic question prompt what this will do is ask the llm to provide a semantically similar question to the one being asked allowing us to normalize questions for easier Vector searching next we need to build an answer prompt this prompt will provide the video data to the llm in addition to the question in return the llm is asked to answer the question using the provided video info if possible to get our questions answered we're going to use a lot of the same logic we used in the beginning to summarize videos this includes the summarization model embedding model and Vector store next we'll use our question prompt to create a semantic question chain with Lang chain we can use this to get a semantically similar question to the one being asked we also need to create an answer chain using the answer prompt we just wrote finally we can write a function that takes in a question generates a semantically similar question uses redis Vector store to find videos that might answer the question and invokes our answer chain to get an answer from the llm finally it will return the relevant videos answer and question to the user using Postman I've asked our API to answer the question how do I build with reddest streams here you can see the response with three videos that are relevant as well as the answer provided by the llm and there you have it we're getting answers and the relevant videos for questions you may already know this but what we've done here is called retrieval augmented generation or rag you might also notice that answering questions is not fast this is because the round trip time to the llm is slow not only that calling the llm for every question can be costly click the link in the description down below to find out how you can reduce the number of calls to an llm making things fast and keeping cost down

Timestamped Segments​

  • [00:00] YouTube is widely used but the search functionality is notoriously underwhelming if you try to search YouTube to find specific answers to a question you're lucky if the recommended videos have anything to do with the question and you're left having to click through them to figure it out what you need is a way to summarize a collection of YouTube videos so you can look them up later when a question is asked this way you can both answer the question and provide relevant videos related to the question this is possible today using Ai and reddis with just a few lines of code the process is as follows first you
  • [00:34] take a list of videos and you use an llm to Summarize each video it helps to ask for example questions so that you can more easily match the videos later next you use the llm to generate Vector embeddings for each video summary and store the vectors in a vector database like redis now when someone asks a question you use Vector similarity to look up relevant videos in your vector database then you take the list of videos and pass them to the llm to generate the answer finally you serve the answer back to the user in theory this sounds great in practice there are
  • [01:08] some tricks you'll want to understand let's look at how you can do this in code we're going to be taking video transcripts and using them to ask an llm for a summary and Sample questions to do this we need to come up with a very detailed prompt for the llm we need to ask it to specifically summarize videos and generate sample questions here we're importing ing Lang chain. JS and then we're writing our summary prompt template we're going to supply the transcript text to the llm and in response we want a summary and example
  • [01:42] questions we'll use this to generate a prompt template with Lang chain next we need to import all our dependencies including the Lang chain dependencies for summarization open AI embeddings open AI chat and reddis Vector store we'll also need to import the reddis client and the summary prompt we just created now we need to create a summarization model using chat open AI we'll also need to create an embeddings model using open AI embeddings we'll store the embeddings in reddis using
  • [02:14] reddis Vector store R Vector store lets you specify options for indexing and searching and automatically creates an index for you with our summary model and our prompt we can create a summarization chain using Lang chain this will be used to summarize videos the last thing we need to do here is write a function that will take in video documents with metadata like ID link title description and thumbnail then it will iterate over each video and use the summarization chain to summarize the
  • [02:48] video content finally it will add all the documents to reddis using the reddis vector store let's see what these summaries look like in reddis Insight I've gone through the process of populating a reddis database already so I already have video summaries in reddis with an index set up using the index filter in reddis Insight you can easily get to the Keys associated with an index looking at one of the videos you can see the summary content the vector embedding and the metadata now we're almost ready to start answering questions but we're
  • [03:21] missing the critical piece of looking up relevant videos and calling the llm to retrieve the answer here's how that works first we need to build a semantic question prompt what this will do is ask the llm to provide a semantically similar question to the one being asked allowing us to normalize questions for easier Vector searching next we need to build an answer prompt this prompt will provide the video data to the llm in addition to the question in return the llm is asked to answer the question using the provided video info if
  • [03:56] possible to get our questions answered we're going to use a lot of the same logic we used in the beginning to summarize videos this includes the summarization model embedding model and Vector store next we'll use our question prompt to create a semantic question chain with Lang chain we can use this to get a semantically similar question to the one being asked we also need to create an answer chain using the answer prompt we just wrote finally we can write a function that takes in a question generates a semantically similar question uses redis
  • [04:30] Vector store to find videos that might answer the question and invokes our answer chain to get an answer from the llm finally it will return the relevant videos answer and question to the user using Postman I've asked our API to answer the question how do I build with reddest streams here you can see the response with three videos that are relevant as well as the answer provided by the llm and there you have it we're getting answers and the relevant videos for questions you may already know this
  • [05:03] but what we've done here is called retrieval augmented generation or rag you might also notice that answering questions is not fast this is because the round trip time to the llm is slow not only that calling the llm for every question can be costly click the link in the description down below to find out how you can reduce the number of calls to an llm making things fast and keeping cost down