What is RAG?
RAG (Retrieval-Augmented Generation) enhances AI responses by providing relevant domain knowledge as additional context. The Lead Intelligence Engine uses RAG to inject expertise about lead qualification, strategy frameworks, and service definitions into the evaluation process.Think of RAG as giving the AI a “cheat sheet” - instead of relying only on its training data, it has access to your specific business knowledge.
How It Works
Knowledge Base Loading
On initialization, the RAG system loads all
.md files from the knowledge/ directory into memory.Query Processing
When evaluating a business, the extracted website content is used as a search query.
Knowledge Base Structure
Theknowledge/ directory contains domain expertise:
Lead Qualification Criteria
File:Lead_q_criteria.md
Contents:
- Geography: Target markets (Yangon, Mandalay, Bangkok)
- Industry Types: Medical, Education, F&B, Construction, etc.
- Business Size: Team size, revenue proxies, growth stage
- Digital Maturity Framework: 5-layer evaluation (Visibility, Website, Measurement, Operational, Asset Ownership)
- Industry Exclusions: Don’t sell marketing to marketing agencies, don’t sell dev to dev shops
Strategy Framework
File:strategy_efficiency.md
Contents:
- Conversation Angle Prediction: 6 strategic entry points
- Revenue Visibility Gap
- Conversion Infrastructure Weakness
- Operational Bottleneck
- Platform Dependency Risk
- Underutilized Attention
- Competitive Positioning Gap
- Trigger Conditions: What patterns indicate each angle
- Framing Language: Analytical, neutral, structured tone
Strategy Framework
Full documentation of the 6 conversation angles
Retrieval Algorithm
The RAG system uses keyword-based retrieval with scoring:rag.py (lines 21-69)
Scoring Mechanics
Base Score: Word Intersection
Base Score: Word Intersection
Counts how many words appear in both the query and the document.Example:
- Query: “medical clinic bangkok”
- Document contains: “medical”, “bangkok”, “clinic”
- Score: 3
Boost: Long Words
Boost: Long Words
Adds +0.5 for each word longer than 4 characters that appears in the document.Reasoning: Long words are more specific and meaningful (e.g., “marketing” vs “to”).Example:
- Query: “restaurant automation system”
- “automation” (10 chars) found → +0.5
- “restaurant” (10 chars) found → +0.5
- “system” (6 chars) found → +0.5
- Total boost: +1.5
Fallback: High-Value Terms
Fallback: High-Value Terms
If no matches found, searches for these domain-specific keywords:
- Geographic: yangon, mandalay, bangkok
- Industry: medical, education, marketing, digital
Why This Works
This simple algorithm is effective because:
- Business websites repeat key terms (“dental”, “clinic”, “Bangkok”)
- Knowledge base is small (2 files), so computation is fast (<0.1s)
- No training or embedding models needed - zero setup
Context Injection
Retrieved documents are injected into the AI prompt:evaluator.py (lines 69-73)
Prompt Structure
Without RAG:The
--- separator helps the AI distinguish between reference material and the actual content to analyze.Impact on Evaluation
RAG improves AI evaluation accuracy by:1. Industry Exclusion Enforcement
Without RAG, the AI might suggest marketing services to a marketing agency. With RAG, the qualification criteria explicitly states:“Digital Marketing Agencies: Do NOT sell ‘Marketing Services’ or ‘Marketing Packages’. Focus on ‘Technology Services’.“
2. Geography-Aware Scoring
RAG provides context about target markets:“Geography: Yangon, Mandalay, Bangkok”Businesses in these locations get higher fit scores.
3. Strategic Outreach Angles
The strategy framework guides the AI to generate specific outreach angles:“Platform Dependency Risk - Current growth relies on rented Facebook platform.”Instead of generic:
“They need a website.”
4. Digital Maturity Assessment
The 5-layer framework helps the AI identify structural gaps:- Visibility Layer → “Low posting frequency”
- Website Evaluation → “No lead capture form”
- Measurement Signals → “No tracking scripts detected”
Performance Characteristics
Latency
- Loading corpus: ~10ms (on initialization)
- Retrieval per query: ~50ms (keyword matching)
- Total overhead: <0.1s per URL
Token Consumption
RAG adds ~500-1,000 tokens to each prompt:- Lead criteria: ~300 tokens
- Strategy framework: ~400 tokens
- Separator and formatting: ~50 tokens
Customizing the Knowledge Base
You can add your own knowledge files:Limitations
1. No Semantic Understanding
Keyword matching doesn’t understand meaning:- “bank” (financial) vs “river bank” → same score
- “clinic” and “healthcare” → no match (despite similarity)
2. Fixed Limit (Top 3)
Only 3 documents are retrieved, regardless of relevance. Mitigation: Keep knowledge base small and well-organized.3. No Re-Ranking
Once scored, results are not re-evaluated based on AI feedback. Future: Could implement feedback loop where AI rates context usefulness.Comparison: RAG vs No RAG
- With RAG
- Without RAG
Analysis of a Bangkok dental clinic:✅ Recognizes target geography
✅ Applies strategic framework
✅ Uses domain-specific language
✅ Applies strategic framework
✅ Uses domain-specific language
Next Steps
Evaluation Pipeline
Learn how RAG context is used in AI evaluation
Knowledge Base
Explore the lead qualification criteria
RAG API Reference
Programmatic usage of the RAG system
Configuration
Customize your knowledge base