Spaces:
Sleeping
Atlas Intelligent Search Management - Implementation Tasks
Project Overview
Optimize the web search functionality in Atlas to avoid unnecessary searches when conversation history already contains sufficient context to answer user questions.
Current Problem: Web search is performed regardless of conversation history, leading to increased latency, API costs, and poor user experience for follow-up questions.
Goal: Reduce unnecessary web searches by 40-60% while maintaining response quality.
Phase 1: Rule-Based Context Analysis (Quick Win)
Target Timeline: 1-2 weeks
Expected Impact: 40% reduction in unnecessary searches
Core Implementation
Create search decision function
- Add
should_perform_search()function inapp.py - Implement pattern detection for follow-up questions
- Add referential question detection logic
- Create question type classification
- Add
Define detection patterns
- Elaboration patterns: "elaborate", "explain more", "tell me more", "expand on"
- Clarification patterns: "what do you mean", "can you clarify", "I don't understand"
- Referential patterns: "this", "that", "it", "the previous", "above mentioned"
- Continuation patterns: "and what about", "what else", "continue"
Integrate with chat endpoint
- Modify chat endpoint (
app.py:479-487) to call search decision function - Add conditional search logic before
search_web_combined() - Preserve existing search behavior as fallback
- Add logging for search decision tracking
- Modify chat endpoint (
Configuration & Control
Add configuration options
- Search sensitivity level (conservative/balanced/aggressive)
- Pattern matching thresholds
- Fallback behavior settings
- Debug mode for search decisions
Request model updates
- Add optional
force_searchparameter toChatRequest - Add
search_decision_modeparameter - Update API documentation
- Add optional
Testing & Validation
Unit tests for search decision logic
- Test common follow-up question patterns
- Test referential question detection
- Test edge cases and false positives
- Test with various conversation histories
Integration testing
- Test full chat flow with search decisions
- Validate response quality maintenance
- Test fallback mechanisms
- Performance impact measurement
Analytics Integration
- Track search decision metrics
- Add search_decision_made field to message tracking
- Track search_skipped_reason
- Update analytics dashboard with search optimization metrics
- Monitor false positive/negative rates
Phase 2: AI-Based Search Decision Engine (Enhanced Intelligence)
Target Timeline: 2-3 weeks
Expected Impact: Additional 15-20% optimization + better edge case handling
AI Decision Engine
Implement AI-based search analyzer
- Create
analyze_search_necessity()function - Design prompt template for search decision
- Implement lightweight Gemini call for decision making
- Add confidence scoring for search decisions
- Create
Context analysis enhancement
- Analyze conversation history relevance
- Implement topic continuity detection
- Add semantic similarity analysis between current question and history
- Create information sufficiency assessment
Smart Query Classification
Question type classification
- New information requests vs. clarifications
- Factual questions vs. opinion/analysis requests
- Time-sensitive vs. evergreen information needs
- Broad topics vs. specific details
Context sufficiency analysis
- Analyze if conversation history contains answer
- Detect information gaps that require search
- Assess recency requirements for information
- Evaluate completeness of existing context
Hybrid Decision Logic
Combine rule-based and AI decisions
- Use rules for obvious cases (performance)
- Use AI for ambiguous cases (accuracy)
- Implement decision confidence thresholds
- Add override mechanisms
Fallback and error handling
- Handle AI decision timeouts
- Implement graceful degradation to rule-based
- Add decision audit logging
- Create manual override capabilities
Performance Optimization
- Optimize AI decision calls
- Implement decision result caching
- Use minimal token prompts for decisions
- Add async processing for decision analysis
- Batch decision calls where possible
Phase 3: Universal Search Caching with Vector Database (Resource Optimization)
Target Timeline: 3-4 weeks
Expected Impact: Additional 10-15% optimization + improved response times + cache persistence
Phase 3a: Cache-First Architecture Optimization (COMPLETED)
Universal cache implementation
- Replace session-based caches with single universal cache
- Implement cache data structure with TTL and LRU eviction
- Add semantic similarity matching using spaCy
- Create cache analytics and monitoring endpoints
Cache integration
- Integrate cache with search flow in chat endpoint
- Add cache hit/miss tracking and statistics
- Implement cache clearing and maintenance endpoints
- Add cache information to API responses
Phase 3b: Context-Aware Request Flow Optimization (COMPLETED)
Implement conversation history detection
- Add logic to detect if request has meaningful conversation history
- Handle edge cases (empty history, malformed history entries)
- Create helper function for history validation (
has_meaningful_conversation_history) - Add history detection to cache_info tracking
Implement dual flow paths
- First Message (No History): Cache-first approach
- Check cache immediately after search term extraction
- Skip search decision logic for performance
- Perform web search only on cache miss
- Follow-up Messages (Has History): Search decision first
- Run hybrid search decision analysis
- Check cache only if search is determined necessary
- Skip cache entirely if search not needed
- First Message (No History): Cache-first approach
Enhanced request handling
- Preserve force_search override functionality
- Add context-aware performance metrics to analytics (flow_type tracking)
- Enhanced logging for monitoring flow paths
- Test performance improvements for both scenarios
Phase 3c: ChromaDB Vector Database Migration
ChromaDB integration setup
- Add ChromaDB dependency to requirements.txt
- Design vector database schema for search cache
- Implement embedding function configuration (SentenceTransformer)
- Create persistent storage directory structure
Vector cache implementation
- Replace hash-based cache with ChromaDB collection
- Implement unified semantic search (eliminates dual lookup paths)
- Add TTL filtering in vector queries
- Store search results as separate JSON files with metadata
Migration and testing
- Create cache migration script from current to ChromaDB
- Implement performance benchmarking (O(log n) vs O(n))
- Test persistent cache across server restarts
- Validate semantic similarity improvements
Phase 3d: System Design Documentation
- Architecture documentation
- Create current system design diagram
- Create proposed system architecture diagram (with context-aware flow)
- Document performance characteristics and trade-offs
- Add vector database operational guide
Advanced Features and Monitoring
Enhanced cache analytics
- Popular queries tracking and visualization
- Cache effectiveness scoring and recommendations
- Memory usage optimization and reporting
- Cross-restart cache persistence validation
Performance optimization
- Batch query operations for ChromaDB
- Cache warming strategies for popular queries
- Background cleanup and maintenance tasks
- Load testing and scalability validation
Cross-Phase Implementation Tasks
Code Quality & Maintenance
Documentation updates
- Update API documentation with new parameters
- Add developer documentation for search decision logic
- Create troubleshooting guides
- Update README with optimization features
Code organization
- Create separate module for search optimization (
search_optimizer.py) - Refactor search-related functions into dedicated module
- Add type hints and docstrings
- Implement proper error handling throughout
- Create separate module for search optimization (
Monitoring & Analytics
Enhanced analytics dashboard
- Add search optimization metrics section
- Create search decision breakdown charts
- Add cache performance monitoring
- Implement A/B testing capabilities for optimization
Performance monitoring
- Track response time improvements
- Monitor API cost reductions
- Add search decision accuracy metrics
- Create performance regression alerts
Configuration Management
- Environment configuration
- Add optimization settings to environment variables
- Create configuration profiles (development/production)
- Implement runtime configuration updates
- Add feature flags for gradual rollout
Deployment & Rollout
- Gradual rollout strategy
- Implement feature flags for each phase
- Create rollback mechanisms
- Add canary deployment support
- Plan staged user group rollouts
Success Metrics
Performance Metrics
- Search Reduction: Target 40-60% reduction in unnecessary searches
- Response Time: Improve average response time by 20-30% for follow-up questions
- API Cost: Reduce search API costs by 35-50%
- User Experience: Improve conversation flow satisfaction
Quality Metrics
- Response Accuracy: Maintain >95% response quality
- False Negatives: Keep search-skipped-but-needed rate <5%
- Cache Hit Rate: Achieve >60% cache hit rate in Phase 3
- User Satisfaction: Maintain or improve user satisfaction scores
Technical Metrics
- Code Coverage: Maintain >80% test coverage
- Error Rate: Keep optimization-related errors <1%
- Performance Impact: Add <50ms overhead for decision making
- Memory Usage: Keep cache memory usage <100MB per session
Implementation Notes
Development Priorities
- Start with Phase 1 for immediate impact and user feedback
- Validate thoroughly before moving to next phase
- Monitor metrics continuously during each phase
- Maintain backward compatibility throughout implementation
Risk Mitigation
- Implement comprehensive fallback mechanisms
- Add detailed logging for troubleshooting
- Create feature flags for quick rollback
- Plan gradual user rollout to minimize impact
Future Enhancements
- Machine learning models for search decision optimization
- User behavior-based search prediction
- Advanced semantic analysis for context understanding
- Multi-language support for search optimization