রোডম্যাপ
PHASE 6 · অধ্যায় 31

চ্যাটবট

Chatbots

নিজের intelligent conversation system বানানো।

ভূমিকা

১৯৬৬ সালে ELIZA — প্রথম chatbot যা psychologist এর মত response দিত। ২০২৪ এ ChatGPT — যা প্রায় human level conversation করে। মাঝখানে ৬০ বছরের evolution: rule-based → retrieval-based → generative → LLM-powered। নিজের chatbot বানানো এখন কয়েক ঘণ্টার কাজ।

ধারণা

Chatbot তিন ধরনের: (1) Rule-based — if/else, pattern matching (ELIZA), (2) Retrieval-based — predefined response থেকে best match (FAQ bot), (3) Generative — LLM দিয়ে dynamic response। Modern chatbot = LLM + system prompt + conversation history + tools (function calling) + memory (vector DB) — এটাই agent pattern।

সহজ ব্যাখ্যা

ভাবুন একজন receptionist — কিছু standard question এ pre-defined answer (rule), customer question বুঝে FAQ থেকে relevant entry দেখায় (retrieval), অথবা নিজে think করে context-aware answer দেয় (generative)। Modern chatbot এই তিনটাই combine করে — simple greeting এ rule, FAQ এ retrieval, complex query তে LLM।

বাস্তব ব্যবহার

  • ChatGPT, Claude, Gemini — generative LLM chatbot।
  • Bank/telco customer support — hybrid retrieval + LLM।
  • E-commerce — product recommendation, order tracking।
  • Healthcare triage — symptom check।
  • Discord/Slack bot — workflow automation।

ধাপে ধাপে বিশ্লেষণ

1
Step 1 — Conversation history maintain
User + bot turn list।
2
Step 2 — System prompt set
Bot এর personality, rules, scope।
3
Step 3 — Generate response
LLM এ history + new message pass।
4
Step 4 — Post-process
Format, safety filter, source citation।
5
Step 5 — Loop
Response history তে add, পরের turn এর জন্য।

Python কোড

from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

model_name = "microsoft/DialoGPT-small"
tok = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
model.eval()

def chat():
    history_ids = None
    print("Bot: Hi! Type 'quit' to exit.\n")
    for step in range(6):
        user = input("You: ")
        if user.strip().lower() == "quit":
            break
        new_input_ids = tok.encode(user + tok.eos_token, return_tensors="pt")
        bot_input_ids = (
            torch.cat([history_ids, new_input_ids], dim=-1)
            if history_ids is not None else new_input_ids
        )
        history_ids = model.generate(
            bot_input_ids,
            max_length=1000,
            pad_token_id=tok.eos_token_id,
            do_sample=True,
            top_p=0.9,
            temperature=0.8,
            no_repeat_ngram_size=3,
        )
        reply = tok.decode(
            history_ids[:, bot_input_ids.shape[-1]:][0],
            skip_special_tokens=True,
        )
        print(f"Bot: {reply}\n")

if __name__ == "__main__":
    chat()
ব্যাখ্যা

DialoGPT GPT-2 এর dialogue-tuned variant। প্রতিটা user turn কে EOS token দিয়ে separate করে history এর সাথে concatenate। model.generate() পুরো history দেখে next reply generate। history_ids loop এ update হয় — তাই context maintained।

সাধারণ ভুল

  • History truncate না করা — context length exceed করে error।
  • System prompt না দেওয়া — bot off-topic chatter করে।
  • Safety filter না — toxic/unsafe response।
  • Memory persist না করা — user re-login এ সব ভুলে যায়।

অনুশীলন

  1. OpenAI API দিয়ে system prompt সহ chatbot বানান।
  2. Conversation history 10 turn এর বেশি হলে summarize করে কমান।
  3. Streamlit/Gradio দিয়ে web UI বানান।
  4. Tool calling (function calling) যোগ করুন — calculator, weather।

ছোট প্রজেক্ট

Persona Chatbot

একটা chatbot যেখানে user একটা persona (teacher, doctor, comedian) choose করে। System prompt সেই অনুযায়ী set হয়, conversation history maintain, এবং Gradio দিয়ে simple web UI। DialoGPT বা OpenAI/Gemini API ব্যবহার করুন।

সারাংশ

  • Chatbot evolution: rule → retrieval → generative → agent।
  • Modern: LLM + system prompt + history + tools।
  • Conversation history context maintain করে।
  • Production এ safety, memory, scaling চিন্তা।
  • Phase 6 complete! আপনি এখন HuggingFace, tokenizer, generation, QA, chatbot — production NLP tool master।