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.
🧠 How Chatbots Work
- Intent recognition – understanding what the user wants (rule‑based or ML).
- Dialogue management – tracking the conversation state and deciding the next action.
- Response generation – producing a reply (retrieval from a database or generative AI).
- Memory – storing previous exchanges to maintain context.
🚀 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.
📘 How the chatbot works
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.").
const systemPrompt = "You are a friendly AI assistant specialising in web development.";
// In buildPrompt(), prepend system prompt
const fullPrompt = systemPrompt + "\n" + conversation.map(...).join("\n");
// Then send fullPrompt to the LLM.
❤️ 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)