Topview Logo
  • Create viral videos with
    GPT-4o + Ads library
    Use GPT-4o to edit video empowered by Youtube & Tiktok & Facebook ads library. Turns your links or media assets into viral videos in one click.
    Try it free
    gpt video

    Multi Document RAG Chatbot - Streamlit Langchain Groq ChromaDB - LLAMA 3.1 | Generative AI

    blog thumbnail

    Introduction

    In the realm of conversational AI, building effective chatbots that can handle multiple documents is a game-changer. This article guides you through creating a multi-document retrieval-augmented generation (RAG) chatbot using Streamlit, Langchain, Groq, and ChromaDB with the LLAMA 3.1 model. By the end of this article, you will have a solid understanding of setting up a multi-document chatbot that can provide accurate responses based on context from various healthcare-related documents.

    Introduction

    In previous discussions, we focused on building single-document chatbots. However, this article shifts gears to explore how we can effectively manage and utilize multiple documents in our chatbot's framework. We will be using a combination of PDF documents related to healthcare and respiratory infections, utilizing a folder structure that organizes our project files and dependencies appropriately.

    Project Structure

    Your project will ideally be structured like this:

    /project_directory
    ├── requirements.txt
    ├── main.py
    ├── vectorized_documents.py
    ├── data/
    │   ├── paper1.pdf
    │   ├── paper2.pdf
    │   └── paper3.pdf
    └── Vector_DB_directory/
    

    The data folder is where you will store your PDF files, while the Vector_DB_directory is designated for vectorizing these documents using ChromaDB.

    Dependencies

    Ensure your requirements.txt is populated beforehand, including essential libraries like:

    • streamlit
    • langchain
    • unstructured
    • chromadb
    • sentence-transformers
    • nltk

    You may encounter compatibility issues with different library versions, so it’s advisable to keep your dependencies updated and troubleshoot as necessary.

    Vectorizing Documents

    Your first step is to create the vectorized_documents.py script to extract text from your PDF files and convert it into vector embeddings.

    from langchain.document_loaders import UnstructuredPDFLoader, DirectoryLoader
    from langchain.text_splitter import CharacterTextSplitter
    from langchain.embeddings import HuggingFaceEmbeddings
    from langchain.chains import RetrievalQA
    from langchain.chains.retrieval import ConversationalRetrievalChain
    from langchain.memory import ConversationBufferMemory
    from langchain.chains import Chroma
    
    ## Introduction
    loader = DirectoryLoader(path="data", glob="*.pdf", loader_class=UnstructuredPDFLoader)
    documents = loader.load()
    
    ## Introduction
    text_splitter = CharacterTextSplitter(chunk_size=2000, chunk_overlap=500)
    text_chunks = text_splitter.split_documents(documents)
    
    ## Introduction
    embeddings = HuggingFaceEmbeddings()
    vector_db = Chroma.from_documents(text_chunks, embeddings, persist_directory="Vector_DB_directory")
    print("Documents vectorized.")
    

    This script reads PDF files, extracts the text, splits the documents into smaller chunks, converts them into embeddings, and saves them to the Chroma database.

    Building the Streamlit Application

    Now we move to the main.py file where we implement the Streamlit interface, setting it up to interact with our vector store and engage in conversations based on user queries.

    import os
    import json
    import streamlit as st
    from langchain.embeddings import HuggingFaceEmbeddings
    from langchain.chains import RetrievalQA
    from langchain.retrievers import Chroma
    from langchain.memory import ConversationBufferMemory
    
    ## Introduction
    with open('config.json') as f:
        config_data = json.load(f)
        os.environ["GRO_API_KEY"] = config_data["GRO_API_KEY"]
    
    ## Introduction
    st.set_page_config(page_title="Multi Document RAG Chatbot", page_icon="?")
    
    ## Introduction
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []
    if 'vector_store' not in st.session_state:
        st.session_state.vector_store = load_vector_store()
    if 'chain' not in st.session_state:
        st.session_state.chain = create_chain(st.session_state.vector_store)
    
    ## Introduction
    user_input = st.chat_input("Ask a question:")
    if user_input:
        st.session_state.chat_history.append(('role': 'user', 'content': user_input))
    
        # Get response from the chain
        response = st.session_state.chain(('question': user_input))
        st.session_state.chat_history.append(('role': 'assistant', 'content': response['answer']))
    
    ## Introduction
    for message in st.session_state.chat_history:
        with st.chat_message(message['role']):
            st.markdown(message['content'])
    

    In this script:

    • We load the environment variables and configuration for our API key.
    • Use Streamlit to create an interactive user interface where users can input questions.
    • Retrieve responses based on stored context using the conversational chain.

    Conclusion

    You now have a multi-document RAG chatbot that can interact with users and provide contextual answers based on healthcare-related documents. By leveraging Streamlit for the user interface, Langchain for document management, and ChromaDB for persistent vector storage, you have created a scalable conversational agent.

    Keyword

    Multi-document, RAG, Streamlit, Langchain, Groq, ChromaDB, LLAMA 3.1, chatbot, textual embeddings.

    FAQ

    1. What libraries are needed to create a multi-document chatbot?
    You will need streamlit, langchain, unstructured, chromadb, and sentence-transformers, among others.

    2. How do I structure my project for this chatbot?
    You should have your Python scripts (main.py, vectorized_documents.py), a folder for your PDF documents (data/), and a directory for your vector database (Vector_DB_directory/).

    3. What is the purpose of ChromaDB in this setup?
    ChromaDB is used to store vector embeddings persistently, allowing the chatbot to retrieve relevant information based on user queries.

    4. Can I use different models instead of LLAMA 3.1?
    Yes, you can replace LLAMA 3.1 with other models compatible with your application.

    5. How do I deploy the Streamlit app?
    You can run your application locally with the command streamlit run main.py or deploy it on cloud platforms like Streamlit Sharing, Heroku, etc.

    One more thing

    In addition to the incredible tools mentioned above, for those looking to elevate their video creation process even further, Topview.ai stands out as a revolutionary online AI video editor.

    TopView.ai provides two powerful tools to help you make ads video in one click.

    Materials to Video: you can upload your raw footage or pictures, TopView.ai will edit video based on media you uploaded for you.

    Link to Video: you can paste an E-Commerce product link, TopView.ai will generate a video for you.

    You may also like