|
1 | | -# LangGraph Integration |
| 1 | +# MemU LangGraph Integration |
2 | 2 |
|
3 | | -This integration provides a seamless way to use MemU's long-term memory capabilities within LangGraph and LangChain agents. |
| 3 | +The MemU LangGraph Integration provides a seamless adapter to expose MemU's powerful memory capabilities (`memorize` and `retrieve`) as standard [LangChain](https://python.langchain.com/) / [LangGraph](https://langchain-ai.github.io/langgraph/) tools. This allows your agents to persist information and recall it across sessions using MemU as the long-term memory backend. |
4 | 4 |
|
5 | | -## Installation |
6 | | - |
7 | | -Ensure you have MemU installed with the LangGraph optional dependencies: |
8 | | - |
9 | | -```bash |
10 | | -uv sync --extra langgraph |
11 | | -# OR |
12 | | -pip install memu[langgraph] |
13 | | -``` |
| 5 | +## Overview |
14 | 6 |
|
15 | | -## Usage |
| 7 | +This integration wraps the `MemoryService` and exposes two key tools: |
| 8 | +- **`save_memory`**: Persists text, conversation snippets, or facts associated with a user. |
| 9 | +- **`search_memory`**: Retrieves relevant memories based on semantic search queries. |
16 | 10 |
|
17 | | -The integration exposes a helper class `MemULangGraphTools` that wraps MemU's `MemoryService` into LangChain-compatible tools. |
| 11 | +These tools are fully typed and compatible with LangGraph's `prebuilt.ToolNode` and LangChain's agents. |
18 | 12 |
|
19 | | -### 1. Initialize MemoryService |
20 | | - |
21 | | -First, initialize the core memory service. |
| 13 | +## Installation |
22 | 14 |
|
23 | | -```python |
24 | | -from memu.app.service import MemoryService |
| 15 | +To use this integration, you need to install the optional dependencies: |
25 | 16 |
|
26 | | -service = MemoryService() |
| 17 | +```bash |
| 18 | +uv add langgraph langchain-core |
27 | 19 | ``` |
28 | 20 |
|
29 | | -### 2. Create Tools |
| 21 | +## Quick Start |
30 | 22 |
|
31 | | -Pass the service to the adapter to generate the tools. |
| 23 | +Here is a complete example of how to initialize the MemU memory service and bind it to a LangGraph agent. |
32 | 24 |
|
33 | 25 | ```python |
| 26 | +import asyncio |
| 27 | +import os |
| 28 | +from memu.app.service import MemoryService |
34 | 29 | from memu.integrations.langgraph import MemULangGraphTools |
35 | 30 |
|
36 | | -adapter = MemULangGraphTools(service) |
37 | | -tools = adapter.tools() |
38 | | -# tools now contains [save_memory, search_memory] |
| 31 | +# Ensure you have your configuration set (e.g., env vars for DB connection) |
| 32 | +# os.environ["MEMU_DATABASE_URL"] = "..." |
| 33 | + |
| 34 | +async def main(): |
| 35 | + # 1. Initialize MemoryService |
| 36 | + memory_service = MemoryService() |
| 37 | + # If your service requires async init (check your specific implementation): |
| 38 | + # await memory_service.initialize() |
| 39 | + |
| 40 | + # 2. Instantiate MemULangGraphTools |
| 41 | + memu_tools = MemULangGraphTools(memory_service) |
| 42 | + |
| 43 | + # Get the list of tools (BaseTool compatible) |
| 44 | + tools = memu_tools.tools() |
| 45 | + |
| 46 | + # 3. Example Usage: Manually invoking a tool |
| 47 | + # In a real app, you would pass 'tools' to your LangGraph agent or StateGraph. |
| 48 | + |
| 49 | + # Save a memory |
| 50 | + save_tool = memu_tools.save_memory_tool() |
| 51 | + print("Saving memory...") |
| 52 | + result = await save_tool.ainvoke({ |
| 53 | + "content": "The user prefers dark mode.", |
| 54 | + "user_id": "user_123", |
| 55 | + "metadata": {"category": "preferences"} |
| 56 | + }) |
| 57 | + print(f"Save Result: {result}") |
| 58 | + |
| 59 | + # Search for a memory |
| 60 | + search_tool = memu_tools.search_memory_tool() |
| 61 | + print("\nSearching memory...") |
| 62 | + search_result = await search_tool.ainvoke({ |
| 63 | + "query": "What are the user's preferences?", |
| 64 | + "user_id": "user_123" |
| 65 | + }) |
| 66 | + print(f"Search Result:\n{search_result}") |
| 67 | + |
| 68 | +if __name__ == "__main__": |
| 69 | + asyncio.run(main()) |
39 | 70 | ``` |
40 | 71 |
|
41 | | -### 3. Integrate with LangGraph |
42 | | - |
43 | | -Add the tools to your LangGraph `ToolNode` or bind them to your LLM. |
44 | | - |
45 | | -```python |
46 | | -from langgraph.prebuilt import ToolNode |
47 | | -from langchain_openai import ChatOpenAI |
| 72 | +## API Reference |
48 | 73 |
|
49 | | -# Bind tools to LLM |
50 | | -llm = ChatOpenAI(model="gpt-4") |
51 | | -llm_with_tools = llm.bind_tools(tools) |
| 74 | +### `MemULangGraphTools` |
52 | 75 |
|
53 | | -# Create ToolNode |
54 | | -tool_node = ToolNode(tools=tools) |
| 76 | +The main adapter class. |
55 | 77 |
|
56 | | -# ... Build your StateGraph using tool_node ... |
| 78 | +```python |
| 79 | +class MemULangGraphTools(memory_service: MemoryService) |
57 | 80 | ``` |
58 | 81 |
|
59 | | -## Available Tools |
60 | | - |
61 | | -### `save_memory` |
62 | | -Allows the agent to save important information, user preferences, or conversation snippets to long-term memory. |
63 | | - |
64 | | -- **Arguments:** |
65 | | - - `content` (str): The information to save. |
66 | | - - `user_id` (str): The user associated with this memory. |
67 | | - - `metadata` (dict, optional): Additional context (e.g., category, importance). |
68 | | - |
69 | | -### `search_memory` |
70 | | -Allows the agent to retrieve relevant information from memory based on a query. |
| 82 | +#### `save_memory_tool() -> StructuredTool` |
| 83 | +Returns a tool named `save_memory`. |
| 84 | +- **Inputs**: `content` (str), `user_id` (str), `metadata` (dict, optional). |
| 85 | +- **Description**: Save a piece of information, conversation snippet, or memory for a user. |
71 | 86 |
|
72 | | -- **Arguments:** |
73 | | - - `query` (str): The question or topic to search for. |
74 | | - - `user_id` (str): The user associated with this memory. |
75 | | - - `limit` (int, default=5): Number of results to return. |
| 87 | +#### `search_memory_tool() -> StructuredTool` |
| 88 | +Returns a tool named `search_memory`. |
| 89 | +- **Inputs**: `query` (str), `user_id` (str), `limit` (int, default=5), `metadata_filter` (dict, optional), `min_relevance_score` (float, default=0.0). |
| 90 | +- **Description**: Search for relevant memories or information for a user based on a query. |
76 | 91 |
|
77 | | -## Example |
| 92 | +## Troubleshooting |
78 | 93 |
|
79 | | -See `examples/langgraph_demo.py` for a complete running example of a chatbot that uses MemU to remember user details across sessions. |
| 94 | +### Import Errors |
| 95 | +If you see an `ImportError` regarding `langchain_core` or `langgraph`: |
| 96 | +1. Ensure you have installed the extras: `uv add langgraph langchain-core` (or `pip install langgraph langchain-core`). |
| 97 | +2. Verify your virtual environment is active. |
0 commit comments