Skip to content

Commit 86dc981

Browse files
committed
Refactor duplicated code to function
1 parent 5b76466 commit 86dc981

File tree

1 file changed

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

1 file changed

+21
-28
lines changed

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

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ def context_showing_tab(contexts):
1515
st.header(tab_titles[i])
1616
st.write(tab_contents[tab_titles[i]])
1717

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+
1833
st.set_page_config(layout="wide")
1934
st.title("AWS Q&A Bot with Advanced RAG!") # page 제목
2035

@@ -100,7 +115,7 @@ def context_showing_tab(contexts):
100115
st.chat_message("assistant").write(answer)
101116

102117
with st.chat_message("assistant"):
103-
with st.expander("정확도 별 답변 보기 (semantic) ⬇️"):
118+
with st.expander("정확도 별 답변 보기 ⬇️"): # 정확도 별 답변 보기 (semantic)
104119
context_showing_tab(contexts1)
105120

106121
# with st.chat_message("assistant"):
@@ -116,7 +131,6 @@ def context_showing_tab(contexts):
116131
st.session_state.messages.append({"role": "assistant_context", "content": contexts1})
117132
# st.session_state.messages.append({"role": "assistant_context", "content": contexts2})
118133
# st.session_state.messages.append({"role": "assistant_context", "content": contexts3})
119-
# st.session_state.messages.append({"role": "assistant_context", "content": contexts4})
120134

121135
# Thinking을 complete로 수동으로 바꾸어 줌
122136
st_cb._complete_current_thought()
@@ -131,20 +145,7 @@ def context_showing_tab(contexts):
131145
for msg in st.session_state.messages:
132146
if msg["role"] == "assistant_column":
133147
answers = msg["content"]
134-
col1, col2, col3, col4 = st.columns(4)
135-
with col1:
136-
st.markdown('''### option 1 ''')
137-
st.write(answers[0])
138-
with col2:
139-
st.markdown('''### option 2 ''')
140-
st.write(answers[1])
141-
with col3:
142-
st.markdown('''### option 3 ''')
143-
st.write(answers[2])
144-
with col4:
145-
st.markdown('''### option 4 ''')
146-
st.write(answers[3])
147-
148+
multi_answer_column(answers)
148149
else:
149150
st.chat_message(msg["role"]).write(msg["content"])
150151

@@ -182,7 +183,6 @@ def context_showing_tab(contexts):
182183
st.write(answer1)
183184
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
184185
with col2:
185-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
186186
st_cb = StreamlitCallbackHandler(
187187
st.chat_message("assistant"),
188188
collapse_completed_thoughts=True
@@ -194,9 +194,8 @@ def context_showing_tab(contexts):
194194
reranker=False
195195
)[0]
196196
st.write(answer2)
197-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
197+
st_cb._complete_current_thought()
198198
with col3:
199-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
200199
st_cb = StreamlitCallbackHandler(
201200
st.chat_message("assistant"),
202201
collapse_completed_thoughts=True
@@ -208,9 +207,8 @@ def context_showing_tab(contexts):
208207
reranker=True
209208
)[0]
210209
st.write(answer3)
211-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
210+
st_cb._complete_current_thought()
212211
with col4:
213-
# Streamlit callback handler로 bedrock streaming 받아오는 컨테이너 설정
214212
st_cb = StreamlitCallbackHandler(
215213
st.chat_message("assistant"),
216214
collapse_completed_thoughts=True
@@ -222,14 +220,9 @@ def context_showing_tab(contexts):
222220
reranker=True
223221
)[0]
224222
st.write(answer4)
225-
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
223+
st_cb._complete_current_thought()
226224

227225
# Session 메세지 저장
228226
answer = [answer1, answer2, answer3, answer4]
229227
st.session_state.messages.append({"role": "assistant_column", "content": answer})
230-
231-
# UI 출력
232-
# st.chat_message("assistant").write(answer)
233-
# st.chat_message("assistant").write(contexts)
234-
# st.chat_message("assistant").write(ref)
235-
228+

0 commit comments

Comments
 (0)