tharungajula2 commited on
Commit
8eea387
·
1 Parent(s): 6ea9636

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -18
app.py CHANGED
@@ -1,55 +1,85 @@
1
 
2
- import streamlit as st
 
 
3
 
 
 
4
 
 
5
  from langchain.chat_models import ChatOpenAI
 
 
6
  from langchain.schema import (
7
  AIMessage,
8
  HumanMessage,
9
  SystemMessage
10
  )
11
 
12
- # From here down is all the StreamLit UI.
13
- st.set_page_config(page_title="Langchain Prototype", page_icon=":robot:")
14
- st.header("Hey, I'm your Chat GPT")
15
 
 
 
16
 
 
 
17
 
 
 
18
  if "sessionMessages" not in st.session_state:
19
- st.session_state.sessionMessages = [
20
  SystemMessage(content="You are a helpful assistant.")
21
  ]
22
 
 
 
 
23
 
24
-
25
  def load_answer(question):
26
-
 
27
  st.session_state.sessionMessages.append(HumanMessage(content=question))
28
-
29
- assistant_answer = chat(st.session_state.sessionMessages )
30
-
 
 
31
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
32
-
 
33
  return assistant_answer.content
34
 
35
-
36
  def get_text():
37
- input_text = st.text_input("You: ", key= input)
38
  return input_text
39
 
40
-
41
  chat = ChatOpenAI(temperature=0)
42
 
 
 
 
43
 
 
 
44
 
45
-
46
- user_input=get_text()
47
  submit = st.button('Generate')
48
 
 
49
  if submit:
50
-
51
  response = load_answer(user_input)
 
 
52
  st.subheader("Answer:")
 
53
 
54
- st.write(response,key= 1)
 
 
55
 
 
1
 
2
+ # -----------------------------------------
3
+ # LIBRARY IMPORTS
4
+ # -----------------------------------------
5
 
6
+ # Streamlit is a library for creating web apps with Python.
7
+ import streamlit as st
8
 
9
+ # langchain.chat_models provides classes related to chat models.
10
  from langchain.chat_models import ChatOpenAI
11
+
12
+ # langchain.schema provides schemas for system, human, and AI messages.
13
  from langchain.schema import (
14
  AIMessage,
15
  HumanMessage,
16
  SystemMessage
17
  )
18
 
19
+ # -----------------------------------------
20
+ # STREAMLIT UI CONFIGURATION
21
+ # -----------------------------------------
22
 
23
+ # Set the Streamlit page title and icon.
24
+ st.set_page_config(page_title="Langchain Prototype", page_icon=":robot:")
25
 
26
+ # Display a header for the application.
27
+ st.header("Hey, I'm your Chat GPT")
28
 
29
+ # Initialize the session state variable 'sessionMessages' if it doesn't exist.
30
+ # This variable will store the conversation.
31
  if "sessionMessages" not in st.session_state:
32
+ st.session_state.sessionMessages = [
33
  SystemMessage(content="You are a helpful assistant.")
34
  ]
35
 
36
+ # -----------------------------------------
37
+ # FUNCTION DEFINITIONS
38
+ # -----------------------------------------
39
 
40
+ # This function fetches a response to a given question from the ChatOpenAI model.
41
  def load_answer(question):
42
+
43
+ # Append the human's message to the session's message list.
44
  st.session_state.sessionMessages.append(HumanMessage(content=question))
45
+
46
+ # Get the assistant's response using the chat function.
47
+ assistant_answer = chat(st.session_state.sessionMessages)
48
+
49
+ # Append the assistant's message to the session's message list.
50
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
51
+
52
+ # Return the content of the assistant's answer.
53
  return assistant_answer.content
54
 
55
+ # Function to get text input from the user via Streamlit's interface.
56
  def get_text():
57
+ input_text = st.text_input("You: ", key=input)
58
  return input_text
59
 
60
+ # Initialize the ChatOpenAI model with temperature set to 0.
61
  chat = ChatOpenAI(temperature=0)
62
 
63
+ # -----------------------------------------
64
+ # STREAMLIT INTERACTION HANDLING
65
+ # -----------------------------------------
66
 
67
+ # Get the user input from the Streamlit interface.
68
+ user_input = get_text()
69
 
70
+ # Display a button on the Streamlit page.
 
71
  submit = st.button('Generate')
72
 
73
+ # If the 'Generate' button is clicked:
74
  if submit:
75
+ # Fetch the response from the chat function.
76
  response = load_answer(user_input)
77
+
78
+ # Display the response below the subheader "Answer:".
79
  st.subheader("Answer:")
80
+ st.write(response, key=1)
81
 
82
+ # -----------------------------------------
83
+ # END OF STREAMLIT APPLICATION
84
+ # -----------------------------------------
85