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)