Skip to content

Latest commit

 

History

History
160 lines (129 loc) · 5.4 KB

File metadata and controls

160 lines (129 loc) · 5.4 KB

Architecture Deep-Dive

RuleBot - Rule-Based AI Chatbot


The Logic Engine: How It Works

The Two Minds of AI (from DecodeLabs Briefing)

SYSTEM 1: THE ARTIST (Probabilistic)      SYSTEM 2: THE ENGINEER (Deterministic)
──────────────────────────────────────    ──────────────────────────────────────
• Machine Learning                        • Rule-Based (THIS PROJECT)
• Learns from data                        • Explicit if-else / dictionary logic
• Flexible but unpredictable              • Rigid but 100% traceable
• Can hallucinate                         • ZERO hallucination risk
• Black box                               • White box — full transparency

The White Box Principle

RuleBot is a White Box system:

  • Traceability: Input → Logic → Output. No mystery.
  • Safety: Zero hallucination risk. 100% hard-coded.
  • Compliance: Used in Finance & Healthcare AI systems.

The Anti-Pattern We Avoided

❌ The If-Elif Ladder (O(n) - Unstable)

# BAD — High technical debt, O(n) complexity
if user_input == "hello":
    print("Hi!")
elif user_input == "bye":
    print("Goodbye!")
elif user_input == "joke":
    print("...")
# ...imagine 30 more elif blocks

Problems: Linear O(n) complexity, hard to maintain, cascading failures.

✅ The Dictionary Approach (O(1) — Professional)

# GOOD — O(1) lookup, clean, scalable
responses = {
    "hello": ["Hi!", "Hey there!"],
    "bye":   ["Goodbye!", "See you!"],
    "joke":  ["Why do programmers..."],
}
reply = responses.get(user_input, "I do not understand.")

The IPO Model in Code

Phase 1: Input & Sanitization

raw_input  = input("You: ")               # Raw feed
clean_input = raw_input.lower().strip()   # Normalized
# "Hello  " → "hello"
# "BYE"    → "bye"
# " Hi! "  → "hi!"

Phase 2: Process (The Logic Skeleton)

# O(1) dictionary lookup with fallback
for keyword in KNOWLEDGE_BASE:
    if keyword in clean_input:
        matched = KNOWLEDGE_BASE[keyword]
        break
response = random.choice(matched) if matched else FALLBACK

Phase 3: Output (Feedback Loop)

print(f"RuleBot: {response}")
if is_exit:
    break  # Clean kill command

The Heartbeat: Infinite Loop

while True:                          # ← HEARTBEAT STARTS
    raw = input("You: ")             # ← INPUT
    clean = sanitize(raw)            # ← SANITIZE
    response, is_exit = get_response(clean)  # ← PROCESS
    print(response)                  # ← OUTPUT
    if is_exit:
        break                        # ← KILL COMMAND

The while True loop is the heartbeat — it keeps the chatbot alive until the exit command fires the break.


Modern Application: AI Guardrails

This architecture is directly used in production today:

┌────────────────────────────────────────────┐
│              USER INPUT                    │
└────────────────────┬───────────────────────┘
                     │
                     ▼
┌────────────────────────────────────────────┐
│     RULE-BASED GUARDRAILS ← YOU ARE HERE  │
│     (Filtering, Redaction, Blocking)       │
│     e.g. NVIDIA NeMo, Llama Guard          │
└────────────────────┬───────────────────────┘
                     │
                     ▼
┌────────────────────────────────────────────┐
│     LARGE LANGUAGE MODEL                   │
│     (Probabilistic Core)                   │
└────────────────────────────────────────────┘

Algorithmic Efficiency Comparison

Time to Execute
│
5s ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─/  ← If-Elif O(n)
│                                         /
1s ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ /
│                                      /
│  ──────────────────────────────────────    ← Dictionary O(1)
0ms ───────────────────────────────────────▶
     0        100       1000     10000
              Number of Rules (Scale)

Dictionary lookup stays flat at O(1) regardless of scale.


File Responsibilities

File Responsibility
chatbot.py Core brain — knowledge base, sanitization, response engine, terminal loop
app.py Streamlit frontend — UI, session state, chat display, sidebar stats
requirements.txt Dependency management
README.md Project overview and usage guide
ARCHITECTURE.md This file — deep technical explanation
REFLECTION.md Personal learning notes for portfolio

Architecture Briefing