-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_frontend.py
More file actions
183 lines (153 loc) Β· 7.18 KB
/
Copy pathchat_frontend.py
File metadata and controls
183 lines (153 loc) Β· 7.18 KB
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
import json
import requests
import streamlit as st
import uuid
import yaml
config = yaml.safe_load(open("./config.yaml"))
API_URL = config["rag_api_endpoint"] + "chat-response"
FEEDBACK_URL = config["rag_api_endpoint"] + "feedback"
API_KEY = config["api_key"]
def display_response(raw_text: str):
# Decode escaped characters like \n and \"
decoded_text = raw_text.encode('utf-8').decode('utf-8').replace('"', "")
st.markdown(decoded_text)
def send_feedback_callback(timestamp: int, rating: str, feedback_text: str = ""):
"""Callback function for feedback buttons."""
if send_feedback(st.session_state.session_id, timestamp, rating, feedback_text):
if rating in ["thumbs_up", "thumbs_down"]:
thumb_key = f"{timestamp}_thumb"
st.session_state.feedback_sent.add(thumb_key)
# Store which thumb was pressed
rating_key = f"{timestamp}_rating"
st.session_state[rating_key] = rating
else:
text_key = f"{timestamp}_text"
st.session_state.feedback_sent.add(text_key)
st.success("Thanks for your feedback!")
def send_feedback(session_id: str, timestamp: int, rating: str, feedback_text: str = ""):
"""Send feedback to the API."""
headers = {"x-api-key": API_KEY}
data = {
"session_id": session_id,
"timestamp": timestamp,
"rating": rating,
"feedback_text": feedback_text
}
try:
response = requests.post(FEEDBACK_URL, json=data, headers=headers)
response.raise_for_status()
return True
except Exception as e:
st.error(f"Error sending feedback: {e}")
return False
# Initialize session state
if "messages" not in st.session_state:
st.session_state.messages = []
if "session_id" not in st.session_state:
st.session_state.session_id = str(uuid.uuid4())
if "feedback_sent" not in st.session_state:
st.session_state.feedback_sent = set()
# Streamlit App Setup
st.set_page_config(page_title="Internet2 Chatbot PoC", page_icon="π¬ ")
st.title("Internet2 Chatbot PoC")
with st.sidebar:
st.markdown("""
*Some questions you can ask me*
- What workloads can I run on AWS?
- What workloads can I run on GCP?
- What did Lee Pang say about Amazon Omics?
- What is AWS Omics?
- How do I convince my leadership of the importance of FinOps practices?
- Who has a Cloud Center of Excellence?
- How are people doing account provisioning?
- I've got a consultant coming in to install Control Tower for us, but they don't have any higher ed experience. What questions should I be asking to make sure I don't have to redo the work later?
- Do I have to set up a cloud networking architecture for each platform or is there a single strategy to rule them all?
""")
# Add session reset button
if st.button("New Conversation"):
st.session_state.messages = []
st.session_state.session_id = str(uuid.uuid4())
st.session_state.feedback_sent = set()
st.rerun()
# Display the chat messages
for i, msg in enumerate(st.session_state.messages):
role = "You" if msg["role"] == "user" else "Bot"
with st.chat_message(msg["role"]):
display_response(msg["content"])
# Add feedback buttons for assistant messages
if msg["role"] == "assistant" and "timestamp" in msg:
timestamp = msg["timestamp"]
thumb_key = f"{timestamp}_thumb"
text_key = f"{timestamp}_text"
rating_key = f"{timestamp}_rating"
col1, col2, col3 = st.columns([1, 1, 8])
with col1:
if thumb_key in st.session_state.feedback_sent:
if st.session_state.get(rating_key) == "thumbs_up":
st.markdown('<div style="background-color: #90EE90; padding: 5px; border-radius: 5px; text-align: center;">π</div>', unsafe_allow_html=True)
else:
st.text("π")
else:
if st.button("π", key=f"up_{i}", on_click=lambda t=timestamp: send_feedback_callback(t, "thumbs_up")):
pass
with col2:
if thumb_key in st.session_state.feedback_sent:
if st.session_state.get(rating_key) == "thumbs_down":
st.markdown('<div style="background-color: #FFB6C1; padding: 5px; border-radius: 5px; text-align: center;">π</div>', unsafe_allow_html=True)
else:
st.text("π")
else:
if st.button("π", key=f"down_{i}", on_click=lambda t=timestamp: send_feedback_callback(t, "thumbs_down")):
pass
# Feedback text input
if text_key not in st.session_state.feedback_sent:
feedback_text = st.text_input("Additional feedback (optional):", key=f"feedback_{i}")
if st.button("Submit Feedback", key=f"submit_{i}") and feedback_text:
send_feedback_callback(timestamp, "text_feedback", feedback_text)
else:
st.text("β Text feedback submitted")
# User input field
user_input = st.chat_input("Type your question here...")
if user_input:
# Add user message to history
st.session_state.messages.append({"role": "user", "content": user_input})
with st.chat_message("user"):
display_response(user_input)
# Send to API with session ID
headers = {"x-api-key": API_KEY}
data = {
"query": user_input,
"session_id": st.session_state.session_id
}
try:
response = requests.post(API_URL, json=data, headers=headers)
response.raise_for_status()
response_data = json.loads(response.text)
bot_reply = response_data.get("response", response.text)
timestamp = response_data.get("timestamp")
# Update session ID if provided
if "session_id" in response_data:
st.session_state.session_id = response_data["session_id"]
except Exception as e:
bot_reply = f"Error: {e}"
timestamp = None
# Add bot response to history with timestamp
message_data = {"role": "assistant", "content": bot_reply}
if timestamp:
message_data["timestamp"] = timestamp
st.session_state.messages.append(message_data)
with st.chat_message("assistant"):
display_response(bot_reply)
# Add feedback buttons for the new response
if timestamp:
col1, col2, col3 = st.columns([1, 1, 8])
with col1:
if st.button("π", key=f"up_new", on_click=lambda: send_feedback_callback(timestamp, "thumbs_up")):
pass
with col2:
if st.button("π", key=f"down_new", on_click=lambda: send_feedback_callback(timestamp, "thumbs_down")):
pass
# Feedback text input
feedback_text = st.text_input("Additional feedback (optional):", key=f"feedback_new")
if st.button("Submit Feedback", key=f"submit_new") and feedback_text:
send_feedback_callback(timestamp, "text_feedback", feedback_text)