Further enhance and add in an advanced Primary COT Layer, and Meta-Cognitive Layer.

```
import g4f
import random
from g4f.Provider import (
    Blackbox
)

class AdvancedChatbot:
    def __init__(self):
        # Initialize available providers
        self.providers = [
            Blackbox
        ]
        
        # Chat history to maintain context
        self.chat_history = []
        
        # System prompt to define chatbot behavior
        self.system_prompt = """You are a helpful and friendly AI assistant. 
        You provide clear, accurate, and engaging responses while maintaining a natural conversation flow."""

    def get_working_provider(self):
        """Try different providers until finding one that works"""
        random.shuffle(self.providers)  # Randomize provider selection
        
        for provider in self.providers:
            try:
                response = g4f.ChatCompletion.create(
                    model="gpt-3.5-turbo",
                    provider=provider,
                    messages=[{"role": "user", "content": "Hi"}],
                    stream=False
                )
                return provider
            except Exception as e:
                continue
        return None

    def generate_response(self, user_input, provider):
        """Generate response using the selected provider"""
        try:
            # Prepare conversation context
            messages = [
                {"role": "system", "content": self.system_prompt},
            ]
            
            # Add chat history for context
            for hist in self.chat_history[-5:]:  # Last 5 exchanges
                messages.append({"role": "user", "content": hist["user"]})
                if "assistant" in hist:
                    messages.append({"role": "assistant", "content": hist["assistant"]})
            
            # Add current user input
            messages.append({"role": "user", "content": user_input})

            # Generate response
            response = g4f.ChatCompletion.create(
                model="gpt-3.5-turbo",
                provider=provider,
                messages=messages,
                stream=False
            )
            
            return response
        except Exception as e:
            return f"Error generating response: {str(e)}"

    def chat(self, user_input):
        """Main chat function"""
        if not user_input.strip():
            return "Please enter a valid message."

        # Get working provider
        provider = self.get_working_provider()
        if not provider:
            return "Sorry, no available providers at the moment."

        # Generate response
        response = self.generate_response(user_input, provider)

        # Update chat history
        self.chat_history.append({
            "user": user_input,
            "assistant": response
        })

        return response

def main():
    chatbot = AdvancedChatbot()
    print("Advanced Chatbot initialized. Type 'quit' to exit.")
    print("-" * 50)

    while True:
        user_input = input("You: ").strip()
        
        if user_input.lower() in ['quit', 'exit', 'bye']:
            print("Chatbot: Goodbye!")
            break

        response = chatbot.chat(user_input)
        print("Chatbot:", response)
        print("-" * 50)

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print("\nChatbot: Session terminated by user.")
    except Exception as e:
        print(f"\nAn error occurred: {str(e)}")