Long-term memory

更新时间:
复制 MD 格式

Agent applications in Alibaba Cloud Model Studio can remember a limited amount of conversation history. Large models may forget information because of limitations in their attention mechanism. To address this, you can store specific information from conversations, such as personalized details about user features and preferences, in a long-term memory store. This allows the agent application to learn continuously, remember user preferences more effectively, and use this information in future conversations to improve the interaction experience.

Function introduction

When you enable the long-term memory feature in an agent application, a long-term memory store is automatically created. This store manages memory through memory segments and memory variables. When you call the agent application, the system uses the provided long-term memory ID (Memory ID) to automatically recall the content from the memory store. This content, including memory segments and memory variables, is passed to the model along with the current user query to generate an answer.

  • Memory segment (MemoryNode):

    Alibaba Cloud Model Studio automatically extracts personalized user information from conversations and stores it in memory segments. For example, if a user says, "I like spicy food," the system stores "user likes spicy food." This information is then used for recommendations in future conversations. However, memory segments can be affected by temporary information, such as a user mentioning they are "trying vegetarian food." Memory variables are used to calibrate these segments.

  • Memory Variable:

    Memory variables are key-value pairs whose fields you must define manually. Values are generated either by model inference based on user queries or by manual input, such as entering external behavioral data. Memory variables have a higher weight than memory segments. They can correct for the incompleteness and real-time drift of memory segments to ensure recommendation accuracy.

Scenario examples

  • Intelligent shopping guide: Collects and remembers customer information such as budget range, brand preferences, and specific features of interest to provide personalized purchase recommendations.

  • Fitness coach assistant: Collects and remembers user information such as physical condition, fitness goals, and available equipment to provide personalized workout plans and dietary advice.

  • Travel planning assistant: Collects and remembers user information such as budget, hobbies, and travel dates to recommend suitable destinations, attractions, and itineraries.

  • Learning tutor assistant: Collects and remembers student information such as grade level, knowledge level, and weak subjects to provide targeted learning materials and exercises.

You can also manually add specific information to the long-term memory. For example, if you add the key-value pair "Language: Chinese", the agent application will consistently reply in Chinese in future conversations.

How to use long-term memory

Step 1: Create a long-term memory store and get the Memory ID

You can create a long-term memory store in one of two ways. The Memory ID is unique within an Alibaba Cloud account (UID).

Create in the console

  1. Create a long-term memory store.

    When you enable the long-term memory feature for an agent application in Alibaba Cloud Model Studio Applications (see ① in the figure below), a long-term memory store is automatically created. The current application is bound to this memory store by default and uses it for memory retrieval and storage. To bind a different long-term memory store, see Call a long-term memory store in an application.

  2. Obtain the Memory ID of the bound memory store.

    On the right side of the page, click the image icon (see ② in the figure above). Then, click Memory ID (see ③ in the figure below) and record the Memory ID of the bound memory store (see ④ in the figure below) for future calls.

  3. Memory segment information is stored and isolated by a Memory ID. To achieve more granular memory isolation, flexibly manage different long-term memory stores, and prevent memory confusion between different scenarios or users, click Create Memory ID (see ⑤ in the figure above) to create a new ID. Pass this ID when you call the long-term memory store in the application.

Create using an API

Method 1: Call the CreateMemory API operation to create a new long-term memory store. Record the memoryId (the Memory ID) from the returned parameters for future calls.

Method 2: Call the ListMemories API operation to retrieve a list of existing long-term memory stores. Record the memoryId (the Memory ID) from the returned parameters for future calls.

Step 2: Create memory variables and memory segments in the long-term memory store

A new long-term memory store is empty. You can store specific information from conversations, such as memory segments and memory variables, in the memory store. The agent application can then use this information in future conversations.

Create in the console

  1. Click the image icon next to the Long-term Memory switch. In the Memory Variable panel on the right, click Add Field to configure the fields.

    image

    Parameter

    Description

    Configuration example

    Field name

    The name of the memory variable field. Enter a custom name.

    The field name should be semantic to ensure that users can quickly understand its meaning and that machines can easily perform label extraction and aggregation and analysis.

    image

    Field description

    A natural language description of the memory variable field. This description prompts the large model about the field's purpose.

    Enter a custom description.

    Use model inference

    Specifies whether to automatically update the field's value using model inference. The switch status does not affect the content extracted into memory segments.

    Switch is on image: The value of this memory variable is extracted from memory segments. As user information is updated in the conversation, this value is also automatically updated.

    This setting is recommended for subjective information, such as user preferences. Examples include dietary preferences and exercise habits. This information is not static and may change over time. The system should be able to update it dynamically. Ensure the stored information is current by periodically asking questions or analyzing user behavior.

    Switch is off image: The value of this memory variable is not automatically updated. You can enter the value manually or pass it through an API.

    This setting is recommended for objective information, such as user features. Examples include gender and name. This information is stable and does not change frequently. Periodically review and clean this data to ensure its accuracy and timeliness. Update or delete irrelevant or outdated information to avoid misleading the system's decision-making process.

  2. Click OK to complete the configuration. You can view the configured memory variable fields in the Long-term memory area.

    image

  3. To publish the application, click Publish.

    You must publish the application after adding a memory variable field for the change to take effect.
  4. Enter the following user queries to start a conversation. After about 1 minute, click the image icon on the right side of the page to view the memory segments and memory variables that were automatically extracted from the user queries.

    User query

    Content in memory segment

    Value of memory variable

    I like seafood.

    User likes seafood.

    image

    When the Use model inference switch for the "Dietary preference" field is turned on, the system automatically extracts the value of the memory variable from memory segments, such as "seafood, pasta, does not eat rice, does not eat spicy food, especially Sichuan cuisine".

    image

    I love pasta and don't eat rice.

    User loves pasta and does not eat rice.

    Sichuan cuisine is too spicy for me.

    User does not like spicy food, especially Sichuan cuisine.

    I play badminton every weekend.

    User plays badminton every weekend.

    When the Use model inference switch for the "Exercise preference" and "Age" fields is turned off, the system cannot automatically extract their values, leaving the fields empty.

    You can manually enter a value for "Exercise preference", such as "swimming". In subsequent conversations, the memory segments and memory variables are recalled, and the model generates a response based on its judgment. For an example, see Modify the value of "Exercise preference".

    I am 20 years old.

    User was born in 2005. (Inference time: 2005)

    It's raining heavily today.

    Not personalized user data. No content is extracted.

  5. When the user enters a new query, the system answers the question based on the memory variables combined with the prompt.

    image

  6. Manually enter a value for "Exercise preference", then clear the history and start a new conversation.

    image

    If no value is entered for "Exercise preference", the large model bases its answer on the content of the memory segment.

    If you manually enter "swimming" for "Exercise preference", the memory segments and memory variables are recalled in subsequent conversations. The model then uses this information to generate a response.

    image

    image

Create using an API

Method 1: Call the CreateMemoryNode API operation. In the content parameter, enter the following examples to create memory segments. If the provided Memory ID is already bound to an application in the console, you can view the content in the memory segments in the console after creation. Otherwise, you can only view the content by calling the GetMemoryNode or ListMemoryNodes API operations.

Examples:

  • User likes seafood.

  • User likes pasta and does not eat rice.

  • User cannot eat food that is too spicy.

  • User plays badminton every weekend.

  • User is 20 years old.

Method 2: When using the DashScope API Reference, pass the Memory ID and enter the following examples in the prompt. The system automatically creates memory segments in the specified long-term memory store based on the prompt. In the DashScope API Reference, you can search for "long-term memory" to view code samples and detailed calling methods. After creation, you can view the content in the memory segments in the console.

Examples:

  • I like seafood.

  • I love pasta and don't eat rice.

  • Sichuan cuisine is too spicy for me.

  • I play badminton every weekend.

  • I am 20 years old.

Step 3: Call a long-term memory store in an application

An application must be bound to a long-term memory store to enable memory capabilities. Calling a long-term memory store in an application integrates different memory segment and memory variable data into that application. In each round of conversation, storage and retrieval are based on the Memory ID, which provides a more comprehensive user persona. This process also ensures user information consistency across different applications, enhances the agent's personalized service capabilities, optimizes the user experience, and prevents conflicting information.

Call in the console

  1. In Alibaba Cloud Model Studio Applications, find your agent application and click Configure.

  2. When you enable the long-term memory feature, a long-term memory store is bound to the current application by default. Therefore, you do not need to rebind it for retrieval and storage. Simply start a session. To bind a different long-term memory store, click the image icon, click Memory ID, select a Memory ID from the list, and click OK.

    A Memory ID that is already in use by another application cannot be deleted.
  3. Publish the application.

  4. When you start a new session, the system automatically recalls the relevant memory content based on the Memory ID bound to the application. It then passes this content, along with the current user query, to the model to generate an answer.

Call using an API

  1. Obtain the Memory ID.

    For instructions, see Step 1: Create a long-term memory store and obtain the Memory ID.

  2. In the DashScope API Reference, search for the "long-term memory" parameter to view code samples and detailed calling methods.

    When you call the agent application, the system automatically recalls the relevant memory content based on the provided Memory ID. It then passes this content, along with the current user query, to the model to generate an answer.

    Because the long-term memory store is bound to the application by default when the feature is enabled, if you do not pass a Memory ID, the system continues to use this default memory store for retrieval and storage.

Related API operations

You can use the API operations in Long-term memory to manage long-term memory stores and their content.

FAQ

  1. How long is memory content (memory segments and memory variables) stored?

    Alibaba Cloud Model Studio does not save original conversation records. It only extracts content based on the memory variable fields that you configure. This information currently has no expiration date. You can delete unwanted memory content as follows:

    • Delete a single memory segment: Memory segments created using CreateMemoryNode can be deleted using DeleteMemoryNode. Memory segments and variables created in the Alibaba Cloud Model Studio console can only be deleted one by one in the UI.

    • Delete all content in a memory store: A long-term memory store created using CreateMemory can be deleted using DeleteMemory. This action removes all content within the store.

  2. How is the long-term memory feature billed?

    Data storage is free of charge.

    When you call an application for Q&A, the memory content is merged into the prompt and passed to the large model, which increases token consumption. Currently, tokens that are used by memory content are not billed.

  3. How do I implement memory isolation when calling an agent?

    Memory segment information (MemoryNode) is stored and isolated by Memory ID.

    You can use CreateMemory to create a long-term memory store and obtain a new Memory ID. Pass the specific Memory ID when making an API call to implement memory isolation. If you do not pass a Memory ID, the default Memory ID that is bound to the application is used.