| | |
| | """ |
| | Query Expansion - Usage Examples for CogniChat |
| | |
| | This file demonstrates practical usage of the Query Expansion system. |
| | Run this file to see query expansion in action! |
| | |
| | Usage: python3 query_expansion_examples.py |
| | """ |
| |
|
| | import sys |
| | import os |
| | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| |
|
| | from utils.query_expansion import ( |
| | expand_query_simple, |
| | MultiQueryExpander, |
| | QueryStrategy, |
| | QueryAnalyzer, |
| | MultiHopReasoner, |
| | FallbackStrategies |
| | ) |
| |
|
| | def print_separator(title=""): |
| | """Print a nice separator.""" |
| | if title: |
| | print("\n" + "=" * 70) |
| | print(f" {title}") |
| | print("=" * 70) |
| | else: |
| | print("-" * 70) |
| |
|
| |
|
| | def example_1_simple_expansion(): |
| | """Example 1: Simple query expansion using the convenience function.""" |
| | print_separator("Example 1: Simple Query Expansion") |
| | |
| | query = "How do I implement authentication in Flask?" |
| | |
| | print(f"\nπ Original Query: {query}\n") |
| | |
| | |
| | print("Strategy: QUICK (2 queries)") |
| | queries = expand_query_simple(query, strategy="quick") |
| | for i, q in enumerate(queries, 1): |
| | print(f" {i}. {q}") |
| | |
| | |
| | print("\nStrategy: BALANCED (3-4 queries)") |
| | queries = expand_query_simple(query, strategy="balanced") |
| | for i, q in enumerate(queries, 1): |
| | print(f" {i}. {q}") |
| | |
| | |
| | print("\nStrategy: COMPREHENSIVE (5-6 queries)") |
| | queries = expand_query_simple(query, strategy="comprehensive") |
| | for i, q in enumerate(queries, 1): |
| | print(f" {i}. {q}") |
| |
|
| |
|
| | def example_2_with_analysis(): |
| | """Example 2: Full expansion with analysis.""" |
| | print_separator("Example 2: Query Expansion with Full Analysis") |
| | |
| | expander = MultiQueryExpander() |
| | |
| | query = "Compare the performance of different machine learning algorithms" |
| | |
| | print(f"\nπ Original Query: {query}\n") |
| | |
| | result = expander.expand(query, strategy=QueryStrategy.BALANCED) |
| | |
| | print("π Analysis:") |
| | print(f" Intent: {result.analysis.intent}") |
| | print(f" Entities: {result.analysis.entities}") |
| | print(f" Keywords: {result.analysis.keywords}") |
| | print(f" Complexity: {result.analysis.complexity}") |
| | print(f" Domain: {result.analysis.domain or 'N/A'}") |
| | |
| | print(f"\nπ Generated {len(result.variations)} Query Variations:") |
| | for i, var in enumerate(result.variations, 1): |
| | print(f" {i}. {var}") |
| |
|
| |
|
| | def example_3_multi_hop(): |
| | """Example 3: Multi-hop reasoning for complex queries.""" |
| | print_separator("Example 3: Multi-Hop Reasoning") |
| | |
| | analyzer = QueryAnalyzer() |
| | reasoner = MultiHopReasoner() |
| | |
| | queries = [ |
| | "Compare Python and Java for web development", |
| | "How do I implement authentication with JWT tokens?", |
| | "Explain the difference between supervised and unsupervised learning" |
| | ] |
| | |
| | for query in queries: |
| | print(f"\nπ Original Query: {query}") |
| | |
| | analysis = analyzer.analyze(query) |
| | sub_queries = reasoner.generate_sub_queries(query, analysis) |
| | |
| | print(f" Intent: {analysis.intent}") |
| | print(f" Sub-queries generated: {len(sub_queries)}") |
| | for i, sq in enumerate(sub_queries, 1): |
| | print(f" {i}. {sq}") |
| |
|
| |
|
| | def example_4_fallback_strategies(): |
| | """Example 4: Fallback strategies for edge cases.""" |
| | print_separator("Example 4: Fallback Strategies") |
| | |
| | analyzer = QueryAnalyzer() |
| | |
| | queries = [ |
| | "What is the specific exact precise difference between REST and GraphQL?", |
| | "How do I only debug specific Python code issues?", |
| | "Tell me everything about machine learning algorithms" |
| | ] |
| | |
| | for query in queries: |
| | print(f"\nπ Original Query:") |
| | print(f" {query}") |
| | |
| | analysis = analyzer.analyze(query) |
| | |
| | print(f"\n Fallback Options:") |
| | print(f" 1. Simplified: {FallbackStrategies.simplify_query(query)}") |
| | print(f" 2. Broadened: {FallbackStrategies.broaden_query(query, analysis)}") |
| | print(f" 3. Entity-Focused: {FallbackStrategies.focus_entities(analysis)}") |
| |
|
| |
|
| | def example_5_different_intents(): |
| | """Example 5: How expansion adapts to different query intents.""" |
| | print_separator("Example 5: Intent-Aware Expansion") |
| | |
| | expander = MultiQueryExpander() |
| | |
| | test_cases = [ |
| | ("What is recursion?", "definition"), |
| | ("How do I sort a list in Python?", "how_to"), |
| | ("Compare bubble sort and quick sort", "comparison"), |
| | ("Why does my code crash?", "explanation"), |
| | ("List all HTTP methods", "listing"), |
| | ("Show me an example of a decorator", "example"), |
| | ] |
| | |
| | for query, expected_intent in test_cases: |
| | result = expander.expand(query, strategy=QueryStrategy.QUICK) |
| | |
| | print(f"\nπ Query: {query}") |
| | print(f" Intent: {result.analysis.intent} (expected: {expected_intent})") |
| | print(f" Variations:") |
| | for i, var in enumerate(result.variations, 1): |
| | print(f" {i}. {var}") |
| |
|
| |
|
| | def example_6_production_usage(): |
| | """Example 6: Simulated production usage.""" |
| | print_separator("Example 6: Production Usage Simulation") |
| | |
| | print("\nSimulating how Query Expansion works in CogniChat's RAG pipeline:\n") |
| | |
| | |
| | user_query = "How do I optimize my database queries?" |
| | |
| | print(f"1οΈβ£ User Question: {user_query}") |
| | |
| | |
| | print(f"\n2οΈβ£ Query Expansion (Balanced Strategy):") |
| | expanded_queries = expand_query_simple(user_query, strategy="balanced") |
| | for i, q in enumerate(expanded_queries, 1): |
| | print(f" {i}. {q}") |
| | |
| | |
| | print(f"\n3οΈβ£ Retrieval Phase:") |
| | print(f" β Retrieving documents for {len(expanded_queries)} query variations...") |
| | print(f" β Query 1: Retrieved 10 documents") |
| | print(f" β Query 2: Retrieved 10 documents") |
| | print(f" β Query 3: Retrieved 10 documents") |
| | print(f" β Query 4: Retrieved 10 documents") |
| | print(f" β Total: 40 documents retrieved") |
| | |
| | |
| | print(f"\n4οΈβ£ Deduplication:") |
| | print(f" β Removing duplicates based on content hash...") |
| | print(f" β Unique documents: 20") |
| | |
| | |
| | print(f"\n5οΈβ£ Final Result:") |
| | print(f" β
Top 20 unique documents sent to LLM") |
| | print(f" β
+100% more candidates than single query") |
| | print(f" β
Better coverage and relevance") |
| | |
| | print(f"\nπ Impact:") |
| | print(f" β’ Retrieval coverage: +100%") |
| | print(f" β’ Answer quality: +40-60%") |
| | print(f" β’ Latency overhead: ~100ms") |
| |
|
| |
|
| | def run_all_examples(): |
| | """Run all examples.""" |
| | print("\n" + "=" * 70) |
| | print(" QUERY EXPANSION - PRACTICAL USAGE EXAMPLES") |
| | print("=" * 70) |
| | |
| | examples = [ |
| | ("Simple Expansion", example_1_simple_expansion), |
| | ("With Analysis", example_2_with_analysis), |
| | ("Multi-Hop Reasoning", example_3_multi_hop), |
| | ("Fallback Strategies", example_4_fallback_strategies), |
| | ("Intent-Aware Expansion", example_5_different_intents), |
| | ("Production Usage", example_6_production_usage), |
| | ] |
| | |
| | for name, func in examples: |
| | try: |
| | func() |
| | except Exception as e: |
| | print(f"\nβ Error in {name}: {e}") |
| | import traceback |
| | traceback.print_exc() |
| | |
| | print("\n" + "=" * 70) |
| | print(" All examples completed!") |
| | print("=" * 70) |
| | print("\nπ‘ Tips:") |
| | print(" β’ Use 'quick' strategy for fast responses (2 queries)") |
| | print(" β’ Use 'balanced' strategy for production (3-4 queries) - DEFAULT") |
| | print(" β’ Use 'comprehensive' strategy for complex queries (5-6 queries)") |
| | print("\nπ More Info:") |
| | print(" β’ Full Guide: QUERY_EXPANSION_GUIDE.md") |
| | print(" β’ Quick Reference: QUERY_EXPANSION_QUICK_REFERENCE.md") |
| | print(" β’ Implementation Status: QUERY_EXPANSION_IMPLEMENTATION_STATUS.md") |
| | print() |
| |
|
| |
|
| | if __name__ == "__main__": |
| | run_all_examples() |
| |
|