The AI with a Memory
Ever talk to an AI and feel like you have to re-introduce yourself every single time? What if it could actually remember you? Today, we're building an AI that has a memory. It's like giving your robot a magic diary! โ๏ธ Let's go!
Meet the Dumb Bot!
Experience AI amnesia right here. No new tabs needed.
See? It forgot instantly! ๐ฒ That's the problem we're going to solve today. Let's build an AI that actually remembers.
How AI Remembers: The Magic Diary Trick
You just saw that most AI has amnesia. To fix this, we build a "memory" for it. Itโs a simple but super powerful trick. Let's play a game to see how it works.
"My secret is I love space pizza! ๐"
Drag the secret here!
That's it! The AI "remembers" because it secretly looks at its diary (the context) before it replies to you. Now you know one of the biggest secrets in AI!
๐ฎ Knowledge Check: How AI Remembers
An AI remembers your name because it truly understands who you are. Truth or myth?
๐ก๏ธ Safety Check: Your Data is YOURS
When you give an AI a "memory," you're giving it data. That's powerful! But always remember:
- Use Made-Up Info! For these challenges, use a fun nickname or a made-up mission. Never use your real full name, address, phone number, or school name.
- You're the Boss! Think of yourself as the boss of your AI sidekick. You decide what's important for it to remember, and what's private.
- AIs Can Be Confidently Wrong! An AI might make up facts (we call these "hallucinations"). It's just guessing patterns. Always double-check important info from a trusted source, like a parent or a safe search engine.
Build Your AI Sidekick's Memory Core
Your mission: Teach your AI to give it a personality. This is how you create your very own AI agent! Let's do this one step at a time.
So, what happens when you click "Save to Memory"? Our system takes the facts you wrote and puts them on a secret checklist for your AI. Before it talks, it peeks at the list to remember what to say!
curious older builders, peek inside! โ๏ธ
Under the Hood: The Real Magic
How does the AI *actually use* the memory? It stuffs the memory into the prompt *before* sending it to the AI brain! Try changing the code below and running it yourself!
Change the `SYSTEM_PROMPT` to make the AI talk like a pirate.
Uncomment the line about "favorite color," add your own, and then change the `USER_QUESTION` to ask about it!
# The AI's personality is set with a system prompt.
SYSTEM_PROMPT = "You are a helpful AI assistant."
# The facts about you are stored as context.
CONTEXT = """
- User's sidekick name: Captain Sparkle
- Mission: find the best pizza on Mars
- Quirk: speaks like a pirate
# - User's favorite color: [Your Favorite Color]
"""
# You ask a new question. (You can edit this too!)
USER_QUESTION = "So, where should we look first?"
๐ก Pro-Tip: Context Injection
In professional tools, this "memory log" is called "chat history" or "context." The process of adding it to the prompt is called "context injection." You now know a real-deal AI engineering term!
๐ก Pro-Tip: Where does the memory *live*?
In our simple example, this memory is just a temporary variable that disappears when you refresh the page. In a real app like ChatGPT, this data is saved! It might be stored in a database (like Firebase) on a server, or sometimes right in your browser's localStorage to remember you between visits. This is how apps create a persistent memory for you.
What you built today is a *temporary* memory. The next step is a *permanent* one. In our 'AI Web Wizards' track, you'll learn how to connect an AI to a real-time database (like Google Firebase) to build a chatbot that remembers you forever, even after you close the tab. You've learned the 'what' โ that's where you'll learn the 'how'.
Imagine building an AI character that not only remembers your conversations, but also has a unique look you designed (with Leonardo AI) and a 3D model of its special tool (with Spline AI). That's what's next!
๐จโ๐ฉโ๐ง Parent Corner: AI Personalities
The "memory" and "personality" your child is building is the first step to creating an AI agent. This is a great chance to talk about how technology can be tailored to be more helpful and fun!
Conversation Starter: Ask them, "If you were building a homework helper AI, what are 3 key things it should remember about you to be most helpful?" (e.g., "I'm good at math but not history," "I learn best with pictures," or "Don't give me the answer, just a hint.") This turns a technical concept into a creative, practical conversation.