"""Streamlit app for the ChatGPT clone."""

import dotenv
import langchain
import streamlit as st
import streamlit_chat

dotenv.load_dotenv(dotenv.find_dotenv(), override=True)

st.set_page_config(
    page_title='You Custom Assistant',
    page_icon='🤖'
)
st.subheader('Your Custom ChatGPT 🤖')

chat = langchain.chat_models.ChatOpenAI(
    model_name='gpt-3.5-turbo', temperature=0.5)

# creating the messages (chat history) in the Streamlit session state
if 'messages' not in st.session_state:
    st.session_state.messages = []

# creating the sidebar
with st.sidebar:
    # streamlit text input widget for the system message (role)
    system_message = st.text_input(label='System role')
    # streamlit text input widget for the user message
    user_prompt = st.text_input(label='Send a message')

    if system_message:
        if not any(isinstance(x, langchain.schema.SystemMessage) for x in st.session_state.messages):
            st.session_state.messages.append(
                langchain.schema.SystemMessage(content=system_message)
            )

    # if the user entered a question
    if user_prompt:
        st.session_state.messages.append(
            langchain.schema.HumanMessage(content=user_prompt)
        )

        with st.spinner('Working on your request ...'):
            # creating the ChatGPT response
            response = chat(st.session_state.messages)

        # adding the response's content to the session state
        st.session_state.messages.append(
            langchain.schema.AIMessage(content=response.content))


# adding a default SystemMessage if the user didn't entered one
if len(st.session_state.messages) >= 1:
    if not isinstance(st.session_state.messages[0], langchain.schema.SystemMessage):
        st.session_state.messages.insert(0, langchain.schema.SystemMessage(
            content='You are a helpful assistant.'))

# displaying the messages (chat history)
for i, msg in enumerate(st.session_state.messages[1:]):
    if i % 2 == 0:
        streamlit_chat.message(msg.content, is_user=True,
                               key=f'{i} + 🤓')  # user's question
    else:
        streamlit_chat.message(msg.content, is_user=False,
                               key=f'{i} +  🤖')  # ChatGPT response

langchain.schema.AIMessage langchain.schema.HumanMessage langchain.schema.SystemMessage langchain.chat_models.ChatOpenAI
[((178, 246), 'streamlit.set_page_config', 'st.set_page_config', ([], {'page_title': '"""You Custom Assistant"""', 'page_icon': '"""🤖"""'}), "(page_title='You Custom Assistant', page_icon='🤖')\n", (196, 246), True, 'import streamlit as st\n'), ((257, 294), 'streamlit.subheader', 'st.subheader', (['"""Your Custom ChatGPT 🤖"""'], {}), "('Your Custom ChatGPT 🤖')\n", (269, 294), True, 'import streamlit as st\n'), ((303, 380), 'langchain.chat_models.ChatOpenAI', 'langchain.chat_models.ChatOpenAI', ([], {'model_name': '"""gpt-3.5-turbo"""', 'temperature': '(0.5)'}), "(model_name='gpt-3.5-turbo', temperature=0.5)\n", (335, 380), False, 'import langchain\n'), ((140, 160), 'dotenv.find_dotenv', 'dotenv.find_dotenv', ([], {}), '()\n', (158, 160), False, 'import dotenv\n'), ((657, 691), 'streamlit.text_input', 'st.text_input', ([], {'label': '"""System role"""'}), "(label='System role')\n", (670, 691), True, 'import streamlit as st\n'), ((765, 802), 'streamlit.text_input', 'st.text_input', ([], {'label': '"""Send a message"""'}), "(label='Send a message')\n", (778, 802), True, 'import streamlit as st\n'), ((2004, 2069), 'streamlit_chat.message', 'streamlit_chat.message', (['msg.content'], {'is_user': '(True)', 'key': 'f"""{i} + 🤓"""'}), "(msg.content, is_user=True, key=f'{i} + 🤓')\n", (2026, 2069), False, 'import streamlit_chat\n'), ((2138, 2205), 'streamlit_chat.message', 'streamlit_chat.message', (['msg.content'], {'is_user': '(False)', 'key': 'f"""{i} +  🤖"""'}), "(msg.content, is_user=False, key=f'{i} +  🤖')\n", (2160, 2205), False, 'import streamlit_chat\n'), ((1172, 1222), 'langchain.schema.HumanMessage', 'langchain.schema.HumanMessage', ([], {'content': 'user_prompt'}), '(content=user_prompt)\n', (1201, 1222), False, 'import langchain\n'), ((1247, 1288), 'streamlit.spinner', 'st.spinner', (['"""Working on your request ..."""'], {}), "('Working on your request ...')\n", (1257, 1288), True, 'import streamlit as st\n'), ((1505, 1557), 'langchain.schema.AIMessage', 'langchain.schema.AIMessage', ([], {'content': 'response.content'}), '(content=response.content)\n', (1531, 1557), False, 'import langchain\n'), ((1794, 1864), 'langchain.schema.SystemMessage', 'langchain.schema.SystemMessage', ([], {'content': '"""You are a helpful assistant."""'}), "(content='You are a helpful assistant.')\n", (1824, 1864), False, 'import langchain\n'), ((991, 1045), 'langchain.schema.SystemMessage', 'langchain.schema.SystemMessage', ([], {'content': 'system_message'}), '(content=system_message)\n', (1021, 1045), False, 'import langchain\n')]