Skip to content

Commit df0e45a

Browse files
Tutorials for Vector Search and HFE
1 parent 8e41678 commit df0e45a

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-0
lines changed

src/manifest.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,26 @@
3434
"initialIsOpen": true,
3535
"path": "/uc/rag.md"
3636
}
37+
},
38+
{
39+
"type": "internal-link",
40+
"id": "ai_assistant",
41+
"label": "Creating an AI Assistant",
42+
"summary": "An assistant that temporarily retains conversations and understands by their meaning.",
43+
"args": {
44+
"initialIsOpen": true,
45+
"path": "/uc/ai_assistant.md"
46+
}
47+
},
48+
{
49+
"type": "internal-link",
50+
"id": "redis_use_cases_rag",
51+
"label": "Building personalized recommendations",
52+
"summary": "Not just keyword matching, but semantic understanding.",
53+
"args": {
54+
"initialIsOpen": true,
55+
"path": "/uc/personalized_recommendations.md"
56+
}
3757
}
3858
]
3959
},

src/uc/ai_assistant.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Imagine you are building a smart AI assistant that:
2+
- Remembers chats, but only temporarily
3+
- Thinks by meaning, not by matching exact words
4+
- Cleans up after itself, with no manual scripts
5+
6+
Redis 8 has two powerful capabilities to make this happen:
7+
- Field-level expiration: Let individual chat messages expire on their own
8+
- Vector similarity search: Find past messages based on meaning, not keywords
9+
10+
Let’s dive in.
11+
12+
### Short-Term Memory with Field-Level Expiry
13+
Each chat session is stored as a Redis Hash.
14+
Each message is a field in the hash.
15+
Redis 8’s new HSETEX command allows you to assign a TTL to each field, perfect for building ephemeral, session-level memory.
16+
17+
```redis:[run_confirmation=true] Upload Session Data
18+
// session:42 is the session ID
19+
// msg:<timestamp> ensures uniqueness and traceability
20+
HSETEX session:42 msg:1717935301 120 "Hi Chatbot!"
21+
HSETEX session:42 msg:1717935361 180 "What can you do?"
22+
HSETEX session:42 msg:1717935440 90 "Can you remind me about my tasks?"
23+
HSETEX session:42 msg:1717935720 30 "What's the news today?"
24+
```
25+
26+
Each field automatically expires after its TTL (in seconds).
27+
No need for cron jobs or background workers.
28+
What you get:
29+
- Clean memory
30+
- Zero manual cleanup
31+
- Session-scoped retention, just like short-term memory in humans
32+
33+
34+
Try it: After a few minutes, run `HGETALL session:42` and see what's left.
35+
36+
### Vector Search for Semantic Recall
37+
Now, your assistant needs to “recall” semantically related messages, not just match by words.
38+
To do that, you’ll:
39+
- Convert messages to vector embeddings
40+
- Store them in Redis
41+
- Use Vector Search with FT.SEARCH for semantic retrieval
42+
43+
```redis:[run_confirmation=true] Create a Vector Index
44+
FT.CREATE idx:memory ON HASH PREFIX 1 memory: SCHEMA
45+
message TEXT
46+
embedding VECTOR FLAT // FLAT = exact vector search
47+
6
48+
TYPE FLOAT32
49+
DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536 dimensions.
50+
DISTANCE_METRIC COSINE // COSINE = measures semantic closeness
51+
```
52+
53+
Now, let’s add entries for your chatbots:
54+
55+
```redis:[run_confirmation=true] Add entries for the chatbot
56+
// Embeddings are stored as binary FLOAT32 vectors - this is a compact format required by Redis Vector Serch indexes
57+
HSET memory:1 message "Book a dentist appointment" embedding "\x00\x00\x80?\x00\x00\x00@\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@"
58+
HSET memory:2 message "Remind me to water plants" embedding "\x00\x00\x80@\x00\x00\x80@\x00\x00\x80@\x00\x00\x80?\x00\x00\x80?\x00\x00@@"
59+
HSET memory:3 message "What’s the weather like?" embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x00@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80?"
60+
HSET memory:4 message "Cancel my gym session" embedding "\x00\x00@@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80@\x00\x00\x00@\x00\x00\x00@"
61+
HSET memory:5 message "Start a new shopping list" embedding "\x00\x00\x00@\x00\x00\x00@\x00\x00\x80?\x00\x00\x80@\x00\x00\x80?\x00\x00@@"
62+
```
63+
64+
Now your messages are vectorized and ready for search.
65+
66+
### Let Chatbot Think – Semantic Search with Vectors
67+
When a user sends a new message, convert it to an embedding and run a KNN search:
68+
69+
```redis:[run_confirmation=true] Search For Similar Messages
70+
// Returns the top 3 semantically similar messages, even if no words match directly.
71+
FT.SEARCH idx:memory "*=>[KNN 3 @embedding $vec AS score]"
72+
PARAMS 2 vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@"
73+
SORTBY score
74+
DIALECT 2
75+
```
76+
77+
Now your assistant “remembers” things it’s heard before - by meaning.
78+
79+
### Real-Time Session Cleanup – Redis Handles It
80+
Want to check what's still in memory?
81+
82+
```redis:[run_confirmation=false] Check Sessions
83+
HGETALL session:42
84+
```
85+
86+
Only the unexpired fields remain. Redis does the cleanup invisibly in the background.
87+
Your assistant has a clean, focused mind at all times.
88+
89+
### Next Steps
90+
Now that your assistant has memory and meaning, you can:
91+
- Tie session messages to store embeddings for per-session recall
92+
- Use RAG (Retrieval-Augmented Generation) by combining Redis Vector Search with LLMs
93+
- Add per-user memory: prefix session keys with a user ID (user:42:session:...)
94+
- Introduce a fallback to persistent storage for long-term memory using Redis Flex

0 commit comments

Comments
 (0)