lorentz commited on
Commit
b11da8b
·
verified ·
1 Parent(s): 4c0413d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -29
app.py CHANGED
@@ -1,65 +1,103 @@
1
  import streamlit as st
2
- from streamlit_chat import message # Ensure you have streamlit_chat installed
3
  from langchain_openai import ChatOpenAI
4
  from langchain.chains import ConversationChain
5
- from langchain.chains.conversation.memory import ConversationSummaryMemory
 
 
 
 
6
 
7
- # Initialize session state variables if they don't exist
8
  if 'conversation' not in st.session_state:
9
- st.session_state['conversation'] = None
10
  if 'messages' not in st.session_state:
11
- st.session_state['messages'] = []
12
  if 'API_Key' not in st.session_state:
13
- st.session_state['API_Key'] = ''
14
 
15
- # Set page configuration
16
- st.set_page_config(page_title="ChatMate: Your AI Conversation Partner", page_icon=":robot_face:")
17
  st.markdown("<h1 style='text-align: center; color: navy;'>ChatMate</h1>", unsafe_allow_html=True)
18
- st.markdown("<h4 style='text-align: center;'>Engage with a cutting-edge language model.</h4>", unsafe_allow_html=True)
19
-
20
- # Sidebar for API Key Input
21
- st.sidebar.title("API Key 🔑")
22
- st.session_state['API_Key'] = st.sidebar.text_input(
23
- "Enter your OpenAI API Key:",
24
- type="password",
25
- help="Your API Key is safe with us and only used to power this conversation."
26
- )
27
-
28
- # Function to get a response from the model
29
- def get_response(user_input, api_key):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30
  if st.session_state['conversation'] is None:
 
31
  llm = ChatOpenAI(
32
  temperature=0,
33
  openai_api_key=api_key,
34
  model_name='gpt-3.5-turbo'
35
  )
 
36
  st.session_state['conversation'] = ConversationChain(
37
  llm=llm,
38
  verbose=True,
39
  memory=ConversationSummaryMemory(llm=llm)
40
  )
41
 
42
- response = st.session_state['conversation'].predict(input=user_input)
 
 
 
43
  return response
44
 
45
- # Main container for user input and displaying the conversation
 
46
  response_container = st.container()
 
47
  container = st.container()
48
 
 
49
  with container:
50
  with st.form(key='my_form', clear_on_submit=True):
51
- user_input = st.text_area("Ask me anything.", height=100)
52
  submit_button = st.form_submit_button(label='Send')
53
 
54
  if submit_button:
55
  st.session_state['messages'].append(user_input)
56
- model_response = get_response(user_input, st.session_state['API_Key'])
57
  st.session_state['messages'].append(model_response)
 
58
 
59
  with response_container:
60
- for i, msg in enumerate(st.session_state['messages']):
61
- # Alternate messages between user and AI
62
- is_user = (i % 2) == 0
63
- message(msg, is_user=is_user, key=f"msg_{i}")
 
64
 
65
- # Optionally, you can add a feature to clear the conversation or summarize it using sidebar options
 
1
  import streamlit as st
2
+ from streamlit_chat import message
3
  from langchain_openai import ChatOpenAI
4
  from langchain.chains import ConversationChain
5
+ from langchain.chains.conversation.memory import (ConversationBufferMemory,
6
+ ConversationSummaryMemory,
7
+ ConversationBufferWindowMemory
8
+
9
+ )
10
 
 
11
  if 'conversation' not in st.session_state:
12
+ st.session_state['conversation'] =None
13
  if 'messages' not in st.session_state:
14
+ st.session_state['messages'] =[]
15
  if 'API_Key' not in st.session_state:
16
+ st.session_state['API_Key'] =''
17
 
18
+ # Setting page title and header
19
+ st.set_page_config(page_title="ChatMate: Your Professional AI Conversation Partner Solution", page_icon=":robot_face:")
20
  st.markdown("<h1 style='text-align: center; color: navy;'>ChatMate</h1>", unsafe_allow_html=True)
21
+ st.markdown("<h4 style='text-align: center;'>A cutting-edge language model</h4>", unsafe_allow_html=True)
22
+ st.markdown("<p style='text-align: right'>By <a href='https://entzyeung.github.io/portfolio/index.html'>Lorentz Yeung</a></p>", unsafe_allow_html=True)
23
+
24
+ st.markdown("<p style='text-align: left;'>I am capable of recalling previous parts of our conversation, such as remembering your name if you share it with me.</p>", unsafe_allow_html=True)
25
+ st.session_state['API_Key']= st.text_input("First, to get it work, put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
26
+ st.markdown("<p style='text-align: left;'>Then Tell me how I can help:</p>", unsafe_allow_html=True)
27
+
28
+
29
+
30
+ # API Keys
31
+ # st.sidebar.text_input() will automatically update st.session_state['API_Key'] with the input value whenever the user types into the field.
32
+ st.sidebar.title("Introduction")
33
+ st.sidebar.markdown("""
34
+ ChatMate is an advanced conversational AI interface, expertly crafted to demonstrate the fusion of Streamlit's user-friendly design and OpenAI's powerful GPT-3.5 model. Here are its highlights:
35
+
36
+ <ul style='text-align: left;'>
37
+ <li><strong>Intuitive Interface</strong>: Built with Streamlit, ChatMate offers a clean, responsive user experience, allowing for natural dialogue with the AI.</li>
38
+ <li><strong>Advanced NLP</strong>: Incorporating OpenAI's most advanced GPT model, the app provides nuanced understanding and generation of human-like text, showcasing the model's impressive capabilities.</li>
39
+ <li><strong>State Management</strong>: Utilizes <code>ConversationChain</code> and <code>ConversationMemory</code> from <code>langchain</code> to preserve the context and flow, ensuring coherent and engaging interactions.</li>
40
+ <li><strong>Python Proficiency</strong>: The app's robust backend, written in Python, reflects the data scientist’s adeptness in programming and system design.</li>
41
+ <li><strong>Secure Interaction</strong>: Streamlit's session state management is used for secure API key handling and user input retention across sessions.</li>
42
+ </ul>
43
+
44
+ ChatMate is developed by Lorentz Yeung
45
+ """, unsafe_allow_html=True)
46
+
47
+ #st.session_state['API_Key']= st.sidebar.text_input("Put your OpenAI API Key here please, the system will enter for you automatically.",type="password")
48
+
49
+ # summarise_button = st.sidebar.button("Summarise the conversation", key="summarise")
50
+ #if summarise_button:
51
+ # summarise_placeholder = st.sidebar.write("Nice chatting with you my friend ❤️")
52
+
53
+
54
+
55
+ # Function to get response from the model
56
+ def getresponse(userInput, api_key):
57
+
58
  if st.session_state['conversation'] is None:
59
+
60
  llm = ChatOpenAI(
61
  temperature=0,
62
  openai_api_key=api_key,
63
  model_name='gpt-3.5-turbo'
64
  )
65
+
66
  st.session_state['conversation'] = ConversationChain(
67
  llm=llm,
68
  verbose=True,
69
  memory=ConversationSummaryMemory(llm=llm)
70
  )
71
 
72
+ response=st.session_state['conversation'].predict(input=userInput)
73
+ print(st.session_state['conversation'].memory.buffer)
74
+
75
+
76
  return response
77
 
78
+
79
+
80
  response_container = st.container()
81
+ # Here we will have a container for user input text box
82
  container = st.container()
83
 
84
+ # User input and response display
85
  with container:
86
  with st.form(key='my_form', clear_on_submit=True):
87
+ user_input = st.text_area("Ask me questions please", key='input', height=100)
88
  submit_button = st.form_submit_button(label='Send')
89
 
90
  if submit_button:
91
  st.session_state['messages'].append(user_input)
92
+ model_response=getresponse(user_input,st.session_state['API_Key'])
93
  st.session_state['messages'].append(model_response)
94
+
95
 
96
  with response_container:
97
+ for i in range(len(st.session_state['messages'])):
98
+ if (i % 2) == 0:
99
+ message(st.session_state['messages'][i], is_user=True, key=str(i) + '_user')
100
+ else:
101
+ message(st.session_state['messages'][i], key=str(i) + '_AI')
102
 
103
+