A full-stack chatbot application using Google's Gemini Pro API with a Django REST API backend and a React TypeScript frontend.
- π€ Powered by Google Gemini Pro AI
- π¬ Real-time chat interface
- π Conversation history and session management
- π Session-based chat persistence
- π± Responsive design
- π― TypeScript for type safety
- π Django REST API backend
- Django 5.2 - Web framework
- Django REST Framework - API development
- Google Generative AI - Gemini Pro API integration
- SQLite - Database (easily replaceable)
- Django CORS Headers - Cross-origin requests
- React 18 - Frontend framework
- TypeScript - Type safety
- Axios - HTTP client
- CSS3 - Styling
gemini-chatbot/
βββ backend/ # Django backend
β βββ chatbot_project/ # Django project
β βββ chatbot/ # Django app
β β βββ models.py # Database models
β β βββ views.py # API views
β β βββ serializers.py # DRF serializers
β β βββ gemini_service.py # Gemini API service
β β βββ urls.py # URL routing
β βββ manage.py
β βββ .env # Environment variables
βββ frontend/ # React TypeScript frontend
βββ src/
β βββ components/ # React components
β βββ services/ # API services
β βββ types/ # TypeScript types
β βββ App.tsx
βββ package.json
- Python 3.8+
- Node.js 14+
- npm or yarn
- Google Gemini API key
-
Navigate to backend directory:
cd backend
-
Install Python dependencies:
pip install django djangorestframework django-cors-headers google-generativeai python-dotenv
-
Configure environment variables: Edit
backend/.env
and add your Gemini API key:GEMINI_API_KEY=your_actual_gemini_api_key_here DEBUG=True SECRET_KEY=your-secret-key-here
-
Run migrations:
python manage.py makemigrations python manage.py migrate
-
Start the Django server:
python manage.py runserver
Backend will be available at
http://localhost:8000
-
Navigate to frontend directory:
cd frontend
-
Install dependencies:
npm install
-
Start the React development server:
npm start
Frontend will be available at
http://localhost:3000
- Go to Google AI Studio
- Sign in with your Google account
- Create a new API key
- Copy the key and add it to your
.env
file
POST /api/chat/
- Send a chat messagePOST /api/new-session/
- Create a new chat sessionGET /api/history/{session_id}/
- Get chat history for a session
Send a message:
POST /api/chat/
{
"message": "Hello, how are you?",
"session_id": "optional-session-id"
}
Response:
{
"session_id": "uuid-session-id",
"message": "Hello, how are you?",
"response": "Hello! I'm doing well, thank you for asking...",
"message_id": 123
}
- Start both backend and frontend servers
- Open
http://localhost:3000
in your browser - Start chatting with the Gemini AI
- Use "New Session" button to start a fresh conversation
- Your conversation history is automatically saved
- Each conversation is stored in a session
- Sessions persist across page refreshes
- Users can start new sessions anytime
- The chatbot maintains context within a session
- Previous messages are included for better responses
- Last 5 messages are used for context to avoid token limits
- Graceful error handling for API failures
- User-friendly error messages
- Loading states for better UX
- Modify CSS files in
frontend/src/components/
- Update color scheme in CSS variables
- Responsive design included
- Adjust context length in
gemini_service.py
- Modify database models in
models.py
- Add authentication if needed
- Set
DEBUG=False
in production - Use a production database (PostgreSQL recommended)
- Configure proper CORS settings
- Use environment variables for sensitive data
- Build the project:
npm run build
- Deploy the
build
folder to your hosting service - Update API URL in
api.ts
for production
-
CORS Errors:
- Ensure
django-cors-headers
is installed and configured - Check CORS_ALLOWED_ORIGINS in settings.py
- Ensure
-
API Key Issues:
- Verify your Gemini API key is correct
- Check if the API key has proper permissions
-
Database Issues:
- Run migrations:
python manage.py migrate
- Reset database if needed: Delete
db.sqlite3
and re-run migrations
- Run migrations:
-
Frontend Build Issues:
- Clear npm cache:
npm cache clean --force
- Delete
node_modules
and reinstall:rm -rf node_modules && npm install
- Clear npm cache:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is open source and available under the MIT License.
If you encounter any issues or have questions, please:
- Check the troubleshooting section
- Review the API documentation
- Create an issue in the repository
- Google Gemini Pro API for AI capabilities
- Django and React communities for excellent frameworks
- Contributors and testers