Skip to content

Commit 752d619

Browse files
authored
Merge pull request #73 from ottlseo/master
Add a new streamlit UI for previous log
2 parents 8bc19d0 + 754510a commit 752d619

File tree

1 file changed

+87
-81
lines changed
  • genai/aws-gen-ai-kr/20_applications/02_qa_chatbot/04_web_ui

1 file changed

+87
-81
lines changed

genai/aws-gen-ai-kr/20_applications/02_qa_chatbot/04_web_ui/streamlit.py

Lines changed: 87 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
import bedrock as glib # 로컬 라이브러리 스크립트에 대한 참조
33
from langchain.callbacks import StreamlitCallbackHandler
44

5+
def context_showing_tab(contexts):
6+
tab_titles = []
7+
tab_contents = {}
8+
for i, context in enumerate(contexts):
9+
title = str(context[0])
10+
tab_titles.append(title)
11+
tab_contents[title] = context[1][0]
12+
tabs = st.tabs(tab_titles)
13+
for i, tab in enumerate(tabs):
14+
with tab:
15+
st.header(tab_titles[i])
16+
st.write(tab_contents[tab_titles[i]])
17+
18+
def multi_answer_column(answers):
19+
col1, col2, col3, col4 = st.columns(4)
20+
with col1:
21+
st.markdown('''### option 1 ''')
22+
st.write(answers[0])
23+
with col2:
24+
st.markdown('''### option 2 ''')
25+
st.write(answers[1])
26+
with col3:
27+
st.markdown('''### option 3 ''')
28+
st.write(answers[2])
29+
with col4:
30+
st.markdown('''### option 4 ''')
31+
st.write(answers[3])
32+
533
st.set_page_config(layout="wide")
634
st.title("AWS Q&A Bot with Advanced RAG!") # page 제목
735

@@ -17,7 +45,6 @@
1745
st.session_state.showing_option = "Separately"
1846

1947
with st.sidebar: # Sidebar 모델 옵션
20-
# st.title("Set showing method 👇")
2148
with st.container(height=170):
2249
st.radio(
2350
"Set showing method 👇",
@@ -27,21 +54,37 @@
2754
)
2855

2956
st.title("Set parameter for your Bot 👇")
30-
parent = st.toggle("Parent_docs", disabled=st.session_state.showing_option=="All at once")
57+
58+
# semantic = st.toggle("Semantic", disabled=st.session_state.showing_option=="All at once")
59+
# lexical = st.toggle("Lexical", disabled=st.session_state.showing_option=="All at once")
60+
61+
# hybrid = st.slider('Alpha value of Hybrid Search: lexical(0.0) / semantic(1.0)', 0.0, 1.0, 0.5)
62+
3163
reranker = st.toggle("Reranker", disabled=st.session_state.showing_option=="All at once")
32-
# hyde = st.toggle("HyDE")
33-
# custom_model = st.toggle("Custom Model")
34-
# alpha = st.slider('Alpha value of Hybrid Search: lexical(0.0) / semantic(1.0)', 0.0, 1.0, 0.5)
64+
65+
# ragfusion = st.toggle("RAG-Fusion", disabled=st.session_state.showing_option=="All at once")
66+
# hyde = st.toggle("HyDE", disabled=st.session_state.showing_option=="All at once")
67+
68+
parent = st.toggle("Parent_docs", disabled=st.session_state.showing_option=="All at once")
3569

3670
### 1) 'Separately' 옵션 선택한 경우 ###
3771
if st.session_state.showing_option == "Separately":
3872
if "messages" not in st.session_state:
3973
st.session_state["messages"] = [
4074
{"role": "assistant", "content": "How can I help you?"}
4175
]
76+
# 지난 답변 출력
4277
for msg in st.session_state.messages:
43-
st.chat_message(msg["role"]).write(msg["content"])
44-
78+
# 지난 답변에 대한 컨텍스트 출력
79+
if msg["role"] == "assistant_context":
80+
with st.chat_message("assistant"):
81+
with st.expander("정확도 별 답변 보기 ⬇️"):
82+
context_showing_tab(contexts=msg["content"])
83+
if msg["role"] == "assistant_column":
84+
st.chat_message("assistant").write(msg["content"][0])
85+
else:
86+
st.chat_message(msg["role"]).write(msg["content"])
87+
4588
# 유저가 쓴 chat을 query라는 변수에 담음
4689
query = st.chat_input("Search documentation")
4790
if query:
@@ -74,53 +117,22 @@
74117
st.chat_message("assistant").write(answer)
75118

76119
with st.chat_message("assistant"):
77-
with st.expander("정확도 별 답변 보기 (semantic) ⬇️"):
78-
tab_titles = []
79-
tab_contents = {}
80-
for i, context in enumerate(contexts1):
81-
title = str(context[0])
82-
tab_titles.append(title)
83-
tab_contents[title] = context[1][0]
84-
tabs = st.tabs(tab_titles)
85-
for i, tab in enumerate(tabs):
86-
with tab:
87-
st.header(tab_titles[i])
88-
st.write(tab_contents[tab_titles[i]])
120+
with st.expander("정확도 별 답변 보기 ⬇️"): # 정확도 별 답변 보기 (semantic)
121+
context_showing_tab(contexts1)
122+
123+
# with st.chat_message("assistant"):
124+
# with st.expander("정확도 별 답변 보기 (lexical) ⬇️"):
125+
# context_showing_tab(contexts2)
89126

90-
with st.chat_message("assistant"):
91-
with st.expander("정확도 별 답변 보기 (lexical) ⬇️"):
92-
tab_titles = []
93-
tab_contents = {}
94-
for i, context in enumerate(contexts2):
95-
title = str(context[0])
96-
tab_titles.append(title)
97-
tab_contents[title] = context[1][0]
98-
tabs = st.tabs(tab_titles)
99-
for i, tab in enumerate(tabs):
100-
with tab:
101-
st.header(tab_titles[i])
102-
st.write(tab_contents[tab_titles[i]])
103-
104-
with st.chat_message("assistant"):
105-
with st.expander("정확도 별 답변 보기 (reranker) ⬇️"):
106-
tab_titles = []
107-
tab_contents = {}
108-
for i, context in enumerate(contexts3):
109-
title = str(context[0])
110-
tab_titles.append(title)
111-
tab_contents[title] = context[1][0]
112-
tabs = st.tabs(tab_titles)
113-
for i, tab in enumerate(tabs):
114-
with tab:
115-
st.header(tab_titles[i])
116-
st.write(tab_contents[tab_titles[i]])
127+
# with st.chat_message("assistant"):
128+
# with st.expander("정확도 별 답변 보기 (reranker) ⬇️"):
129+
# context_showing_tab(contexts3)
117130

118131
# Session 메세지 저장
119132
st.session_state.messages.append({"role": "assistant", "content": answer})
120-
# st.session_state.messages.append({"role": "assistant", "content": contexts1})
121-
# st.session_state.messages.append({"role": "assistant", "content": contexts2})
122-
# st.session_state.messages.append({"role": "assistant", "content": contexts3})
123-
# st.session_state.messages.append({"role": "assistant", "content": contexts4})
133+
st.session_state.messages.append({"role": "assistant_context", "content": contexts1})
134+
# st.session_state.messages.append({"role": "assistant_context", "content": contexts2})
135+
# st.session_state.messages.append({"role": "assistant_context", "content": contexts3})
124136

125137
# Thinking을 complete로 수동으로 바꾸어 줌
126138
st_cb._complete_current_thought()
@@ -131,9 +143,14 @@
131143
st.session_state["messages"] = [
132144
{"role": "assistant", "content": "How can I help you?"}
133145
]
146+
# 지난 답변 출력
134147
for msg in st.session_state.messages:
135-
st.chat_message(msg["role"]).write(msg["content"])
136-
148+
if msg["role"] == "assistant_column":
149+
answers = msg["content"]
150+
multi_answer_column(answers)
151+
else:
152+
st.chat_message(msg["role"]).write(msg["content"])
153+
137154
# 유저가 쓴 chat을 query라는 변수에 담음
138155
query = st.chat_input("Search documentation")
139156
if query:
@@ -145,80 +162,69 @@
145162

146163
col1, col2, col3, col4 = st.columns(4)
147164
with col1:
148-
st.markdown('''#### parent=:red[False], reranker=:red[False]''')
165+
st.markdown('''### option 1 ''')
149166
with col2:
150-
st.markdown('''#### parent=:green[True], reranker=:red[False]''')
167+
st.markdown('''### option 2 ''')
151168
with col3:
152-
st.markdown('''#### parent=:red[False], reranker=:green[True]''')
169+
st.markdown('''### option 3 ''')
153170
with col4:
154-
st.markdown('''#### parent=:green[True], reranker=:green[True]''')
171+
st.markdown('''### option 4 ''')
155172

156173
with col1:
157174
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
158175
st_cb = StreamlitCallbackHandler(
159176
st.chat_message("assistant"),
160177
collapse_completed_thoughts=True
161178
)
162-
answer = glib.invoke(
179+
answer1 = glib.invoke(
163180
query=query,
164181
streaming_callback=st_cb,
165182
parent=False,
166183
reranker=False
167184
)[0]
168-
# st.subheader("parent=False, reranker=False ⬇️")
169-
st.write(answer)
185+
st.write(answer1)
170186
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
171187
with col2:
172-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
173188
st_cb = StreamlitCallbackHandler(
174189
st.chat_message("assistant"),
175190
collapse_completed_thoughts=True
176191
)
177-
answer = glib.invoke(
192+
answer2 = glib.invoke(
178193
query=query,
179194
streaming_callback=st_cb,
180195
parent=True,
181196
reranker=False
182197
)[0]
183-
# st.subheader("parent=True, reranker=False ⬇️")
184-
st.write(answer)
185-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
198+
st.write(answer2)
199+
st_cb._complete_current_thought()
186200
with col3:
187-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
188201
st_cb = StreamlitCallbackHandler(
189202
st.chat_message("assistant"),
190203
collapse_completed_thoughts=True
191204
)
192-
answer = glib.invoke(
205+
answer3 = glib.invoke(
193206
query=query,
194207
streaming_callback=st_cb,
195208
parent=False,
196209
reranker=True
197210
)[0]
198-
# st.subheader("parent=False, reranker=True ⬇️")
199-
st.write(answer)
200-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
211+
st.write(answer3)
212+
st_cb._complete_current_thought()
201213
with col4:
202-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
203214
st_cb = StreamlitCallbackHandler(
204215
st.chat_message("assistant"),
205216
collapse_completed_thoughts=True
206217
)
207-
answer = glib.invoke(
218+
answer4 = glib.invoke(
208219
query=query,
209220
streaming_callback=st_cb,
210221
parent=True,
211222
reranker=True
212223
)[0]
213-
# st.subheader("parent=True, reranker=True ⬇️")
214-
st.write(answer)
215-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
224+
st.write(answer4)
225+
st_cb._complete_current_thought()
216226

217227
# Session 메세지 저장
218-
st.session_state.messages.append({"role": "assistant", "content": answer})
219-
# st.session_state.messages.append({"role": "assistant", "content": contexts})
220-
221-
# UI 출력
222-
# st.chat_message("assistant").write(answer)
223-
# st.chat_message("assistant").write(contexts)
224-
# st.chat_message("assistant").write(ref)
228+
answer = [answer1, answer2, answer3, answer4]
229+
st.session_state.messages.append({"role": "assistant_column", "content": answer})
230+

0 commit comments

Comments
 (0)