import streamlit as st
from streamlit_chat import message
from langchain.llms import GooglePalm
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationSummaryMemory
import os # Import the os module
# Initialize session state variables
if 'conversation' not in st.session_state:
st.session_state['conversation'] = None
if 'messages' not in st.session_state:
st.session_state['messages'] = []
if 'API_Key' not in st.session_state:
st.session_state['API_Key'] = ''
# Streamlit UI
st.set_page_config(page_title="ChatGPT Clone", page_icon=":robot_face:")
st.markdown("
How can I assist you?
", unsafe_allow_html=True)
# Sidebar
st.sidebar.title(":D")
st.session_state['API_Key'] = st.sidebar.text_input("What's your API key?", type="password") # Accepts any API key here
summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
if summarise_button:
summarise_placeholder = st.sidebar.write("Nice chatting with you my friend <3️:\n\n" + st.session_state['conversation'].memory.buffer)
# Function to get response from Google Palm
def get_response(user_input, api_key):
if st.session_state['conversation'] is None:
google_api_key = os.environ.get('GOOGLE_API_KEY') # Get API key from environment variable
llm = GooglePalm(google_api_key=google_api_key, temperature=0)
st.session_state['conversation'] = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationSummaryMemory(llm=llm)
)
response = st.session_state['conversation'].predict(input=user_input)
return response
# Chat UI
response_container = st.container()
container = st.container()
with container:
with st.form(key='my_form', clear_on_submit=True):
user_input = st.text_area("Your question goes here:", key='input', height=100)
submit_button = st.form_submit_button(label='Send')
if submit_button:
st.session_state['messages'].append(user_input)
model_response = get_response(user_input, st.session_state['API_Key'])
st.session_state['messages'].append(model_response)
with response_container:
for i in range(len(st.session_state['messages'])):
if (i % 2) == 0:
message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
else:
message(st.session_state['messages'][i], key=str(i) + '_AI')