ashupanda123 commited on
Commit
bfbc8d4
·
verified ·
1 Parent(s): 509dcfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -32
app.py CHANGED
@@ -1,53 +1,42 @@
1
  import streamlit as st
2
-
3
  from langchain_openai import ChatOpenAI
 
4
 
5
- from langchain.schema import (
6
- AIMessage,
7
- HumanMessage,
8
- SystemMessage
9
- )
10
-
11
- #From here down is all the StreamLit UI
12
  st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
13
  st.header("Hey, I'm your Chat GPT")
14
 
15
-
16
-
17
  if "sessionMessages" not in st.session_state:
18
- st.session_state.sessionMessages = [
19
  SystemMessage(content="You are a helpful assistant.")
20
  ]
21
 
22
-
23
-
24
- def load_answer(question):
25
-
26
  st.session_state.sessionMessages.append(HumanMessage(content=question))
27
 
28
- assistant_answer = chat.invoke(st.session_state.sessionMessages )
29
-
30
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
31
 
32
  return assistant_answer.content
33
 
 
 
 
 
34
 
35
- def get_text():
36
- input_text = st.text_input("You: ")
37
- return input_text
38
-
39
-
40
- chat = ChatOpenAI(temperature=0)
41
-
42
-
43
-
44
-
45
- user_input=get_text()
46
- submit = st.button('Generate')
47
 
 
48
  if submit:
49
-
50
- response = load_answer(user_input)
51
  st.subheader("Answer:")
52
-
53
  st.write(response)
 
 
 
 
1
  import streamlit as st
 
2
  from langchain_openai import ChatOpenAI
3
+ from langchain.schema import AIMessage, HumanMessage, SystemMessage
4
 
5
+ # Streamlit UI configuration
 
 
 
 
 
 
6
  st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
7
  st.header("Hey, I'm your Chat GPT")
8
 
9
+ # Initialize session state for messages
 
10
  if "sessionMessages" not in st.session_state:
11
+ st.session_state.sessionMessages = [
12
  SystemMessage(content="You are a helpful assistant.")
13
  ]
14
 
15
+ # Function to load the assistant's answer
16
+ def load_answer(system_message, question):
17
+ # Clear previous session messages and add the new system message
18
+ st.session_state.sessionMessages = [SystemMessage(content=system_message)]
19
  st.session_state.sessionMessages.append(HumanMessage(content=question))
20
 
21
+ # Invoke the chat model and append the assistant's response
22
+ assistant_answer = chat.invoke(st.session_state.sessionMessages)
23
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
24
 
25
  return assistant_answer.content
26
 
27
+ # Input field for the system message
28
+ system_message = st.text_input("System Message:", value="You are a helpful assistant.")
29
+ # Input field for the user question
30
+ user_input = st.text_input("You: ")
31
 
32
+ # Create a button to generate the response
33
+ submit = st.button('Generate')
 
 
 
 
 
 
 
 
 
 
34
 
35
+ # Generate and display the response if the button is clicked
36
  if submit:
37
+ response = load_answer(system_message, user_input)
 
38
  st.subheader("Answer:")
 
39
  st.write(response)
40
+
41
+ # Initialize the chat model
42
+ chat = ChatOpenAI(temperature=0)