Skip to content

Commit ff4a673

Browse files
Update ai_assistant.md
1 parent 455384c commit ff4a673

File tree

1 file changed

+59
-51
lines changed

1 file changed

+59
-51
lines changed

src/uc/ai_assistant.md

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,55 @@ What you get:
55
- **Semantic Search**: Recall relevant info by meaning using vector search
66
- **Zero Maintenance**: Auto-expiring short-term memory without a need to track timestamps manually
77
- **Multi-User**: Isolated memory per user
8-
- **Learning**: Assistant can "understand" each user better the more it's used
8+
- **Learning**: Assistant learns user preferences and context over time.
99

1010
**Note**: Requires [Redis 8](https://hub.docker.com/_/redis/tags) for `HSETEX`, which adds per-field TTL for hashes, which is ideal for managing short-term memory with precision.
1111

1212
### Architecture Overview
1313
| Layer | Description |
1414
| ---------- | ---------- |
1515
| `Working Memory`| `Short-term chat context (ephemeral)` |
16-
| `Knowledge Base` | `Persistent facts, user preferences` |
16+
| `Knowledge Base` | `Long-term memory: persistent facts and preferences` |
1717
| `Vector Search` | `Unified semantic recall across both layers` |
1818

1919
### Working Memory (Ephemeral)
20-
Stores recent user messages with TTL based on importance. Automatically expires to prevent bloat.
21-
This uses `HSETEX`, a Redis 8 command that adds field-level expiration to hashes. It allows storing all temporary messages in a single key while managing TTLs per message, simplifying short-term memory management without needing multiple keys.
20+
Stores recent user and assistant messages with per-message TTL. Uses:
21+
- `HSETEX` to add field-level expiration to hashes to store all temporary messages in a single key while managing TTLs per message, simplifying short-term memory management without needing multiple keys.
22+
- `ZADD` for message ordering (based on timestamp)
2223

23-
```redis:[run_confirmation=true] Recent Conversations with TTL Based on Importance.
24-
// Quick exchanges (5 min)
24+
```redis:[run_confirmation=true] Recent conversations with TTL based on importance
25+
// Step 1: Add message with TTL (5 mins)
2526
HSETEX user:alice:session:001 EX 300 FIELDS 1 msg:001 "What's the weather?"
26-
// Session context (30 min)
27+
// Step 2: Track order with timestamp (epoch seconds)
28+
ZADD user:alice:session:001:zorder 1717935001 msg:001
29+
// Another message (30 min TTL)
2730
HSETEX user:alice:session:001 EX 1800 FIELDS 1 msg:002 "I need a dentist appointment"
28-
// Important decisions (2 hours)
29-
HSETEX user:alice:session:001 EX 7200 FIELDS 1 msg:003 "Book it for Tuesday 2 PM"
31+
ZADD user:alice:session:001:zorder 1717935030 msg:002
32+
// Assistant reply (2 hour TTL)
33+
HSETEX user:alice:session:001 EX 7200 FIELDS 1 msg:003 "Booked for Tuesday 2 PM"
34+
ZADD user:alice:session:001:zorder 1717935090 msg:003
35+
```
36+
37+
```redis:[run_confirmation=true] Reading the session in order
38+
ZRANGEBYSCORE user:alice:session:001:zorder -inf +inf
39+
// Then for each msg ID:
40+
HGET user:alice:session:001 msg:001
41+
HGET user:alice:session:001 msg:002
42+
HGET user:alice:session:001 msg:003
43+
```
44+
45+
```redis:[run_confirmation=true] ZSET cleanup (recommended in background):
46+
// For each msg:XXX in the ZSET, check if it still exists
47+
HEXISTS user:alice:session:001 msg:003
48+
// If not, clean up:
49+
ZREM user:alice:session:001:zorder msg:003
3050
```
3151

3252
### Knowledge Base (Persistent)
3353
Long-term memory: stores important facts, user preferences, and context across sessions. These never expire.
3454
`embedding` is a binary-encoded `FLOAT32[]` used for vector similarity that can be generated using sentence-transformers or similar libraries. Demo uses 8-dim vectors; production models typically use 128–1536 dimensions.
3555

36-
```redis:[run_confirmation=true] Important User Information That Never Expires.
56+
```redis:[run_confirmation=true] Important user information that never expires
3757
// User preferences - need vector fields for search
3858
HSET user:alice:knowledge:pref:001 user_id "alice" memory_type "knowledge" content "prefers mornings 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"
3959
HSET user:alice:knowledge:pref:002 user_id "alice" memory_type "knowledge" content "likes detailed explanations" 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"
@@ -46,12 +66,12 @@ HSET user:alice:knowledge:work:002 user_id "alice" memory_type "knowledge" conte
4666
```
4767

4868
### Vector Search: Semantic Memory Recall
49-
Unify working + knowledge memory into a vector index for semantically meaningful search.
69+
Indexing persistent memory (knowledge base) for semantically meaningful search.
5070

51-
```redis:[run_confirmation=true] Create a Vector Index
52-
FT.CREATE idx:memory
71+
```redis:[run_confirmation=true] Create a vector index
72+
FT.CREATE idx:kbmemory
5373
ON HASH
54-
PREFIX 2 vmemory: user:
74+
PREFIX 1 user:
5575
SCHEMA
5676
user_id TAG
5777
memory_type TAG
@@ -64,65 +84,53 @@ FT.CREATE idx:memory
6484
DISTANCE_METRIC COSINE // COSINE = measures semantic closeness
6585
```
6686

67-
### Add Indexed Memory
68-
Populate the vector index with memory items from both ephemeral and persistent layers.
69-
70-
```redis:[run_confirmation=true] Add entries for the chatbot
71-
// Working memory (expires from Redis, stays in search until rebuild)
72-
HSET vmemory:alice:001 user_id "alice" memory_type "working" content "Book dentist for Tuesday 2 PM" importance 8 timestamp 1717935310 embedding "\x3f\x80\x00\x00\x40\x00\x00\x00\x40\x40\x00\x00\x40\x80\x00\x00\x3f\x00\x00\x00\x40\x20\x00\x00\x40\x60\x00\x00\x3f\x40\x00\x00"
73-
74-
// Knowledge base (persistent)
75-
HSET vmemory:alice:kb:001 user_id "alice" memory_type "knowledge" content "allergic to shellfish" 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"
76-
```
87+
### Search for most relevant memory entries
88+
Find the top 5 most semantically relevant knowledge memory entries for user "alice" by performing a vector similarity search on the embedding field.
7789

78-
### Full Memory Search
79-
Search across all memories to recall relevant data.
80-
81-
```redis:[run_confirmation=false] Find Top 5 Related Messages By Meaning
82-
FT.SEARCH idx:memory
90+
```redis:[run_confirmation=false] Find top 5 related messages by meaning
91+
FT.SEARCH idx:kbmemory
8392
"(@user_id:{alice}) => [KNN 5 @embedding $vec AS score]"
8493
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"
8594
RETURN 4 content memory_type importance score
8695
SORTBY score ASC
8796
DIALECT 2
8897
```
8998

90-
### Session-Only Search
91-
Limit results to current conversation context (ephemeral memory).
99+
### Search for High-Importance Knowledge Items Only
100+
This query finds the top 3 most relevant knowledge memories for user "alice" that have an importance score above 7.
92101

93-
```redis:[run_confirmation=false] Session-Only Search
94-
FT.SEARCH idx:memory
95-
"(@user_id:{alice} @memory_type:{working}) => [KNN 5 @embedding $vec AS score]"
96-
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"
97-
RETURN 4 content score timestamp memory_type
98-
SORTBY score ASC
99-
DIALECT 2
102+
```redis:[run_confirmation=false] Knowledge-only search
103+
FT.SEARCH idx:kbmemory
104+
"(@user_id:{alice} @memory_type:{knowledge} @importance:[7 +inf]) => [KNN 3 @embedding $vec AS score]"
105+
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"
106+
RETURN 4 content importance score timestamp
107+
SORTBY score ASC
108+
DIALECT 2
100109
```
101110

102-
### Knowledge-Only Search
103-
Focus only on persistent memory: facts, preferences, decisions.
104-
105-
```redis:[run_confirmation=false] Knowledge-Only Search
106-
FT.SEARCH idx:memory
107-
"(@user_id:{alice} @memory_type:{knowledge}) => [KNN 8 @embedding $vec AS score]"
108-
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"
109-
RETURN 4 content importance score timestamp
110-
SORTBY score ASC
111-
DIALECT 2
111+
### Search Working Memory Excluding Messages Older Than a Certain Timestamp
112+
Retrieve the most similar knowledge memories for user "alice" that were created after a given timestamp (e.g., 1717935000), ensuring you only get recent context:
113+
```redis:[run_confirmation=false] Session-only search
114+
FT.SEARCH idx:kbmemory
115+
"(@user_id:{alice} @memory_type:{knowledge} @timestamp:[1717935000 +inf]) => [KNN 5 @embedding $vec AS score]"
116+
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"
117+
RETURN 4 content timestamp memory_type score
118+
SORTBY score ASC
119+
DIALECT 2
112120
```
113121

114122
### Monitoring Memory State
115123
Use these queries to inspect what’s stored in memory.
116124

117-
```redis:[run_confirmation=false] Check Memory State
125+
```redis:[run_confirmation=false] Check memory state
118126
// Check active session memory
119127
HGETALL user:alice:knowledge:pref:001 // Example for one preference item
120128
121129
// View user knowledge
122130
HGETALL user:alice:knowledge:pref:001
123131
124132
// Search user's memories
125-
FT.SEARCH idx:memory
133+
FT.SEARCH idx:kbmemory
126134
"@user_id:{alice}"
127135
RETURN 3 content memory_type importance
128136
```
@@ -147,4 +155,4 @@ Now that your assistant has memory and meaning, you can:
147155
- Combine with RAG Pipelines
148156
- Use sentence-transformers to generate high-dimensional vectors
149157
- Add [Redis Flex](https://redis.io/solutions/flex/?utm_source=redisinsight&utm_medium=app&utm_campaign=tutorials) for fallback persistence
150-
- Use Redis ACLs to isolate users, enforce quotas, and monitor usage
158+
- Use Redis ACLs to isolate users, enforce quotas, and monitor usage

0 commit comments

Comments
 (0)