Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,60 @@
|
|
| 1 |
-
#
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
# import openai from langchain_openai
|
| 5 |
-
from langchain_openai import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
# Function to return the response
|
| 8 |
def load_answer(question):
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# App UI starts here
|
| 15 |
-
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
|
| 16 |
-
st.header("LangChain Demo")
|
| 17 |
|
| 18 |
-
# Gets the user input
|
| 19 |
def get_text():
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
return input_text
|
| 23 |
|
|
|
|
|
|
|
|
|
|
| 24 |
# Get user input
|
| 25 |
user_input=get_text()
|
| 26 |
|
| 27 |
-
# Get response
|
| 28 |
-
response = load_answer(user_input)
|
| 29 |
-
|
| 30 |
# Add button to generate response
|
| 31 |
-
submit = st.button('Generate')
|
| 32 |
|
| 33 |
-
# If generate button is clicked
|
| 34 |
if submit:
|
| 35 |
-
|
|
|
|
| 36 |
st.subheader("Answer:")
|
| 37 |
|
| 38 |
st.write(response)
|
|
|
|
| 1 |
+
#
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
# import openai from langchain_openai
|
| 5 |
+
from langchain_openai import ChatOpenAI
|
| 6 |
+
|
| 7 |
+
# import HumanMessage,SystemMessage and AIMessage from the 'schema' module of the 'langchain' library.
|
| 8 |
+
from langchain.schema import (
|
| 9 |
+
AIMessage,
|
| 10 |
+
HumanMessage,
|
| 11 |
+
SystemMessage
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# From here down is all the StreamLit UI
|
| 15 |
+
st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
|
| 16 |
+
st.header("Hey, I'm your Chat GPT")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# If "sessionMessages" not in st.session_state, then create a list of SystemMessage
|
| 20 |
+
if "sessionMessages" not in st.session_state:
|
| 21 |
+
st.session_state.sessionMessages = [
|
| 22 |
+
SystemMessage(content="You are a helpful assistant.")
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
|
|
|
|
| 26 |
def load_answer(question):
|
| 27 |
+
# Append the question to the sessionMessages list
|
| 28 |
+
st.session_state.sessionMessages.append(HumanMessage(content=question))
|
| 29 |
|
| 30 |
+
# Invoke the chat with the sessionMessages list
|
| 31 |
+
assistant_answer = chat.invoke(st.session_state.sessionMessages )
|
| 32 |
+
|
| 33 |
+
# Append the assistant's answer to the sessionMessages list
|
| 34 |
+
st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
|
| 35 |
+
|
| 36 |
+
# Return the assistant's answer
|
| 37 |
+
return assistant_answer.content
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
|
|
|
|
| 40 |
def get_text():
|
| 41 |
+
|
| 42 |
+
# Get the user input
|
| 43 |
+
input_text = st.text_input("You: ")
|
| 44 |
return input_text
|
| 45 |
|
| 46 |
+
# Initialize the ChatOpenAI object
|
| 47 |
+
chat = ChatOpenAI(temperature=0)
|
| 48 |
+
|
| 49 |
# Get user input
|
| 50 |
user_input=get_text()
|
| 51 |
|
|
|
|
|
|
|
|
|
|
| 52 |
# Add button to generate response
|
| 53 |
+
submit = st.button('Generate')
|
| 54 |
|
|
|
|
| 55 |
if submit:
|
| 56 |
+
# Get response from the user input and display it
|
| 57 |
+
response = load_answer(user_input)
|
| 58 |
st.subheader("Answer:")
|
| 59 |
|
| 60 |
st.write(response)
|