examples.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #!/usr/bin/env python3
  2. """
  3. Example usage of the Research Lookup skill with automatic model selection.
  4. This script demonstrates:
  5. 1. Automatic model selection based on query complexity
  6. 2. Manual model override options
  7. 3. Batch query processing
  8. 4. Integration with scientific writing workflows
  9. """
  10. import os
  11. from research_lookup import ResearchLookup
  12. def example_automatic_selection():
  13. """Demonstrate automatic model selection."""
  14. print("=" * 80)
  15. print("EXAMPLE 1: Automatic Model Selection")
  16. print("=" * 80)
  17. print()
  18. research = ResearchLookup()
  19. # Simple lookup - will use Sonar Pro Search
  20. query1 = "Recent advances in CRISPR gene editing 2024"
  21. print(f"Query: {query1}")
  22. print(f"Expected model: Sonar Pro Search (fast lookup)")
  23. result1 = research.lookup(query1)
  24. print(f"Actual model: {result1.get('model')}")
  25. print()
  26. # Complex analysis - will use Sonar Reasoning Pro
  27. query2 = "Compare and contrast the efficacy of mRNA vaccines versus traditional vaccines"
  28. print(f"Query: {query2}")
  29. print(f"Expected model: Sonar Reasoning Pro (analytical)")
  30. result2 = research.lookup(query2)
  31. print(f"Actual model: {result2.get('model')}")
  32. print()
  33. def example_manual_override():
  34. """Demonstrate manual model override."""
  35. print("=" * 80)
  36. print("EXAMPLE 2: Manual Model Override")
  37. print("=" * 80)
  38. print()
  39. # Force Sonar Pro Search for budget-constrained rapid lookup
  40. research_pro = ResearchLookup(force_model='pro')
  41. query = "Explain the mechanism of CRISPR-Cas9"
  42. print(f"Query: {query}")
  43. print(f"Forced model: Sonar Pro Search")
  44. result = research_pro.lookup(query)
  45. print(f"Model used: {result.get('model')}")
  46. print()
  47. # Force Sonar Reasoning Pro for critical analysis
  48. research_reasoning = ResearchLookup(force_model='reasoning')
  49. print(f"Query: {query}")
  50. print(f"Forced model: Sonar Reasoning Pro")
  51. result = research_reasoning.lookup(query)
  52. print(f"Model used: {result.get('model')}")
  53. print()
  54. def example_batch_queries():
  55. """Demonstrate batch query processing."""
  56. print("=" * 80)
  57. print("EXAMPLE 3: Batch Query Processing")
  58. print("=" * 80)
  59. print()
  60. research = ResearchLookup()
  61. # Mix of simple and complex queries
  62. queries = [
  63. "Recent clinical trials for Alzheimer's disease", # Sonar Pro Search
  64. "Compare deep learning vs traditional ML in drug discovery", # Sonar Reasoning Pro
  65. "Statistical power analysis methods", # Sonar Pro Search
  66. ]
  67. print("Processing batch queries...")
  68. print("Each query will automatically select the appropriate model")
  69. print()
  70. results = research.batch_lookup(queries, delay=1.0)
  71. for i, result in enumerate(results):
  72. print(f"Query {i+1}: {result['query'][:50]}...")
  73. print(f" Model: {result.get('model')}")
  74. print(f" Type: {result.get('model_type')}")
  75. print()
  76. def example_scientific_writing_workflow():
  77. """Demonstrate integration with scientific writing workflow."""
  78. print("=" * 80)
  79. print("EXAMPLE 4: Scientific Writing Workflow")
  80. print("=" * 80)
  81. print()
  82. research = ResearchLookup()
  83. # Literature review phase - use Pro for breadth
  84. print("PHASE 1: Literature Review (Breadth)")
  85. lit_queries = [
  86. "Recent papers on machine learning in genomics 2024",
  87. "Clinical applications of AI in radiology",
  88. "RNA sequencing analysis methods"
  89. ]
  90. for query in lit_queries:
  91. print(f" - {query}")
  92. # These will automatically use Sonar Pro Search
  93. print()
  94. # Discussion phase - use Reasoning Pro for synthesis
  95. print("PHASE 2: Discussion (Synthesis & Analysis)")
  96. discussion_queries = [
  97. "Compare the advantages and limitations of different ML approaches in genomics",
  98. "Explain the relationship between model interpretability and clinical adoption",
  99. "Analyze the ethical implications of AI in medical diagnosis"
  100. ]
  101. for query in discussion_queries:
  102. print(f" - {query}")
  103. # These will automatically use Sonar Reasoning Pro
  104. print()
  105. def main():
  106. """Run all examples (requires OPENROUTER_API_KEY to be set)."""
  107. if not os.getenv("OPENROUTER_API_KEY"):
  108. print("Note: Set OPENROUTER_API_KEY environment variable to run live queries")
  109. print("These examples show the structure without making actual API calls")
  110. print()
  111. # Uncomment to run examples (requires API key)
  112. # example_automatic_selection()
  113. # example_manual_override()
  114. # example_batch_queries()
  115. # example_scientific_writing_workflow()
  116. # Show complexity assessment without API calls
  117. print("=" * 80)
  118. print("COMPLEXITY ASSESSMENT EXAMPLES (No API calls required)")
  119. print("=" * 80)
  120. print()
  121. os.environ.setdefault("OPENROUTER_API_KEY", "test")
  122. research = ResearchLookup()
  123. test_queries = [
  124. ("Recent CRISPR studies", "pro"),
  125. ("Compare CRISPR vs TALENs", "reasoning"),
  126. ("Explain how CRISPR works", "reasoning"),
  127. ("Western blot protocol", "pro"),
  128. ("Pros and cons of different sequencing methods", "reasoning"),
  129. ]
  130. for query, expected in test_queries:
  131. complexity = research._assess_query_complexity(query)
  132. model_name = "Sonar Reasoning Pro" if complexity == "reasoning" else "Sonar Pro Search"
  133. status = "✓" if complexity == expected else "✗"
  134. print(f"{status} '{query}'")
  135. print(f" → {model_name}")
  136. print()
  137. if __name__ == "__main__":
  138. main()