What is LangMem (memory for LangGraph)?

What is LangMem (memory for LangGraph)?

AI agents, like humans, benefit from different types of memory: semantic (facts), episodic (experiences), and procedural (skills).

Effective memory management allows agents to learn from past interactions, personalize user experiences, and continuously improve their behavior.

Why Memory Matters for AI Agents

Imagine interacting with an AI assistant that forgets everything you tell it after each interaction. It would be frustrating and inefficient, right? Just like humans, AI agents need memory to learn, adapt, and provide personalized experiences.

Long-term memory allows agents to retain information across interactions, understand user preferences, and improve their responses over time. This is crucial for building truly intelligent and helpful AI systems.

What We'll Cover

In this article, we'll explore how AI agents can leverage different types of memory – semantic, episodic, and procedural – using the LangMem and LangGraph libraries within the Langchain framework.

Understanding Memory Types in AI Agents

Just as humans have different types of memory, AI agents can also benefit from distinct memory systems. Let's break down the three key types we'll be focusing on:

Semantic Memory: Facts and Knowledge

This is your agent's knowledge base. It stores facts, concepts, and general information. Think of it as the agent knowing that "Python is a programming language" or remembering a user's preferred writing style. Semantic memory can be stored in two main ways within LangMem:

Collections: These are for storing a large, potentially unbounded amount of knowledge. Imagine a vast database of facts that the agent can search through. Collections are useful when you need to recall information contextually and track knowledge across many interactions.

Profiles: These are structured documents designed for task-specific information. A profile represents the current state of a user or a specific task, containing key details like user preferences, goals, or current project status. Profiles are ideal when you need quick access to the most up-to-date information and want to avoid storing extraneous details. When new information arrives, profiles are updated, ensuring the agent always has the latest context.

Episodic Memory: Past Experiences

Episodic memory is all about remembering specific events and interactions. For an AI agent, this could be recalling past conversations with a user, remembering successful interaction patterns, or storing few-shot examples of desired behavior. Episodic memories are typically stored in collections, allowing the agent to learn from a history of interactions. For example, an agent might remember a previous conversation where a user asked for practical examples instead of theoretical explanations and apply that learning in future interactions.

Procedural Memory: System Behavior

Procedural memory governs how an agent behaves and responds. It's about learning and refining the agent's operational instructions and response patterns. This type of memory starts with initial system prompts that define the agent's core behavior. As the agent interacts with users and receives feedback, procedural memory evolves, allowing the agent to learn which approaches work best in different situations. For instance, an agent might initially be instructed to be a helpful assistant, but through interactions, it might learn to adopt a more concise or detailed style based on user preferences. Procedural memory can be stored as prompt rules or within collections.

Semantic Memory

  • Purpose: Facts & Knowledge
  • Agent Example: User preferences; knowledge triplets
  • Human Example: Knowing Python is a programming language
  • Typical Storage Pattern: Profile or Collection

Episodic Memory

  • Purpose: Past Experiences
  • Agent Example: Few-shot examples; Summaries of past conversations
  • Human Example: Remembering your first day at work
  • Typical Storage Pattern: Collection

Procedural Memory

  • Purpose: System Behavior
  • Agent Example: Core personality and response patterns
  • Human Example: Knowing how to ride a bicycle

LangMem

LangMem is a library specifically designed to enhance the memory capabilities of AI agents within the Langchain ecosystem. It provides tools and mechanisms to effectively manage the different types of memory we just discussed.

One of LangMem's key features is its memory enrichment process. This process automatically extracts and stores relevant information from conversations. For example, if a user mentions "I prefer to be called Lex," LangMem can extract this preference and store it as part of the user's semantic memory profile.

Let's look at an example of how memories are extracted and stored:

memories = manager.invoke({"messages": conversation})

#Example memories:

ExtractedMemory(
    id="27e96a9d-8e53-4031-865e-5ec50c1f7ad5",
    content=Memory(content="[IMPORTANT] User prefers to be called Lex (short for Alex) and appreciates casual, witty communication style with relevant emojis."
    ),
)

ExtractedMemory(
    id="e2f6b646-cdf1-4be1-bb40-0fd91d25d00f",
    content=Memory(
        content="[BACKGROUND] Lex is proficient in Python programming and specializes in developing AI systems with a focus on making them sound more natural and less corporate."
    ),
)

ExtractedMemory(

    id="c1e03ebb-a393-4e8d-8eb7-b928d8bed510",

    content=Memory(

        content="[HOBBY] Lex is a competitive speedcuber (someone who solves Rubik's cubes competitively), showing an interest in both technical and recreational puzzle-solving."
    ),
)
ExtractedMemory(
    id="ee7fc6e4-0118-425f-8704-6b3145881ff7",
    content=Memory(content="[PERSONALITY] Based on communication style and interests, Lex appears to value creativity, and technical excellence while maintaining a fun, approachable demeanor."
    ),
)        

As you can see, LangMem extracts various pieces of information, categorizes them (IMPORTANT, BACKGROUND, HOBBY, PERSONALITY), and stores them with unique IDs. This structured approach allows the agent to efficiently retrieve and utilize specific memories when needed.

Furthermore, LangMem allows for direct modification of user profiles. This means you can update an agent's knowledge about a user in real-time, ensuring the agent is always working with the most current information.

Building Adaptable Agents with Memory

LangGraph, built on top of Langchain, is a framework for creating applications using large language models, with a strong emphasis on memory management. LangGraph leverages LangMem and its core memory types to enable the development of truly adaptable agents.

LangGraph helps you structure your AI applications in a way that naturally incorporates memory. It allows you to define how different memory types are used within your agent's workflow. For example, you can design a LangGraph application where:

Semantic memory (user profiles) is loaded at the beginning of each interaction to personalize the agent's responses.

Episodic memory (conversation history) is consulted to maintain context and avoid repeating information.

Procedural memory (optimized prompts) is used to guide the agent's behavior and response style.

Learning and Adaptation through Memory

The combination of LangMem and LangGraph empowers AI agents to learn and adapt over time. This learning happens through two main mechanisms:

Conscious Memory Formation: This occurs actively during conversations. When important context emerges in a user interaction, LangMem can immediately extract and store it, ensuring the agent can utilize this information in the current conversation and future interactions.

Subconscious Memory Formation: This happens in the background, between interactions. LangMem can analyze conversation history and identify patterns, refine procedural memory, and update semantic and episodic memories for long-term learning. This background processing allows for deeper analysis without impacting the agent's immediate responsiveness.

One powerful example of adaptation is prompt optimization. Procedural memory, which includes system prompts, can be dynamically refined based on user feedback and interaction history. Langchain's prompt optimizers, integrated with LangMem and LangGraph, allow you to automatically improve prompts over time.

Here's an example of how to optimize a prompt using Langchain:

from langchain.prompts import PromptOptimizerChain # Assuming this is where the optimizer is located

prompt = "You are a helpful assistant."

trajectory = [
{
"role": "user", "content": "Explain inheritance in Python"},
    {"role": "assistant", "content": "Here's a detailed theoretical explanation..."},
    {"role": "user", "content": "Show me a practical example instead"},
]

optimizer = PromptOptimizerChain(...) # Instantiate your prompt optimizer

optimized = optimizer.invoke({
    "trajectories": [(trajectory, {"user_score": 0})], # User score could reflect satisfaction
    "prompt": prompt
})

print(optimized)        

The output of this optimization process might be a refined prompt like this:

# You are a helpful assistant with expertise in explaining technical concepts clearly and practical:

# 1. Start with a brief, practical explanation supported by a concrete code example

# 2. If the user requests more theoretical details, provide them after the practical example

# 3. Always include working code examples for programming-related questions

# 4. Pay close attention to user preferences if they ask for a specific approach (like practical,

# 5. Use simple, clear language and break down complex concepts into digestible parts

# When users ask follow-up questions or request a different approach, immediately adjust your explai        

This optimized prompt now incorporates learnings from past interactions, guiding the agent to provide more practical explanations first and adapt to user preferences, demonstrating the power of procedural memory and prompt optimization.

Building Smarter, More Human-Like Agents

By leveraging semantic, episodic, and procedural memory, agents can:

Personalize user experiences: Remember preferences, past interactions, and individual needs.

Learn from experience: Adapt their behavior and improve their responses over time based on user feedback and interaction history.

Maintain context: Engage in more natural and coherent conversations by recalling previous turns and user goals.

As you build your own AI agents, consider how you can incorporate long-term memory using LangMem and LangGraph. By doing so, you can create agents that are not only more effective but also more engaging and human-like in their interactions.

Chris "The Wiz ??" Alexiuk

Deep Learning @ NVIDIA - ??? Build | ?? Ship | ?? Share

1 个月

LangMem is great!

Ugo Osuji

Customer AI Engineering @Writer | Enterprise Generative AI ?????? | Builder??? | AI Educator ????

1 个月

I remember getting excited about this more than a year ago. Nice that it’s launched now. Agents are only as good as their short and long term memory.

要查看或添加评论,请登录

Hai N.的更多文章