The RAG (Retrieval-Augmented Generation) class provides a lightweight knowledge retrieval system that enhances lead evaluations with relevant domain-specific context. It loads markdown files from a knowledge directory and uses keyword-based ranking to retrieve the most relevant documents for a given query.
Directory path containing markdown files (.md) to load into the knowledge corpus. Relative paths are resolved from the current working directory.
from rag import RAG# Use default knowledge directoryrag = RAG()# Custom knowledge directoryrag = RAG(knowledge_dir="docs/context")# Absolute pathrag = RAG(knowledge_dir="/path/to/knowledge")
Automatic Initialization:
The constructor automatically loads all .md files from the knowledge directory into memory. No separate loading step is required.
The knowledge corpus is loaded once at initialization and stored in memory for fast retrieval.
List of relevant document contents (strings) ranked by relevance score. Returns an empty list if no relevant documents are found.
from rag import RAGrag = RAG()# Retrieve relevant contextquery = "We need help automating our sales process and lead qualification."results = rag.retrieve(query, limit=3)print(f"Found {len(results)} relevant documents")for i, content in enumerate(results, 1): print(f"\n--- Document {i} ---") print(content[:200] + "...")
The method returns only the document contents (strings), not the full document objects. This format is optimized for direct injection into LLM prompts.
from rag import RAGfrom evaluator import Evaluatorrag = RAG()evaluator = Evaluator()content = """We are a medical clinic in Yangon offering general practice, diagnostics, and preventive care services."""# Retrieve relevant knowledgecontext = rag.retrieve(content, limit=3)# Enhance evaluation with contextresult = evaluator.evaluate(content, rag_context=context)print(f"Evaluation with {len(context)} context documents")print(f"Fit Score: {result['fit_score']}")print(f"Reasoning: {result['reasoning']}")
from rag import RAGrag = RAG()query = "Healthcare services in Southeast Asia"# Get different amounts of contextfor limit in [1, 3, 5]: results = rag.retrieve(query, limit=limit) total_chars = sum(len(r) for r in results) print(f"Limit {limit}: {len(results)} docs, {total_chars} total chars")
from rag import RAGrag = RAG()# Query that may not match anythingquery = "Quantum computing blockchain AI"results = rag.retrieve(query)if not results: print("No relevant context found. Proceeding without RAG enhancement.") # Continue with evaluation without contextelse: print(f"Found {len(results)} relevant documents") # Use the context
Organize your knowledge directory with focused markdown files:
knowledge/├── sales-automation.md # Sales process and automation best practices├── lead-qualification.md # Lead scoring and qualification guidelines├── industry-healthcare.md # Healthcare industry insights├── industry-education.md # Education industry insights├── geography-myanmar.md # Myanmar market context└── geography-thailand.md # Thailand market context
Example markdown file (sales-automation.md):
# Sales Automation Best PracticesBusinesses that struggle with manual sales processes benefit greatly from automation.Key indicators: manual data entry, inconsistent follow-up, lack of lead tracking.Recommended approach: Start with CRM integration and automated lead scoring.
Keep knowledge files focused on specific topics for better retrieval accuracy. Split large documents into smaller, topic-specific files.
Initialization: O(n) where n = number of markdown files (one-time cost)
Retrieval: O(m × k) where m = corpus size, k = average document length
Memory Usage: All documents loaded into memory (~1-5 MB typical)
Latency: <10ms for typical queries with corpus size <100 documents
For very large knowledge bases (>1000 documents), consider implementing a more sophisticated retrieval system with vector embeddings or full-text search.
from rag import RAGfrom evaluator import Evaluatorevaluator = Evaluator()rag = RAG()# Try to get context, but don't fail if RAG failstry: context = rag.retrieve(content)except Exception as e: print(f"RAG failed: {e}. Continuing without context.") context = None# Evaluate with or without contextresult = evaluator.evaluate(content, rag_context=context)
from rag import RAGrag = RAG()# Only use RAG for specific business typesif business_type in ["healthcare", "education", "legal"]: context = rag.retrieve(content, limit=5)else: context = Noneresult = evaluator.evaluate(content, rag_context=context)
Retrieved 3 items:--- Result 1 ---Sales automation helps businesses streamline their lead qualification process...--- Result 2 ---Healthcare providers in Southeast Asia often need...--- Result 3 ---Education institutions benefit from digital marketing...