Bot making Bot: Data to Dialog

with Streamlit, Langchain, HuggingFace and VertexAI Palm APIs

--

Photo by Vidar Nordli-Mathisen on Unsplash

In a world where technology continues to push boundaries, a remarkable feat is emerging at the intersection of AI and conversation. Imagine building a Google Bard or ChatGPT-style chatbot that not only mimics human interaction but responds using your own data. Intrigued? Let’s delve into a transformative workflow that brings this innovation to life. A bot making a Bot.

Demo

Watch the GST FAQs bot in action — a testament to this tech’s prowess, designed from another bot-wizard. However, this isn’t just about one bot. Swap data files and envision your custom creation, equipped to handle your domain’s demands.

The process is both fascinating and intuitive — a wizard-like interface guides you through the steps, from naming your bot to specifying training data. The result? A fully functional bot that’s ready to converse in a matter of seconds.

Code Explanation

bot_config_wizard.py: This code defines a graphical user interface (GUI) application using the tkinter library to create a configuration wizard for generating a FAQ bot. The output bot_config.json looks like below:

{
"APP_NAME": "MyApp",
"DOCS_INDEX":"/fullpath/to/docs.index",
"FAISS_STORE_PKL":"/fullpath/to/faiss_store.pkl",
"FILES_PATHS": [
"/fullpath/to/file1.csv",
"/fullpath/to/file2.txt",
"/fullpath/to/file3.pdf"
]
}

You can manually edit it or run following wizard app to set the same parameters, it writes the same.

The BotConfigWizard class is the heart of the application. Its constructor takes a root parameter, which is a main window where all GUI components will be displayed. The constructor initializes the GUI and calls the create_widgets method to create various components.

class BotConfigWizard:
def __init__(self, root):
# ...
self.create_widgets()
    def create_widgets(self):
# ...

This method creates various widgets like labels, entry fields, listboxes, buttons, etc., and organizes them within the main window.

    def browse_models_folder(self):
# ...

def browse_files(self):
# ...

def ok_button_callback(self):
# ...

def cancel_button_callback(self):
# ...

These callback functions are triggered by various user interactions:

  • browse_models_folder: Opens a folder selection dialog and sets the selected folder path to a variable.
  • browse_files: Opens a file selection dialog and inserts selected file paths into a listbox.
  • ok_button_callback: Collects the entered bot name, selected files, and models folder, then creates a dictionary with this data. This dictionary is then saved as a JSON file named `bot_config.json`.
  • cancel_button_callback: Closes the application when the "Cancel" button is clicked.

streamlit_main.py

This Python script serves as the frontend for a FAQ bot built using the Streamlit framework. The bot interacts with users, retrieves information, and generates responses based on configured data sources.

class MyFAQsBot:
def __init__(self, config_json):
# ...
self.create_chain()

# ... (other methods)

The MyFAQsBot class is responsible for creating and interacting with the bot. The constructor initializes the bot's attributes using data from the configuration JSON file. It then calls the create_chain method to set up the bot's retrieval and language model chain.

    def create_chain(self):
# ...
return chain

This method generates a retrieval and language model chain. If the bot’s index exists, it’s loaded; otherwise, data is loaded from various file types (CSV, PDF, TXT, HTML), embeddings are generated, and the FAISS index is created. The chain is then constructed using the LLM and retriever.

    def run_ui(self):
# ...
bot.run_ui()

The run_ui method executes the Streamlit interface for user interaction. It handles user inputs, processes them through the chain, and displays messages using the streamlit_chat module.

The whole working code for the app is made available at

A Journey Beyond Proof of Concept

While this workflow might start as a proof of concept (PoC), its potential for growth is boundless. What begins on a small scale has the power to evolve into a robust enterprise-level application. Picture deploying these AI-powered bots across various industries — from customer support to content creation — enhancing efficiency and customer engagement.

Exploring New Horizons

In a world brimming with possibilities, the Vertex AI and PaLM APIs are your starting point. However, don’t hesitate to explore alternatives that resonate with you. Langchain introduces an array of language models to choose from, allowing you to customize your AI’s personality and responses.

A Weekend Project?

This transformative journey isn’t just for students. It’s an invitation for generative AI enthusiasts and professionals alike to take part. Think of it as a weekend project that promises both intellectual satisfaction and tangible outcomes.

Conclusion

As you embark on this journey, imagine the doors that will open. Your personalized AI bot, conversing fluently in your domain, stands as a testament to innovation’s unending potential. From conceptualizing a bot to fostering insightful dialogues, you’re at the helm of reshaping interactions in the digital age. Embrace this journey and uncover the future of conversational AI, one dialogue at a time.

Feel free to follow me at LinkedIn

--

--

Yogesh Haribhau Kulkarni (PhD)
Google Developer Experts

PhD in Geometric Modeling | Google Developer Expert (Machine Learning) | Top Writer 3x (Medium) | More at https://www.linkedin.com/in/yogeshkulkarni/