DEV Community

Akshay Keerthi
Akshay Keerthi

Posted on

Buiding a Youtube Chatbot using Lyzr SDK’s — An In-depth Guide!

A hands-on introduction to building a conversational AI chatbot with Lyzr SDK’s. In this blog, we will explore an innovative approach to building a Chatbot for YouTube by integrating Weaviate, OpenAI’s LLM, and Lyzr SDKs into our tech stack.

Image description

In the current era of Large Language Models (LLMs), the evolution of chatbots is remarkable. Just a few years back, creating a chatbot capable of engaging with YouTube video content was quite daunting. However, with the advent of tools like the Lyzr SDK’s, the process has become incredibly streamlined and accessible. Now, building a chatbot that interacts with YouTube video’s is as straightforward as can be, thanks to the seamless functionalities provided by the Lyzr SDK’s.

Why use Lyzr SDK’s?

With Lyzr SDKs, crafting your own GenAI application is a breeze, requiring only a few lines of code to get up and running swiftly.

Lets get Started!

Create a new file YTChatbot.py and use that

import os
import openai
import weaviate
import streamlit as st
from lyzr import ChatBot
Enter fullscreen mode Exit fullscreen mode

In this snippet, we’ve imported the ChatBot from the Lyzr SDK, which will serve as the backbone for our Chatbot for YouTube. Additionally, we’ve mentioned the need for importing Weaviate for semantic search and OpenAI’s LLM for NLP.

Next Set Up OpenAI API Key and using Streamlit’s secrets management, set up the OpenAI API key within your Streamlit application. Replace "OPENAI_API_KEY" with the actual key name defined in your Streamlit secrets where your OpenAI API key is stored.

openai.api_key = st.secrets["OPENAI_API_KEY"]
Enter fullscreen mode Exit fullscreen mode
vector_store_params = {
    "vector_store_type": "WeaviateVectorStore",
    "url": "https://sample.network",
    "api_key": "your_weaviate_api_key",
    "index_name": "Indexname"  # First letter should be capital
}

Enter fullscreen mode Exit fullscreen mode

By defining these parameters correctly, you can establish a connection to your Weaviate vector store for further processing and retrieval of data.

def initialize_chatbot(video_url):
    return ChatBot.youtube_chat(urls=[video_url], vector_store_params=vector_store_params)
Enter fullscreen mode Exit fullscreen mode

Here’s the function initialize_chatbot which initializes the chatbot with the provided video URL.This function takes a YouTube video URL as input and returns an initialized chatbot object.

It utilizes theChatBot.youtube_chat method, passing the provided video URL and the Weaviate vector store parameters (vector_store_params).

def main():
    st.title("YouTube Chatbot")

    # Input field for entering the YouTube video URL
    video_url = st.text_input("Enter YouTube Video URL:")

    # Initialize the Chatbot if the video URL is provided
    if video_url:
        chatbot = initialize_chatbot(video_url)

        # Input field for asking questions
        question = st.text_input("Ask a question related to the video content:")

        # Button to submit the question and get the response
        if st.button("Ask"):
            if question:
                response = chatbot.chat(question)
                st.write("Chatbot's Response:")
                st.write(response.response)
            else:
                st.warning("Please enter a question.")
Enter fullscreen mode Exit fullscreen mode

This code snippet defines a main function for a Streamlit application that creates a YouTube Chatbot interface:

Title Display: Sets the title of the Streamlit application to “YouTube Chatbot” using st.title.

Input Field:Provides a text input field where users can enter the URL of the YouTube video they want to ask questions about. It uses st.text_input with the prompt "Enter YouTube Video URL:".

Chatbot Initialization:If a video URL is provided by the user, the initialize_chatbot function is called to initialize the chatbot for that specific video.

Question Input:Provides another text input field where users can ask questions related to the content of the video. It uses st.text_input with the prompt "Ask a question related to the video content:".

Ask Button:Displays a button labeled “Ask” which, when clicked, triggers the chatbot to respond to the question entered by the user.

Chatbot Response: Upon clicking the “Ask” button, if a question is entered, the chatbot responds with an appropriate answer, which is then displayed using st.write.

Validation: Warns the user if they attempt to submit a question without entering anything, using st.warning.

# Run the Streamlit app
if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

This part of the code checks if the script is being run directly by verifying if the name variable is set to "main". If so, it calls the main() function, which initiates the Streamlit application to run the YouTube Chatbot interface.

With Lyzr SDK’s, the journey from concept to creation is streamlined, empowering developers to craft captivating applications with ease. Imagine the possibilities — whether it’s the creation of a Chatbot for YouTube or the exploration of other innovative projects. The possibilities are endless, and with Lyzr SDK’s, the future of Gen-AI applications is limited only by imagination.

For more information explore the website: Lyzr

Source Code: YouTube Chatbot- https://github.com/isakshay007/youtubechatbot111

Book your demo: https://www.lyzr.ai/book-demo/](https://www.lyzr.ai/book-demo/)

Top comments (0)