These notes are about how to build an AI agent from scratch.
We will use Python and Django to develop this project. For details on installation and setup, see here.
Overall, we need:
url.pyviews.pyNote: for all the complete code mentioned in these notes, check the GitHub repo.
User sends a message
↓
Backend receives the message
↓
Backend calls an AI API
↓
AI API returns a response
↓
Backend optionally saves the conversation
↓
Backend sends the response back to the user
User opens /chat/
↓
Django renders chat.html
↓
User types message and clicks send
↓
chat.js sends POST request to /api/chat/
↓
Django chat_api view receives the request
↓
Django reads the message from request.body
↓
Django calls ask_ai(message) from ai_service.py
↓
ai_service.py sends the message to Gemini API
↓
Gemini returns AI response
↓
Django saves user message + AI response to SQLite
↓
Django returns JSON response to frontend
↓
chat.js displays AI reply on the page
urls.py = where the request goes
views.py = what happens when request arrives
templates/chat.html = what user sees
ai_service.py = how Django talks to AI API
models.py = database, only needed if saving data