Building a Policy Information Chatbot with Streamlit and Lyzr
Published: 2024-05-07 12:37:00

The insurance industry traditionally relies on phone calls, emails, and in-person interactions for customer inquiries about policies. This approach can be time-consuming and frustrating for customers seeking quick answers.

In today’s digital age, customers expect efficient and readily available information. This is where chatbots can play a transformative role.

Here’s the challenge: How can we leverage chatbot technology to create a user-friendly experience for insurance policyholders, allowing them to access information, get assistance with policy inquiries, and receive support quickly and easily, all within a secure platform?

By developing a chatbot specifically focused on health insurance policies, we can empower customers with 24/7 access to relevant information, improve overall customer satisfaction, and potentially reduce the burden on traditional customer service channels.

In this blog post, we’ll explore how to build a Policy Information Chatbot using Streamlit and Lyzr, making it easier for users to inquire about health insurance policies.

**Setting Up the Environment**
**Imports:**

```
pip install lyzr streamlit
import streamlit as st
import os
from lyzr import QABot
import openai
from dotenv import load_dotenv
from PIL import Image
```
First, let’s ensure we have the necessary libraries installed. We’ll need Streamlit, PIL (Python Imaging Library), Lyzr, OpenAI, and dotenv. If you don’t have them installed already.

**Chatbot Prompt:**

```
prompt=f"""
You are an Health Insurance Policy Chatbot.
Task: Your Task Is to provide information about available health insurance plans, assist with policy inquiries,
coverage limits,terms and conditions, help with claims processing, or offer general healthcare advice from given Document.
Context : 
1/ Collect comprehensive information about the health insurance policies from the document.Ensure that the given Answer complies with relevant healthcare regulations and data privacy laws, such as HIPAA (Health Insurance Portability and Accountability Act) in the United States. 
2/ Implement measures to protect sensitive user information and provide clear disclosures about data handling practices.
3/ Understand the needs and preferences of User.

Review:
The Answer is retrieved from document only.
"""
```
A long string variable prompt defines instructions for the Lyzr QABot.

It specifies the role as a health insurance policy chatbot.
The task involves providing information on plans, handling policy inquiries, and offering general healthcare advice based on a document.

**Using Lyzr QABot:**

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

        )
    return qa
```
The @st.cache_resource decorator ensures the QABot model is generated only once.

It uses st.spinner to display a loading message while the model is created.
The QABot.pdf_qa function from the lyzr library is used to create the QABot model, processing the "insurance.pdf" document and providing the defined prompt instructions.

**Session State and Chat History:**

```
st.session_state["chatbot"] = rag_implementation()

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"])
```
The st.session_state["chatbot"] variable stores the generated QABot model in the session state for persistence across user interactions.
An empty list st.session_state.messages is created to store chat history.
The code iterates through the messages list and displays each message ("user" or "assistant") and its content using st.chat_message and st.markdown.

**User Interaction and Chat Response:**

```
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}
        )
```
If the chatbot model exists in the session state:

The code displays a chat input box titled “What is up?” using st.chat_input.
When the user enters a prompt and submits it:
The user prompt is stored in st.session_state.messages with a "user" role.
The prompt is displayed as a chat message from the user.
The query method of the QABot model is called with the user prompt to retrieve a response.
The retrieved response is displayed as a chat message from the assistant (“assistant”).
The response is also stored in st.session_state.messages with an "assistant" role.

try it now: [https://lyzr-insurance-chatbot.streamlit.app/](https://lyzr-insurance-chatbot.streamlit.app/)

Github: [https://github.com/harshit-lyzr/policy_info](https://github.com/harshit-lyzr/policy_info)

For more information explore the website: [Lyzr](https://www.lyzr.ai/)