Have you ever found yourself needing to analyze your backend data while working on a Django project? It can be a tedious task, often involving diving into SQL tables and crafting complex queries.
Imagine simplifying this process by “chatting” with your data directly within Django Admin. With PandasAI, you harness the power of large language models (LLMs) to ask natural language questions and get instant insights, making data analysis intuitive and efficient.
In this article, I guide you through integrating PandasAI with Django, transforming how you interact with your project’s data. You can find the complete source code for this project in this GitHub repository, which will help you follow along with the steps outlined in this guide.
What Is PandasAI?
PandasAI is a Python library built on top of the popular pandas library that enables users to perform various data related tasks, such as natural language querying, data visualization and data cleaning.
We’ll focus on its natural language querying capabilities, which involve using LLMs to translate natural language questions into Python code or SQL queries to obtain immediate insights from the data.
Setting Up The Chats App
To introduce a chatbot feature within Django Admin, we first need to create an app dedicated to managing chat sessions and individual messages, which we’ll call chats. This setup involves creating models, configuring the agent and its components, implementing services and views, and finally customizing the admin interface.
Models
The core of the chats app lies in its models. The Chat model represents a chat session and is linked to an admin user. Each chat session can include multiple messages, which are managed by the Message model. Messages can be sent by either the user or the chatbot agent.
Additionally, we have the QueryableModel. By extending this abstract model, any Django model becomes queryable, making its data accessible for natural language queries processed by PandasAI. You can optionally add descriptions for the model and its fields, which PandasAI uses to determine the most relevant data when answering questions. While optional, these descriptions significantly enhance response quality.
from typing import Dict
from django.contrib.auth.models import User
from django.core.exceptions import ValidationError
from django.db import models
from django.utils.translation import gettext_lazy as _
class Chat(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="chats")
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def clean(self):
if not self.user.is_staff:
raise ValidationError(_("Only admin users can create chats."))
class Meta:
verbose_name = _("chat")
verbose_name_plural = _("chats")
class Message(models.Model):
class Sender(models.TextChoices):
AGENT = "AGENT", _("Agent")
USER = "USER", _("User")
chat = models.ForeignKey(Chat, on_delete=models.CASCADE, related_name="messages")
sender = models.CharField(max_length=5, choices=Sender)
content = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def save(self, *args, **kwargs):
self.chat.updated_at = self.updated_at
self.chat.save(update_fields=["updated_at"])
super().save(*args, **kwargs)
class Meta:
verbose_name = _("message")
verbose_name_plural = _("messages")
class QueryableModel(models.Model):
description: str = None
field_descriptions: Dict[str, str] = None
class Meta:
abstract = TrueAgent




