-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.py
229 lines (192 loc) · 6.28 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#pylint: disable=invalid-name
"""
GenAI applications on enterprise data with Amazon Kendra, 🦜️🔗 LangChain and LLMs
https://aws.amazon.com/blogs/machine-learning/quickly-build-high-accuracy-generative-ai-applications-on-enterprise-data-using-amazon-kendra-langchain-and-large-language-models/
"""
import sys
import uuid
import streamlit as st
import kendra_chat_anthropic_nb as anthropic
import kendra_chat_flan_xl_nb as flanxl
import kendra_chat_flan_xxl_nb as flanxxl
import kendra_chat_open_ai_nb as openai
USER_ICON = "images/user-icon.png"
AI_ICON = "images/ai-icon.png"
MAX_HISTORY_LENGTH = 5
PROVIDER_MAP = {
'openai': 'Open AI',
'anthropic': 'Anthropic',
'flanxl': 'Flan XL',
'flanxxl': 'Flan XXL'
}
# Check if the user ID is already stored in the session state
if 'user_id' in st.session_state:
user_id = st.session_state['user_id']
# If the user ID is not yet stored in the session state, generate a random UUID
else:
user_id = str(uuid.uuid4())
st.session_state['user_id'] = user_id
if 'llm_chain' not in st.session_state:
if len(sys.argv) > 1:
if sys.argv[1] == 'anthropic':
st.session_state['llm_app'] = anthropic
st.session_state['llm_chain'] = anthropic.build_chain()
elif sys.argv[1] == 'flanxl':
st.session_state['llm_app'] = flanxl
st.session_state['llm_chain'] = flanxl.build_chain()
elif sys.argv[1] == 'flanxxl':
st.session_state['llm_app'] = flanxxl
st.session_state['llm_chain'] = flanxxl.build_chain()
elif sys.argv[1] == 'openai':
st.session_state['llm_app'] = openai
st.session_state['llm_chain'] = openai.build_chain()
else:
raise Exception("Unsupported LLM: ", sys.argv[1])
else:
raise Exception("Usage: streamlit run app.py <anthropic|flanxl|flanxxl|openai>")
if 'chat_history' not in st.session_state:
st.session_state['chat_history'] = []
if "chats" not in st.session_state:
st.session_state.chats = [
{
'id': 0,
'question': '',
'answer': ''
}
]
if "questions" not in st.session_state:
st.session_state.questions = []
if "answers" not in st.session_state:
st.session_state.answers = []
if "input" not in st.session_state:
st.session_state.input = ""
st.markdown("""
<style>
.block-container {
padding-top: 32px;
padding-bottom: 32px;
padding-left: 0;
padding-right: 0;
}
.element-container img {
background-color: #000000;
}
.main-header {
font-size: 24px;
}
</style>
""", unsafe_allow_html=True)
def write_logo():
"""
Displays logo
"""
_, col2, _ = st.columns([5, 1, 5])
with col2:
st.image(AI_ICON, use_column_width='always')
def write_top_bar():
"""
Displays top bar
"""
col1, col2, col3 = st.columns([1,10,2])
with col1:
st.image(AI_ICON, use_column_width='always')
with col2:
selected_provider = sys.argv[1]
provider = PROVIDER_MAP.get(selected_provider, selected_provider.capitalize())
header = f"An AI App powered by Amazon Kendra and {provider}!"
st.write(f"<h3 class='main-header'>{header}</h3>", unsafe_allow_html=True)
with col3:
clear = st.button("Clear Chat")
return clear
if write_top_bar():
st.session_state.questions = []
st.session_state.answers = []
st.session_state.input = ""
st.session_state["chat_history"] = []
def handle_input():
"""
Processes user input
"""
question_with_id = {
'question': st.session_state.input,
'id': len(st.session_state.questions)
}
st.session_state.questions.append(question_with_id)
chat_history = st.session_state["chat_history"]
if len(chat_history) == MAX_HISTORY_LENGTH:
chat_history = chat_history[:-1]
llm_chain = st.session_state['llm_chain']
chain = st.session_state['llm_app']
result = chain.run_chain(llm_chain, st.session_state.input, chat_history)
answer = result['answer']
chat_history.append((st.session_state.input, answer))
document_list = []
if 'source_documents' in result:
for d in result['source_documents']:
if not d.metadata['source'] in document_list:
document_list.append((d.metadata['source']))
st.session_state.answers.append({
'answer': result,
'sources': document_list,
'id': len(st.session_state.questions)
})
st.session_state.input = ""
def write_user_message(md):
"""
Displays the user message
"""
col1, col2 = st.columns([1,12])
with col1:
st.image(USER_ICON, use_column_width='always')
with col2:
st.warning(md['question'])
def render_result(result):
"""
Shows the results to the user
"""
answer, sources = st.tabs(['Answer', 'Sources'])
with answer:
render_answer(result['answer'])
with sources:
if 'source_documents' in result:
render_sources(result['source_documents'])
else:
render_sources([])
def render_answer(answer):
"""
Displays the answer to the user
"""
col1, col2 = st.columns([1,12])
with col1:
st.image(AI_ICON, use_column_width='always')
with col2:
st.info(answer['answer'])
def render_sources(sources):
"""
Display a list of sources to the user
"""
_, col2 = st.columns([1,12])
with col2:
with st.expander("Sources"):
for s in sources:
st.write(s)
# Each answer will have context of the question asked in order
# to associate the provided feedback with the respective question
def write_chat_message(md):
"""
Displays chat message
"""
chat = st.container()
with chat:
render_answer(md['answer'])
render_sources(md['sources'])
with st.container():
for (q, a) in zip(st.session_state.questions, st.session_state.answers):
write_user_message(q)
write_chat_message(a)
st.markdown('---')
ss_input = st.text_input(
"You are talking to an AI, ask any question.",
key="input",
on_change=handle_input
)