Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Building a Long-Term Memory Solution for AI Agents Based on PolarDB-PG

Tech May 16 4

Application Background

Large Language Models (LLMs) have achieved remarkable success in understanding and generating contextually coherent conversations. However, their inherent 'memory deficit'—a limited context window—severely restricts their ability to maintain consistency across long-term interactions spanning sessions and applications. Once a conversation exceeds the context length, the LLM behaves like an amnesiac partner, forgetting user preferences, repeating questions, or even contradicting previously established facts.

Imagine this scenario: You tell an AI assistant that you are a vegetarian and do not consume dairy products. A few days later, when you ask for dinner suggestions, it recommends roast chicken. Such an experience undoubtedly undermines user trust and reliance on the AI.

An AI lacking memory (left side) forgets key user information, whereas an AI with effective memory (right side) maintains context across sessions, delivering precise and personalized services.

This article presents a one-stop long-term memory solution based on PolarDB-PG. PolarDB-PG relies on the long-term memory framework Mem0, providing integrated capabilities including a vector database engine, a graph database engine, along with large language model (LLM) and embedding model services. Mem0 (pronounced 'mem-zero') is a memory module designed for modern AI agents. It acts as a unified persistent memory layer, enabling agents to continuously retain user preferences, factual background, and historical interaction information across sessions and applications, thus achieving true personalization and continuous learning.

Scenario Classification

Mem0 supports two types of long-term memory solutions: a pure vector database solution and a combined vector database + graph database solution, each suitable for specific scenarios.

1. Pure Vector Database Solution

Applicable Scenarios:

  • Conversational scenarios requiring fast semantic retrieval, such as online customer service and real-time chatbots.
  • Cost-sensitive applications. If purchasing both vector and graph database products separately is necessary, a pure vector solution can reduce product costs by at least half.

Technical Characteristics:

  • Extracts key facts from conversations via LLMs and stores them as vectorized data.
  • Uses dynamic thresholds to control retrieval range, balancing recall and precision.

2. Vector Database + Graph Database Solution

Applicable Scenarios:

  • Complex relational reasoning scenarios: e.g., medical diagnosis (tracking patient history and drug interactions), travel planning (integrating relationships among flights, hotels, attractions), etc.
  • Long-term knowledge management: Stores knowledge structurally via triples (entity1-relationship-entity2), suitable for building enterprise-level knowledge bases or intelligent assistants requiring cross-session coherence, such as in-cabin AI assistants tracking user preference evolution, AI companions, etc., for long-term personalized service.
  • Dynamic evolving systems: Knowledge graphs support incremental updates and subgraph retrieval, ideal for scenarios with frequently changing business rules (e.g., dynamic rule bases in financial risk control).

Technical Characteristics:

  • The vector store handles semantic search; the graph database stores relationships between entities.
  • Supports time-aware dynamic knowledge graph updates.
  • Implements structured memory via a two-stage pipeline based on the Mem0g approach.

Complementarity: While the vector+graph approach can handle complex relationships, it presents greater challenges in retrieval efficiency. Conversely, the pure vector solution is more efficient in simple scenarios but lacks the ability to model deep relationships. In production, a hybrid architecture can be designed based on business complexity and real-time requirements.

One-Stop Solution Overview

The one-stop long-term memory solution based on PolarDB-PG comprises five main components:

1. Memory Engine

It is recommended to use the Mem0 framework, whose core capabilities include:

  • Long-term/short-term memory fusion (LTM/STM) + adaptive context compression.
  • Four types of memory capabilities (layered memory):
    • Working memory: short-term session awareness.
    • Factual memory: long-term structured knowledge (e.g., preferences, settings).
    • Episodic memory: records specific past conversations.
    • Semantic memory: knowledge extracted over time.
  • Four memory operations: add, search, update, delete.
  • Support for multimodal memory: relationships + text + images.
  • Support for Graph Memory: integrated with the PolarDB-PG graph engine AGE, enabling real-time establishment of entity relationships (e.g., person-person, person-location) from session information and retrieving relational insights from the graph during queries.
  • Support for Vector Memory: integrated with the PolarDB-PG vector engine PGVector, enabling vector-based retrieval of historical conversations.

2. Core Data Engine

  • One-stop combination of PolarDB-PG vector database engine + graph database engine. The vector database engine uses the optimized PGVector plugin, which is widely adopted in the PG community and boasts a robust AI ecosystem. The graph database engine is compatible with the open-source AGE (A Graph Extension, a top-level project of the Apache Software Foundation) and has been enhanced through deep integration with PolarDB-PG and cloud-native features, aswell as years of application improvement and performance optimization for numerous graph clients. It has proven to be mature and stable, capable of maintaining over ten thousand QPS and sub-100ms query latency even at the scale of tens of billions of graph elements.
  • Supports centralized or distributed versions, leveraging the storage-side PolarFS cloud-native distributed file system to eliminate scalability concerns.

3. Model Services

  • The large model plays a central role in Mem0, primarily serving the following functions:
    • Automatically extracts key information from conversations between the user and the agent, identifying and extracting information with long-term value.
    • Converts this information into high-dimensional vectors for efficient semantic retrieval.
    • When graph database storage is required, leverages the large model to extract triple information from user semantics.
  • Model invocation efficiency is critical to customer experience. This solution supports adding AI nodes (with GPU) to PolarDB-PG, using optimized local LLM and embedding model services for memory content extraction, updates, vector embedding, etc. Through highly optimized pipelines, it significantly improves memory-related inference efficiency.

4. AI Application Development Platform

The PolarDB-PG AI Application (store) supports one-click activation of the Dify framework, and Mem0 can be integrated as a plugin into Dify, offering a more convenient overall solution. Mem0 also supports frameworks/platforms such as LangChain, LangGraph, AgentOps, LlamaIndex, and AutoGen.

5. KV Cache Enhancement

In scenarios requiring high real-time performance with a mix of long-term and short-term memory, PolarDB-PG supports enabling a distributed KV Cache layer for context caching acceleration. By adding the KV Cache layer, redundant computation of converting historical tokens to vectors is reduced, context association is accelerated, and inference latency is stabilized.

Core Advantages

Summarizing the core advantages of the long-term memory solution based on PolarDB-PG:

  • One-Stop: PolarDB-PG, as Alibaba Cloud's strategic core database product, simultaneously provides both vector database and graph database engines. This multi-engine one-stop solution replaces combinations requiring multiple database products (customers would otherwise need to purchase vector and graph databases separately), reducing procurement costs by at least 50% and significantly lowering cross-system maintenance costs and project risks. Additionally, one-click activation and combined use of built-in AI applications such as model services, KV Cache, and Dify greatly reduce self-built complexity and accelerate deployment timelines. Necessary components can be selectively used based on project needs.
  • Scalable: The distributed version of PolarDB-PG supports vector storage at the billion scale. Both vector index creation and search are OOM-free, with retrieval performance (QPS, accuracy) surpassing industry competitors. The graph database engine manages large-scale graphs (nodes + edges) at the billion level, supports graph search scalability with over ten thousand QPS, and includes TTL-based automatic lifecycle management for records and visual graph management via the console.
  • High Performance: Supports real-time vector and graph writes with automatic index updates; data is available immediately upon insertion. Optimal query round-trip time (RT) is under 50 milliseconds. Combined with AI nodes and local model deployment, inference efficiency improves 1-2 times compared to remote services. Further combining with KV Cache, inference speed increases over 5 times, and GPU memory usage decreases by approximately 30%-50% compared to repeated computation. These comprehensive optimizations provide significant improvements in write throughput, data visibility, and interaction latency for the graph+vector hybrid architecture designed for mixed long/short-term memory scenarios.

Usage Workflow

Using the PolarDB-PG graph+vector engine combination solusion as an example, along with Alibaba Cloud's Qwen large model, the following example demonstrates the usage workflow based on Mem0.

Basic Environment

Purchase an ECS instance and a PolarDB-PG instance in the same region. The ECS connects to the PolarDB-PG database via the internal network address.

Deploy Mem0

Download the Mem0 source code on the ECS: https://github.com/mem0ai/mem0

(Note: The open-source Mem0 code only supports PolarDB's pure vector long-term memory functionality. To experience the graph+vector long-term memory combination, please contact us via ticket or scan the QR code at the end of the article to join the DingTalk group.)

# Install dependency packages
pip3 install mem0ai
pip3 install rank_bm25

Using PolarDB-PG 16 as an example, the system provides the polar_age + pgvector plugins:

-- Create database
CREATE DATABASE mem0;
ALTER DATABASE mem0 SET session_preload_libraries TO 'polar_age';
-- Create extensions
CREATE EXTENSION polar_age;
CREATE EXTENSION vector;

Mem0 Configuration

Use the Qwen large language model and the text-embedding-v4 text vector model provided by Bailian. The specific configuration is as follows:

from mem0 import Memory

# This example must connect to an age-graph instance with 1536 vector dimensions specified.
config = {
    "llm": {
        "provider": "bailian",
        "config": {"model": "qwen-plus"},
    },
    "embedder": {
        "provider": "bailian",
        "config": {"model": "text-embedding-v4", "embedding_dims": 1536},
    },
    "vector_store": {
        "provider": "pgvector",
        "config": {
            "host": "pc-********.pg.polardb.rds.aliyuncs.com",
            "port": 5432,
            "dbname": "mem0",
            "user": "ganos",
            "password": "ganos",
            "collection_name": "memories",
        },
    },
    "graph_store": {
        "provider": "polardb",
        "config": {
            "url": "pc-********.pg.polardb.rds.aliyuncs.com",
            "port": 5432,
            "database": "mem0",
            "username": "ganos",
            "password": "ganos",
            "graphname": "mem0",
        },
    },
}

m = Memory.from_config(config_dict=config)

Create Graph

\c mem0
SELECT ag_catalog.create_graph('mem0');
ALTER TABLE mem0._ag_label_vertex ADD COLUMN embedding vector(1536);

Note: PolarDB-PG adds a vector field to the vertex table of the graph, enabling hybrid graph+vector retrieval at the underlying level.

Q&A Memory Flow Analysis

The following shows example results returned after the user calls the Mem0 interface.

  1. add("I like pizza", user_id="alice")
{
    "results": [{
        "id": "43558f37-b22c-4157-94db-c935728c74f6",
        "memory": "I like pizza",
        "event": "ADD"
    }],
    "relations": {
        "deleted_entities": [],
        "added_entities": [
            [{
                "source": "i",
                "relationship": "likes",
                "target": "pizza",
                "source_id": "3_14",
                "destination_id": "3_15",
                "relationship_id": "4_13"
            }]
        ]
    }
}
  1. add("I hate pizza", user_id="alice")
{
    "results": [{
        "id": "43558f37-b22c-4157-94db-c935728c74f6",
        "memory": "I like pizza",
        "event": "DELETE"
    }],
    "relations": {
        "deleted_entities": [
            []
        ],
        "added_entities": [
            [{
                "source": "user_id:_alice",
                "relationship": "hates",
                "target": "pizza",
                "source_id": "3_16",
                "destination_id": "3_15",
                "relationship_id": "5_5"
            }]
        ]
    }
}
  1. add("I like football", user_id="alice", metadata={"category": "hobbies"})
{
    "results": [{
        "id": "a3e8ae1e-3d11-4574-849c-63c058ba32cf",
        "memory": "I like football",
        "event": "ADD"
    }],
    "relations": {
        "deleted_entities": [],
        "added_entities": [
            [{
                "source": "i",
                "relationship": "likes",
                "target": "football",
                "source_id": "3_14",
                "destination_id": "3_17",
                "relationship_id": "4_14"
            }]
        ]
    }
}
  1. add("I like pizza", user_id="alice", agent_id="food-assistant")
{
    "results": [{
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "event": "ADD"
    }],
    "relations": {
        "deleted_entities": [],
        "added_entities": [
            [{
                "source": "alice",
                "relationship": "likes",
                "target": "pizza",
                "source_id": "3_18",
                "destination_id": "3_19",
                "relationship_id": "4_15"
            }]
        ]
    }
}
  1. get_all(user_id="alice")
{
    "results": [{
        "id": "a3e8ae1e-3d11-4574-849c-63c058ba32cf",
        "memory": "I like football",
        "hash": "db38eb7ce206333d12734e4251d964c1",
        "metadata": {
            "category": "hobbies"
        },
        "created_at": "2025-08-01T02:27:43.750094-07:00",
        "updated_at": None,
        "user_id": "alice"
    }, {
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }],
    "relations": [{
        "source": "user_id:_alice",
        "relationship": "hates",
        "target": "pizza"
    }, {
        "source": "i",
        "relationship": "likes",
        "target": "pizza"
    }, {
        "source": "i",
        "relationship": "likes",
        "target": "football"
    }, {
        "source": "alice",
        "relationship": "likes",
        "target": "pizza"
    }]
}
  1. get_all(user_id="alice", agent_id="food-assistant")
{
    "results": [{
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }],
    "relations": []
}
  1. search("tell me my name.", user_id="alice")
{
    "results": [{
        "id": "a3e8ae1e-3d11-4574-849c-63c058ba32cf",
        "memory": "I like football",
        "hash": "db38eb7ce206333d12734e4251d964c1",
        "metadata": {
            "category": "hobbies"
        },
        "score": 0.696223974227903,
        "created_at": "2025-08-01T02:27:43.750094-07:00",
        "updated_at": None,
        "user_id": "alice"
    }, {
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "score": 0.7073328582247228,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }],
    "relations": [{
        "source": "\"alice\"",
        "relationship": "likes",
        "destination": "\"pizza\""
    }]
}
  1. search("tell me my name.", user_id="alice", agent_id="food-assistant")
{
    "results": [{
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "score": 0.7073328582247228,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }],
    "relations": [{
        "source": "\"alice\"",
        "relationship": "likes",
        "destination": "\"pizza\""
    }]
}
  1. search("what food do you like.", user_id="alice")
{
    "results": [{
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "score": 0.41329889774629736,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }, {
        "id": "a3e8ae1e-3d11-4574-849c-63c058ba32cf",
        "memory": "I like football",
        "hash": "db38eb7ce206333d12734e4251d964c1",
        "metadata": {
            "category": "hobbies"
        },
        "score": 0.4586412037228734,
        "created_at": "2025-08-01T02:27:43.750094-07:00",
        "updated_at": None,
        "user_id": "alice"
    }],
    "relations": []
}
  1. search("what sport do you like.", user_id="alice")
{
    "results": [{
        "id": "a3e8ae1e-3d11-4574-849c-63c058ba32cf",
        "memory": "I like football",
        "hash": "db38eb7ce206333d12734e4251d964c1",
        "metadata": {
            "category": "hobbies"
        },
        "score": 0.2785342501720326,
        "created_at": "2025-08-01T02:27:43.750094-07:00",
        "updated_at": None,
        "user_id": "alice"
    }, {
        "id": "c2246008-8935-43ab-a6ed-f08dab766686",
        "memory": "I like pizza",
        "hash": "3acb2fb7961d7f09e42b99b96b605f3f",
        "metadata": None,
        "score": 0.4836080516459955,
        "created_at": "2025-08-01T02:27:48.270658-07:00",
        "updated_at": None,
        "user_id": "alice",
        "agent_id": "food-assistant"
    }],
    "relations": [{
        "source": "\"i\"",
        "relationship": "likes",
        "destination": "\"football\""
    }]
}
  1. delete_all(user_id="alice")
{
    "message": "Memories deleted successfully!"
}
  1. delete_all(user_id="alice", agent_id="food-assistant")
{
    "message": "Memories deleted successfully!"
}

Memory Classification and Tagging

Input:

# Add conversation memory
messages = [
    {"role": "user", "content": "I like sci-fi movies, especially Interstellar."},
    {"role": "assistant", "content": "Interstellar is indeed a classic sci-fi film! I'll remember you like this genre."},
    {"role": "user", "content": "I also like other movies by Christopher Nolan."},
    {"role": "assistant", "content": "Nolan's works are indeed outstanding! Besides Interstellar, he also directed Inception, Tenet, Memento, and other classics. Do you have any particular thoughts on these movies?"}
]
# Add memory with metadata
m.add(messages, user_id="alice", metadata={"category": "movies", "tags": ["sci-fi", "preferences"], "importance": "high"})
# Retrieve memory
memories = m.search(query="What type of movies do I like?", user_id="alice", filters={"category": "movies"})
import json
print(json.dumps(memories, indent=2, ensure_ascii=False))

Output:

{
  "results": [
    {
      "id": "cf931a04-628d-438a-9cf9-fd90c6d53eab",
      "memory": "I like sci-fi movies, especially Interstellar",
      "hash": "d3747e8081810fc535f641ee28207d90",
      "metadata": {
        "tags": ["sci-fi", "preferences"],
        "category": "movies",
        "importance": "high"
      },
      "created_at": "2025-08-04T05:16:03.481050-07:00",
      "updated_at": null,
      "user_id": "alice"
    },
    {
      "id": "8a6ad8ac-ce78-4a0a-9042-8475bd76f90e",
      "memory": "I also like other movies by Christopher Nolan",
      "hash": "77408cade088bfad20138a6b405f9e65",
      "metadata": {
        "tags": ["sci-fi", "preferences"],
        "category": "movies",
        "importance": "high"
      },
      "created_at": "2025-08-04T05:16:03.493277-07:00",
      "updated_at": null,
      "user_id": "alice"
    }
  ],
  "relations": [
    {"source": "user_id:_alice", "relationship": "likes", "target": "sci-fi movies"},
    {"source": "user_id:_alice", "relationship": "likes", "target": "Interstellar"},
    {"source": "Interstellar", "relationship": "belongs to", "target": "sci-fi movies"},
    {"source": "Christopher Nolan", "relationship": "directed", "target": "Interstellar"},
    {"source": "Christopher Nolan", "relationship": "directed", "target": "Inception"},
    {"source": "Christopher Nolan", "relationship": "directed", "target": "Tenet"},
    {"source": "Christopher Nolan", "relationship": "directed", "target": "Memento"}
  ]
}

Observing the above example, compared to pure vector memory, the combination with graph search adds the output of entity relationships (relations). That is, which movies the user has a 'likes' relationship with, and whether the movies 'belong to' the sci-fi category, etc. This overlays semantic information with relational connections, providing structured and hierarchical extended content for further business rule filtering or as input to large language model prompts.

Conclusion

This article introduces a long/short-term memory fusion solution based on Mem0 and PolarDB (PostgreSQL-compatible version). It covers the application background, core components, solution advantages, and usage process. Compared to similar products, the long-term memory solution based on PolarDB-PG offers comprehensive advantages: one-stop integration of vector and graph databases, system scalability, cloud-native architecture + model inference acceleration + distributed KV Cache for high performance. In terms of product maturity, cost, performance, security, and scalability, it provides essential foundational components for building long-term memory in AI agent applications.

Trial Experience

Please visit the PolarDB free trial page to select and try 'Cloud Native Database PolarDB PostgreSQL Edition' and experience PolarDB's AI capabilities.

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.