🧠 Context Management
🎯 Mục tiêu bài học
Sau bài học này, bạn sẽ:
✅ Hiểu các challenges trong context management
✅ Biết cách quản lý context window hiệu quả
✅ Implement conversation memory với Buffer và Redis
✅ Nắm vững contextual query rewriting
✅ Xây dựng hybrid context strategy và session management
Quản lý context hiệu quả giúp RAG chatbot nhớ lịch sử hội thoại và trả lời đúng ngữ cảnh.
⚠️ Context Challenges
Checkpoint
3 challenges chính trong context management là gì? Giải pháp cho mỗi challenge?
📏 Context Window Management
1// LLM context windows (approximate):2// GPT-4o-mini: 128K tokens3// GPT-4o: 128K tokens4// Claude 3.5 Sonnet: 200K tokens56// Rule of thumb: keep context under 50% of window7// Reserve rest for system prompt + response89function estimateTokens(text) {10 // Rough: 1 token per 4 chars for English11 // 1 token per 2-3 chars for Vietnamese12 return Math.ceil(text.length / 3);13}1415function trimContext(chunks, maxTokens = 3000) {16 let totalTokens = 0;17 const selected = [];18 19 for (const chunk of chunks) {20 const tokens = estimateTokens(chunk.content);21 if (totalTokens + tokens > maxTokens) break;22 selected.push(chunk);23 totalTokens += tokens;24 }25 26 return { chunks: selected, totalTokens };27}Checkpoint
Context window của GPT-4o-mini là bao nhiêu tokens? Rule of thumb khi sử dụng context?
🧠 Conversation Memory trong n8n
Buffer Memory
1// n8n Window Buffer Memory node2// Configuration:3// - Session Key: {{ $json.sessionId }}4// - Context Window Length: 10 (messages)5// - Memory Type: Window Buffer67// This automatically:8// 1. Stores conversation messages9// 2. Retrieves last N messages10// 3. Includes in AI contextRedis Memory (Persistent)
1// For persistent memory across sessions2// n8n Redis Chat Memory node34// Configuration:5// - Session Key: {{ $json.userId }}_{{ $json.conversationId }}6// - Session TTL: 3600 (1 hour)7// - Context Window Length: 2089// Redis automatically manages:10// - Session isolation per user11// - TTL-based cleanup12// - Resumable conversationsCheckpoint
So sánh Buffer Memory và Redis Memory trong n8n. Khi nào dùng cái nào?
🔄 Contextual Query Rewriting
1// Problem: "What about the pricing?"2// Without history, this is ambiguous3// With history: user was asking about Product X45// OpenAI node: Rewrite query with context6const rewritePrompt = `7Given the conversation history, rewrite the latest query 8to be self-contained and specific.910History:11${$json.history.map(m => `${m.role}: ${m.content}`).join('\n')}1213Latest query: "${$json.query}"1415Rewritten query (self-contained):`;Checkpoint
Tại sao cần rewrite query với conversation history? Cho ví dụ cụ thể.
🔗 Hybrid Context Strategy
1// Code node: Combine retrieval + history context2const retrievedChunks = $json.chunks;3const conversationHistory = $json.history.slice(-5);45const systemPrompt = `You are a helpful Q&A assistant for our knowledge base.67Conversation history for context:8${conversationHistory.map(m => `${m.role}: ${m.content}`).join('\n')}910Retrieved information:11${retrievedChunks.map((c, i) => `[${i + 1}] ${c.content}`).join('\n\n')}1213Instructions:14- Use retrieved information to answer15- Consider conversation context16- If follow-up question, connect to previous answers17- Cite sources: [Source N]`;1819return { json: { systemPrompt } };Checkpoint
Hybrid context combine những gì? Mô tả flow từ Query đến Generate Answer.
📦 Context Compression
1// When context is too long, compress it2const compressPrompt = `3Compress the following text while preserving all key information 4relevant to answering: "${$json.query}"56Text to compress:7${$json.longContext}89Compressed version (keep essential facts, remove filler):`;1011// This can reduce context by 50-70%Checkpoint
Khi nào cần compress context? Compression có thể giảm context bao nhiêu phần trăm?
🔧 Session Management
1// Code node: Manage chat sessions2const sessionStore = {3 // Create new session4 createSession(userId) {5 return {6 sessionId: `${userId}_${Date.now()}`,7 userId,8 startedAt: new Date().toISOString(),9 messageCount: 0,10 topics: []11 };12 },13 14 // Check if session should expire15 shouldExpire(session, maxIdleMinutes = 30) {16 const lastActivity = new Date(session.lastActivity);17 const now = new Date();18 const idle = (now - lastActivity) / 60000;19 return idle > maxIdleMinutes;20 }21};- Limit history: Last 5-10 messages, không cần full history
- Rewrite queries: Giúp ambiguous queries trở nên rõ ràng
- Compress when needed: Context quá dài thì nên compress
- Session TTL: Set expiry cho inactive sessions
- Separate contexts: Retrieval context + History context riêng biệt
Checkpoint
Session management cần track những gì? Khi nào nên expire session?
📝 Bài tập thực hành
- Setup Window Buffer Memory cho RAG chatbot
- Implement contextual query rewriting
- Build hybrid context (retrieval + history)
- Test multi-turn conversation quality
Checkpoint
Liệt kê 4 exercises. Tại sao multi-turn conversation testing quan trọng?
🚀 Bài tiếp theo
Response Generation → — Tạo responses chất lượng cao với citations và formatting.
