- unwind ai
- Posts
- Build an LLM app with RAG to Chat with YouTube Videos
Build an LLM app with RAG to Chat with YouTube Videos
LLM App using GPT-4o in less than 30 lines of Python code (step-by-step instructions)
Building a RAG app that interacts with YouTube videos might sound complicated—especially since most LLMs can’t natively process videos. But with the right tools, it’s a cakewalk.
In this tutorial, we’ll walk you through building an LLM app with RAG to interact with YouTube videos using the Embedchain framework. And the best part? You can get this up and running in just 30 lines of Python code!
Embedchain abstracts the complexities of extracting and processing unstructured video content, making it accessible to LLMs through embedding and vector storage.
🎁 $50 worth AI Bonus Content at the end!
What We’re Building
We’ll create a Streamlit app that can take any YouTube video URL and respond to questions about its content. We’ll leverage Embedchain for its ability to extract data from videos and store it as vector embedding.
With this app, you can:
Input a YouTube video URL
Ask questions about the content of the video
Get accurate answers using RAG with GPT-4o (you can choose the LLM you want to use)
Prerequisites
Before we begin, make sure you have:
Python installed on your machine (version 3.7 or higher is recommended)
Your OpenAI API Key
Basic familiarity with Python programming
A code editor of your choice (we recommend VS Code or PyCharm for their excellent Python support)
Step-by-Step Instructions
Setting Up the Environment
First, let's get our development environment ready:
Clone the GitHub repository:
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
Go to the chat_with_youtube_videos folder:
cd chat_with_youtube_videos
Install the required dependencies:
pip install -r requirements.txt
Get your OpenAI API Key:
Sign up for an OpenAI account (or the LLM provider of your choice) and obtain your API key.
Creating the Streamlit App
Let’s create our Streamlit app. Create a new file chat_youtube.py
and add the following code:
Import Required Libraries:
• Streamlit for building the web app
• Embedchain for the RAG functionality
• tempfile for creating temporary files and directories
import tempfile
import streamlit as st
from embedchain import App
Configure the Embedchain App:
For this application, we will use GPT-4o. You can choose from Cohere, Anthropic, or any other LLM of your choice.
Select the vector database as the opensource chroma db (you are free to choose any other vector database of your choice)
def embedchain_bot(db_path, api_key):
return App.from_config(
config={
"llm": {"provider": "openai", "config": {"model": "gpt-4o", "temperature": 0.5, "api_key": api_key}},
"vectordb": {"provider": "chroma", "config": {"dir": db_path}},
"embedder": {"provider": "openai", "config": {"api_key": api_key}},
}
)
Set up the Streamlit App:
Streamlit lets you create the user interface with just Python code. For this app we will:
• Add a title to the app using 'st.title()'
• Create a text input box for the user to enter their OpenAI API key using 'st.text_input()'
st.title("Chat with YouTube Video 📺")
st.caption("This app allows you to chat with a YouTube video using OpenAI API")
openai_access_token = st.text_input("OpenAI API Key", type="password")
Initialize the Embedchain App:
• If the OpenAI API key is provided, create a temporary directory for the vector database using 'tempfile.mkdtemp()'
• Initialize the Embedchain app using the 'embedchain_bot' function
if openai_access_token:
db_path = tempfile.mkdtemp()
app = embedchain_bot(db_path, openai_access_token)
Get the YouTube Video URL from the user and add the video to the knowledge base:
• Use 'st.text_input()' to get the YouTube video URL from the user
• Given the video URL, add it to the embedchain application
video_url = st.text_input("Enter YouTube Video URL", type="default")
if video_url:
app.add(video_url, data_type="youtube_video")
st.success(f"Added {video_url} to knowledge base!")
Ask question about the YouTube video and display the answer:
• Create a text input for the user to enter their question using 'st.text_input()'
• If a question is asked, get the answer from the Embedchain app and display it using 'st.write()'
prompt = st.text_input("Ask any question about the YouTube Video")
if prompt:
answer = app.chat(prompt)
st.write(answer)
Running the App
With our code in place, it's time to launch the app.
Start the Streamlit App: In your terminal, navigate to the project folder, and run the following command
streamlit run chat_youtube.py
Access Your AI Assistant: Streamlit will provide a local URL (typically http://localhost:8501). Open this in your web browser, give it the URL of the website you want the AI, and have fun!
Working Application Demo
Conclusion
And that’s it! You’ve just built an AI app to chat with YouTube videos using RAG, all within 30 lines of Python code. With GPT-4o handling the interactions and Embedchain taking care of video content extraction.
For the next steps, consider refining the app by adding functionality to handle multiple videos or improving the question-answering capabilities by integrating more advanced vector search techniques. You could also explore extending the app to work with other data sources like PDFs or webpages using Embedchain’s flexibility.
Keep experimenting and refining to build even smarter AI solutions!
We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about levelling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.
Reply