✅ Lesson 5.4: Personalizing the Chatbot with System Prompts & Memory
🎯 Lesson Objectives
By the end of this lesson, you will be able to:
-
Understand the role of system prompts and how they shape chatbot personality and behavior.
-
Implement short-term and long-term memory in your chatbot.
-
Customize chatbot tone, function, and response style using prompt engineering.
-
Retain context across multiple user turns for a more coherent conversation.
-
Build a chatbot that “remembers” user inputs during a session—or across sessions using a database.
🧠 1. Why Personalization Matters
A generic chatbot gives generic answers. But when you personalize the tone, style, and memory of your chatbot, you transform it into:
-
A friendly tutor 👨🏫
-
A quirky assistant 🤖
-
A professional customer support agent 💼
-
A companion that “remembers” your preferences ❤️
Personalization = Increased user trust, satisfaction, and retention.
💬 2. Understanding System Prompts
System prompts are special instructions sent to the LLM at the beginning of a conversation. They define:
-
Tone (“You are a helpful and empathetic assistant.”)
-
Behavior (“Only respond with one sentence unless asked to elaborate.”)
-
Rules (“Avoid political opinions or personal advice.”)
✅ Example:
system_prompt = {
"role": "system",
"content": "You are a friendly travel assistant that provides concise suggestions for international destinations. Avoid talking about domestic travel unless asked."
}
🔁 Usage in Chat API:
messages = [
system_prompt,
{"role": "user", "content": "Where should I go in Europe in April?"}
]
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
🧠 3. Short-Term Memory (Session Memory)
Short-term memory allows your chatbot to:
-
Refer back to previous user messages
-
Understand conversation flow
-
Maintain natural conversation style
✅ Example in Streamlit:
if "history" not in st.session_state:
st.session_state.history = [
{"role": "system", "content": "You are an AI assistant who remembers context."}
]
user_input = st.text_input("You:")
if user_input:
st.session_state.history.append({"role": "user", "content": user_input})
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=st.session_state.history
)
assistant_reply = response.choices[0].message.content
st.session_state.history.append({"role": "assistant", "content": assistant_reply})
st.write(assistant_reply)
This memory is only for the current session and disappears once the page is refreshed.
🗂️ 4. Long-Term Memory (Persistent Across Sessions)
Long-term memory lets your chatbot remember users across sessions or devices.
✅ Techniques:
| Approach | Description |
|---|---|
| Database | Store conversation history in SQLite, MongoDB |
| Vector Store | Store past questions/answers as embeddings |
| Key-Value Store | Store preferences ({"user": "ronald", "prefers": "concise replies"}) |
✅ Example with SQLite:
import sqlite3
conn = sqlite3.connect("memory.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS memory (user TEXT, data TEXT)")
def remember(user, data):
cursor.execute("INSERT INTO memory (user, data) VALUES (?, ?)", (user, data))
conn.commit()
def recall(user):
cursor.execute("SELECT data FROM memory WHERE user=?", (user,))
return cursor.fetchall()
Use recall() to customize future responses:
user_memory = recall("ronald")
if user_memory:
messages.insert(1, {"role": "system", "content": f"User prefers: {user_memory}"})
🛠️ 5. Customizing Personality with Prompt Templates
Change how the chatbot speaks and behaves:
| Personality | Prompt Example |
|---|---|
| Teacher | “You are a patient English teacher. Use simple words and ask questions to test understanding.” |
| Therapist | “You are a calming listener. Avoid giving direct advice. Ask how the user feels.” |
| Pirate Bot | “You speak like a pirate. Use ‘Arrr!’ and nautical slang in every response.” |
| Professional | “You are a formal customer service agent. Avoid humor and slang.” |
✅ Create prompt presets:
personalities = {
"friendly": "You are a kind and cheerful assistant that uses emojis!",
"serious": "You are a professional assistant. Do not use informal language.",
"coach": "You motivate users like a life coach. Be energetic and inspiring!"
}
🔁 6. Personalization Workflow in Your App
[ User Input ]
↓
[ Detect or Recall User Preferences ]
↓
[ Build System Prompt + History ]
↓
[ Send to LLM ]
↓
[ Return Personalized Output ]
You can dynamically adjust system prompts with user preferences:
pref = "motivational coach"
system = {"role": "system", "content": personalities[pref]}
messages = [system] + history
🧪 7. Practice Activity
🔧 Assignment:
Create a chatbot with at least two personality modes (e.g., friendly, serious).
Add short-term memory using session state or conversation history.
Add a persistent memory layer (SQLite, JSON file, or DB) that:
Stores and retrieves user preferences
Customize LLM responses based on retrieved memory.
Let the user switch between personalities via UI buttons or dropdown.
🔐 8. Best Practices
| Practice | Why It Matters |
|---|---|
| Keep memory token-efficient | Summarize long chats to avoid hitting token limits |
| Add forget commands | Let users reset or erase their memory |
| Use metadata wisely | Track date, user ID, purpose of memory |
| Test prompt styles | Different LLMs respond differently to prompt formats |
❓ 9. Comprehension Check
-
What’s the difference between system prompts and chat memory?
-
Why is short-term memory useful in a chat interface?
-
How can you store user preferences permanently?
-
How does system prompt engineering affect chatbot tone?
📘 10. Further Resources
NOTE
-
A Streamlit chatbot with memory + personality toggle
-
A Flask backend with long-term memory using MongoDB or SQLite
-
A React UI with dynamic system prompt switching?
121
