From Single-turn Q&A to True "Conversational Memory"

In our previous breakfast shop chatbot implementation, we successfully connected OpenAI (ChatGPT) to LINE's Webhook. At that time, the bot would respond to each customer message individually. While this appeared intelligent, careful testing would reveal a critical flaw: it had no memory.

For example: Customer: "Boss, I'd like to order an egg pancake." Bot: "Got it! One original egg pancake, total 30 NTD. Would you like anything else?" Customer: "Add a large iced milk tea please." Bot: "Sure! One large iced milk tea for 20 NTD. Anything else?" Customer: "That's all, what's the total?" Bot: "What did you order again?"

This is what we call "single-turn conversation." Every time the Webhook receives a message and calls OpenAI, OpenAI treats it as a completely new customer - it remembers nothing from the previous exchange!

To build an AI customer service system that truly replaces human labor, we must give our bot "short-term memory."


🧠 Giving AI a Hippocampus: The Messages Array

The solution to giving OpenAI memory is surprisingly simple: Instead of sending just the latest message to the API each time, you need to package and send the entire conversation history.

In OpenAI's API specifications, we send a request containing a messages array:

const messages = [
  { role: "system", content: "You are a breakfast shop owner..." }, // System persona (God's perspective)
  { role: "user", content: "I want an egg pancake" },              // Customer's first message (History 1)
  { role: "assistant", content: "Got it, anything else?" },        // AI's previous response (History 2)
  { role: "user", content: "Add an iced milk tea" }                // Customer's latest message
];

When you send this complete messages array to OpenAI, it can instantly understand the context like reading a script, then provide perfect responses!


🛠️ Vibe Coding in Practice: Temporary Memory Storage

Implementing this memory management logic manually would be extremely complex: You'd need to use each user's Line ID as a Key to retrieve their conversation history, and delete old messages when exceeding length limits to prevent token overflow.

This is when we summon our ultimate cheat code: Cursor + Vibe Prompt.

【AI Memory Customer Service Vibe Prompt】 I'm developing a Node.js server for a Line Bot handling customer service chats. The previous version couldn't remember context, now I need you to help refactor the logic to add "conversation memory functionality."

Implementation details:

  1. Use a global variable const memoryStore = new Map() as temporary memory storage. Use the user's lineUserId as Key, with Value being an array storing that user's messages.
  2. When receiving a new message, first retrieve history from memoryStore. If none exists, initialize a new array containing the System Prompt.
  3. .push() the user's latest message into the array, then send to OpenAI gpt-4o-mini.
  4. After receiving OpenAI's response, also .push() the AI's reply into the same array for storage.
  5. 【Critical Mechanism】To prevent infinite array growth causing memory overflow or token limits, implement "auto-trimming": If a user's history exceeds 10 messages (array length > 11, including system), automatically remove (splice) the oldest Q&A pair.
  6. Provide complete Node.js Express code with clear Chinese comments.

After casting this spell, the AI will instantly generate perfect code containing Map data structures and Array.splice logic.

The Power of This System: Multi-task Customer Service

Once your LINE Bot has memory, its commercial value multiplies:

  • Sequential Data Collection: "May I have your name?" ➡️ "Mr. Lin, may I have your phone number?" ➡️ "Confirmed Mr. Lin, phone 0912, what's your preferred appointment time?" (It can guide customers through forms step-by-step).
  • Technical Support: "My screen won't turn on." ➡️ AI provides three troubleshooting steps ➡️ "I'm stuck on step two, the light won't turn on." ➡️ AI can focus specifically on step two.
  • High-engagement Companion Bots: Tarot readers, counseling AIs, even virtual girlfriends are all built on this powerful "memory management mechanism."

🚨 Advanced Considerations: When the Server Restarts

Astute readers may notice we're storing conversation history in new Map() in the above Prompt, which is "in-memory storage."

While extremely fast and convenient for development/testing, it has a fatal flaw: If your server crashes or you redeploy on Vercel/Render, this memoryStore gets wiped instantly, causing all bots to "collectively forget."

For a paid customer service system charging 30,000 NTD monthly, we can't tolerate this. In enterprise-grade architecture, we'd convert this messages array to JSON format and store it in Redis (in-memory database) or Supabase (PostgreSQL) for persistent storage.

However, for rapid MVP validation, using Node.js's Map array is enough to impress clients and secure contracts! Ready to upgrade your breakfast shop auntie? Next chapter we'll teach you LINE's secret weapon: LIFF webpages!

Chapter Summary

  • Understand core concepts and principles
  • Master implementation methods and techniques
  • Familiar with common issues and solutions
  • Able to apply in real projects

Further Reading

  • Official documentation and API references
  • Open source examples on GitHub
  • Technical books and online courses
  • Community discussions and tech blogs

Implementation Example

Basic Example

# This section provides a complete implementation example

Steps

  1. Setup: Configure development environment
  2. Data: Prepare required data
  3. Implementation: Build core functionality
  4. Testing: Verify correctness
  5. Optimization: Improve performance

Common Errors

| Error Type | Cause | Solution | |------------|-------|----------| | Compilation | Syntax | Check code syntax | | Runtime | Environment | Verify dependencies installed | | Logic | Algorithm | Step-by-step debugging | | Performance | Efficiency | Use profilers |

Code Example

import sys

def main():
    print("Hello, World!")

if __name__ == "__main__":
    main()

References

  • Official documentation
  • API reference
  • Open source examples
  • Community discussions

Member Exclusive Free Tutorial

This chapter is free exclusive content for registered members! Please login or register to unlock immediately.

Login / Register Now