Skip to content

Commit 6802a71

Browse files
Update ai_assistant.md
1 parent b093e1c commit 6802a71

File tree

1 file changed

+47
-49
lines changed

1 file changed

+47
-49
lines changed

src/uc/ai_assistant.md

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
This tutorial demonstrates how to build an AI assistant's memory system with Redis as its memory core.
22

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.
44

55
### Architecture Overview
66
| Layer | Description | Data type |
77
| ---------- | ---------- | ---------- |
88
| `Session History`| `Recent conversation context` | List |
99
| `Rate Limiting` | `Per-user request throttling` | Hash |
10-
| `Knowledge Base` | `Long-term facts and preferences` | Hash |
10+
| `User Memory` | `Long-term facts and preferences` | Hash |
1111

1212
### 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.
1414

1515
```redis:[run_confirmation=true] Store conversation history
1616
// Add user message to session
@@ -26,23 +26,22 @@ LPUSH user:alice:history:session_001 '{"type": "human", "content": "Should I bri
2626
LPUSH user:alice:history:session_001 '{"type": "ai", "content": "No umbrella needed today!", "timestamp": 1717935004}'
2727
```
2828
### Reading Conversation History
29-
Now we can retrieve conversation history to provide context to the AI.
29+
You can retrieve recent messages to provide context to the AI.
3030

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
3333
LRANGE user:alice:history:session_001 0 4
3434
35-
// Get all messages in session
35+
// Get the full session
3636
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
4337
```
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+
4443
### 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.
4645

4746
```redis:[run_confirmation=true] Session expiration
4847
// Set session to expire in 24 hours
@@ -59,14 +58,11 @@ PERSIST user:alice:history:session_001
5958
```
6059

6160
### Rate Limiting
62-
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.
6362

6463
```redis:[run_confirmation=true] Initialize Rate Limiting
65-
// First request - set counter with 1-minute TTL
64+
// On first request - set counter with 1-minute TTL
6665
HSETEX user:alice:rate_limit EX 60 FIELDS 1 requests_per_minute 1
67-
68-
// Check current request count
69-
HGET user:alice:rate_limit requests_per_minute
7066
```
7167

7268
The `HINCR` command allows you to atomically increment the counter, preventing race conditions in high-concurrency scenarios.
@@ -82,6 +78,7 @@ HGET user:alice:rate_limit requests_per_minute
8278
// Check TTL on the hash
8379
TTL user:alice:rate_limit
8480
```
81+
**Optionally**: if the count exceeds the allowed threshold, deny the operation.
8582

8683
Different time windows serve different purposes - per-minute prevents burst attacks, per-hour prevents sustained abuse, per-day enforces usage quotas.
8784
```redis:[run_confirmation=true] Rate Limiting with Different Time Windows
@@ -95,39 +92,38 @@ HSETEX user:alice:rate_limit EX 86400 FIELDS 1 requests_per_day 1
9592
HGETALL user:alice:rate_limit
9693
```
9794

98-
### Knowledge Base (Persistent Memory)
99-
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.
10197

10298
```redis:[run_confirmation=true] Store User Preferences
10399
// Always secure sensitive data using encryption at rest, access control (Redis ACLs), and comply with data protection laws (e.g., GDPR).
104-
// Store morning preference
105-
HSET user:alice:knowledge:pref:001 user_id "alice" content "prefers morning appointments before 10 AM" importance 9 timestamp 1717935000 embedding "\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x3f\x40\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x00\x00\x00\x40\x80\x00\x00"
100+
// These values are stored with embeddings to support semantic recall later using vector search.
101+
HSET user:alice:pref:001 user_id "alice" content "prefers morning appointments before 10 AM" importance 9 timestamp 1717935000 embedding "\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x3f\x40\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x00\x00\x00\x40\x80\x00\x00"
106102
107103
// Storing communication preference
108-
HSET user:alice:knowledge:pref:002 user_id "alice" content "likes detailed explanations with examples" importance 8 timestamp 1717935000 embedding "\x3f\x40\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00\x3f\x00\x00\x00"
104+
HSET user:alice:pref:002 user_id "alice" content "likes detailed explanations with examples" importance 8 timestamp 1717935000 embedding "\x3f\x40\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00\x3f\x00\x00\x00"
109105
110106
// Store work schedule preference
111-
HSET user:alice:knowledge:pref:003 user_id "alice" content "works remotely on Fridays" importance 7 timestamp 1717935000 embedding "\x40\x80\x00\x00\x3f\x00\x00\x00\x40\x40\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00"
107+
HSET user:alice:pref:003 user_id "alice" content "works remotely on Fridays" importance 7 timestamp 1717935000 embedding "\x40\x80\x00\x00\x3f\x00\x00\x00\x40\x40\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00"
112108
113109
// Store health information
114-
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"
115111
116112
// 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"
118114
119115
// 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"
121117
```
122118

123119
### 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."
125121

126-
Indexing persistent memory (knowledge base) for semantically meaningful search.
122+
Indexing persistent memory (User Memory) for semantically meaningful search.
127123

128124
```redis:[run_confirmation=true] Create a Vector Index
129125
// Create index for semantic search
130-
FT.CREATE idx:knowledge
126+
FT.CREATE idx:preferences
131127
ON HASH
132128
PREFIX 1 user:
133129
SCHEMA
@@ -137,14 +133,14 @@ FT.CREATE idx:knowledge
137133
timestamp NUMERIC
138134
embedding VECTOR HNSW 6
139135
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).
141137
DISTANCE_METRIC COSINE
142138
```
143139

144140
### Search for most relevant memory entries
145141

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
148144
"(@user_id:{alice}) => [KNN 5 @embedding $vec AS score]"
149145
PARAMS 2 vec "\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"
150146
RETURN 4 content importance score timestamp
@@ -153,15 +149,15 @@ FT.SEARCH idx:knowledge
153149
```
154150

155151
```redis:[run_confirmation=false] Search High-Importance Items Only
156-
FT.SEARCH idx:knowledge
152+
FT.SEARCH idx:preferences
157153
"(@user_id:{alice} @importance:[8 +inf]) => [KNN 3 @embedding $vec AS score]"
158154
PARAMS 2 vec "\x40\x40\x00\x00\x3f\x00\x00\x00\x40\x80\x00\x00\x40\x20\x00\x00\x3f\x80\x00\x00\x40\x60\x00\x00\x40\x00\x00\x00\x3f\x40\x00\x00"
159155
RETURN 4 content importance score timestamp
160156
SORTBY score ASC
161157
DIALECT 2
162158
```
163-
```redis:[run_confirmation=false] Search Recent Knowledge Only
164-
FT.SEARCH idx:knowledge
159+
```redis:[run_confirmation=false] Search Recent User Memories
160+
FT.SEARCH idx:preferences
165161
"(@user_id:{alice} @timestamp:[1717935000 +inf]) => [KNN 5 @embedding $vec AS score]"
166162
PARAMS 2 vec "\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"
167163
RETURN 3 content timestamp score
@@ -172,28 +168,30 @@ FT.SEARCH idx:knowledge
172168
### Memory State Monitoring
173169
Understanding what's stored in memory helps debug issues, optimize performance, and ensure data quality. It's also essential for user privacy compliance.
174170
```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
177173
178174
// Get session statistics
179175
LLEN user:alice:history:session_001
180176
TTL user:alice:history:session_001
181177
```
182178
### Data Cleanup
179+
Remove all data related to a user (e.g., for GDPR compliance).
180+
183181
```redis:[run_confirmation=true] Delete user data
184182
// Remove all user data (GDPR compliance)
185183
DEL user:alice:history:session_001
186184
DEL user:alice:history:session_002
187185
DEL user:alice:rate_limit
188-
DEL user:alice:knowledge:pref:001
189-
DEL user:alice:knowledge:pref:002
190-
DEL user:alice:knowledge:pref:003
191-
DEL user:alice:knowledge:personal:001
192-
DEL user:alice:knowledge:personal:002
193-
DEL user:alice:knowledge:personal:003
194-
DEL user:alice:knowledge:work:001
195-
DEL user:alice:knowledge:work:002
196-
DEL user:alice:knowledge:work:003
186+
DEL user:alice:pref:001
187+
DEL user:alice:pref:002
188+
DEL user:alice:pref:003
189+
DEL user:alice:personal:001
190+
DEL user:alice:personal:002
191+
DEL user:alice:personal:003
192+
DEL user:alice:work:001
193+
DEL user:alice:work:002
194+
DEL user:alice:work:003
197195
```
198196

199197
### Next Steps

0 commit comments

Comments
 (0)