Chat with Multiple PDFs | LangChain App Tutorial in Python (Free LLMs and Embeddings)

2 min read 5 months ago
Published on Apr 21, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Step-by-Step Tutorial: Building a Chatbot Application with Multiple PDFs

Introduction

In this tutorial, we will learn how to build a chatbot application that allows you to interact with multiple PDF documents using Python. We will be using openAI and hugging face libraries to create this application.

Step 1: Setting up the Environment

  1. Create a virtual environment for your project.
  2. Install the necessary dependencies using pip:
    • pip install streamlit
    • pip install transformers
    • pip install sentence-transformers

Step 2: Creating the Graphical User Interface

  1. Import the required libraries:
    import streamlit as st
    
  2. Set the page configuration:
    st.set_page_config(page_title='Chat with Multiple PDFs', page_icon='📚')
    
  3. Create the text input and sidebar for uploading PDFs:
    user_question = st.text_input('Ask a question about your documents')
    st.sidebar.file_uploader('Upload your PDFs', accept_multiple_files=True, label='Upload your PDFs here and click on process')
    

Step 3: Handling User Input and Displaying Chat History

  1. Handle user input and display the chat history:
    if user_question:
        response = conversation.get_response(user_question)
        st.write(response)
    

Step 4: Using Hugging Face Models

  1. Import the Hugging Face library:
    from langchain.lms import huggingface
    
  2. Initialize the language model:
    chat_model = huggingface.ChatModel(model_name='Google/ft5')
    

Step 5: Displaying Chat Messages

  1. Create custom HTML templates for chat messages:

    • Create CSS styles for chat_message and bot.
    • Define templates for the user and bot messages.
  2. Display chat messages using templates:

    for index, message in enumerate(st.session_state.chat_history):
        if index % 2 == 0:
            st.write(user_template.format(message['content']))
        else:
            st.write(bot_template.format(message['content']))
    

Step 6: Running the Application

  1. Run the Streamlit application:
    streamlit run app.py
    

Conclusion

Congratulations on building a chatbot application that can interact with multiple PDFs. Feel free to customize and enhance the application further based on your requirements. If you have any questions or feedback, feel free to reach out. Thank you for following along with this tutorial!