import streamlit as st
import requests
from pocketgroq import GroqProvider

# Initialize session state
if 'messages' not in st.session_state:
    st.session_state.messages = []
if 'api_key' not in st.session_state:
    st.session_state.api_key = ''
if 'available_models' not in st.session_state:
    st.session_state.available_models = []
if 'selected_model' not in st.session_state:
    st.session_state.selected_model = "llama2-70b-4096"  # Default model

def get_groq_provider():
    if not st.session_state.api_key:
        st.error("Please enter your Groq API key.")
        return None
    return GroqProvider(api_key=st.session_state.api_key)

def fetch_available_models():
    api_key = st.session_state.api_key
    url = "https://api.groq.com/openai/v1/models"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    try:
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        models_data = response.json()
        st.session_state.available_models = [model['id'] for model in models_data['data']]
        if st.session_state.selected_model not in st.session_state.available_models:
            st.session_state.selected_model = st.session_state.available_models[0]
    except requests.RequestException as e:
        st.error(f"Error fetching models: {str(e)}")

def generate_response(prompt: str, use_cot: bool, model: str) -> str:
    groq = get_groq_provider()
    if not groq:
        return "Error: No API key provided."
    
    # Include chat history in the prompt
    history = "\n".join([f"{msg['role']}: {msg['content']}" for msg in st.session_state.messages])
    full_prompt = f"{history}\nUser: {prompt}"
    
    if use_cot:
        cot_prompt = f"Solve the following problem step by step, showing your reasoning:\n\n{full_prompt}\n\nSolution:"
        return groq.generate(cot_prompt, max_tokens=1000, temperature=0, model=model)
    else:
        return groq.generate(full_prompt, temperature=0, model=model)

def on_model_change():
    st.session_state.selected_model = st.session_state.model_selectbox

def main():
    st.title("Strawberry Groq DEMO")
    st.write("This is a simple demo of the PocketGroq library's new 'Chain of Thought' functionality.")
    st.write("<a href='https://github.com/jgravelle/pocketgroq'>https://github.com/jgravelle/pocketgroq</a> |    <a href='https://www.youtube.com/watch?v=S5dY0DG-q-U'>https://www.youtube.com/watch?v=S5dY0DG-q-U</a>", unsafe_allow_html=True)

    # API Key input
    api_key = st.text_input("Enter your Groq API Key:", type="password")
    if api_key:
        st.session_state.api_key = api_key
        fetch_available_models()
    
    # Model selection
    if st.session_state.available_models:
        st.selectbox(
            "Select a model:", 
            st.session_state.available_models, 
            index=st.session_state.available_models.index(st.session_state.selected_model),
            key="model_selectbox",
            on_change=on_model_change
        )
    
    st.write(f"Current model: {st.session_state.selected_model}")
    
    # CoT toggle
    use_cot = st.checkbox("Use Chain of Thought")
    
    # Display chat messages
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.write(message["content"])
    
    # Chat input
    if prompt := st.chat_input("What would you like to know?"):
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.write(prompt)
        
        with st.chat_message("assistant"):
            if use_cot:
                st.write("Thinking step-by-step...")
            
            response = generate_response(prompt, use_cot, st.session_state.selected_model)
            st.write(response)
        
        st.session_state.messages.append({"role": "assistant", "content": response})

if __name__ == "__main__":
    main() поправь код: import streamlit as st
import requests
from pocketgroq import GroqProvider

# Функция для инициализации состояния сессии
def initialize_session_state():
    default_values = {
        'messages': [],
        'api_key': '',
        'available_models': [],
        'selected_model': "llama2-70b-4096",  # Модель по умолчанию
    }

    for key, value in default_values.items():
        if key not in st.session_state:
            st.session_state[key] = value

# Инициализация состояния сессии
initialize_session_state()

def get_groq_provider():
    api_key = st.session_state.api_key
    if not api_key:
        st.error("Please enter your Groq API key.")
        return None
    return GroqProvider(api_key=api_key)

@st.cache_data(show_spinner=False)
def fetch_available_models(api_key):
    """Получение списка доступных моделей и кеширование результата."""
    url = "https://api.groq.com/openai/v1/models"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    models_data = response.json()
    return [model['id'] for model in models_data['data']]

def generate_response(prompt: str, use_cot: bool, model: str) -> str:
    groq = get_groq_provider()
    if not groq:
        return "Error: No API key provided."

    # Формируем историю сообщений для отправки
    messages = st.session_state.messages.copy()
    messages.append({"role": "user", "content": prompt})

    # Преобразование истории в формат для GroqProvider
    formatted_messages = [
        {"role": msg["role"], "content": msg["content"]} for msg in messages
    ]

    if use_cot:
        # Если используется Chain of Thought
        cot_prompt = (
            "Solve the following problem step by step, showing your reasoning:"
        )
        response = groq.generate(
            cot_prompt,
            messages=formatted_messages,
            max_tokens=1000,
            temperature=0,
            model=model
        )
    else:
        response = groq.generate(
            messages=formatted_messages,
            temperature=0,
            model=model
        )

    return response

def on_model_change():
    st.session_state.selected_model = st.session_state.model_selectbox

def main():
    st.title("Strawberry Groq DEMO")
    st.markdown(
        "This is a simple demo of the PocketGroq library's new 'Chain of Thought' functionality."
    )
    st.markdown(
        "GitHub Repository | "
        "Demo Video"
    )
    st.markdown("---")

    # Ввод API ключа
    with st.sidebar:
        st.header("Settings")
        api_key = st.text_input("Enter your Groq API Key:", type="password", value=st.session_state.api_key)

        if api_key:
            st.session_state.api_key = api_key
            try:
                # Получаем список доступных моделей и обновляем состояние сессии
                st.session_state.available_models = fetch_available_models(api_key)
                if st.session_state.selected_model not in st.session_state.available_models:
                    st.session_state.selected_model = st.session_state.available_models[0]
            except requests.RequestException as e:
                st.error(f"Error fetching models: {e}")

        # Выбор модели
        if st.session_state.available_models:
            st.selectbox(
                "Select a model:",
                st.session_state.available_models,
                index=st.session_state.available_models.index(st.session_state.selected_model),
                key="model_selectbox",
                on_change=on_model_change
            )
            st.write(f"Current model: {st.session_state.selected_model}")
        else:
            st.info("No available models. Please enter a valid API key.")

        # Переключатель Chain of Thought
        use_cot = st.checkbox("Use Chain of Thought", key="use_cot")

    # Основная область приложения
    st.markdown("---")

    # Отображение истории сообщений
    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.write(message["content"])

    # Ввод сообщения пользователем
    prompt = st.chat_input("What would you like to know?")

    if prompt:
        # Добавляем сообщение пользователя в историю
        st.session_state.messages.append({"role": "user", "content": prompt})
        with st.chat_message("user"):
            st.write(prompt)

        # Генерируем ответ от ассистента
        with st.chat_message("assistant"):
            if st.session_state.get("use_cot", False):
                st.write("Thinking step-by-step...")

            with st.spinner("Generating response..."):
                response = generate_response(prompt, st.session_state.get("use_cot", False), st.session_state.selected_model)

            st.write(response)

        # Добавляем ответ ассистента в историю
        st.session_state.messages.append({"role": "assistant", "content": response})

if __name__ == "__main__":
    main()