Further improve and add in advanced reasoning capabilities:

```
import streamlit as st
import g4f
from g4f.client import Client
import os
import json
import time
import re
from typing import List, Tuple, Dict
import numpy as np

class AdvancedReasoningEngine:
    def __init__(self, model="gpt-4o"):
        self.client = Client()
        self.model = model
        self.reasoning_strategies = [
            "Analytical Decomposition",
            "Probabilistic Reasoning",
            "Multi-Perspective Analysis",
            "Counterfactual Reasoning",
            "Meta-Cognitive Evaluation"
        ]

        # Comprehensive JSON Structure Documentation
        self.json_structure_guide = {
            "next_action": {
                "continue": {
                    "description": "Indicates the AI should proceed with further reasoning steps",
                    "use_case": "When the current reasoning is incomplete or requires deeper exploration",
                    "example": {
                        "title": "Initial Problem Decomposition",
                        "content": "Breaking down the problem into key components...",
                        "strategy": "Analytical Decomposition",
                        "confidence": 60,
                        "next_action": "continue",
                        "key_points": ["Identified main problem areas"]
                    }
                },
                "final_answer": {
                    "description": "Signals that the AI has reached a conclusive reasoning point",
                    "use_case": [
                        "Problem fully analyzed",
                        "Sufficient evidence gathered",
                        "No further reasoning steps needed"
                    ],
                    "example": {
                        "title": "Definitive Conclusion",
                        "content": "Based on comprehensive analysis, the answer is...",
                        "strategy": "Comprehensive Synthesis",
                        "confidence": 85,
                        "next_action": "final_answer",
                        "key_points": ["Consolidated all findings", "Final answer derived from analysis"]
                    }
                }
            },
            "early_final_answer_scenarios": [
                {
                    "scenario": "Immediate Resolution",
                    "description": "When the problem is trivially simple",
                    "example": {
                        "title": "Instant Solution",
                        "content": "The answer is straightforward and requires no complex reasoning",
                        "strategy": "Direct Inference",
                        "confidence": 95,
                        "next_action": "final_answer",
                        "key_points": ["Problem is straightforward", "Direct inference applied"]
                    }
                },
                {
                    "scenario": "Insufficient Information",
                    "description": "When the problem lacks necessary details for in-depth analysis",
                    "example": {
                        "title": "Reasoning Limitation",
                        "content": "Cannot proceed due to lack of critical information",
                        "strategy": "Meta-Cognitive Evaluation",
                        "confidence": 30,
                        "next_action": "final_answer",
                        "key_points": ["Insufficient data provided"]
                    }
                }
            ]
        }

    def make_api_call(self, messages, max_tokens, is_final_answer=False):
        for attempt in range(3):
            try:
                response = self.client.chat.completions.create(
                    model=self.model,
                    messages=messages,
                    max_tokens=max_tokens,
                    temperature=0.7,
                    top_p=0.9,
                    frequency_penalty=0.5,
                    response_format={"type": "json_object"}
                )
                return json.loads(response.choices[0].message.content)
            except Exception as e:
                if attempt == 2:
                    return self._generate_fallback_response(e, is_final_answer)
                time.sleep(1)

    def generate_system_prompt(self) -> str:
        """
        Generate a comprehensive system prompt that includes JSON structure guidelines.
        
        The prompt now explicitly explains the 'next_action' mechanism and provides 
        clear guidance on when to use 'continue' vs 'final_answer', along with instructions
        for handling cumulative key points.
        """
        json_structure = """
{
    "title": "Step Title",             // Descriptive name of reasoning step
    "content": "Detailed explanation", // In-depth reasoning process
    "strategy": "Reasoning Strategy",  // Method used (e.g., Analytical Decomposition)
    "confidence": 0-100,               // Certainty percentage
    "next_action": "continue/final_answer",  // Reasoning progression indicator
    "key_points": ["Key point 1", "Key point 2"] // Cumulative key points
}
"""
        return f"""You are an advanced AI reasoning engine with multi-dimensional cognitive capabilities.

Core Reasoning Strategies:
{', '.join(self.reasoning_strategies)}

Reasoning Protocol:
1. Decompose problem into fundamental components
2. Apply diverse reasoning strategies
3. Cross-validate reasoning paths
4. Quantify confidence levels
5. Identify potential biases and limitations
6. Accumulate key points to build comprehensive understanding

JSON Response Mechanics - CR ITICAL UNDERSTANDING:

1. 'next_action' Mechanism:
   - 'continue': 
     * Signals MORE reasoning steps are required
     * Used when current analysis is INCOMPLETE
     * Indicates: "I need to dig deeper"
     * Typical use: Complex problems needing multi-step breakdown

   - 'final_answer':
     * Signals REASONING IS COMPLETE
     * Used when you have a definitive conclusion
     * Indicates: "I'm ready to provide the final answer"
     * Typical use: Problem fully analyzed, clear solution found

2. Early 'final_answer' Scenarios:
   - Trivially simple problems
   - Insufficient information to proceed
   - Immediate, obvious solution
   - Reasoning hits an insurmountable barrier

3. Confidence Level Guidance:
   - 20-40%: Speculative (use 'continue')
   - 41-70%: Needs further investigation (use 'continue')
   - 71-90%: Strong evidence (can use 'final_answer')
   - 91-100%: Definitive (strongly use 'final_answer')

4. Cumulative Key Points:
   - After each reasoning step, generate key points summarizing critical insights.
   - Use accumulated key points to inform subsequent reasoning steps.
   - Ensure key points are concise and capture the essence of the reasoning so far.

JSON Structure Requirements:
{json_structure}

CRITICAL INSTRUCTION: 
- ALWAYS choose 'next_action' thoughtfully
- Provide CLEAR RATIONALE for your action choice
- Be EXPLICIT about reasoning limitations
- Utilize and update key points to enhance cumulative understanding
"""
    
    def _generate_fallback_response(self, error, is_final_answer):
        base_response = {
            "title": "Reasoning Resilience",
            "content": f"AI encountered a processing limitation. Error: {str(error)}",
            "strategy": "System Error Handling",
            "confidence": 50,
            "next_action": "final_answer",
            "key_points": ["Encountered processing error"]
        }
        if is_final_answer:
            base_response["title"] = "Final Answer Approximation"
            base_response["strategy"] = "Error-Based Synthesis"
            base_response["key_points"] = ["Provided best approximation despite error"]
        return base_response

    def generate_response(self, prompt):
        """
        Enhanced response generation with explicit handling of 'next_action'
        and cumulative key points.
        
        Demonstrates different scenarios of reasoning progression:
        1. Multi-step complex reasoning
        2. Immediate final answer
        3. Reasoning limitation
        """
        # Initial messages setup with enhanced system prompt
        system_prompt = self.generate_system_prompt()
        messages = [
            {"role": "system", "content": system_prompt},
            {"role": "user", "content": prompt}
        ]

        steps = []
        step_count = 1
        total_thinking_time = 0
        key_points = []

        while True:
            start_time = time.time()
            # Include accumulated key points in the context
            context = "\n".join([f"- {kp}" for kp in key_points])
            user_message = f"As the reasoning progresses, consider the following key points:\n{context}\n\nContinue reasoning."
            messages.append({"role": "user", "content": user_message})

            step_data = self.make_api_call(messages, 350)
            end_time = time.time()
            
            thinking_time = end_time - start_time
            total_thinking_time += thinking_time

            # Update key points
            new_key_points = step_data.get('key_points', [])
            key_points.extend(new_key_points)

            # Detailed step tracking with strategy and confidence
            steps.append({
                "number": f"Step {step_count}",
                "title": step_data.get('title', 'Unnamed Step'),
                "content": step_data.get('content', 'No content generated'),
                "strategy": step_data.get('strategy', 'Unspecified'),
                "confidence": step_data.get('confidence', 50),
                "thinking_time": thinking_time,
                "next_action": step_data.get('next_action', 'continue'),
                "key_points": key_points.copy()
            })

            messages.append({"role": "assistant", "content": json.dumps(step_data)})

            # Explicit handling of reasoning progression
            if step_data.get('next_action') == 'final_answer':
                break

            step_count += 1

            # Safeguard against infinite reasoning
            if step_count > 10:  # Prevent excessive reasoning steps
                break

            yield steps, None

        # Final answer generation with comprehensive review
        synthesis_prompt = (
            "Synthesize a comprehensive final answer based on the accumulated key points. "
            "Explicitly state the reasoning path taken and any limitations encountered."
        )
        messages.append({"role": "user", "content": synthesis_prompt})

        start_time = time.time()
        final_data = self.make_api_call(messages, 400, is_final_answer=True)
        end_time = time.time()
        
        thinking_time = end_time - start_time
        total_thinking_time += thinking_time

        # Update key points with final step
        final_key_points = final_data.get('key_points', [])
        key_points.extend(final_key_points)

        steps.append({
            "number": "Final Answer",
            "title": final_data.get('title', 'Synthesized Conclusion'),
            "content": final_data.get('content', 'No conclusive answer generated'),
            "strategy": final_data.get('strategy', 'Comprehensive Synthesis'),
            "confidence": final_data.get('confidence', 75),
            "thinking_time": thinking_time,
            "key_points": key_points.copy()
        })

        yield steps, total_thinking_time

def main():
    st.set_page_config(page_title="Advanced AI Reasoning Engine", page_icon="🧠", layout="wide")

    st.title("🔬 Advanced Multi-Dimensional Reasoning Engine")

    st.markdown("""
    ### Cognitive Processing Framework
    - **Multi-Strategy Reasoning**: Combines analytical, probabilistic, and meta-cognitive approaches
    - **Confidence-Based Reasoning**: Quantifies certainty at each step
    - **Adaptive Cognitive Processing**: Dynamically adjusts reasoning strategy
    - **Cumulative Reasoning with Key Points**: Builds upon previous insights to enhance understanding
    """)

    reasoning_engine = AdvancedReasoningEngine()

    user_query = st.text_input("Enter complex query or problem:", placeholder="Provide a challenging problem or query")

    if user_query:
        st.write("🔍 Initiating Advanced Reasoning Process...")

        response_container = st.empty()
        time_container = st.empty()
        keypoints_container = st.empty()

        all_key_points = []

        for steps, total_thinking_time in reasoning_engine.generate_response(user_query):
            with response_container.container():
                for step in steps:
                    if step['number'] == "Final Answer":
                        st.markdown(f"### 🏁 {step['title']}")
                        st.markdown(f"**Confidence Level: {step['confidence']}%**")
                        st.markdown(step['content'].replace('\n', '<br>'), unsafe_allow_html=True)
                    else:
                        with st.expander(f"{step['number']}: {step['title']} (Strategy: {step['strategy']})", expanded=False):
                            st.markdown(f"**Confidence: {step['confidence']}%**")
                            st.markdown(f"**Thinking Time: {step['thinking_time']:.2f} seconds**")
                            st.markdown(step['content'].replace('\n', '<br>'), unsafe_allow_html=True)

            if total_thinking_time is not None:
                time_container.markdown(f"**🕒 Total Reasoning Time: {total_thinking_time:.2f} seconds**")

            # Update and display key points
            all_key_points = steps[-1].get('key_points', [])
            formatted_keypoints = "\n".join([f"- {kp}" for kp in all_key_points])
            keypoints_container.markdown("### 🔑 **Cumulative Key Points**")
            keypoints_container.markdown(formatted_keypoints)

if __name__ == "__main__":
    main()