-
Notifications
You must be signed in to change notification settings - Fork 40
/
app.py
executable file
·95 lines (74 loc) · 2.43 KB
/
app.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from pathlib import Path
import streamlit as st
from streamlit_chat import message
import os
from gentopia.assembler.agent_assembler import AgentAssembler
@st.cache_resource
def get_configs(dir: Path):
assert dir.is_dir()
configs = []
for file in dir.iterdir():
if file.is_file() and file.suffix == '.yaml':
configs.append(file)
return configs
def on_btn_click():
del st.session_state.past[:]
del st.session_state.generated[:]
@st.cache_resource
def get_agent_selected(agent):
if 'agent' not in st.session_state or st.session_state['agent'].name != agent:
assembler = AgentAssembler(file=agent)
st.session_state['agent'] = assembler.get_agent()
return st.session_state['agent']
agent_selected = st.sidebar.selectbox(
'Select agent',
get_configs(Path('configs'))
)
agent = get_agent_selected(agent_selected)
st.session_state.setdefault(
'output',
[f'Hello, I am a bot named {agent_selected}.']
)
st.session_state.setdefault(
'user',
[]
)
st.title("Chat placeholder")
chat_placeholder = st.empty()
with chat_placeholder.container():
index = 0
while index < len(st.session_state['output']) or index < len(st.session_state['user']):
if index < len(st.session_state['output']):
message(st.session_state['output'][index], key=f"{index}_agent")
if index < len(st.session_state['user']):
message(
st.session_state['user'][index],
key=f"{index}",
is_user=True,
allow_html=True,
is_table=False
)
index += 1
st.button("Clear message", on_click=on_btn_click)
with st.container():
text_element = st.empty()
def on_input_change():
user_input = st.session_state.user_input
if not user_input:
return
st.session_state.user.append(user_input)
ans = ""
l = 0
for i in agent.llm.stream_chat_completion(user_input):
if not i.content:
continue
if i.content.startswith("[") or len(i.content) + l >= 80:
break
# ans += "\n\n"
# l = 0
l += len(i.content)
ans += i.content
text_element.text(ans)
st.session_state.output.append(ans)
text_element.text_area("")
st.text_input("User Input:", on_change=on_input_change, key="user_input")