Skip to content

Commit 1da2158

Browse files
Updating the tutorial for AI chatbot
1 parent fbaa0b1 commit 1da2158

File tree

1 file changed

+32
-37
lines changed

1 file changed

+32
-37
lines changed

src/uc/ai_assistant.md

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,28 @@ Store chat sessions as Redis hashes. Each message is a field with its own TTL.
1515
```redis:[run_confirmation=true] Upload Session Data
1616
// User-scoped sessions for isolation
1717
// Pattern: user:{user_id}:session:{session_id}
18-
HSETEX user:alice:session:morning msg:1717935301 3600 "Good morning! What's my schedule today?"
19-
HSETEX user:alice:session:morning msg:1717935361 1800 "Remind me about the team meeting at 2 PM"
20-
HSETEX user:alice:session:morning msg:1717935420 900 "What's the weather forecast?"
21-
HSETEX user:alice:session:morning msg:1717935480 300 "Thanks, that's all for now"
18+
HSETEX user:alice:session:morning EX 3600 FIELDS 1 msg:1717935301 "Good morning! What's my schedule today?"
19+
HSETEX user:alice:session:morning EX 1800 FIELDS 1 msg:1717935361 "Remind me about the team meeting at 2 PM"
20+
HSETEX user:alice:session:morning EX 900 FIELDS 1 msg:1717935420 "What's the weather forecast?"
21+
HSETEX user:alice:session:morning EX 300 FIELDS 1 msg:1717935480 "Thanks, that's all for now"
2222
2323
// Different user, same session pattern
24-
HSETEX user:bob:session:work msg:1717935500 7200 "I need to prepare for the client presentation"
25-
HSETEX user:bob:session:work msg:1717935560 3600 "What are the key points I should cover?"
26-
24+
HSETEX user:bob:session:work EX 7200 FIELDS 1 msg:1717935500 "I need to prepare for the client presentation"
25+
HSETEX user:bob:session:work EX 3600 FIELDS 1 msg:1717935560 "What are the key points I should cover?"
2726
```
2827

2928
### Memory Tiers for Different Lifetimes
3029
Control how long messages last depending on their importance.
3130

3231
```redis:[run_confirmation=true] Memory Tiers Strategy
3332
// Short-term (5 minutes) - Immediate context
34-
HSETEX user:alice:session:current msg:1717935301 300 "Current conversation context"
33+
HSETEX user:alice:session:current EX 300 FIELDS 1 msg:1717935301 "Current conversation context"
3534
3635
// Medium-term (30 minutes) - Session memory
37-
HSETEX user:alice:session:current msg:1717935302 1800 "Important session details"
36+
HSETEX user:alice:session:current EX 1800 FIELDS 1 msg:1717935302 "Important session details"
3837
3938
// Long-term (2 hours) - Cross-session context
40-
HSETEX user:alice:session:current msg:1717935303 7200 "Key user preferences and facts"
39+
HSETEX user:alice:session:current EX 7200 FIELDS 1 msg:1717935303 "Key user preferences and facts"
4140
```
4241

4342
### Check Current Session Memory
@@ -52,43 +51,40 @@ HGETALL user:alice:session:morning
5251
Create an index to store messages as vectors for semantic search.
5352

5453
```redis:[run_confirmation=true] Create a Vector Index
55-
FT.CREATE idx:ai_memory
56-
ON HASH
57-
PREFIX 1 memory:
58-
SCHEMA
54+
FT.CREATE idx:ai_memory
55+
ON HASH
56+
PREFIX 1 memory:
57+
SCHEMA
5958
user_id TAG SORTABLE
60-
session_id TAG SORTABLE
61-
message TEXT WEIGHT 2.0 PHONETIC dm:en
62-
context TEXT WEIGHT 1.0
59+
session_id TAG SORTABLE
60+
message TEXT
61+
context TEXT
6362
timestamp NUMERIC SORTABLE
64-
embedding VECTOR HNSW 6
65-
TYPE FLOAT32
66-
DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536 dimensions.
63+
embedding VECTOR HNSW 6
64+
TYPE FLOAT32
65+
DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536
6766
DISTANCE_METRIC COSINE // COSINE = measures semantic closeness
68-
INITIAL_CAP 10000
69-
M 16
70-
EF_CONSTRUCTION 200
7167
```
7268

7369
Add sample vectorized messages (embedding dims are demo-sized):
7470

7571
```redis:[run_confirmation=true] Add entries for the chatbot
76-
HSET memory:alice:1 user_id "alice" session_id "morning" message "I have a dentist appointment at 3 PM today" context "healthcare scheduling appointment" timestamp 1717935301 embedding "\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@"
77-
HSET memory:alice:2 user_id "alice" session_id "morning" message "Remind me to water the plants in my office" context "task reminder plants office" timestamp 1717935361 embedding "\x00\x00\x80@\x00\x00\x80@\x00\x00\x80@\x00\x00\x80?\x00\x00\x80?\x00\x00@@"
78-
HSET memory:alice:3 user_id "alice" session_id "work" message "Schedule a meeting with the engineering team" context "work scheduling meeting team" timestamp 1717935420 embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x00@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80?"
79-
HSET memory:bob:1 user_id "bob" session_id "work" message "I need to review the quarterly sales report" context "business analysis quarterly report" timestamp 1717935480 embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@"
72+
HSET memory:alice:1 user_id "alice" session_id "morning" message "I have a dentist appointment at 3 PM today" context "healthcare scheduling appointment" timestamp 1717935301 embedding "\x3f\x00\x00\x00\x40\x00\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00"
73+
HSET memory:alice:2 user_id "alice" session_id "morning" message "Remind me to water the plants in my office" context "task reminder plants office" timestamp 1717935361 embedding "\x40\x00\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00\x3f\x00\x00\x00\x3f\x00\x00\x00\x40\x00\x00\x00"
74+
HSET memory:alice:3 user_id "alice" session_id "work" message "Schedule a meeting with the engineering team" context "work scheduling meeting team" timestamp 1717935420 embedding "\x40\x40\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00\x3f\x00\x00\x00\x3f\x00\x00\x00"
75+
HSET memory:bob:1 user_id "bob" session_id "work" message "I need to review the quarterly sales report" context "business analysis quarterly report" timestamp 1717935480 embedding "\x40\x40\x00\x00\x40\x00\x00\x00\x3f\x00\x00\x00\x40\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00"
8076
```
8177

8278
### Let Chatbot Think – Semantic Search with Vectors
8379
When a user says something new, find all related past conversations across your entire system based on semantic meaning.
8480

8581
```redis:[run_confirmation=false] Find Top 5 Related Messages By Meaning
86-
FT.SEARCH idx:ai_memory
87-
"*=>[KNN 5 @embedding $query_vec AS vector_score]"
88-
PARAMS 2 query_vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@"
89-
RETURN 6 user_id message context vector_score timestamp
90-
SORTBY vector_score ASC
91-
DIALECT 2
82+
FT.SEARCH idx:ai_memory
83+
"*=>[KNN 5 @embedding $vec AS score]"
84+
PARAMS 2 vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@"
85+
RETURN 4 user_id message context timestamp
86+
SORTBY score ASC
87+
DIALECT 2
9288
```
9389

9490
Now your assistant “remembers” things it’s heard before - by meaning.
@@ -100,7 +96,7 @@ Your AI should only recall memories from the specific user it's talking to, not
10096
FT.SEARCH idx:ai_memory
10197
"(@user_id:{alice}) => [KNN 3 @embedding $query_vec AS vector_score]"
10298
PARAMS 2 query_vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@"
103-
RETURN 6 user_id message context vector_score session_id
99+
RETURN 5 user_id message context vector_score session_id
104100
SORTBY vector_score ASC
105101
DIALECT 2
106102
```
@@ -124,8 +120,8 @@ When users refer to something from "earlier in our conversation," search only wi
124120
FT.SEARCH idx:ai_memory
125121
"(@user_id:{alice} @session_id:{morning}) => [KNN 10 @embedding $query_vec AS vector_score]"
126122
PARAMS 2 query_vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@"
127-
RETURN 6 message context vector_score timestamp
128-
SORTBY vector_score ASC
123+
RETURN 4 message context vector_score timestamp
124+
SORTBY vector_score
129125
DIALECT 2
130126
```
131127

@@ -138,7 +134,6 @@ HGETALL memory:alice:1
138134
HGETALL memory:alice:2
139135
HGETALL memory:alice:3
140136
HGETALL memory:bob:1
141-
HGETALL memory:bob:2
142137
```
143138

144139
### Next Steps

0 commit comments

Comments
 (0)