CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 1 β€’ Lesson 9

πŸ’¬ Building a Chatbot

A chatbot is an AI system that can hold conversations with users. In this lesson, you'll build a functional chatbot with memory – it remembers the conversation context and can respond intelligently.

"A good chatbot doesn't just answer – it remembers, understands context, and engages naturally."

🧠 How Chatbots Work

πŸš€ Live Chatbot Demo

Chat with a generative chatbot (using google/flan-t5-base via Hugging Face). It remembers the conversation. Adjust temperature to control creativity.

πŸ€– Bot
Hello! I'm your friendly AI. Ask me anything or chat with me!
πŸ€– Bot is typing...
πŸ“˜ How the chatbot works
The chatbot sends your message (with conversation history) to a free LLM (google/flan-t5-base) via Hugging Face API. It includes the last 5 exchanges to provide context. Temperature controls creativity.
chatbot_memory.js
// Store conversation history
let conversation = [];

// Add a message to history
function addMessage(role, content) {
    conversation.push({ role, content });
    // Keep only last 10 exchanges to avoid token limits
    if (conversation.length > 20) {
        conversation.shift();
    }
}

// Build a prompt with history
function buildPrompt() {
    return conversation
        .map(msg => `${msg.role}: ${msg.content}`)
        .join("\n");
}

✨ Challenge: Add a System Prompt

Modify the chatbot to include a system prompt – a fixed instruction that guides the model's behaviour (e.g., "You are a helpful assistant specialised in coding.").

❀️ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

β˜• Buy Me a Coffee

➑️ One more to go!

Next lesson: AI Ethics & Future – wrap up and look ahead.

Continue to Lesson 1.10 β†’

(Coming soon – check back or buy Pro Pack for instant access)