LangChain: Chat with Tesla’s Q2 Earnings Call on YouTube
Using LLMs to analyze the speech in YouTube videos and ask them questions. In this tutorial, we chat with Tesla’s Q2 Earnings Call.

Don’t have enough time to watch a complete earnings call of a company? Maybe you have only a few questions. Then, it’s not necessary to listen to the whole earnings call. An AI chatbot can assist you by analyzing the YouTube video and answering your questions. This way, you can save time and still be well-informed.
In this tutorial, we implement an AI assistant with the framework LangChain, a very user-friendly framework for working with LLMs. And the best, you can follow the tutorial with basic Python knowledge. In addition, we’ll analyze the YouTube video of Tesla’s Q2 earnings call. But you can also use another YouTube video. Our approach works with all YouTube videos, you just need to enter the URL to your video.
Now, we’ve no time to waste. Let’s start with what you can expect in this tutorial.
😎 What can you expect?
First, we’ll give you a technical overview. The overview guides you through the components that we need for our chatbot. In addition, we explain the loading of videos, the data preparation, and the retrieval process with LangChain.
Are you interested in what the finished application looks like? Here’s a demo of the final chatbot:

Impressive or? We’ve implemented this beautiful-looking web UI with Plotly Dash. The article focuses on the LangChain implementation and not on the web UI. Next, we jump into the setup process!
🖥️ Set up the environment
There are some technical requirements before we can start with the implementation.
Prerequisites
You’ll need the following prerequisites:
- Installed Python
- Installed conda and pip
- Access to a bash (macOS, Linux, or Windows)
- Code editor of your choice (We use VSCode.)
Setup
First, we need a terminal for setting up the environment. Besides, you should ensure that you’ve installed Python, conda, and pip. Next, we create a virtual Python environment. You can use the following commands:
- Create a conda environment (env):
conda create -n langchain-youtube python=3.9.12-> Answer the question Proceed ([y]/n)? with y. - Activate the environment:
conda activate langchain-youtube
If no error occurs, we have a working environment. Great! Next, we’ve to install all required dependencies. You can save all the requirements in a requirements text file. For this, create a file with the name requirements.txt. Insert the following in this file:
langchain
openai
tiktoken
chromadb
youtube-transcript-api
pytubeTo install all required dependencies, you have to run the following command in your terminal:
pip install -r requirements.txt
Great, we’ve installed all the required dependencies! In the next step, you must set your OpenAI API key. If you have no API key, you can follow our instructions in a previous article.
Next, we set the API key as an environment variable. You can do this with the following command:
# macOS and linux
export OPENAI_API_KEY=[Insert your API Key here.]
# windows
set OPENAI_API_KEY=[Insert your API Key here.]Insert your API key at the marked point and delete the square brackets as well. Now, the setup process is finished, and we can start with the LangChain implementation.
Do you want to take your Python and ML skills to the next level? Discover The Complete Python, Machine Learning, AI Mega Bundle*. It is a completely self-paced online learning course.
🤔 Implementation
This article focuses on the LangChain implementation and not on the implementation of the web UI. If you’re interested in the code of the web UI, you can download it on our digital products page.
Overview
In this section, we look at the steps we need to implement the logic for the chatbot. The following figure shows the workflow:

First, we load Tesla’s Q2 earnings call via a URL. You can use a YouTube video of your choice. Then, we split the YouTube video into small and meaningful chunks. Next, we put these chunks into a vector database. Then, we create our LLM. In this article, we use a chat model of OpenAI. Now, the model can answer user questions based on relevant splits. That’s roughly the process of chatting with YouTube videos. In addition, we can display the final answer on our web UI. Let’s jump into the detailed implementation.
Video Loading
First, we must load our YouTube video with LangChain. For this, we use the YoutubeLoader.from_youtube_url(...) function.
loader = YoutubeLoader.from_youtube_url('https://www.youtube.com/watch?v=eUEojntzzAk&t=376s', add_video_info=True)
data = loader.load()The function takes the URL of the YouTube video. In addition, we set the parameter add_video_info to True. This means that we get more video meta info such as title, description, thumbnail url and publish date. Alright, that’s all we need to load the video in LangChain.
Document Splitting
In this section, we split our video into semantically relevant chunks. Every chunk must contain meaningful information. Look at the following code:
chunk_size = 1000
chunk_overlap = 200
text_splitter = CharacterTextSplitter.from_tiktoken_encoder(
chunk_size=chunk_size, chunk_overlap=chunk_overlap)
splits = text_splitter.split_documents(data)First, we define the parameters chunk_size and chunk_overlap. You can also choose other values for the parameters, but for our video, this size worked best. The parameter chunk_size defines the length of the chunks in characters, and the parameter chunk_overlap specifies the overlap between two chunks. The code above splits our YouTube video into meaningful chunks so that we can use them in our chatbot later. Next, we have to put our chunks into a vector database.
Embeddings and Vector Database
In this section, we store our chunks/ splits in a vector database. A vector database makes our splits accessible through an LLM. This is an important concept for building a chatbot over our own data. We do this with the following code:
persist_directory = 'files/chroma/'
# vectorize the splits and create a retriever
embedding = OpenAIEmbeddings()
retriever = Chroma.from_documents(
documents=splits,
embedding=embedding,
persist_directory=persist_directory
).as_retriever()First, we define where we want to store our vector database. In this article, we use the vector database Chroma. But you also can use a vector database of your choice. Chroma is a lightweight and in-memory vector database. Next, we create an embedding with the function OpenAIEmbeddings(). An embedding takes a piece of text and converts it into a numerical representation. The embedding vectors are similar for texts with similar content. The function from_documents(...) takes the splits, embedding, and the path to the vector database. In addition, this function creates embeddings for all splits and stores them in the vector database. After that, we create a retriever for our YouTube video. Let’s look at how we can implement the retrieval process.
Retrieval and LLM
First, we initialize our LLM. We set the temperature parameter to 0.2. You can use this parameter to control the level of randomness or creativity. A lower temperature results in more conservative LLM answers. A higher temperature leads to more unexpected or random LLM answers. We use the GPT-3.5 model with a 16k context window. In the following, you can see the code:
# initialize the LLM
llm = ChatOpenAI(
temperature=0.2,
model="gpt-3.5-turbo-16k"
)
# create qa chain
question_answer = RetrievalQA.from_chain_type(llm=llm, retriever=retriever)
# run the qa chain
answer = question_answer.run(question)In addition, we create a question-and-answer chain. For this, we use the function from_chain_type(...). The function takes our LLM and our retriever. You can run the chain with the command question_answer.run(). The run(...) function takes the user question and returns the LLM answer.
Chat with the YouTube video
In this section, we chat with Elon Musk about the future of Tesla. For that, we have thought of two questions that we ask our chatbot. In this context, we compare the chatbot’s answer with the information in the earnings call. So we can check whether the answers are correct. Let’s chat with Elon!
Question 1: What does Elon Musk say about the Tesla Model Y?
Chatbot answer: Elon Musk mentions in the context that the Model Y became the best-selling vehicle of any kind globally in Q1, surpassing popular models like the Corolla and Golf. He considers this achievement to be incredible and thanks the Tesla team and customers for their support.
Is the answer correct? Yes, that’s correct!
Question 2: What does Elon Musk say about the long-term value of Tesla?
Chatbot answer: Elon Musk believes that Tesla has the potential for significant long-term value. He sees a path to a 5x to 10x increase in the value of the company and believes that the Tesla fleet’s value will experience a dramatic increase when full self-driving is approved by regulators. Musk emphasizes that the success of Tesla is driven by its ability to make great products and services, and he encourages investors to focus on the long-term value of the company rather than short-term fluctuations in the stock market.
Is the answer correct? Wow, that’s also correct!
We have seen that the chatbot works well. But keep in mind, that’s only a tool. It can also make mistakes. We’ve also experimented with different values for the parameters chunk_size and chunk_overlap. We have found that the chunk_overlap has a big impact on the results. We recommend you play around with these parameters as well. We’ve used the OpenAI model gpt-3.5-turbo-16k for our chatbot. If you want to use the gpt-4 model, you can change the model in the code.
Finally, let’s look at the cost of the two questions. You can output it with the following code:
with get_openai_callback() as cb:
# your chatbot logic
print(cb)In the following, you can see the output:

The screenshot shows the used tokens and how many successful requests we have made. In addition, we see the costs for the two questions.
That’s it! We have built together a chatbot for Tesla’s Q2 earnings call.
🎬 Conclusion
In this article, we have shown you a workflow of how to create a chatbot for YouTube videos. First, we load the YouTube video (in our case Tesla’s Q2 earnings call). Then, we split the video into semantically relevant chunks. After that, we create embeddings for each chunk. An embedding takes a chunk and converts it into a numerical representation. Next, we create an LLM and a question-and-answer chain. Then, we asked the LLM our questions. In addition, we display the question and the answer in our web UI. The chatbot has answered our questions correctly. But keep in mind: AI is only a tool, nothing more!
👉🏽 Join our free weekly Magic AI newsletter for the latest AI updates!
👉🏽 You can find all our Freebies on our digital products page!
Subscribe for free to get notified when we publish a new story:
Learn more about us on our About page. Don’t forget to follow us on X. Thanks so much for reading. If you liked this article, feel free to share it. Have a great day!
Sign up for a Medium membership using our link to read unlimited Medium stories.
🔍 References
- LangChain Documentation (Accessed on 22.08.2023)
* Disclosure: The links are affiliate links, which means we will receive a commission if you purchase through these links. There are no additional costs for you.
Towards Finance
Thank you for being part of the TF community.
- 📚 View more content in the Towards Finance publication.
- 🚀 Subscribe to our monthly Towards Finance Newsletter.
- 🪄 Join our weekly Magic AI Newsletter for the latest AI updates.
- 🤝🏽 Follow us on X and LinkedIn for more finance and data science content.





