diff --git a/python/docs/src/user-guide/agentchat-user-guide/memory.ipynb b/python/docs/src/user-guide/agentchat-user-guide/memory.ipynb index 35b14939e82b..5b8c88e311ba 100644 --- a/python/docs/src/user-guide/agentchat-user-guide/memory.ipynb +++ b/python/docs/src/user-guide/agentchat-user-guide/memory.ipynb @@ -730,6 +730,183 @@ "config_json = mem0_memory.dump_component().model_dump_json()\n", "print(f\"Memory config JSON: {config_json[:100]}...\")" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Zep Memory Example\n", + "\n", + "[Zep](https://www.getzep.com/) is a [context engineering platform](https://www.getzep.com/solutions/context-engineering/) that provides advanced memory capabilities for AI agents. It offers both user-specific conversation memory and structured knowledge graph memory, enabling agents to maintain context across interactions and build rich knowledge representations.\n", + "\n", + "### Setup\n", + "\n", + "To use Zep memory, install the required package:\n", + "\n", + "```bash\n", + "pip install zep-autogen\n", + "```\n", + "\n", + "Set your API keys as environment variables:\n", + "\n", + "```bash\n", + "export ZEP_API_KEY=\"your_zep_api_key\"\n", + "export OPENAI_API_KEY=\"your_openai_api_key\"\n", + "```\n", + "\n", + "### User Memory\n", + "\n", + "In the following example, we'll demonstrate how to use `ZepUserMemory` to maintain persistent conversation context:\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import os\n", + "from autogen_agentchat.agents import AssistantAgent\n", + "from autogen_agentchat.ui import Console\n", + "from autogen_ext.models.openai import OpenAIChatCompletionClient\n", + "from zep_autogen import ZepUserMemory\n", + "from zep_cloud import AsyncZep\n", + "\n", + "# Initialize Zep client\n", + "zep_client = AsyncZep(api_key=os.environ.get(\"ZEP_API_KEY\"))\n", + "\n", + "# Create user memory with perpetual mode for persistent context\n", + "zep_memory = ZepUserMemory(\n", + " client=zep_client,\n", + " user_id=\"user123\",\n", + " session_id=\"conversation_001\",\n", + " memory_type=\"perpetual\" # Persistent memory across sessions\n", + ")\n", + "\n", + "# Create assistant with Zep memory\n", + "assistant_agent = AssistantAgent(\n", + " name=\"assistant_agent\",\n", + " model_client=OpenAIChatCompletionClient(\n", + " model=\"gpt-4o-2024-08-06\",\n", + " ),\n", + " memory=[zep_memory],\n", + " system_message=\"You are a helpful assistant with persistent memory.\"\n", + ")\n", + "\n", + "# The agent will automatically maintain conversation context\n", + "stream = assistant_agent.run_stream(\n", + " task=\"I prefer metric units and I'm vegan. Remember this for our conversation.\"\n", + ")\n", + "await Console(stream)\n", + "\n", + "# In a follow-up conversation, the agent remembers preferences\n", + "stream = assistant_agent.run_stream(\n", + " task=\"What's the temperature in New York?\"\n", + ")\n", + "await Console(stream)\n", + "\n", + "# Clean up resources\n", + "await zep_memory.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "The example above shows how ZepUserMemory automatically:\n", + "\n", + "1. Stores all conversation history in Zep's memory system\n", + "2. Retrieves relevant context based on the conversation\n", + "3. Maintains user preferences and important facts across interactions\n", + "4. Provides automatic summarization for longer conversations\n", + "\n", + "### Knowledge Graph Memory\n", + "\n", + "For more complex knowledge representation, Zep also supports structured knowledge graphs:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from zep_autogen import ZepGraphMemory\n", + "\n", + "# Initialize knowledge graph memory\n", + "kg_memory = ZepGraphMemory(\n", + " client=zep_client,\n", + " user_id=\"user123\"\n", + ")\n", + "\n", + "# Add structured knowledge\n", + "await kg_memory.add(\n", + " MemoryContent(\n", + " content=\"Project Alpha deadline is March 15, 2025\",\n", + " mime_type=MemoryMimeType.TEXT,\n", + " metadata={\"type\": \"project\", \"importance\": \"high\"}\n", + " )\n", + ")\n", + "\n", + "await kg_memory.add(\n", + " MemoryContent(\n", + " content=\"Team lead for Project Alpha is Sarah Johnson\",\n", + " mime_type=MemoryMimeType.TEXT,\n", + " metadata={\"type\": \"project\", \"relation\": \"team\"}\n", + " )\n", + ")\n", + "\n", + "# Create an agent with both user and knowledge graph memory\n", + "multi_memory_agent = AssistantAgent(\n", + " name=\"project_assistant\",\n", + " model_client=OpenAIChatCompletionClient(model=\"gpt-4o\"),\n", + " memory=[zep_memory, kg_memory],\n", + " system_message=\"You help manage project information and team coordination.\"\n", + ")\n", + "\n", + "# The agent can now access both conversation history and structured knowledge\n", + "stream = multi_memory_agent.run_stream(\n", + " task=\"When is the Project Alpha deadline and who's leading it?\"\n", + ")\n", + "await Console(stream)\n", + "\n", + "await kg_memory.close()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Configuration Options\n", + "\n", + "Zep memory provides several configuration options to customize behavior:\n", + "\n", + "- **memory_type**: Choose \"perpetual\" for persistent context or other modes for session-specific memory\n", + "- **user_id**: Unique identifier for the user (required for memory persistence)\n", + "- **session_id**: Conversation session identifier for organizing related interactions\n", + "- **Knowledge graph scopes**: Query specific types of information (edges, nodes, episodes)\n", + "\n", + "Zep is particularly useful for:\n", + "- Applications requiring long-term memory persistence\n", + "- Complex conversations needing context summarization\n", + "- Knowledge-intensive tasks with structured data relationships\n", + "- Multi-session user interactions requiring continuity\n", + "\n", + "For more advanced features including custom entity models, memory search, and manual memory tools, refer to the [Zep documentation](https://help.getzep.com/ecosystem/autogen-memory).\n", + "\n", + "Just like other memory implementations, you can serialize ZepUserMemory configurations:" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Serialize the memory configuration\n", + "config_json = zep_memory.dump_component().model_dump_json()\n", + "print(f\"Zep memory config: {config_json[:100]}...\")" + ] } ], "metadata": {