-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit.py
48 lines (44 loc) · 1.5 KB
/
streamlit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import streamlit as st
import time
import traceback
from app import super_graph
from langchain_core.messages.human import HumanMessage
# import langchain
# langchain.debug = True
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
def main():
st.title('Chat')
user_input = st.chat_input("Your message")
if user_input:
start = time.time()
try:
result = ""
for s in super_graph.stream(
{
"messages": [
HumanMessage(
content=user_input
)
],
},
{"recursion_limit": 50},
):
if "__end__" not in s:
st.write(f"Receiving: {s}")
st.write("---")
item = next(iter(s.values()))
if "messages" in item:
result = item["messages"][-1].content
st.session_state['chat_history'].append({"user": user_input, "assistant": result})
st.write(time.time() - start)
except Exception as e:
st.write(f"Exception: {e}")
st.write(f"Traceback: {traceback.format_exc()}")
for chat in st.session_state['chat_history']:
with st.chat_message(chat["user"]):
st.markdown(chat["user"])
with st.chat_message("assistant"):
st.markdown(chat["assistant"])
if __name__ == "__main__":
main()