🔎 Query Pipeline
🎯 Mục tiêu bài học
Sau bài học này, bạn sẽ:
✅ Hiểu kiến trúc query pipeline hoàn chỉnh
✅ Biết cách pre-process và enhance queries
✅ Nắm vững retrieval, re-ranking, và context building
✅ Implement answer generation với proper prompting
✅ Xử lý edge cases: no results, low confidence
Query pipeline là nơi user question được xử lý, tìm context, và tạo answer. Bài này cover full pipeline từ query đến response.
🏗️ Pipeline Architecture
Checkpoint
Query pipeline gồm bao nhiêu bước chính? Liệt kê từ Query đến Final Answer.
🔧 Step 1: Query Pre-processing
1// Code node: Clean and enhance query2function preprocessQuery(query) {3 // Clean4 let clean = query.trim();5 6 // Expand abbreviations7 const expansions = {8 "API": "Application Programming Interface",9 "RAG": "Retrieval Augmented Generation",10 "LLM": "Large Language Model"11 };12 13 // Add context for better retrieval14 return {15 originalQuery: query,16 cleanQuery: clean,17 searchQuery: clean // Can add synonyms or rephrase18 };19}2021return { json: preprocessQuery($json.query) };Query Enhancement with AI
1// OpenAI node: Enhance query for better retrieval2const enhancePrompt = `3Rephrase this query to improve search results. 4Generate 3 alternative phrasings that capture the same intent.56Original query: "${$json.query}"78Return JSON:9{10 "queries": ["original", "rephrasing1", "rephrasing2"]11}`;Checkpoint
Query pre-processing bao gồm những gì? Tại sao cần query enhancement với AI?
🔎 Step 2: Retrieve Relevant Chunks
1// Vector Store Search node2// Top K: 10 (retrieve more, filter later)3// Score Threshold: 0.645// With multi-query: search with each rephrased query6// then deduplicate results1// Code node: Deduplicate results from multi-query2const allResults = $input.all();3const seen = new Set();4const unique = [];56for (const result of allResults) {7 const content = result.json.document.pageContent;8 const hash = content.substring(0, 100); // Simple dedup9 10 if (!seen.has(hash)) {11 seen.add(hash);12 unique.push(result);13 }14}1516// Sort by score descending17unique.sort((a, b) => (b.json.score || 0) - (a.json.score || 0));1819// Keep top 520return unique.slice(0, 5);Checkpoint
Tại sao cần deduplicate khi dùng multi-query? Top K ban đầu nên set bao nhiêu?
📊 Step 3: Re-ranking
1// OpenAI node: Re-rank results by relevance2const rerankPrompt = `3Given the question and search results, rank the results by relevance.45Question: "${$json.query}"67Results:8${$json.results.map((r, i) => `[${i}] ${r.content}`).join('\n\n')}910Return JSON array of indices ordered by relevance: [most_relevant_index, ...]11Only include indices of chunks that actually help answer the question.`;Checkpoint
Re-ranking là gì? Tại sao vector search results cần được re-rank?
🧠 Step 4: Build Context
1// Code node: Build optimized context2const query = $json.query;3const topResults = $json.rankedResults.slice(0, 5);45const context = topResults.map((r, i) => {6 const source = r.metadata.source || "unknown";7 return `--- Source ${i + 1}: ${source} ---8${r.content}`;9}).join('\n\n');1011const systemPrompt = `You are a helpful assistant that answers questions 12based on the provided context. 1314Rules:151. Only use information from the context below162. If the answer is not in the context, say "I could not find this information in the knowledge base"173. Cite sources when possible: [Source 1], [Source 2]184. Be concise but complete195. Use Vietnamese for the response`;2021const userPrompt = `Context:22${context}2324Question: ${query}2526Answer:`;2728return { json: { systemPrompt, userPrompt, sourceCount: topResults.length } };Checkpoint
Context prompt cần bao gồm những rules nào? Tại sao cần cite sources?
⚡ Step 5: Generate Answer
1// OpenAI Chat node2// System Message: {{ $json.systemPrompt }}3// User Message: {{ $json.userPrompt }}4// Model: gpt-4o-mini5// Temperature: 0.3 (lower for factual answers)6// Max Tokens: 500Checkpoint
Temperature nên set bao nhiêu cho factual Q&A? Tại sao?
📋 Step 6: Post-processing
1// Code node: Format response2const answer = $json.message.content;3const sources = $json.sources;45const response = {6 answer: answer,7 sources: sources.map(s => ({8 title: s.metadata.source,9 relevance: s.score10 })),11 confidence: sources[0]?.score || 0,12 timestamp: new Date().toISOString()13};1415return { json: response };Checkpoint
Response cần bao gồm những fields nào sau post-processing?
🤖 Full Pipeline in n8n
Checkpoint
Mô tả 7 nodes trong full pipeline workflow từ Webhook đến Respond to Webhook.
⚠️ Handling No Results
1// Code node: Check if results are relevant2const results = $json.searchResults;3const threshold = 0.7;45const relevant = results.filter(r => r.score >= threshold);67if (relevant.length === 0) {8 return {9 json: {10 answer: "I could not find relevant information for your question.",11 fallback: true,12 suggestion: "Try rephrasing or ask a more specific question."13 }14 };15}1617return { json: { results: relevant, fallback: false } };- Temperature low: Dùng 0.1-0.3 cho factual Q and A
- Top K tuning: Bắt đầu với 5, tăng nếu missing context
- Score threshold: 0.7 là good starting point
- Cite sources: Luôn reference sources trong answer
Checkpoint
Khi không tìm thấy results relevant, system nên respond như thế nào?
📝 Bài tập thực hành
- Build complete query pipeline với 6 steps
- Implement multi-query enhancement
- Add no-result handling với fallback messages
- Test với 10 queries, measure answer quality
Checkpoint
Liệt kê 4 exercises cần hoàn thành. Exercise nào challenging nhất?
🚀 Bài tiếp theo
Context Management → — Quản lý context window, conversation history, và memory.
