Skip to content

Commit 272555a

Browse files
author
Yoonseo Kim
committed
Add previous context log to session state
1 parent a4256f8 commit 272555a

File tree

1 file changed

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

1 file changed

+50
-55
lines changed

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

Lines changed: 50 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
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+
518
st.set_page_config(layout="wide")
619
st.title("AWS Q&A Bot with Advanced RAG!") # page 제목
720

@@ -17,7 +30,6 @@
1730
st.session_state.showing_option = "Separately"
1831

1932
with st.sidebar: # Sidebar 모델 옵션
20-
# st.title("Set showing method 👇")
2133
with st.container(height=170):
2234
st.radio(
2335
"Set showing method 👇",
@@ -27,21 +39,35 @@
2739
)
2840

2941
st.title("Set parameter for your Bot 👇")
30-
parent = st.toggle("Parent_docs", disabled=st.session_state.showing_option=="All at once")
42+
43+
# semantic = st.toggle("Semantic", disabled=st.session_state.showing_option=="All at once")
44+
# lexical = st.toggle("Lexical", disabled=st.session_state.showing_option=="All at once")
45+
46+
# hybrid = st.slider('Alpha value of Hybrid Search: lexical(0.0) / semantic(1.0)', 0.0, 1.0, 0.5)
47+
3148
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)
49+
50+
# ragfusion = st.toggle("RAG-Fusion", disabled=st.session_state.showing_option=="All at once")
51+
# hyde = st.toggle("HyDE", disabled=st.session_state.showing_option=="All at once")
52+
53+
parent = st.toggle("Parent_docs", disabled=st.session_state.showing_option=="All at once")
3554

3655
### 1) 'Separately' 옵션 선택한 경우 ###
3756
if st.session_state.showing_option == "Separately":
3857
if "messages" not in st.session_state:
3958
st.session_state["messages"] = [
4059
{"role": "assistant", "content": "How can I help you?"}
4160
]
61+
# 지난 답변 출력
4262
for msg in st.session_state.messages:
43-
st.chat_message(msg["role"]).write(msg["content"])
44-
63+
# 지난 답변에 대한 컨텍스트 출력
64+
if msg["role"] == "assistant_context":
65+
with st.chat_message("assistant"):
66+
with st.expander("정확도 별 답변 보기 ⬇️"):
67+
context_showing_tab(contexts=msg["content"])
68+
else:
69+
st.chat_message(msg["role"]).write(msg["content"])
70+
4571
# 유저가 쓴 chat을 query라는 변수에 담음
4672
query = st.chat_input("Search documentation")
4773
if query:
@@ -75,52 +101,22 @@
75101

76102
with st.chat_message("assistant"):
77103
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]])
104+
context_showing_tab(contexts1)
105+
106+
# with st.chat_message("assistant"):
107+
# with st.expander("정확도 별 답변 보기 (lexical) ⬇️"):
108+
# context_showing_tab(contexts2)
89109

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]])
110+
# with st.chat_message("assistant"):
111+
# with st.expander("정확도 별 답변 보기 (reranker) ⬇️"):
112+
# context_showing_tab(contexts3)
117113

118114
# Session 메세지 저장
119115
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})
116+
st.session_state.messages.append({"role": "assistant_context", "content": contexts1})
117+
# st.session_state.messages.append({"role": "assistant_context", "content": contexts2})
118+
# st.session_state.messages.append({"role": "assistant_context", "content": contexts3})
119+
# st.session_state.messages.append({"role": "assistant_context", "content": contexts4})
124120

125121
# Thinking을 complete로 수동으로 바꾸어 줌
126122
st_cb._complete_current_thought()
@@ -131,9 +127,11 @@
131127
st.session_state["messages"] = [
132128
{"role": "assistant", "content": "How can I help you?"}
133129
]
130+
# 지난 답변 출력
134131
for msg in st.session_state.messages:
132+
# 지난 답변에 대한 컨텍스트 출력
135133
st.chat_message(msg["role"]).write(msg["content"])
136-
134+
137135
# 유저가 쓴 chat을 query라는 변수에 담음
138136
query = st.chat_input("Search documentation")
139137
if query:
@@ -165,7 +163,6 @@
165163
parent=False,
166164
reranker=False
167165
)[0]
168-
# st.subheader("parent=False, reranker=False ⬇️")
169166
st.write(answer)
170167
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
171168
with col2:
@@ -180,7 +177,6 @@
180177
parent=True,
181178
reranker=False
182179
)[0]
183-
# st.subheader("parent=True, reranker=False ⬇️")
184180
st.write(answer)
185181
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
186182
with col3:
@@ -195,7 +191,6 @@
195191
parent=False,
196192
reranker=True
197193
)[0]
198-
# st.subheader("parent=False, reranker=True ⬇️")
199194
st.write(answer)
200195
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
201196
with col4:
@@ -210,7 +205,6 @@
210205
parent=True,
211206
reranker=True
212207
)[0]
213-
# st.subheader("parent=True, reranker=True ⬇️")
214208
st.write(answer)
215209
st_cb._complete_current_thought() # Thinking을 complete로 수동으로 바꾸어 줌
216210

@@ -221,4 +215,5 @@
221215
# UI 출력
222216
# st.chat_message("assistant").write(answer)
223217
# st.chat_message("assistant").write(contexts)
224-
# st.chat_message("assistant").write(ref)
218+
# st.chat_message("assistant").write(ref)
219+

0 commit comments

Comments
 (0)