跳到内容

如何使用记忆工具

LangMem 提供的工具可让您的智能体在 LangGraph 存储中存储和搜索记忆。

基本用法

使用记忆工具创建智能体

API:create_react_agent | create_manage_memory_tool | create_search_memory_tool

from langgraph.prebuilt import create_react_agent
from langgraph.store.memory import InMemoryStore
from langmem import create_manage_memory_tool, create_search_memory_tool

# Set up store and memory saver
store = InMemoryStore(
    index={
        "dims": 1536,
        "embed": "openai:text-embedding-3-small",
    }
) # (1)!
  1. 对于生产部署,请使用持久性存储,如 AsyncPostgresStoreInMemoryStore 适用于开发,但在重启后不保留数据。
# Create agent with memory tools
agent = create_react_agent(
    "anthropic:claude-3-5-sonnet-latest",
    tools=[
        # Configure memory tools with runtime namespace (1)
        create_manage_memory_tool(namespace=("memories", "{user_id}")),
        create_search_memory_tool(namespace=("memories", "{user_id}")),
    ],
    store=store,
)
  1. {user_id} 占位符让记忆工具可以访问 LangGraph 的 BaseStore 命名空间,并且所有工具共享同一个命名空间以保持一致性。

    # Example 1: Store and search User A's memories
    response_a = agent.invoke(
        {"messages": [{"role": "user", "content": "Remember my favorite color is blue"}]},
        config={"configurable": {"user_id": "user-a"}}
    )  # Both tools use namespace ("memories", "user-a")
    
    # Example 2: Store and search User B's memories
    response_b = agent.invoke(
        {"messages": [{"role": "user", "content": "Remember I prefer dark mode"}]},
        config={"configurable": {"user_id": "user-b"}}
    )  # Both tools use namespace ("memories", "user-b")
    

    共享命名空间结构 ("memories", "{user_id}") 支持不同的记忆组织模式。

    # Personal memories
    namespace=("memories", "user-123")
    
    # Shared team knowledge
    namespace=("memories", "team-product")
    
    # Project-specific memories
    namespace=("memories", "project-x")
    
    # Use the agent
    config = {"configurable": {"user_id": "user-1"}}
    
    # Store a preference
    response = agent.invoke(
        {"messages": [{"role": "user", "content": "Remember I prefer dark mode"}]},
        config=config,
    )
    
    # Search preferences
    response = agent.invoke(
        {"messages": [{"role": "user", "content": "What are my preferences?"}]},
        config=config,
    )
    
    
    agent_a_tools = [
        # Write to agent-specific namespace
        create_manage_memory_tool(namespace=("memories", "team_a", "agent_a")),
        # Read from shared team namespace
        create_search_memory_tool(namespace=("memories", "team_a"))
    ]
    
    
    
    # Agents with different prompts sharing read access
    agent_a = create_react_agent(
        "anthropic:claude-3-5-sonnet-latest",
        tools=agent_a_tools,
        store=store,
        prompt="You are a research assistant"
    )
    
    # Create tools for agent B with different write space
    agent_b_tools = [
        create_manage_memory_tool(namespace=("memories", "team_a", "agent_b")),
        create_search_memory_tool(namespace=("memories", "team_a"))
    ]
    agent_b = create_react_agent(
        "anthropic:claude-3-5-sonnet-latest",
        tools=agent_b_tools,
        store=store,
        prompt="You are a report writer."
    )
    
    agent_b.invoke({"messages": [{"role": "user", "content": "Hi"}]})
    

共享存储

在给定的部署中,存储是共享的。这让您可以创建命名空间的记忆,以便在团队中的智能体之间共享数据。

agent_a_tools = [
    # Write to agent-specific namespace
    create_manage_memory_tool(namespace=("memories", "team_a", "agent_a")),
    # Read from shared team namespace
    create_search_memory_tool(namespace=("memories", "team_a"))
]



# Agents with different prompts sharing read access
agent_a = create_react_agent(
    "anthropic:claude-3-5-sonnet-latest",
    tools=agent_a_tools,
    store=store,
    prompt="You are a research assistant"
)

# Create tools for agent B with different write space
agent_b_tools = [
    create_manage_memory_tool(namespace=("memories", "team_a", "agent_b")),
    create_search_memory_tool(namespace=("memories", "team_a"))
]
agent_b = create_react_agent(
    "anthropic:claude-3-5-sonnet-latest",
    tools=agent_b_tools,
    store=store,
    prompt="You are a report writer."
)

agent_b.invoke({"messages": [{"role": "user", "content": "Hi"}]})

有关存储模式,请参阅存储系统

评论