DEV Community

Cover image for Creating an Interactive Airbnb Chatbot with Lyzr, OpenAI and Streamlit
harshit-lyzr
harshit-lyzr

Posted on

Creating an Interactive Airbnb Chatbot with Lyzr, OpenAI and Streamlit

In the competitive landscape of the hospitality industry, providing exceptional guest experiences is crucial for retaining customers and gaining positive reviews. Airbnb hosts face the challenge of addressing numerous queries from guests promptly and accurately. Common questions range from property rules and amenities to local recommendations and troubleshooting issues during their stay. Managing these interactions manually can be time-consuming and prone to inconsistencies, especially for hosts managing multiple properties or those who are frequently unavailable to respond immediately.

Airbnb hosts need an efficient, reliable, and consistent way to address guest inquiries to enhance their stay experience. Traditional methods of communication, such as emails and phone calls, can lead to delays and miscommunications. There is a clear need for an automated solution that can provide guests with quick, accurate, and friendly responses to their queries about the property and surrounding area.

The objective is to develop an intelligent chatbot integrated into a Streamlit web application that leverages OpenAI’s API to handle guest inquiries. The chatbot should be able to:

Provide accurate and relevant answers to common questions about the Airbnb property, such as wifi credentials, house rules, check-in/check-out times, and amenities.
Offer personalized local recommendations based on the host’s knowledge.
Operate with a welcoming, friendly, and attentive personality to ensure a positive guest interaction.
Maintain consistency and reliability in the information provided to guests.
We propose the development of the “Airbnb Chatbot,” a Streamlit-based web application that integrates the Lyzr QABot with OpenAI’s API. This chatbot will be trained on a comprehensive FAQ document related to the Airbnb property and will follow a predefined prompt that outlines its personality and response style. The chatbot will:

Utilize advanced natural language processing capabilities to understand and respond to guest inquiries.
Be accessible to guests 24/7 through a user-friendly interface.
Ensure that responses are based strictly on the provided data to avoid misinformation.

Key Metrics for Success:
Response Time: Average time taken to respond to guest queries.
Guest Satisfaction: Measured through feedback and ratings.
Consistency: Accuracy and reliability of the information provided by the chatbot.
Usage Rate: Frequency of guest interactions with the chatbot.

Prerequisites
Before we dive into the code, make sure you have the following:

Python 3.8+ installed.
Streamlit installed (pip install streamlit).
OpenAI API Key.
Lyzr library installed (pip install lyzr).
dotenv library for loading environment variables (pip install python-dotenv).
PIL (Pillow) for image processing (pip install pillow).

Setting Up the Environment
First, ensure that your OpenAI API key is stored in a .env file:

OPENAI_API_KEY=your_openai_api_key_here
Enter fullscreen mode Exit fullscreen mode

Importing Libraries and Loading Environment Variables:


import os
from lyzr import QABot
import openai
from dotenv import load_dotenv
from PIL import Image

load_dotenv()
openai.api_key = os.getenv("OPENAI_API_KEY")
Enter fullscreen mode Exit fullscreen mode

The code starts by importing necessary libraries:
streamlit as st: for building the Streamlit application.
os: for interacting with the operating system.
lyzr and QABot: for using Lyzr's question answering capabilities.
openai: (commented out) potentially for integration with OpenAI services.
dotenv: for loading environment variables (likely containing an OpenAI API key).
It then loads environment variables using load_dotenv() (likely for the OpenAI API key).
An OpenAI API key is set using os.getenv("OPENAI_API_KEY"), but it's currently commented out.

Defining the Chatbot Personality:

prompt=f"""
You are an Airbnb host with a welcoming, friendly, and attentive personality. 
You enjoy meeting new people and providing them with a comfortable and memorable stay. 
You have a deep knowledge of your local area and take pride in offering personalized recommendations. 
You are patient and always ready to address any concerns or questions with a smile. 
You ensure that your space is clean, cozy, and equipped with all the essentials. 
Your goal is to create a home away from home for your guests and to make their stay as enjoyable and stress-free as possible. 
When responding to guest questions, provide clear, helpful, and friendly answers based strictly on the provided data. 
[IMPORTANT]Do not give answers outside of the given information.
"""
Enter fullscreen mode Exit fullscreen mode

A variable prompt is defined as a string containing the desired personality and functionalities for the Lyzr QABot.

Implementing the Chatbot Functionality:


@st.cache_resource
def rag_implementation():
    with st.spinner("Generating Embeddings...."):
        qa = QABot.pdf_qa(
            input_files=["airbnb.pdf"],
            system_prompt=prompt
        )
    return qa

st.session_state["chatbot"] = rag_implementation()
Enter fullscreen mode Exit fullscreen mode

A function rag_implementation() is defined using @st.cache_resource. This function uses Streamlit caching to avoid redundant computations.

It displays a spinner message “Generating Embeddings…” while the code executes.
Inside the function, it creates a QABot instance using QABot.pdf_qa().
It specifies the input file “airbnb.pdf” containing Airbnb information and FAQs.
It sets the system prompt to the previously defined prompt to guide the chatbot's responses.
The function returns the created QABot object.

Managing Chat State and User Interactions:

if "messages" not in st.session_state:
    st.session_state.messages = []

for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

if "chatbot" in st.session_state:
    if prompt := st.chat_input("What is up?"):
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.markdown(prompt)

        with st.chat_message("assistant"):
            response = st.session_state["chatbot"].query(prompt)
            chat_response = response.response
            response = st.write(chat_response)
        st.session_state.messages.append(
            {"role": "assistant", "content": chat_response}
        )
Enter fullscreen mode Exit fullscreen mode

The code retrieves the chatbot instance from the session state (st.session_state["chatbot"]) using the result of rag_implementation().
It checks if a key “messages” exists in the session state. If not, it initializes an empty list for messages (st.session_state.messages = []).
The code iterates through the existing messages in the session state and displays them using st.chat_message() with the message role ("user" or "assistant") and content.
The code checks if the chatbot instance exists in the session state.
If the chatbot exists, it displays a chat input box using st.chat_input() with a prompt "What is up?".
If the user enters a message, it’s stored in the session state messages list with the user role (“user”). The message is also displayed with st.chat_message().
With another st.chat_message(), the code calls the chatbot's query() method with the user message to get a response.
The chatbot response is stored in a variable chat_response.
The response is displayed on the screen using st.write().
Finally, the chatbot response is added to the session state messages with the assistant role (“assistant”).

Running the App:

To run the Streamlit app, save the code in a file (e.g., app.py) and use the following command:

streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

try it now: https://lyzr-airbnb-host.streamlit.app/

For more information explore the website: Lyzr

Github: https://github.com/harshit-lyzr/airbnb_chatbot

Top comments (0)