-
Notifications
You must be signed in to change notification settings - Fork 8
Tutorials for Vector Search and HFE #85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ViktarStarastsenka
wants to merge
6
commits into
main
Choose a base branch
from
ViktarStarastsenka
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
df0e45a
Tutorials for Vector Search and HFE
ViktarStarastsenka fbaa0b1
Changing the AI tutorial
ViktarStarastsenka 1da2158
Updating the tutorial for AI chatbot
ViktarStarastsenka 849c951
Updating the AI assistand guide
ViktarStarastsenka ab887f1
Updating tutorial for recommendations
ViktarStarastsenka 455384c
Updating a tutorial for recommendations
ViktarStarastsenka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
Imagine you are building a smart AI assistant that: | ||
- Remembers chats, but only temporarily | ||
- Thinks by meaning, not by matching exact words | ||
- Cleans up after itself, with no manual scripts | ||
|
||
Redis 8 has two powerful capabilities to make this happen: | ||
- Field-level expiration: Let individual chat messages expire on their own | ||
- Vector similarity search: Find past messages based on meaning, not keywords | ||
|
||
Let’s dive in. | ||
|
||
### Short-Term Memory with Field-Level Expiry | ||
Each chat session is stored as a Redis Hash. | ||
Each message is a field in the hash. | ||
Redis 8’s new HSETEX command allows you to assign a TTL to each field, perfect for building ephemeral, session-level memory. | ||
|
||
```redis:[run_confirmation=true] Upload Session Data | ||
// session:42 is the session ID | ||
// msg:<timestamp> ensures uniqueness and traceability | ||
HSETEX session:42 msg:1717935301 120 "Hi Chatbot!" | ||
HSETEX session:42 msg:1717935361 180 "What can you do?" | ||
HSETEX session:42 msg:1717935440 90 "Can you remind me about my tasks?" | ||
HSETEX session:42 msg:1717935720 30 "What's the news today?" | ||
``` | ||
|
||
Each field automatically expires after its TTL (in seconds). | ||
No need for cron jobs or background workers. | ||
What you get: | ||
- Clean memory | ||
- Zero manual cleanup | ||
- Session-scoped retention, just like short-term memory in humans | ||
|
||
|
||
Try it: After a few minutes, run `HGETALL session:42` and see what's left. | ||
|
||
### Vector Search for Semantic Recall | ||
Now, your assistant needs to “recall” semantically related messages, not just match by words. | ||
To do that, you’ll: | ||
- Convert messages to vector embeddings | ||
- Store them in Redis | ||
- Use Vector Search with FT.SEARCH for semantic retrieval | ||
|
||
```redis:[run_confirmation=true] Create a Vector Index | ||
FT.CREATE idx:memory ON HASH PREFIX 1 memory: SCHEMA | ||
message TEXT | ||
embedding VECTOR FLAT // FLAT = exact vector search | ||
6 | ||
TYPE FLOAT32 | ||
DIM 8 // DIM = embedding size, DIM 8 is just for demo purposes. In real use, embeddings are usually 128–1536 dimensions. | ||
DISTANCE_METRIC COSINE // COSINE = measures semantic closeness | ||
``` | ||
|
||
Now, let’s add entries for your chatbots: | ||
|
||
```redis:[run_confirmation=true] Add entries for the chatbot | ||
// Embeddings are stored as binary FLOAT32 vectors - this is a compact format required by Redis Vector Serch indexes | ||
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@" | ||
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@@" | ||
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?" | ||
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@" | ||
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@@" | ||
``` | ||
|
||
Now your messages are vectorized and ready for search. | ||
|
||
### Let Chatbot Think – Semantic Search with Vectors | ||
When a user sends a new message, convert it to an embedding and run a KNN search: | ||
|
||
```redis:[run_confirmation=true] Search For Similar Messages | ||
// Returns the top 3 semantically similar messages, even if no words match directly. | ||
FT.SEARCH idx:memory "*=>[KNN 3 @embedding $vec AS score]" | ||
PARAMS 2 vec "\x00\x00@@\x00\x00\x80@\x00\x00\x00@\x00\x00\x80?\x00\x00@@\x00\x00\x00@" | ||
SORTBY score | ||
DIALECT 2 | ||
``` | ||
|
||
Now your assistant “remembers” things it’s heard before - by meaning. | ||
|
||
### Real-Time Session Cleanup – Redis Handles It | ||
Want to check what's still in memory? | ||
|
||
```redis:[run_confirmation=false] Check Sessions | ||
HGETALL session:42 | ||
``` | ||
|
||
Only the unexpired fields remain. Redis does the cleanup invisibly in the background. | ||
Your assistant has a clean, focused mind at all times. | ||
|
||
### Next Steps | ||
Now that your assistant has memory and meaning, you can: | ||
- Tie session messages to store embeddings for per-session recall | ||
- Use RAG (Retrieval-Augmented Generation) by combining Redis Vector Search with LLMs | ||
- Add per-user memory: prefix session keys with a user ID (user:42:session:...) | ||
- Introduce a fallback to persistent storage for long-term memory using Redis Flex |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
returns 0 for now since FT.CREATE does not index documents for some reason.
Looking into that.