DEV Community

Cover image for Build Your Own AI Chatbot Like ChatGPT — A Practical Guide with Code
Rajni Devi
Rajni Devi

Posted on

Build Your Own AI Chatbot Like ChatGPT — A Practical Guide with Code

From a Solo Dev Who Actually Did It

Hey, I’m Rajni — an AI developer who once spent 72 hours straight trying to get a language model to write a love poem in Hindi. It failed. But that failure taught me more than any tutorial ever could.

Today, I’m walking you through how I built a working ChatGPT-like AI — not with billion-dollar GPUs, but with free tools, open-source models, and a lot of coffee.

This isn’t theory. This is code that runs today.

Step 1: Pick Your Brain (The Model)

You don’t need GPT-4. Start with open-source LLMs that fit on a laptop.

# Best free models (2025)
- microsoft/DialoGPT-medium      → Fast, conversational
- google/flan-t5-large           → Great for reasoning
- meta-llama/Llama-3-8B-Instruct → Best quality (needs 16GB VRAM)
Enter fullscreen mode Exit fullscreen mode

I chose DialoGPT-medium — it’s lightweight, MIT-licensed, and perfect for chat.

from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = "microsoft/DialoGPT-medium"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
Enter fullscreen mode Exit fullscreen mode

Step 2: Make It Talk (The Chat Loop)

Here’s the core chat engine — 20 lines that power the whole thing:

def chat_with_bot(user_input, chat_history_ids=None):
    # Encode user input
    new_input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')

    # Append to history
    bot_input_ids = torch.cat([chat_history_ids, new_input_ids], dim=-1) if chat_history_ids is not None else new_input_ids

    # Generate response
    chat_history_ids = model.generate(
        bot_input_ids,
        max_length=1000,
        num_beams=5,
        early_stopping=True,
        pad_token_id=tokenizer.eos_token_id
    )

    # Decode reply
    reply = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
    return reply, chat_history_ids
Enter fullscreen mode Exit fullscreen mode

Human Touch: I added a personality_prompt — “You are a sarcastic AI poet from Mumbai.” The bot now replies in shayari when bored.

Step 3: Wrap It in a Web App (FastAPI + Streamlit)

No one wants a terminal bot. I built a beautiful UI in 30 minutes.

# app.py (Streamlit)
import streamlit as st
from chat_engine import chat_with_bot

st.title("RajniGPT — Your Desi AI Friend")
if "history" not in st.session_state:
    st.session_state.history = None

user_input = st.text_input("You:")
if user_input:
    reply, st.session_state.history = chat_with_bot(user_input, st.session_state.history)
    st.write(f"**Bot:** {reply}")
Enter fullscreen mode Exit fullscreen mode

Run:

streamlit run app.py
Enter fullscreen mode Exit fullscreen mode

Step 4: Deploy for Free (Docker + GitHub Pages)

# Dockerfile
FROM python:3.10-slim
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
CMD ["streamlit", "run", "app.py", "--server.port=8501"]
Enter fullscreen mode Exit fullscreen mode

Push to GitHub → GitHub Actions → Deploy to Railway.app (free tier).

Step 5: Make It Smarter (Fine-Tuning)

Want it to sound like you? Fine-tune on your WhatsApp chats.

from transformers import Trainer, TrainingArguments

# Load your chat logs
dataset = load_dataset("text", data_files="my_chats.txt")

training_args = TrainingArguments(
    output_dir="./rajni-gpt",
    num_train_epochs=3,
    per_device_train_batch_size=4,
    save_steps=500,
)

trainer = Trainer(model=model, args=training_args, train_dataset=dataset["train"])
trainer.train()
Enter fullscreen mode Exit fullscreen mode

Pro Tip: Use LoRA (Low-Rank Adaptation) — fine-tune in 2 hours on a $10 Colab GPU.

What I Learned (The Human Part)

  • Week 1: Model hallucinated. It said “Namaste” to a pizza order.
  • Week 3: Added memory. Now it remembers your dog’s name.
  • Week 6: Users asked it for life advice. One cried. I cried.

This isn’t just code. It’s connection.

Your Turn

  1. Fork the repogithub.com/rajni-ai/chatgpt-clone
  2. Run setup.sh → 2 minutes
  3. Change the personality → Make it a chef, a therapist, a stand-up comic
  4. Deploy → Share with friends

Want to Build the Next ChatGPT?

Comment below with:

  • Your GitHub
  • What you want the AI to do (roast code? write songs?)

I’ll personally review your PR and help you ship.

Let’s build AI that feels human.

Rajni

Solo Dev | 2M+ AI Interactions | Building in Public

Top comments (0)