Spaces:
Sleeping
Search Optimizer Developer Guide
Overview
The Atlas search optimizer is a sophisticated system that intelligently determines when web searches are necessary based on conversation context, user intent, and available information. This guide covers the internal architecture, functions, and customization options for developers.
Architecture
Core Components
The search optimization system consists of several interconnected components:
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Chat Endpoint β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ
β Request Flow Router β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββ β
β β Cache-First β β Search-Decision-First β β
β β (No History) β β (Has History) β β
β βββββββββββββββββββ βββββββββββββββββββββββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββ¬ββββββββββββββββββββ
β β
βββββββββββββββββββββββΌββββββββββββββββββββΌββββββββββββββββββββ
β Hybrid Search Engine β
β βββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββ β
β β Rule-Based β β AI Analysis β β Context Analysis β β
β β Patterns β β (Gemini) β β (spaCy) β β
β βββββββββββββββββ ββββββββββββββββ ββββββββββββββββββββ β
βββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββ
β ChromaDB Cache β
β Semantic Vector Matching β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Module Structure
search_optimizer.py
βββ SearchOptimizer class # Main interface
βββ Rule-based functions # Pattern matching
β βββ should_perform_search()
β βββ has_meaningful_conversation_history()
β βββ analyze_conversation_context()
βββ AI-based functions # Intelligent analysis
β βββ analyze_search_necessity()
βββ Hybrid engine # Combined decision making
β βββ hybrid_search_decision()
βββ Utility functions # Support functions
βββ extract_search_terms()
βββ format_search_context()
βββ clean_terms()
Core Functions
Rule-Based Search Decision
should_perform_search(prompt, history, search_decision_mode)
Purpose: Fast pattern-based search decision using linguistic rules.
Parameters:
prompt: str- User's current questionhistory: Optional[List[Dict[str, str]]]- Conversation historysearch_decision_mode: str- Sensitivity mode ("conservative", "balanced", "aggressive")
Returns: Dict[str, Any] with:
should_search: bool- Search decisionreason: str- Explanationconfidence: float- Decision confidence (0.0-1.0)
Pattern Detection:
# Elaboration patterns
["elaborate", "explain more", "tell me more", "expand on", "go deeper"]
# Clarification patterns
["what do you mean", "can you clarify", "i don't understand", "unclear"]
# Referential patterns
["this", "that", "it", "the previous", "above mentioned", "earlier"]
# Continuation patterns
["and what about", "what else", "continue", "also", "additionally"]
Example Usage:
from search_optimizer import should_perform_search
result = should_perform_search(
"Tell me more about neural networks",
[{"user": "What is AI?", "assistant": "AI is artificial intelligence..."}],
"balanced"
)
# Result: {"should_search": False, "reason": "Elaboration request with existing context", "confidence": 0.7}
AI-Based Search Analysis
analyze_search_necessity(prompt, history, conversation_context, gemini_model)
Purpose: Deep AI-powered analysis for ambiguous cases.
Features:
- Question type classification (new info vs clarification)
- Information sufficiency assessment
- Topic continuity detection
- Recency requirements analysis
- Semantic similarity scoring
Caching: Results cached for 5 minutes to optimize performance.
Example Usage:
from search_optimizer import analyze_search_necessity
result = await analyze_search_necessity(
"What are the latest developments?",
history,
conversation_context,
gemini_model
)
# Result includes detailed analysis breakdown
Hybrid Decision Engine
hybrid_search_decision(prompt, history, search_decision_mode, nlp_model, gemini_model)
Purpose: Combines rule-based and AI analysis for optimal decisions.
Decision Logic:
- Rule-based first: Fast pattern matching
- Confidence check: Use AI if rule confidence < threshold
- Weighted combination: Merge rule and AI decisions
- Context analysis: Add semantic similarity metrics
Thresholds by Mode:
conservative: AI threshold 0.8 (prefer rules)balanced: AI threshold 0.6 (balanced approach)aggressive: AI threshold 0.4 (prefer AI analysis)
Context Analysis
analyze_conversation_context(prompt, history, nlp_model)
Purpose: Semantic analysis of conversation continuity.
Metrics Calculated:
topic_continuity: Keyword overlap scoresemantic_similarity: spaCy vector similarityinformation_coverage: History richness scorecontext_richness: Overall context quality
spaCy Integration: Uses en_core_web_sm model for:
- Word vectors and similarity
- Lemmatization and tokenization
- Stop word filtering
- Dependency parsing
Utility Functions
extract_search_terms(text, nlp_model, rake_instance)
NLP Pipeline:
- Named Entity Recognition: Extract proper nouns
- Noun Phrase Extraction: Syntactic analysis
- RAKE Keywords: Top-ranked phrases
- Focus Phrase Detection: Dependency parsing
- Term Cleaning: Deduplication and filtering
Example:
terms = extract_search_terms(
"What is machine learning in healthcare?",
nlp, rake
)
# Returns: ["machine learning", "healthcare", "machine learning healthcare"]
format_search_context(results)
Purpose: Format search results for AI consumption.
Features:
- Source attribution
- Content truncation (1200 chars per result)
- Error handling for malformed results
- Structured output for AI processing
Configuration & Customization
Search Decision Modes
Conservative Mode:
- Elaboration threshold: 0.8 (high confidence required)
- Referential threshold: 0.7
- History weight: 0.9 (heavily favor existing context)
Balanced Mode:
- Elaboration threshold: 0.6
- Referential threshold: 0.5
- History weight: 0.7
Aggressive Mode:
- Elaboration threshold: 0.4 (low confidence required)
- Referential threshold: 0.3
- History weight: 0.5 (prefer fresh searches)
Pattern Customization
Add custom patterns to the decision logic:
# In should_perform_search function
custom_patterns = [
"help me understand",
"break down",
"simplify this"
]
elaboration_patterns.extend(custom_patterns)
AI Prompt Customization
Modify the AI analysis prompt in analyze_search_necessity:
analysis_prompt = f"""
Analyze whether a web search is necessary for: {prompt}
Custom criteria:
1. Domain-specific requirements
2. Company knowledge base availability
3. User expertise level
Respond with JSON: {{"should_search": bool, "confidence": float, "reason": str}}
"""
Performance Optimization
Caching Strategy
AI Decision Cache:
- 5-minute TTL for search decisions
- Hash-based keys using prompt + history
- Automatic cleanup and memory management
ChromaDB Vector Cache:
- Persistent storage across restarts
- Semantic similarity matching (threshold 0.7)
- TTL-based expiration with cleanup
Performance Monitoring
Track key metrics:
# Function execution times
hybrid_decision_time = measure_time(hybrid_search_decision)
# Cache hit rates
cache_stats = search_cache.get_stats()
hit_rate = cache_stats["hit_rate_percentage"]
# AI analysis frequency
ai_calls_percentage = ai_decisions / total_decisions
Optimization Guidelines
- Rule-based first: Fast patterns handle 60-70% of cases
- Cache aggressively: ChromaDB for search results, memory for decisions
- Monitor thresholds: Adjust AI confidence thresholds based on usage
- Batch operations: Group similar requests when possible
Integration Patterns
Basic Integration
from search_optimizer import hybrid_search_decision
# In your chat endpoint
search_decision = await hybrid_search_decision(
request.prompt,
request.history,
request.search_decision_mode,
nlp_model,
gemini_model
)
if search_decision["should_search"]:
# Perform web search
search_results = await search_web_combined(query)
else:
# Use conversation history only
search_results = []
Advanced Integration
# Custom decision logic
async def custom_search_decision(request, models):
# Step 1: Check force_search override
if request.force_search is not None:
return {"should_search": request.force_search, "reason": "User override"}
# Step 2: Domain-specific rules
if is_internal_knowledge(request.prompt):
return {"should_search": False, "reason": "Internal knowledge available"}
# Step 3: Use hybrid engine
return await hybrid_search_decision(
request.prompt, request.history,
request.search_decision_mode,
models.nlp, models.gemini
)
Error Handling
try:
search_decision = await hybrid_search_decision(...)
except Exception as e:
logger.error(f"Search decision failed: {e}")
# Fallback to safe default
search_decision = {
"should_search": True,
"reason": f"Decision engine error: {str(e)[:50]}",
"confidence": 0.5,
"decision_method": "fallback"
}
Testing & Validation
Unit Testing
def test_search_decision_patterns():
# Test elaboration detection
result = should_perform_search("Tell me more", history)
assert not result["should_search"]
assert "elaboration" in result["reason"].lower()
# Test new information requests
result = should_perform_search("What's the latest news?", None)
assert result["should_search"]
assert result["confidence"] > 0.8
Integration Testing
async def test_hybrid_engine():
# Test rule-based path
result = await hybrid_search_decision("Hello", [], "balanced", nlp, model)
assert result["decision_method"] == "rule_based"
# Test hybrid path
result = await hybrid_search_decision(ambiguous_prompt, [], "balanced", nlp, model)
assert result["decision_method"] == "hybrid"
Performance Testing
def benchmark_search_decisions():
import time
start = time.time()
for _ in range(100):
should_perform_search("test prompt", [])
end = time.time()
avg_time = (end - start) / 100
assert avg_time < 0.01 # Sub-10ms performance
Monitoring & Debugging
Logging Integration
import logging
logger = logging.getLogger("search_optimizer")
# Enable debug logging
logger.setLevel(logging.DEBUG)
# In functions, use structured logging
logger.info(f"Search decision: {decision['should_search']}, confidence: {decision['confidence']:.2f}, reason: {decision['reason']}")
Metrics Collection
# Decision distribution
rule_based_count = 0
hybrid_count = 0
ai_only_count = 0
# Performance metrics
decision_times = []
cache_hit_rates = []
false_positive_rate = 0.0 # Search when not needed
false_negative_rate = 0.0 # No search when needed
Debug Utilities
def debug_search_decision(prompt, history):
"""Detailed debugging for search decisions"""
print(f"Analyzing: {prompt}")
print(f"History entries: {len(history or [])}")
# Rule-based analysis
rule_result = should_perform_search(prompt, history)
print(f"Rule decision: {rule_result}")
# Context analysis
context = analyze_conversation_context(prompt, history, nlp)
print(f"Context metrics: {context}")
# Final decision
final_result = await hybrid_search_decision(prompt, history, "balanced", nlp, model)
print(f"Final decision: {final_result}")
Future Enhancements
Planned Features
- Machine Learning Integration: Train models on decision patterns
- User Behavior Analysis: Personalized search thresholds
- Domain-Specific Rules: Industry/topic-specific optimization
- Multimodal Support: Image and document context analysis
- Real-time Learning: Adaptive thresholds based on feedback
Extension Points
# Custom analyzers
class CustomSearchAnalyzer:
def analyze(self, prompt, history, context):
# Custom analysis logic
return {"should_search": bool, "confidence": float}
# Plugin architecture
search_plugins = [
DomainSpecificAnalyzer(),
UserBehaviorAnalyzer(),
CustomSearchAnalyzer()
]
Conclusion
The search optimizer provides a robust, intelligent system for minimizing unnecessary web searches while maintaining response quality. The hybrid approach combining rule-based patterns with AI analysis offers both performance and accuracy.
Key benefits:
- 40-60% reduction in unnecessary searches
- Sub-millisecond rule-based decisions
- Intelligent fallbacks for edge cases
- Comprehensive caching for performance
- Extensive customization options
For questions or contributions, refer to the main Atlas documentation or create issues in the project repository.