Skip to content

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
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
df0e45a
Tutorials for Vector Search and HFE
ViktarStarastsenka Jun 10, 2025
fbaa0b1
Changing the AI tutorial
ViktarStarastsenka Jun 10, 2025
1da2158
Updating the tutorial for AI chatbot
ViktarStarastsenka Jun 10, 2025
849c951
Updating the AI assistand guide
ViktarStarastsenka Jun 11, 2025
ab887f1
Updating tutorial for recommendations
ViktarStarastsenka Jun 11, 2025
455384c
Updating a tutorial for recommendations
ViktarStarastsenka Jun 11, 2025
ff4a673
Update ai_assistant.md
ViktarStarastsenka Jul 1, 2025
cecbf6b
Update ai_assistant.md
ViktarStarastsenka Jul 4, 2025
780ea22
Update ai_assistant.md
ViktarStarastsenka Jul 4, 2025
b093e1c
Update ai_assistant.md
ViktarStarastsenka Jul 7, 2025
6802a71
Update ai_assistant.md
ViktarStarastsenka Jul 7, 2025
1531361
Update src/manifest.json
ViktarStarastsenka Jul 22, 2025
df83936
Update vectors-adv-hash.md
ViktarStarastsenka Jul 24, 2025
1b2d10a
Delete src/vss/vectors-basic.md
ViktarStarastsenka Jul 24, 2025
73f2a4b
Rename vectors-adv-hash.md to vector_bikes.md
ViktarStarastsenka Jul 24, 2025
93c3159
Update manifest.json
ViktarStarastsenka Jul 24, 2025
936a5b7
Rename vector_bikes.md to e-commerce-discovery.md
ViktarStarastsenka Jul 24, 2025
8366183
Create personalized_recommendations.md
ViktarStarastsenka Jul 24, 2025
2662848
Create ai_assistant.md
ViktarStarastsenka Jul 24, 2025
9e63170
Update manifest.json
ViktarStarastsenka Jul 24, 2025
57635ee
Update hashes.md
ViktarStarastsenka Jul 24, 2025
7aad062
Mentioning Redis 8 instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
6e50a31
Mentioning Redis 8 instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
c920ba0
Mentioning JSON data structure instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
0b1ee92
Mentioning Redis 8 instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
16f8cc0
Update more-adv-jsonpath.md
ViktarStarastsenka Jul 24, 2025
7a55741
Mentioning RQE and Redis 8 instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
884b9ad
Update intro.md
ViktarStarastsenka Jul 24, 2025
0a9e338
Update intro.md
ViktarStarastsenka Jul 24, 2025
87294f5
Mentioning Redis 8 instead of Redis Stack
ViktarStarastsenka Jul 24, 2025
9e391cc
Update manifest.json
ViktarStarastsenka Jul 24, 2025
9fee519
Update intro.md
ViktarStarastsenka Jul 29, 2025
2611077
Update intro.md
ViktarStarastsenka Jul 29, 2025
40b59fa
Update intro.md
ViktarStarastsenka Jul 29, 2025
3e46366
Update intro.md
ViktarStarastsenka Jul 29, 2025
2822d9a
Update ai_assistant.md
ViktarStarastsenka Jul 29, 2025
9d7c2fe
Apply suggestions from code review
ViktarStarastsenka Jul 29, 2025
81b9d5c
Update ai_assistant.md
ViktarStarastsenka Jul 29, 2025
432a241
Delete src/uc/ai_assistant.md
ViktarStarastsenka Jul 29, 2025
5c4f1af
Delete src/vss/ai_assistant.md
ViktarStarastsenka Jul 29, 2025
fbf3e89
Delete src/uc/personalized_recommendations.md
ViktarStarastsenka Jul 29, 2025
22934e3
Update personalized_recommendations.md
ViktarStarastsenka Jul 29, 2025
8d4108d
Create ai_assistant.md
ViktarStarastsenka Jul 29, 2025
f0d0b8b
Update personalized_recommendations.md
ViktarStarastsenka Jul 29, 2025
e2d6860
Update manifest.json
ViktarStarastsenka Jul 31, 2025
c90873c
Update src/vss/personalized_recommendations.md
ViktarStarastsenka Aug 11, 2025
7dbf88b
Update manifest.json
ViktarStarastsenka Aug 11, 2025
41bcb8d
Update e-commerce-discovery.md
ViktarStarastsenka Aug 11, 2025
e1b1ea2
Update ai_assistant.md
ViktarStarastsenka Aug 11, 2025
cbc4805
Update src/sq/intro.md
ViktarStarastsenka Aug 11, 2025
ef6a321
Update intro.md
ViktarStarastsenka Aug 11, 2025
48e2e5b
Update e-commerce-discovery.md
ViktarStarastsenka Aug 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@
"initialIsOpen": true,
"path": "/uc/rag.md"
}
},
{
"type": "internal-link",
"id": "ai_assistant",
"label": "Creating an AI Assistant",
"summary": "An assistant that temporarily retains conversations and understands by their meaning.",
"args": {
"initialIsOpen": true,
"path": "/uc/ai_assistant.md"
}
},
{
"type": "internal-link",
"id": "redis_use_cases_rag",
"label": "Building personalized recommendations",
"summary": "Not just keyword matching, but semantic understanding.",
"args": {
"initialIsOpen": true,
"path": "/uc/personalized_recommendations.md"
}
}
]
},
Expand Down
94 changes: 94 additions & 0 deletions src/uc/ai_assistant.md
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
Loading