Skip to content

Commit d098771

Browse files
authored
add redis memory docs (#211)
## What does this PR do? - add redis memory docs in intro, examples and reference. - update path for memory db in intro section.
1 parent 7d20b52 commit d098771

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
### Parameters
2+
3+
| Parameter | Type | Description | Default |
4+
|-----------|------|-------------|---------|
5+
| `prefix` | `str` | Prefix for Redis keys to namespace the memories | `"agno_memory"` |
6+
| `host` | `str` | Redis host address | `"localhost"` |
7+
| `port` | `int` | Redis port number | `6379` |
8+
| `db` | `int` | Redis database number | `0` |
9+
| `password` | `Optional[str]` | Redis password if authentication is required | `None` |
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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>

memory/storage.mdx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ Agno's memory system provides persistent storage options to ensure memories are
1010

1111
The Memory class supports different backend storage options through a pluggable database interface. Currently, Agno provides:
1212

13-
1. [SQLite Storage](/reference/memory/sqlite)
14-
2. [PostgreSQL Storage](/reference/memory/postgres)
15-
3. [MongoDB Storage](/reference/memory/mongodb)
13+
1. [SQLite Storage](/reference/memory/storage/sqlite.mdx)
14+
2. [PostgreSQL Storage](/reference/memory/storage/postgres.mdx)
15+
3. [MongoDB Storage](/reference/memory/storage/mongo.mdx)
16+
4. [Redis Storage](/reference/memory/storage/redis.mdx)
1617

1718
## Setting Up Storage
1819

mint.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@
530530
"examples/concepts/memory/10-multi-user-multi-session-chat",
531531
"examples/concepts/memory/mem-mongodb-memory",
532532
"examples/concepts/memory/mem-postgres-memory",
533+
"examples/concepts/memory/mem-redis-memory",
533534
"examples/concepts/memory/mem0-memory"
534535
]
535536
},
@@ -1107,7 +1108,8 @@
11071108
"pages": [
11081109
"reference/memory/storage/mongo",
11091110
"reference/memory/storage/postgres",
1110-
"reference/memory/storage/sqlite"
1111+
"reference/memory/storage/sqlite",
1112+
"reference/memory/storage/redis"
11111113
]
11121114
}
11131115
]

reference/memory/storage/redis.mdx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: RedisMemoryDb
3+
---
4+
5+
RedisMemoryDb is a class that implements the MemoryDb interface using Redis as the backend storage system. It provides persistent storage for agent memories with support for JSONB data types, timestamps, and efficient querying.
6+
7+
<Snippet file="memory-redis-reference.mdx" />

0 commit comments

Comments
 (0)