MinAI - Về trang chủ
Lý thuyết
9/1330 phút
Đang tải...

Context Management

Quản lý context window, conversation history, và memory cho RAG chatbots

🧠 Context Management

0

🎯 Mục tiêu bài học

TB5 min

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.

1

⚠️ Context Challenges

TB5 min
Diagram
Đang vẽ diagram...

Checkpoint

3 challenges chính trong context management là gì? Giải pháp cho mỗi challenge?

2

📏 Context Window Management

TB5 min
JavaScript
1// LLM context windows (approximate):
2// GPT-4o-mini: 128K tokens
3// GPT-4o: 128K tokens
4// Claude 3.5 Sonnet: 200K tokens
5
6// Rule of thumb: keep context under 50% of window
7// Reserve rest for system prompt + response
8
9function estimateTokens(text) {
10 // Rough: 1 token per 4 chars for English
11 // 1 token per 2-3 chars for Vietnamese
12 return Math.ceil(text.length / 3);
13}
14
15function 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?

3

🧠 Conversation Memory trong n8n

TB5 min

Buffer Memory

Diagram
Đang vẽ diagram...
JavaScript
1// n8n Window Buffer Memory node
2// Configuration:
3// - Session Key: {{ $json.sessionId }}
4// - Context Window Length: 10 (messages)
5// - Memory Type: Window Buffer
6
7// This automatically:
8// 1. Stores conversation messages
9// 2. Retrieves last N messages
10// 3. Includes in AI context

Redis Memory (Persistent)

JavaScript
1// For persistent memory across sessions
2// n8n Redis Chat Memory node
3
4// Configuration:
5// - Session Key: {{ $json.userId }}_{{ $json.conversationId }}
6// - Session TTL: 3600 (1 hour)
7// - Context Window Length: 20
8
9// Redis automatically manages:
10// - Session isolation per user
11// - TTL-based cleanup
12// - Resumable conversations

Checkpoint

So sánh Buffer Memory và Redis Memory trong n8n. Khi nào dùng cái nào?

4

🔄 Contextual Query Rewriting

TB5 min
JavaScript
1// Problem: "What about the pricing?"
2// Without history, this is ambiguous
3// With history: user was asking about Product X
4
5// OpenAI node: Rewrite query with context
6const rewritePrompt = `
7Given the conversation history, rewrite the latest query
8to be self-contained and specific.
9
10History:
11${$json.history.map(m => `${m.role}: ${m.content}`).join('\n')}
12
13Latest query: "${$json.query}"
14
15Rewritten query (self-contained):`;

Checkpoint

Tại sao cần rewrite query với conversation history? Cho ví dụ cụ thể.

5

🔗 Hybrid Context Strategy

TB5 min
Diagram
Đang vẽ diagram...
JavaScript
1// Code node: Combine retrieval + history context
2const retrievedChunks = $json.chunks;
3const conversationHistory = $json.history.slice(-5);
4
5const systemPrompt = `You are a helpful Q&A assistant for our knowledge base.
6
7Conversation history for context:
8${conversationHistory.map(m => `${m.role}: ${m.content}`).join('\n')}
9
10Retrieved information:
11${retrievedChunks.map((c, i) => `[${i + 1}] ${c.content}`).join('\n\n')}
12
13Instructions:
14- Use retrieved information to answer
15- Consider conversation context
16- If follow-up question, connect to previous answers
17- Cite sources: [Source N]`;
18
19return { json: { systemPrompt } };

Checkpoint

Hybrid context combine những gì? Mô tả flow từ Query đến Generate Answer.

6

📦 Context Compression

TB5 min
JavaScript
1// When context is too long, compress it
2const compressPrompt = `
3Compress the following text while preserving all key information
4relevant to answering: "${$json.query}"
5
6Text to compress:
7${$json.longContext}
8
9Compressed version (keep essential facts, remove filler):`;
10
11// 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?

7

🔧 Session Management

TB5 min
JavaScript
1// Code node: Manage chat sessions
2const sessionStore = {
3 // Create new session
4 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 expire
15 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};
Context Best Practices
  1. Limit history: Last 5-10 messages, không cần full history
  2. Rewrite queries: Giúp ambiguous queries trở nên rõ ràng
  3. Compress when needed: Context quá dài thì nên compress
  4. Session TTL: Set expiry cho inactive sessions
  5. 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?

8

📝 Bài tập thực hành

TB5 min
Exercises
  1. Setup Window Buffer Memory cho RAG chatbot
  2. Implement contextual query rewriting
  3. Build hybrid context (retrieval + history)
  4. 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.