| #!/usr/bin/env python3 | |
| """ | |
| Complete Notebook Execution with Proper Cell-by-Cell Execution | |
| ================================================================ | |
| This script properly executes the notebook and captures all outputs. | |
| """ | |
| import sys | |
| import os | |
| # Add paths | |
| sys.path.insert(0, os.path.abspath(".")) | |
| os.chdir("/Users/tahamajs/Documents/uni/Sysmem2_in_AI/CAs/CA11_chain_of_thought") | |
| # Import all required modules | |
| print("=" * 80) | |
| print("IMPORTING MODULES") | |
| print("=" * 80) | |
| from src.advanced.self_consistency_cot import SelfConsistencyCoTReasoner | |
| from src.advanced.code_assisted_cot import ( | |
| CodeAssistedCoTReasoner, | |
| ProgramSynthesisCoTReasoner, | |
| ) | |
| from src.advanced.react_cot import ReActReasoner | |
| from src.advanced.graph_of_thoughts import GraphOfThoughtsReasoner | |
| from src.advanced.auto_cot import AutoCoTReasoner | |
| from src.utils.visualization import CoTVisualizer | |
| import matplotlib | |
| matplotlib.use("Agg") # Use non-interactive backend | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pandas as pd | |
| print("✅ All imports successful!") | |
| # Create visualizations directory | |
| os.makedirs("visualizations", exist_ok=True) | |
| # ============================================================================ | |
| # CELL 1: Self-Consistency CoT | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 1: Self-Consistency CoT") | |
| print("=" * 80) | |
| problem = "What is 25 × 4 + 17?" | |
| # Create Self-Consistency reasoner | |
| sc_reasoner = SelfConsistencyCoTReasoner(num_samples=5, temperature=0.7) | |
| sc_result = sc_reasoner.solve_with_cot(problem) | |
| print(f"\n{'='*60}") | |
| print(f"Self-Consistency CoT Result:") | |
| print(f"Solution: {sc_result.solution}") | |
| print(f"Consistency: {sc_result.metadata.get('consistency_score', 0):.2%}") | |
| print(f"{'='*60}") | |
| # ============================================================================ | |
| # CELL 2: Code-Assisted CoT | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 2: Code-Assisted CoT") | |
| print("=" * 80) | |
| # Create Code-Assisted reasoner | |
| code_reasoner = CodeAssistedCoTReasoner() | |
| code_result = code_reasoner.solve_with_cot(problem) | |
| print(f"\n{'='*60}") | |
| print(f"Code-Assisted CoT Result:") | |
| print(f"Solution: {code_result.solution}") | |
| # Show code summary | |
| code_summary = code_reasoner.get_code_summary() | |
| print(f"\nCode Execution Summary:") | |
| print(f" Total snippets: {code_summary['total_snippets']}") | |
| print(f" Success rate: {code_summary['success_rate']:.2%}") | |
| print(f" Results: {code_summary['execution_results']}") | |
| print(f"{'='*60}") | |
| # ============================================================================ | |
| # CELL 3: ReAct (Reasoning and Acting) | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 3: ReAct") | |
| print("=" * 80) | |
| # Create ReAct reasoner | |
| react_reasoner = ReActReasoner(max_actions=5) | |
| react_result = react_reasoner.solve_with_cot(problem) | |
| print(f"\n{'='*60}") | |
| print(f"ReAct Result:") | |
| print(f"Solution: {react_result.solution}") | |
| # Show action statistics | |
| action_stats = react_reasoner.get_action_statistics() | |
| print(f"\nAction Statistics:") | |
| print(f" Total actions: {action_stats['total_actions']}") | |
| print(f" Success rate: {action_stats['success_rate']:.2%}") | |
| print(f" Action sequence: {action_stats['action_sequence']}") | |
| print(f"{'='*60}") | |
| # ============================================================================ | |
| # CELL 4: Graph of Thoughts | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 4: Graph of Thoughts") | |
| print("=" * 80) | |
| # Create Graph of Thoughts reasoner | |
| got_reasoner = GraphOfThoughtsReasoner(max_vertices=30, enable_aggregation=True) | |
| got_result = got_reasoner.solve_with_got(problem) | |
| print(f"\n{'='*60}") | |
| print(f"Graph of Thoughts Result:") | |
| print(f"Best solution: {got_result['best_solution']}") | |
| # Show graph statistics | |
| stats = got_result["graph_statistics"] | |
| print(f"\nGraph Statistics:") | |
| print(f" Vertices: {stats['num_vertices']}") | |
| print(f" Edges: {stats['num_edges']}") | |
| print(f" Max depth: {stats['max_depth']}") | |
| print(f" Avg confidence: {stats['avg_confidence']:.3f}") | |
| print(f" Number of paths: {stats['num_paths']}") | |
| print(f"{'='*60}") | |
| # ============================================================================ | |
| # CELL 5: Auto-CoT | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 5: Auto-CoT") | |
| print("=" * 80) | |
| # Create Auto-CoT reasoner | |
| auto_reasoner = AutoCoTReasoner(num_clusters=3, diversity_threshold=0.5) | |
| auto_result = auto_reasoner.solve_with_cot(problem) | |
| print(f"\n{'='*60}") | |
| print(f"Auto-CoT Result:") | |
| print(f"Solution: {auto_result.solution}") | |
| # Show clustering info | |
| cluster_info = auto_reasoner.get_clustering_info() | |
| print(f"\nClustering Information:") | |
| print(f" Number of clusters: {cluster_info['num_clusters']}") | |
| print(f" Demonstrations: {cluster_info['num_demonstrations']}") | |
| print(f"{'='*60}") | |
| # ============================================================================ | |
| # CELL 6: Comparative Analysis | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 6: Comparative Analysis") | |
| print("=" * 80) | |
| # Create comparison dataframe | |
| comparison_data = { | |
| "Approach": [ | |
| "Self-Consistency", | |
| "Code-Assisted", | |
| "ReAct", | |
| "Graph of Thoughts", | |
| "Auto-CoT", | |
| ], | |
| "Steps": [ | |
| len(sc_result.steps), | |
| len(code_result.steps), | |
| len(react_result.steps), | |
| got_result["total_vertices"], | |
| len(auto_result.steps), | |
| ], | |
| "Avg Confidence": [ | |
| np.mean([s.confidence for s in sc_result.steps]), | |
| np.mean([s.confidence for s in code_result.steps]), | |
| np.mean([s.confidence for s in react_result.steps]), | |
| got_result["graph_statistics"]["avg_confidence"], | |
| np.mean([s.confidence for s in auto_result.steps]), | |
| ], | |
| "Special Feature": [ | |
| f"{sc_result.metadata.get('consistency_score', 0):.2%} consistency", | |
| f"{code_summary['success_rate']:.2%} code success", | |
| f"{action_stats['total_actions']} actions", | |
| f"{stats['num_paths']} paths", | |
| f"{cluster_info['num_demonstrations']} demos", | |
| ], | |
| } | |
| df = pd.DataFrame(comparison_data) | |
| print("\n" + "=" * 80) | |
| print("COMPARATIVE ANALYSIS") | |
| print("=" * 80) | |
| print(df.to_string(index=False)) | |
| print("=" * 80) | |
| # ============================================================================ | |
| # CELL 7: Visualization | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 7: Creating Visualizations") | |
| print("=" * 80) | |
| # Visualize comparison | |
| fig, axes = plt.subplots(1, 2, figsize=(15, 5)) | |
| # Plot 1: Number of steps/vertices | |
| axes[0].bar( | |
| df["Approach"], df["Steps"], color=["blue", "green", "red", "purple", "orange"] | |
| ) | |
| axes[0].set_xlabel("Approach") | |
| axes[0].set_ylabel("Number of Steps/Vertices") | |
| axes[0].set_title("Reasoning Complexity") | |
| axes[0].tick_params(axis="x", rotation=45) | |
| axes[0].grid(True, alpha=0.3) | |
| # Plot 2: Average confidence | |
| axes[1].bar( | |
| df["Approach"], | |
| df["Avg Confidence"], | |
| color=["blue", "green", "red", "purple", "orange"], | |
| ) | |
| axes[1].set_xlabel("Approach") | |
| axes[1].set_ylabel("Average Confidence") | |
| axes[1].set_title("Confidence Levels") | |
| axes[1].tick_params(axis="x", rotation=45) | |
| axes[1].set_ylim([0, 1]) | |
| axes[1].grid(True, alpha=0.3) | |
| plt.tight_layout() | |
| plt.savefig("visualizations/advanced_cot_comparison.png", dpi=300, bbox_inches="tight") | |
| print("✅ Visualization saved to visualizations/advanced_cot_comparison.png") | |
| # ============================================================================ | |
| # CELL 8: Complex Problem Test | |
| # ============================================================================ | |
| print("\n" + "=" * 80) | |
| print("CELL 8: Complex Problem Test") | |
| print("=" * 80) | |
| complex_problem = "If a train travels 60 mph for 2.5 hours, how far does it go?" | |
| print(f"Complex Problem: {complex_problem}\n") | |
| print("=" * 80) | |
| # Test each approach | |
| results = {} | |
| # Self-Consistency | |
| sc_reasoner2 = SelfConsistencyCoTReasoner(num_samples=7, temperature=0.8) | |
| results["Self-Consistency"] = sc_reasoner2.solve_with_cot(complex_problem) | |
| # Code-Assisted | |
| code_reasoner2 = CodeAssistedCoTReasoner() | |
| results["Code-Assisted"] = code_reasoner2.solve_with_cot(complex_problem) | |
| # ReAct | |
| react_reasoner2 = ReActReasoner(max_actions=6) | |
| results["ReAct"] = react_reasoner2.solve_with_cot(complex_problem) | |
| # Auto-CoT | |
| auto_reasoner2 = AutoCoTReasoner() | |
| results["Auto-CoT"] = auto_reasoner2.solve_with_cot(complex_problem) | |
| print("\n" + "=" * 80) | |
| print("RESULTS SUMMARY") | |
| print("=" * 80) | |
| for approach, result in results.items(): | |
| print(f"\n{approach}:") | |
| print(f" Solution: {result.solution}") | |
| print(f" Steps: {len(result.steps)}") | |
| print(f" Avg Confidence: {np.mean([s.confidence for s in result.steps]):.3f}") | |
| print("\n" + "=" * 80) | |
| print("✅ ALL CELLS EXECUTED SUCCESSFULLY!") | |
| print("=" * 80) | |
Xet Storage Details
- Size:
- 9.05 kB
- Xet hash:
- 2738a6cbb538033f0d71f0015fb6892989bdac920510740315bd54ca8ab677da
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.