Building a Powerful Generative AI Chatbot with Pandas Dataframe: A Step-by-Step Guide Using LangChain and Gradio

Ravindra Elicherla
Chatbots Life
Published in
4 min readMay 7, 2023

--

Chatbots so far are using trained datasets to give correct answers to user queries. Generative AI made life simple. Now anyone can create a chatbot easily with CSV files, PDF documents, and Pandas data frame

What is a chatbot?

A chatbot is a computer program that simulates human conversation through artificial intelligence (AI) and natural language processing (NLP) techniques. Chatbots are designed to understand and respond to user queries in a conversational manner, much like a human would. They can be programmed to provide information, answer questions, complete tasks, and even carry out simple transactions. Chatbots can be integrated into various messaging platforms, websites, and mobile apps to provide automated customer support, improve user engagement, and enhance the overall user experience.

What is LangChain?

Large language models (LLMs) are emerging as a transformative technology, enabling developers to build applications they previously could not. However, using these LLMs in isolation is often insufficient for creating a truly powerful app — the real power comes when you can combine them with other sources of computation or knowledge. LangChain library aims to assist in the development of those types of applications. Applications of LangChain include documentation, creating agents, and Chatbots.

What is Gradio?

Gradio is an open-source Python library that allows developers and researchers to quickly create customizable UIs for their machine-learning models, without requiring any web development experience. With Gradio, users can create interfaces for their models in minutes, using a simple API and a range of pre-built input and output components, including sliders, checkboxes, text boxes, and images. Gradio also supports a range of data types, including numerical data, text data, image data, and audio data, and can be used with a variety of popular machine learning libraries, including TensorFlow, PyTorch, and Scikit-learn. Gradio is designed to be easy to use, flexible, and customizable, making it an ideal tool for anyone looking to create a user-friendly interface for their machine-learning models.

https://chatbotslife.com/get-certified-in-chatgpt-conversational-ux-dialogflow-425c02ec15cb
Chatathon by Chatbot Conference

Let’s start with hands-on.

  1. Create a file called apikey.py

Put your own Open AI API key here. Do not use the above key. It will not work :).

2. You can create your own key at https://platform.openai.com/account/api-keys. I had rate limit errors, so I went for paid subscription.

3. Create a file called chatagent.py and import the below libraries

Import Libraries

CSV agent is only required if you are trying directly query a CSV file.

In this example, I will not be reading from a CSV file but will read from Yahoo Finance. My plan is to download one year of Nifty daily data for analysis.

Import Nifty data

I am using Macbook. The below code will set the API key

4. Let’s create an agent. I am calling it a nifty-agent and passing daily

What is OpenAI temperature? Temperature is a parameter of OpenAI ChatGPT, GPT-3, and GPT-4 models that govern the randomness and, thus, the creativity of the responses. It is always a number between 0 and 1. 0 is more certain, and 1 is inconsistent and creative

5. We are almost there. Let’s try a simple query

We get a response like this.

6. These are trading days in the last year. Of course, this is not a great way to interact with the program. This is where Gradio comes in handy.

Below is the snippet of code to create UI using Gradio

If everything is right, you will see below

7. now go to the browser and enter http://127.0.0.1:7860/?__theme=dark

You will see below

Let’s interact with data frame now.

Some deep analysis questions now. Thursday is the weekly expiry for Nifty.

Buy me a coffee if you liked the blog

--

--

Geek, Painter, Fitness enthusiast, Book worm, Options expert and Simple human