import streamlit as st # import the Streamlit library from langchain.chains import ConversationChain from langchain.llms import OpenAIChat # import OpenAI model from langchain.chains.conversation.memory import ConversationEntityMemory from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE import pickle # Initialize session State st.session_state["show_new_chat_button"] = False if "id" not in st.session_state: st.session_state["id"] = 0 if "conversation" not in st.session_state: st.session_state.conversation = [] if "input" not in st.session_state: st.session_state["input"] = "" if "stored_session" not in st.session_state: st.session_state["stored_session"]={} if "input_temp" not in st.session_state: st.session_state["input_temp"] = "" # Set the title of the Streamlit app st.title("HomemadeGPT 🤖 - The custom chatbot you need") # Historique des conversations conversation_history = st.empty() API_KEY = st.sidebar.text_input("API-Key", type="password") with st.sidebar.expander(" 🛠️ Settings ", expanded=False): # Option to preview memory store if 'entity_memory' in st.session_state: if st.checkbox("Preview memory store"): st.write(st.session_state.entity_memory.store) # Option to preview memory buffer if st.checkbox("Preview memory buffer"): st.write(st.session_state.entity_memory.buffer) MODEL = st.selectbox(label='Model', options=['gpt-3.5-turbo','gpt-4','gpt-4-32k','text-davinci-003','text-davinci-002']) K = st.number_input(' (#)Summary of prompts to consider',min_value=3,max_value=1000) def clear_text(): """ A function that clears the text in the input box when the user type a search query and press enter """ st.session_state["input_temp"] = st.session_state["input"] st.session_state["input"] = "" def get_text(): """ Get the user input text. Returns: (str): The text entered by the user """ input_text = st.text_input("You: ", key="input", placeholder = "Your AI assistant ! Ask me anything...", label_visibility='hidden',on_change=clear_text) return input_text def new_chat(): """ Clears session state and start a new chat """ save_current_chat() clean_screen() clean_memory() st.session_state["id"] += 1 def clean_screen(): """ Clears the current conversation screen """ st.session_state.conversation = [] st.session_state["input"] = "" st.session_state["input_temp"] = "" def clean_memory(): """ Clears the current conversation memory """ st.session_state.entity_memory.store = {} st.session_state.entity_memory.buffer.clear() def save_current_chat(): """ Save the current chat in st.session_state["stored_session"] """ saved_dict=dict() saved_dict['conversation'] = st.session_state['conversation'] saved_dict['conversation_memory'] = pickle.dumps(st.session_state.entity_memory) st.session_state["stored_session"][st.session_state["id"]]=saved_dict def resume_chat(session_id): """ Clears session state and start a new chat """ save_current_chat() clean_screen() clean_memory() st.session_state["id"] = session_id st.session_state["conversation"] = st.session_state["stored_session"][session_id]["conversation"] st.session_state.entity_memory = pickle.loads(st.session_state["stored_session"][session_id]["conversation_memory"]) st.session_state["show_new_chat_button"] = True def show_conv(): """ Render the current conversation in html """ conversation_html = "" for entry in st.session_state.conversation: if 'user' in entry: conversation_html += f'
{entry["chatbot"]}