django-ai-chatbot 0.1.0
AI chatbot assistant for Django/DRF that analyzes your project models and responds to queries
License: MIT
Requires Python: >=3.8
Keywords: ai, chatbot, django, drf, artificial-intelligence, huggingface, tavily

# 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

```bash
pip install django-ai-chatbot
```

## Quick Start

### 1. Add to Django Settings

```python
# 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

```python
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

```python
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

```python
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()

```python
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