You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/uc/ai_assistant.md
+47-49Lines changed: 47 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,16 @@
1
1
This tutorial demonstrates how to build an AI assistant's memory system with Redis as its memory core.
2
2
3
-
**Note**: Requires [Redis 8](https://hub.docker.com/_/redis/tags) for `HSETEX`, which adds per-field TTL for hashes, which is ideal for rate limiting to ensure fair resource usage.
3
+
**Note**: Requires [Redis 8](https://hub.docker.com/_/redis/tags) for `HSETEX`, which adds per-field TTL for hashes - ideal for rate limiting to ensure fair resource usage.
4
4
5
5
### Architecture Overview
6
6
| Layer | Description | Data type |
7
7
| ---------- | ---------- | ---------- |
8
8
|`Session History`|`Recent conversation context`| List |
|`Knowledge Base`|`Long-term facts and preferences`| Hash |
10
+
|`User Memory`|`Long-term facts and preferences`| Hash |
11
11
12
12
### Session History
13
-
AI assistants need context from previous messages to provide coherent responses. Without conversation history, each interaction would be isolated and the AI couldn't reference what was discussed earlier. We can store conversation history using Redis lists - simple, ordered, and efficient for chat scenarios.
13
+
AI assistants need context from previous messages to provide coherent responses. Without conversation history, each interaction would be isolated. Redis lists are simple, ordered, and efficient for storing chat transcripts.
14
14
15
15
```redis:[run_confirmation=true] Store conversation history
Now we can retrieve conversation history to provide context to the AI.
29
+
You can retrieve recent messages to provide context to the AI.
30
30
31
-
```redis:[run_confirmation=true] Read conversation history
32
-
// Get last 5 messages (most recent first)
31
+
```redis:[run_confirmation=no] Read conversation history
32
+
// Get the 5 most recent messages
33
33
LRANGE user:alice:history:session_001 0 4
34
34
35
-
// Get all messages in session
35
+
// Get the full session
36
36
LRANGE user:alice:history:session_001 0 -1
37
-
38
-
// Get specific message by index
39
-
LINDEX user:alice:history:session_001 0
40
-
41
-
// Check how many messages in session
42
-
LLEN user:alice:history:session_001
43
37
```
38
+
You may want to limit the size of history to retain only the N most recent items. Use LTRIM:
39
+
```redis:[run_confirmation=yes] Read conversation history
40
+
LTRIM user:alice:history:session_001 0 29 // keep only latest 30 items
41
+
```
42
+
44
43
### Session Expiration
45
-
Without expiration, conversation history would accumulate indefinitely, consuming memory and potentially exposing sensitive information. TTL ensures privacy and efficient memory usage.
44
+
Without expiration, session history will accumulate indefinitely. Expiring keys improves memory usage and ensures privacy.
Rate limiting prevents abuse and ensures fair resource usage. Without it, users could overwhelm the system with requests, degrading performance for everyone.
61
+
Rate limiting prevents abuse and ensures fair usage across users. Redis hashes with field-level TTL via `HSETEX` are ideal for this.
AI assistants become more helpful when they remember user preferences, important facts, and context across sessions. This creates a personalized experience that improves over time.
100
-
Remembering user preferences (meeting times, communication style) enables the AI to provide more relevant and personalized responses without asking the same questions repeatedly.
95
+
### User Memory (Persistent Preferences)
96
+
AI assistants become more helpful when they remember user preferences, schedules, or relevant facts. This persistent memory enables personalization over time.
101
97
102
98
```redis:[run_confirmation=true] Store User Preferences
103
99
// Always secure sensitive data using encryption at rest, access control (Redis ACLs), and comply with data protection laws (e.g., GDPR).
HSET user:alice:knowledge:personal:001 user_id "alice" content "allergic to shellfish and nuts" importance 10 timestamp 1717935000 embedding "\x40\x00\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x60\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x3f\x00\x00\x00"
110
+
HSET user:alice:personal:001 user_id "alice" content "allergic to shellfish and nuts" importance 10 timestamp 1717935000 embedding "\x40\x00\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x60\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x3f\x00\x00\x00"
115
111
116
112
// Store pet information
117
-
HSET user:alice:knowledge:personal:002 user_id "alice" content "has a golden retriever named Max, 3 years old" importance 7 timestamp 1717935000 embedding "\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x40\x20\x00\x00\x3f\x00\x00\x00\x40\x60\x00\x00"
113
+
HSET user:alice:personal:002 user_id "alice" content "has a golden retriever named Max, 3 years old" importance 7 timestamp 1717935000 embedding "\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x40\x20\x00\x00\x3f\x00\x00\x00\x40\x60\x00\x00"
118
114
119
115
// Store family information
120
-
HSET user:alice:knowledge:personal:003 user_id "alice" content "married to Bob, two kids Sarah (8) and Tom (5)" importance 9 timestamp 1717935000 embedding "\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x3f\x00\x00\x00"
116
+
HSET user:alice:personal:003 user_id "alice" content "married to Bob, two kids Sarah (8) and Tom (5)" importance 9 timestamp 1717935000 embedding "\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00\x40\x80\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x3f\x00\x00\x00"
121
117
```
122
118
123
119
### Vector Search: Semantic Memory Recall
124
-
Traditional keyword search misses semantic meaning. When a user asks about "scheduling meetings," vector search can find relevant information about "prefers morning appointments" even though the keywords don't match exactly.
120
+
Semantic search allows AI to retrieve relevant memory even when exact keywords don't match. For example, a query about "meetings" might return facts about "morning appointments."
125
121
126
-
Indexing persistent memory (knowledge base) for semantically meaningful search.
122
+
Indexing persistent memory (User Memory) for semantically meaningful search.
127
123
128
124
```redis:[run_confirmation=true] Create a Vector Index
129
125
// Create index for semantic search
130
-
FT.CREATE idx:knowledge
126
+
FT.CREATE idx:preferences
131
127
ON HASH
132
128
PREFIX 1 user:
133
129
SCHEMA
@@ -137,14 +133,14 @@ FT.CREATE idx:knowledge
137
133
timestamp NUMERIC
138
134
embedding VECTOR HNSW 6
139
135
TYPE FLOAT32
140
-
DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536 dimensions.
136
+
DIM 8 // DIM 8 is only for demonstration purposes. Real embeddings are typically 128–1536 dimensions depending on the model (e.g., sentence-transformers).
141
137
DISTANCE_METRIC COSINE
142
138
```
143
139
144
140
### Search for most relevant memory entries
145
141
146
-
```redis:[run_confirmation=false] Find Top 5 Most Relevant Knowledge Items
147
-
FT.SEARCH idx:knowledge
142
+
```redis:[run_confirmation=false] Find Top 5 Most Relevant User Memory Items
143
+
FT.SEARCH idx:preferences
148
144
"(@user_id:{alice}) => [KNN 5 @embedding $vec AS score]"
Understanding what's stored in memory helps debug issues, optimize performance, and ensure data quality. It's also essential for user privacy compliance.
174
170
```redis:[run_confirmation=false] Monitor user sessions
175
-
// Scan 10,000 keys to find user sessions
176
-
SCAN 0 MATCH user:*:history:* COUNT 10000
171
+
// Get approximate memory usage of session
172
+
MEMORY USAGE user:alice:history:session_001
177
173
178
174
// Get session statistics
179
175
LLEN user:alice:history:session_001
180
176
TTL user:alice:history:session_001
181
177
```
182
178
### Data Cleanup
179
+
Remove all data related to a user (e.g., for GDPR compliance).
0 commit comments