Overview

These notes are about how to build an AI agent from scratch.

Prerequisite

We will use Python and Django to develop this project. For details on installation and setup, see here.

Overall, we need:

  1. Install the Django project and apps
  2. Adjust settings to connect the project to the apps
  3. Create app-level url.py
  4. Basic View function views.py
  5. Create templates, UI what user sees

Note: for all the complete code mentioned in these notes, check the GitHub repo.

Development Process

General Idea

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

Workflow

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

Structure

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