| """ | |
| Main execution script for Chain of Thought Reasoning System | |
| ========================================================= | |
| This script demonstrates all components of the CoT system including: | |
| - Basic Chain of Thought reasoning | |
| - Tree of Thought exploration | |
| - Graph-based reasoning with self-reflection | |
| - Neural network implementations | |
| - Comprehensive evaluation and visualization | |
| """ | |
| import sys | |
| import os | |
| import traceback | |
| from datetime import datetime | |
| import warnings | |
| warnings.filterwarnings("ignore") | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) | |
| def main(): | |
| print("π§ Chain of Thought Reasoning System") | |
| print("=" * 50) | |
| print(f"Execution started at: {datetime.now()}") | |
| print() | |
| try: | |
| print("π¦ Importing components...") | |
| from src.core.reasoning_structures import ReasoningStep, ReasoningChain | |
| from src.core.basic_cot import ( | |
| BasicCoTReasoner, | |
| MathematicalCoTReasoner, | |
| MedicalDiagnosisCoTReasoner, | |
| BusinessStrategyCoTReasoner, | |
| ) | |
| from src.advanced.tree_of_thought import TreeOfThoughtReasoner | |
| from src.advanced.graph_cot import GraphCoTReasoner | |
| from src.neural.neural_cot import ( | |
| NeuralCoTModel, | |
| NeuralCoTTrainer, | |
| create_synthetic_chains, | |
| ) | |
| from src.evaluation.evaluator import CoTEvaluator | |
| from src.utils.visualization import ( | |
| CoTVisualizer, | |
| CoTDataProcessor, | |
| create_visualizations_folder, | |
| ) | |
| # Try to import Gemini API (optional) | |
| try: | |
| from src.llm.gemini_cot import GeminiCoTReasoner | |
| gemini_available = True | |
| print("β Gemini API integration available!") | |
| except ImportError: | |
| gemini_available = False | |
| print("β οΈ Gemini API not available (google-generativeai not installed)") | |
| except Exception as e: | |
| gemini_available = False | |
| print(f"β οΈ Gemini API not configured: {e}") | |
| print("β All components imported successfully!") | |
| print() | |
| viz_dir = create_visualizations_folder(os.path.dirname(__file__)) | |
| print(f"π Visualizations will be saved to: {viz_dir}") | |
| print() | |
| visualizer = CoTVisualizer() | |
| test_problems = [ | |
| "What is 25 Γ 4 + 17?", | |
| "If a train travels 60 mph for 2.5 hours, how far does it go?", | |
| "Should we implement a new software system?", | |
| "How do we solve climate change?", | |
| "What is the best strategy for team collaboration?", | |
| ] | |
| print("π― Running Basic Chain of Thought Examples") | |
| print("-" * 40) | |
| basic_reasoner = BasicCoTReasoner() | |
| math_reasoner = MathematicalCoTReasoner() | |
| medical_reasoner = MedicalDiagnosisCoTReasoner() | |
| business_reasoner = BusinessStrategyCoTReasoner() | |
| basic_chains = [] | |
| for i, problem in enumerate(test_problems[:3]): | |
| print(f"\nπ Problem {i+1}: {problem}") | |
| try: | |
| if i == 0: | |
| chain = math_reasoner.solve_with_cot(problem) | |
| elif i == 1: | |
| chain = basic_reasoner.solve_with_cot(problem) | |
| else: | |
| chain = business_reasoner.solve_with_cot(problem) | |
| basic_chains.append(chain) | |
| visualizer.visualize_reasoning_chain( | |
| chain, | |
| f"Basic CoT - Problem {i+1}", | |
| f"{viz_dir}/reasoning_chains/basic_cot_problem_{i+1}.png", | |
| ) | |
| print(f"β Chain completed with {len(chain.steps)} steps") | |
| except Exception as e: | |
| print(f"β Error processing problem {i+1}: {e}") | |
| print("\nπ³ Running Tree of Thought Examples") | |
| print("-" * 40) | |
| tot_reasoner = TreeOfThoughtReasoner( | |
| max_depth=4, branching_factor=3, search_strategy="best_first" | |
| ) | |
| tot_results = [] | |
| for i, problem in enumerate(test_problems[:2]): | |
| print(f"\nπ Problem {i+1}: {problem}") | |
| try: | |
| result = tot_reasoner.solve_with_tot(problem) | |
| tot_results.append(result) | |
| if result["best_solution"]: | |
| tot_reasoner.visualize_tree( | |
| result["root_node"], | |
| max_nodes=15, | |
| save_path=f"{viz_dir}/trees/tot_problem_{i+1}.png", | |
| ) | |
| print( | |
| f"β Tree exploration completed with {result['total_nodes_explored']} nodes" | |
| ) | |
| except Exception as e: | |
| print(f"β Error processing problem {i+1}: {e}") | |
| print("\nπΈοΈ Running Graph-based CoT Examples") | |
| print("-" * 40) | |
| graph_reasoner = GraphCoTReasoner(max_reflections=2) | |
| graph_results = [] | |
| for i, problem in enumerate(test_problems[:2]): | |
| print(f"\nπ Problem {i+1}: {problem}") | |
| try: | |
| result = graph_reasoner.solve_with_reflection(problem) | |
| graph_results.append(result) | |
| graph_reasoner.visualize_reasoning_graph( | |
| save_path=f"{viz_dir}/graphs/graph_cot_problem_{i+1}.png" | |
| ) | |
| print( | |
| f"β Graph reasoning completed with {result['total_nodes']} nodes" | |
| ) | |
| except Exception as e: | |
| print(f"β Error processing problem {i+1}: {e}") | |
| print("\nπ€ Running Neural CoT Examples") | |
| print("-" * 40) | |
| try: | |
| print("Creating synthetic training data...") | |
| synthetic_chains = create_synthetic_chains(100) | |
| print("Initializing neural model...") | |
| model = NeuralCoTModel( | |
| vocab_size=10000, embedding_dim=256, hidden_dim=512, num_layers=4 | |
| ) | |
| print("β Neural model initialized successfully!") | |
| print(f"Model parameters: {sum(p.numel() for p in model.parameters()):,}") | |
| except Exception as e: | |
| print(f"β οΈ Neural CoT setup failed: {e}") | |
| print("Continuing with other components...") | |
| print("\nπ Running Comprehensive Evaluation") | |
| print("-" * 40) | |
| evaluator = CoTEvaluator() | |
| reasoners = { | |
| "Basic CoT": basic_reasoner, | |
| "Mathematical CoT": math_reasoner, | |
| "Tree of Thought": TreeOfThoughtReasoner(max_depth=3, branching_factor=2), | |
| "Graph CoT": GraphCoTReasoner(max_reflections=1), | |
| } | |
| # Add Gemini if available | |
| if gemini_available: | |
| try: | |
| gemini_reasoner = GeminiCoTReasoner() | |
| if gemini_reasoner.test_connection(): | |
| reasoners["Gemini CoT"] = gemini_reasoner | |
| print("β Gemini CoT reasoner added to evaluation!") | |
| else: | |
| print("β οΈ Gemini API connection failed - skipping Gemini evaluation") | |
| except Exception as e: | |
| print(f"β οΈ Could not initialize Gemini: {e}") | |
| for name, reasoner in reasoners.items(): | |
| print(f"\nπ Evaluating {name}...") | |
| try: | |
| evaluation = evaluator.evaluate_approach( | |
| name, reasoner, test_problems[:3] | |
| ) | |
| print(f"β {name} evaluation completed") | |
| except Exception as e: | |
| print(f"β Error evaluating {name}: {e}") | |
| print("\nπ Generating comparison visualizations...") | |
| try: | |
| evaluator.plot_comparison_radar( | |
| save_path=f"{viz_dir}/comparisons/radar_comparison.png" | |
| ) | |
| evaluator.plot_performance_metrics( | |
| save_path=f"{viz_dir}/performance/metrics_comparison.png" | |
| ) | |
| evaluator.compare_approaches() | |
| evaluator.export_results( | |
| f"{os.path.dirname(__file__)}/results/evaluation_results.json" | |
| ) | |
| except Exception as e: | |
| print(f"β Error generating comparisons: {e}") | |
| print("\nπ Generating Additional Visualizations") | |
| print("-" * 40) | |
| try: | |
| if basic_chains: | |
| visualizer.visualize_confidence_progression( | |
| basic_chains[0], | |
| save_path=f"{viz_dir}/performance/confidence_progression.png", | |
| ) | |
| visualizer.visualize_step_type_distribution( | |
| basic_chains, | |
| save_path=f"{viz_dir}/performance/step_type_distribution.png", | |
| ) | |
| visualizer.visualize_chain_comparison( | |
| basic_chains, | |
| chain_names=[f"Problem {i+1}" for i in range(len(basic_chains))], | |
| save_path=f"{viz_dir}/comparisons/chain_comparison.png", | |
| ) | |
| print("β Additional visualizations generated!") | |
| except Exception as e: | |
| print(f"β Error generating additional visualizations: {e}") | |
| print("\nπ Generating Summary Report") | |
| print("-" * 40) | |
| try: | |
| with open( | |
| f"{os.path.dirname(__file__)}/results/execution_summary.txt", "w" | |
| ) as f: | |
| f.write("Chain of Thought Reasoning System - Execution Summary\n") | |
| f.write("=" * 60 + "\n\n") | |
| f.write(f"Execution Date: {datetime.now()}\n") | |
| f.write(f"Total Problems Tested: {len(test_problems)}\n") | |
| f.write(f"Basic Chains Generated: {len(basic_chains)}\n") | |
| f.write(f"Tree Results: {len(tot_results)}\n") | |
| f.write(f"Graph Results: {len(graph_results)}\n") | |
| f.write(f"Approaches Evaluated: {len(reasoners)}\n") | |
| f.write( | |
| f"Visualizations Generated: {len(os.listdir(viz_dir)) if os.path.exists(viz_dir) else 0}\n\n" | |
| ) | |
| f.write("Test Problems:\n") | |
| for i, problem in enumerate(test_problems, 1): | |
| f.write(f"{i}. {problem}\n") | |
| f.write("\nComponents Tested:\n") | |
| f.write("- Basic Chain of Thought Reasoning\n") | |
| f.write("- Mathematical CoT Reasoning\n") | |
| f.write("- Medical Diagnosis CoT\n") | |
| f.write("- Business Strategy CoT\n") | |
| f.write("- Tree of Thought Exploration\n") | |
| f.write("- Graph-based Reasoning with Self-Reflection\n") | |
| f.write("- Neural Network CoT (Basic Setup)\n") | |
| f.write("- Comprehensive Evaluation Framework\n") | |
| f.write("- Advanced Visualization Tools\n") | |
| print("β Summary report generated!") | |
| except Exception as e: | |
| print(f"β Error generating summary report: {e}") | |
| print("\nπ Execution Completed Successfully!") | |
| print("=" * 50) | |
| print(f"Results saved to: {os.path.dirname(__file__)}/results/") | |
| print(f"Visualizations saved to: {viz_dir}/") | |
| print(f"Execution completed at: {datetime.now()}") | |
| except Exception as e: | |
| print(f"\nβ Fatal error during execution: {e}") | |
| print("\nTraceback:") | |
| traceback.print_exc() | |
| sys.exit(1) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 11.5 kB
- Xet hash:
- 6ece8046b4046cb60fb198af197a54eede6a0eca2681187b24d93276ebfb2e80
Β·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.