A Django application that provides AI-powered assistance through integration with Anthropic's Claude API. This app allows users to ask questions and receive intelligent responses from Claude AI within a Django web application.
- AI Question Interface: Clean, user-friendly form for submitting questions to AI
- Claude API Integration: Powered by Anthropic's Claude-3-Opus model
- Internationalization Support: Built-in support for multiple languages using Django's i18n framework
- HTMX Integration: Dynamic form submission without page reloads
- Error Handling: Comprehensive error handling for API failures, timeouts, and authentication issues
- Bootstrap UI: Modern, responsive user interface using Bootstrap components
This app is designed to be used as a git submodule in your main Django project:
# Add as submodule
git submodule add git@github.com:marobo/ai_assistance.git ai_assistance
# Initialize and update submodule
git submodule update --init --recursive- Add to INSTALLED_APPS in your Django settings:
INSTALLED_APPS = [
# ... your other apps
'ai_assistance',
]- Configure API Key in your settings:
# Required: Anthropic API key
ANTHROPIC_API_KEY = 'your-anthropic-api-key-here'- Include URLs in your main
urls.py:
from django.urls import path, include
urlpatterns = [
# ... your other URLs
path('', include('ai_assistance.urls')),
]- Run migrations (if any are added in the future):
python manage.py migrateThis app requires the following Python packages:
Django(compatible with Django's internationalization framework)httpx(for HTTP requests to Anthropic API)
Add to your requirements.txt:
httpx>=0.24.0
ANTHROPIC_API_KEY: Your Anthropic API key for Claude access
AI_ASSISTANCE_BASE_TEMPLATE: Template name to extend (default:'base.html')AI_ASSISTANCE_BASE_TEMPLATE_FUNC: Dotted path to a callable(request) -> strreturning a base templateAI_ASSISTANCE_HX_TARGET_ID: DOM id for HTMX swaps (default:'main-content')AI_ASSISTANCE_SYSTEM_PROMPT: Static system prompt stringAI_ASSISTANCE_SYSTEM_PROMPT_FUNC: Dotted path to a callable(request) -> strfor dynamic promptAI_ASSISTANCE_MODEL: Model name (default:'claude-3-opus-20240229')AI_ASSISTANCE_TIMEOUT: Request timeout seconds (default:20)
Internationalization:
LANGUAGE_CODE,USE_I18N,LOCALE_PATHSper standard Django i18n
Once installed and configured, users can access the AI assistant at:
http://localhost:8000/ask-ai/
- Ask Questions: Users can submit any question through a textarea form
- Get AI Responses: Receive intelligent responses from Claude AI
- Error Handling: Graceful handling of API errors with user-friendly messages
- Headless Core: Logic extracted to
ai_assistance/core.py(no Django/UI coupling) - JSON API:
POST /ai/api/ask/returns{ "answer": "..." }
- Model: Claude-3-Opus (claude-3-opus-20240229)
- Max Tokens: 300 per response
- Timeout: 20 seconds
- API Version: 2023-06-01
ai_assistance/
├── __init__.py
├── apps.py # Django app configuration
├── admin.py # Django admin configuration
├── models.py # Database models (currently empty)
├── core.py # Headless core that calls the LLM API (httpx)
├── utils.py # Resolvers for template/prompt/model/timeout/hx target
├── views.py # Thin Django views (HTML + JSON) using core/utils
├── urls.py # URL routing
├── tests.py # Unit tests
├── migrations/ # Database migrations
├── templates/
│ └── ai_assistance/
│ ├── ai_question_page.html # Question form interface
│ └── ai_response.html # Response display page
├── LICENSE # MIT License
├── README.md # This file
└── .gitignore # Git ignore rules
The app includes comprehensive error handling for:
- Missing API Key: Displays configuration error message
- Authentication Failures: API key validation errors
- Rate Limiting: Handles API rate limit responses
- Network Timeouts: Connection timeout handling
- General API Errors: Graceful handling of other API issues
- API keys should be stored securely (use environment variables)
- The app includes CSRF protection for form submissions
- Input validation prevents empty question submissions
This is a git submodule, so contributions should be made to the original repository. When making changes:
- Make changes in the submodule directory
- Commit changes in the submodule
- Update the parent project to reference the new commit
This project is licensed under the MIT License - see the LICENSE file for details.
For issues related to:
- Django Integration: Check Django documentation and ensure proper configuration
- API Issues: Verify your Anthropic API key and account status
- UI Problems: Ensure Bootstrap and HTMX are properly loaded in your base template
- Django: 3.2+ (uses modern class-based views and i18n)
- Python: 3.8+ (required for httpx)
- Anthropic API: Uses 2023-06-01 API version