DEV Community

Cover image for Building a RAG Employee-HR Q&A with Lyzr
Prajjwal Sule
Prajjwal Sule

Posted on

Building a RAG Employee-HR Q&A with Lyzr

In HR departments, managing employee inquiries regarding company policies and procedures is a crucial yet time-consuming task. To streamline this process and ensure prompt and accurate responses, we introduce an innovative solution: an AI-powered HR QA-Bot built with RAG (Retrieval-Augmented Generation) technology. In this article, we'll guide you through the creation of this QA-Bot using Streamlit and Lyzr, facilitating seamless communication between HR professionals and employees.

Introduction

Handling employee inquiries manually can be inefficient and prone to errors. Our goal is to automate this process using RAG, a state-of-the-art model for question-answering tasks. By leveraging Lyzr SDKs and Streamlit, we'll develop a user-friendly application that empowers HR professionals to efficiently address employee questions.

Lyzr SDKs: Your Gateway to GenAI Applications

Lyzr offers an agentic approach to building next-generation GenAI applications, even for users with limited knowledge of generative AI. By simplifying complex AI concepts, Lyzr enables rapid development of custom applications.

If you're new to GenAI or Lyzr, you can explore its capabilities here. Join the Lyzr community on Discord to engage with like-minded developers.

Building the Application

Before diving into development, let's set up the project environment and install necessary dependencies:

Python3 -m venv venv
source venv/bin/activate
pip install lyzr streamlit openai pdfminer.six
Enter fullscreen mode Exit fullscreen mode

Create a utils directory to store common functions

import os
import shutil
import streamlit as st

def remove_existing_files(directory):
    for filename in os.listdir(directory):
        file_path = os.path.join(directory, filename)
        try:
            if os.path.isfile(file_path) or os.path.islink(file_path):
                os.unlink(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path)
        except Exception as e:
            st.error(f"Error while removing existing files: {e}")

def get_files_in_directory(directory):
        files_list = []
    if os.path.exists(directory) and os.path.isdir(directory):
        for filename in os.listdir(directory):
            file_path = os.path.join(directory, filename)

            if os.path.isfile(file_path):
                files_list.append(file_path)
    return files_list

def save_uploaded_file(uploaded_file):
    remove_existing_files('company_documents')
    file_path = os.path.join('company_documents', uploaded_file.name)
    with open(file_path, "wb") as file:
        file.write(uploaded_file.read())
    st.success("File uploaded successfully")

Enter fullscreen mode Exit fullscreen mode

remove_existing_files: This function deletes all files and directories within a specified directory. It ensures a clean slate before saving new files.

get_files_in_directory: Retrieves the list of file paths within a specified directory.

save_uploaded_file: Saves an uploaded file to a designated directory after clearing existing files.

Initialize our Streamlit app

import streamlit as st
import os
from PIL import Image
from utils import utils
from lyzr import QABot 
import openai
from dotenv import load_dotenv; load_dotenv()
Enter fullscreen mode Exit fullscreen mode

We import necessary modules for building the app, including UI components, file handling utilities, and Lyzr functionalities.

Make sure that you have your OpenAI API Key.

Implement the main functionality for the HR Bot

def hr_rag():
    # This function will implement the HR Bot.
    path = utils.get_files_in_directory(company_documents)
    path = path[0]

    rag = QABot.pdf_qa(
        input_files=[str(path)],
        llm_params={"model": "gpt-3.5-turbo"},
    )
    return rag

Enter fullscreen mode Exit fullscreen mode

The hr_rag function initializes the HR Bot instance using Lyzr's QABot, specifying the path to company documents and model parameters.

def hr_page():
    st.title("HR Page")
    st.subheader("Upload Company Documents")

    # Upload PDF file
    uploaded_file = st.file_uploader("Choose PDF file", type=["pdf"])
    if uploaded_file is not None:
        utils.save_uploaded_file(uploaded_file)
    else:
        utils.remove_existing_files(company_documents)

Enter fullscreen mode Exit fullscreen mode

hr_page function will provide the interface for HR to upload the company documents for their employees.

def employee_page():
    file = []
    for filename in os.listdir(company_documents):
        file_path = os.path.join(company_documents, filename)
        file.append(file_path)
    if len(file) > 0:
        st.title("Employee Page")
        st.subheader("Ask your questions")
        question = st.text_input("What is your questions?")
        if st.button("Get Answer"):
            rag = hr_rag()
            response = rag.query(question)
            st.markdown(f"""{response.response}""")
    else:
        st.error('HR has not uploaded the documents')

Enter fullscreen mode Exit fullscreen mode

The employee_page function provides an interface for employees to ask questions and fetches answers from the HR Bot.

def main():
    st.sidebar.title("Navigation")
    selection = st.sidebar.radio("Go to", ["HR Page", "Employee Page"])
    if selection == "HR Page":
        hr_page()
    elif selection == "Employee Page":
        employee_page()

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

The main function presents a sidebar for navigation between HR and Employee pages, calling corresponding functions based on user selection.

This is how to build an AI-powered HR QA-Bot using Lyzr SDKs and Streamlit. By harnessing the power of RAG technology, HR departments can efficiently handle employee inquiries, leading to improved productivity and employee satisfaction. Clone the repository and explore further possibilities with Lyzr SDKs.

HR Bot by Lyzr - GitHub

References

Lyzr Website
Book a Demo
Lyzr Community Channels: Discord
HRBot GitHub

Top comments (0)