Skip to main content

AI chatbot assistant for Django/DRF that analyzes your project models and responds to queries

Project description

Django AI Chatbot Assistant

An intelligent AI chatbot package for Django/DRF that analyzes your project's models and data to provide context-aware responses. Integrates with HuggingFace LLMs and optional Tavily web search.

Features

  • 🤖 HuggingFace Integration: Use any HuggingFace model for responses
  • 📊 Auto Model Discovery: Automatically introspects Django models and fields
  • 🌐 Web Search: Optional Tavily API integration for current web data
  • ⚙️ Easy Configuration: Simple Django settings integration
  • 🔒 Model Filtering: Control which models the chatbot can access

Installation

pip install django-ai-chatbot

Quick Start

1. Add to Django Settings

# settings.py

INSTALLED_APPS = [
    # ... other apps
    'ai_chatbot',
]

# Required: HuggingFace Configuration
AI_CHATBOT_HF_API_KEY = 'your-huggingface-api-key'
AI_CHATBOT_HF_MODEL = 'mistralai/Mistral-7B-Instruct-v0.2'  # Optional, this is default

# Optional: Tavily Web Search
AI_CHATBOT_TAVILY_API_KEY = 'your-tavily-api-key'  # Optional

# Optional: Restrict which models the chatbot can access
AI_CHATBOT_ALLOWED_MODELS = [
    'myapp.User',
    'myapp.Product',
    'blog.Post',
]  # If empty or not set, all models are accessible

2. Use in Your Code

from ai_chatbot import AIChatbot

# Initialize chatbot
chatbot = AIChatbot()

# Ask a question about your models
response = chatbot.ask("What fields does the User model have?")
print(response)

# Use web search for current information
response = chatbot.ask(
    "What are the latest trends in Django development?",
    use_web_search=True
)
print(response)

# Customize generation parameters
response = chatbot.ask(
    "Explain the Product model structure",
    max_tokens=1000,
    temperature=0.5
)

3. Example in Django View

from django.http import JsonResponse
from ai_chatbot import AIChatbot

def chatbot_view(request):
    query = request.GET.get('query', '')
    use_web = request.GET.get('web_search', 'false').lower() == 'true'
    
    chatbot = AIChatbot()
    response = chatbot.ask(query, use_web_search=use_web)
    
    return JsonResponse({'response': response})

4. Example in DRF ViewSet

from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework import viewsets
from ai_chatbot import AIChatbot

class ChatbotViewSet(viewsets.ViewSet):
    @action(detail=False, methods=['post'])
    def ask(self, request):
        query = request.data.get('query')
        use_web = request.data.get('use_web_search', False)
        
        chatbot = AIChatbot()
        response = chatbot.ask(query, use_web_search=use_web)
        
        return Response({'response': response})

Configuration Options

Setting Required Default Description
AI_CHATBOT_HF_API_KEY Yes None Your HuggingFace API key
AI_CHATBOT_HF_MODEL No mistralai/Mistral-7B-Instruct-v0.2 HuggingFace model to use
AI_CHATBOT_TAVILY_API_KEY No None Tavily API key for web search
AI_CHATBOT_ALLOWED_MODELS No [] (all models) List of models to expose (format: app.Model)

API Reference

AIChatbot.ask()

chatbot.ask(
    query: str,
    use_web_search: bool = False,
    max_tokens: int = 500,
    temperature: float = 0.7
) -> str

Parameters:

  • query: The user's question
  • use_web_search: Enable Tavily web search
  • max_tokens: Maximum tokens in response
  • temperature: LLM temperature (0.0-1.0)

Returns: Generated response string

How It Works

  1. Model Introspection: Automatically discovers all Django models and their fields
  2. Context Building: Creates a schema context from your models
  3. Web Search (optional): Fetches current web data via Tavily
  4. LLM Generation: Sends context + query to HuggingFace model
  5. Response: Returns AI-generated answer based on your project data

Requirements

  • Python >= 3.8
  • Django >= 3.2
  • huggingface-hub >= 0.19.0
  • requests >= 2.31.0

License

MIT License - see LICENSE file for details

Contributing

Contributions welcome! Please open an issue or submit a PR.

Support

For issues and questions: https://github.com/hitenjoshi/django-ai-chatbot/issues

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_ai_chatbot-0.1.0.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_ai_chatbot-0.1.0-py3-none-any.whl (6.9 kB view details)

Uploaded Python 3

File details

Details for the file django_ai_chatbot-0.1.0.tar.gz.

File metadata

  • Download URL: django_ai_chatbot-0.1.0.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.9

File hashes

Hashes for django_ai_chatbot-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8c776982309a20f82575f1e6a8a4850ef2ab0849d89346d87687325361f7c3a3
MD5 1a4b1f5919b885267518a5ec902900ae
BLAKE2b-256 1813cebc22ad641f925a57c8f373b564514b40741a4e12b46d9ce8baa2efa49d

See more details on using hashes here.

File details

Details for the file django_ai_chatbot-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ai_chatbot-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f763a8b751b05d8a7215a51b967ce3b666de03bae81d7f4a52dca855084c6f57
MD5 00fd8dc0f0a6635a830c099105ccc2bf
BLAKE2b-256 5d6086f87f607160617ab399c488cb70b1acc09efc281903059225168c1d665e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page