|
| 1 | +--- |
| 2 | +title: Redis Memory Storage |
| 3 | +--- |
| 4 | + |
| 5 | +## Code |
| 6 | + |
| 7 | +```python cookbook/agent_concepts/memory/redis_memory.py |
| 8 | +""" |
| 9 | +This example shows how to use the Memory class with Redis storage. |
| 10 | +""" |
| 11 | + |
| 12 | +from agno.agent.agent import Agent |
| 13 | +from agno.memory.v2.db.redis import RedisMemoryDb |
| 14 | +from agno.memory.v2.memory import Memory |
| 15 | +from agno.models.openai import OpenAIChat |
| 16 | +from agno.storage.redis import RedisStorage |
| 17 | + |
| 18 | +# Create Redis memory database |
| 19 | +memory_db = RedisMemoryDb( |
| 20 | + prefix="agno_memory", # Prefix for Redis keys to namespace the memories |
| 21 | + host="localhost", # Redis host address |
| 22 | + port=6379, # Redis port number |
| 23 | +) |
| 24 | + |
| 25 | +# Create memory instance with Redis backend |
| 26 | +memory = Memory(db=memory_db) |
| 27 | + |
| 28 | +# This will clear any existing memories |
| 29 | +memory.clear() |
| 30 | + |
| 31 | +# Session and user identifiers |
| 32 | +session_id = "redis_memories" |
| 33 | +user_id = "redis_user" |
| 34 | + |
| 35 | +# Create agent with memory and Redis storage |
| 36 | +agent = Agent( |
| 37 | + model=OpenAIChat(id="gpt-4o-mini"), |
| 38 | + memory=memory, |
| 39 | + storage=RedisStorage(prefix="agno_test", host="localhost", port=6379), |
| 40 | + enable_user_memories=True, |
| 41 | + enable_session_summaries=True, |
| 42 | +) |
| 43 | + |
| 44 | +# First interaction - introducing personal information |
| 45 | +agent.print_response( |
| 46 | + "My name is John Doe and I like to hike in the mountains on weekends.", |
| 47 | + stream=True, |
| 48 | + user_id=user_id, |
| 49 | + session_id=session_id, |
| 50 | +) |
| 51 | + |
| 52 | +# Second interaction - testing if memory was stored |
| 53 | +agent.print_response( |
| 54 | + "What are my hobbies?", |
| 55 | + stream=True, |
| 56 | + user_id=user_id, |
| 57 | + session_id=session_id |
| 58 | +) |
| 59 | + |
| 60 | +# Display the memories stored in Redis |
| 61 | +memories = memory.get_user_memories(user_id=user_id) |
| 62 | +print("Memories stored in Redis:") |
| 63 | +for i, m in enumerate(memories): |
| 64 | + print(f"{i}: {m.memory}") |
| 65 | +``` |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +<Steps> |
| 70 | + <Snippet file="create-venv-step.mdx" /> |
| 71 | + |
| 72 | + <Step title="Set environment variables"> |
| 73 | + ```bash |
| 74 | + export OPENAI_API_KEY=xxx |
| 75 | + ``` |
| 76 | + </Step> |
| 77 | + |
| 78 | + <Step title="Install libraries"> |
| 79 | + ```bash |
| 80 | + pip install -U agno openai redis |
| 81 | + ``` |
| 82 | + </Step> |
| 83 | + |
| 84 | + <Step title="Run Redis"> |
| 85 | + ```bash |
| 86 | + docker run --name my-redis -p 6379:6379 -d redis |
| 87 | + ``` |
| 88 | + </Step> |
| 89 | + |
| 90 | + <Step title="Run Example"> |
| 91 | + <CodeGroup> |
| 92 | + ```bash Mac/Linux |
| 93 | + python cookbook/agent_concepts/memory/redis_memory.py |
| 94 | + ``` |
| 95 | + |
| 96 | + ```bash Windows |
| 97 | + python cookbook/agent_concepts/memory/redis_memory.py |
| 98 | + ``` |
| 99 | + </CodeGroup> |
| 100 | + </Step> |
| 101 | +</Steps> |
0 commit comments