#What is Memory in Chatbots?

1 messages · Page 1 of 1 (latest)

woeful pilot
#

Memory in a chatbot refers to the ability to remember and recall past conversations. This makes the interaction feel more natural and personal because the bot can:

  • Remember facts: The bot can store information about the user, their preferences, or the topic being discussed.
  • Maintain context: The bot can remember the flow of the conversation and refer back to previous turns. This is especially helpful in long conversations.
  • Provide personalized responses: The bot can tailor its responses to the user's history and individual needs.
#

Adding memory to your chatbots makes them more intelligent and engaging. Let's break down how to create a memory-enabled chat experience using MyShell Pro Config:

#

1. Core Concepts

#
  • Context Variables: These are your chatbot's "brain." They store information across different states. We'll use a context variable called memory to hold the chat history.
  • LLM Widget with Memory: We'll use a Large Language Model (LLM) widget (e.g., GPT-3.5) and pass the memory variable to its memory parameter. This allows the LLM to remember past interactions.
  • Appending to Memory: After each user message and bot response, we'll update the memory variable.
#

2. Sample Pro Config

#
{
  "type": "automata",
  "id": "memory_chat_demo",
  "initial": "chat_state",
  "context": {
    "memory": "{{[]}}" // Initialize an empty memory array
  },
  "states": {
    "chat_state": {
      "inputs": {
        "user_message": {
          "type": "IM",
          "user_input": true
        }
      },
      "tasks": [
        {
          "name": "llm_with_memory",
          "module_type": "AnyWidgetModule",
          "module_config": {
            "widget_id": "1744214024104448000", // GPT-3.5 Widget ID
            "system_prompt": "You are a friendly and helpful AI assistant with a good memory.",
            "user_prompt": "{{user_message}}",
            "memory": "{{context.memory}}", // Pass memory to the LLM widget
            "output_name": "reply" 
          }
        }
      ],
      "outputs": {
        "context.memory": "{{[...context.memory, {'user': user_message}, {'assistant': reply}]}}" // Append to memory
      },
      "render": {
        "text": "{{reply}}", 
        "buttons": [] 
      },
      "transitions": {
        "CHAT": "chat_state" 
      }
    } 
  } 
}
#

3. Explanation

#
  • initial: The bot starts in the "chat_state".
  • context.memory: We initialize an empty array ({{[]}}) to store the conversation history.
  • chat_state.inputs: The bot waits for user input through instant messaging ("type": "IM").
  • chat_state.tasks:
    • We use an LLM widget (GPT-3.5 in this example) and pass the context.memory to its memory parameter.
  • chat_state.outputs:
    • We append the latest user message and bot reply to context.memory using JavaScript's spread syntax (...) to preserve the previous history.
  • chat_state.render: The bot displays the reply from the LLM.
  • chat_state.transitions: The CHAT event triggers the bot to stay in the "chat_state", allowing for continuous conversation.
#

4. Sample Interaction

#

Imagine the following interaction:

User: What's my name?
Bot: I don't have access to personal information like your name.

User: Okay, call me Bob.
Bot: Got it, Bob.

User: Now, what's my name?
Bot: Your name is Bob.

You can see that the bot remembers the name "Bob" from the previous interaction.

Let's make those chatbots smarter!

#

Implementing Chat Memory in Pro Config

#

Introduction:

Chat memory is essential for building engaging and contextually aware bots. It allows the bot to remember past interactions, creating a more natural and personalized conversation flow. This guide provides a comprehensive example of how to implement chat memory in Pro Config, leveraging the power of JavaScript expressions and context variables.

#

Main Content:

Sample Pro Config with Chat Memory:

#
{
  "type": "automata",
  "id": "chat_memory_demo",
  "initial": "welcome_state",
  "context": {
    "memory": "[]", // Initialize memory as an empty array
    "user_name": ""
  },
  "states": {
    "welcome_state": {
      "inputs": {
        "name": {
          "type": "text",
          "user_input": true,
          "description": "What's your name?"
        }
      },
      "outputs": {
        "context.user_name": "{{ name }}"
      },
      "render": {
        "text": "Nice to meet you, {{ name }}! Let's chat. Ask me anything.",
        "buttons": []
      },
      "transitions": {
        "CHAT": "chat_state" 
      }
    },
    "chat_state": {
      "inputs": {
        "user_message": {
          "type": "IM",
          "user_input": true
        }
      },
      "tasks": [
        {
          "name": "generate_reply",
          "module_type": "AnyWidgetModule",
          "module_config": {
            "widget_id": "1744214024104448000", // GPT-3.5 widget ID
            "system_prompt": "You are a friendly and helpful chatbot. Remember past interactions and address the user by their name.",
            "user_prompt": "{{ user_message }}",
            "memory": "{{ context.memory }}", // Pass memory to the LLM
            "output_name": "reply"
          }
        }
      ],
      "outputs": {
        // Append new messages to the memory
        "context.memory": "{{ [...context.memory, {'user': user_message, 'name': context.user_name }, { 'assistant': reply } ] }}" 
      },
      "render": {
        "text": "{{ reply }}"
      },
      "transitions": {
        "CHAT": "chat_state" // Stay in chat state on new messages
      }
    }
  }
}
#

Explanation:

#
  1. Memory Initialization: We initialize context.memory as an empty array [] within the context of the automata.

  2. Storing User Name: In the welcome_state, the user's name is stored in the context.user_name variable.

  3. Passing Memory to LLM: In the chat_state, the context.memory array is passed to the memory parameter of the GPT-3.5 widget. This enables the language model to access the chat history.

  4. Updating Memory: After the LLM generates a response, the outputs section appends both the user's message and the bot's reply to the context.memory array. The user's name is also included for personalized responses.

  5. Maintaining Chat State: The transitions for chat_state ensure the bot stays in the same state when new messages are received, allowing continuous conversation.

#

Sample Interaction:

#

User: Hi, how are you?

Bot: I'm doing well, thanks for asking! How about you, [user_name]?

User: I'm great! What's your favorite color?

Bot: That's wonderful to hear, [user_name]! My favorite color is blue. What about you?

User: Mine is green. What was the first question I asked you?

Bot: You asked me how I was doing, [user_name].