david-oplatka commited on
Commit
0194c2e
1 Parent(s): d1788ed

Add Amplitude Analytics

Browse files
Files changed (3) hide show
  1. agent.py +1 -0
  2. app.py +66 -0
  3. requirements.txt +2 -0
agent.py CHANGED
@@ -107,6 +107,7 @@ def get_agent_config() -> OmegaConf:
107
  'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
108
  'api_key': str(os.environ['VECTARA_API_KEY']),
109
  'examples': os.environ.get('QUERY_EXAMPLES', None),
 
110
  'demo_welcome': "Welcome to the Justice Harvard e-learning assistant demo.",
111
  'demo_description': "Ask questions about the Justice Harvard class and get answers from an expert teaching assistant.",
112
  'style': teaching_styles[0],
 
107
  'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
108
  'api_key': str(os.environ['VECTARA_API_KEY']),
109
  'examples': os.environ.get('QUERY_EXAMPLES', None),
110
+ 'demo_name': "Justice-Harvard",
111
  'demo_welcome': "Welcome to the Justice Harvard e-learning assistant demo.",
112
  'demo_description': "Ask questions about the Justice Harvard class and get answers from an expert teaching assistant.",
113
  'style': teaching_styles[0],
app.py CHANGED
@@ -1,8 +1,13 @@
1
  from PIL import Image
2
  import sys
 
 
 
 
3
 
4
  import streamlit as st
5
  from streamlit_pills import pills
 
6
 
7
  from vectara_agent.agent import AgentStatusType
8
 
@@ -10,6 +15,42 @@ from agent import initialize_agent, get_agent_config, teaching_styles, languages
10
 
11
  initial_prompt = "How can I help you today?"
12
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  def toggle_logs():
14
  st.session_state.show_logs = not st.session_state.show_logs
15
 
@@ -135,6 +176,24 @@ def launch_bot():
135
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
136
  st.session_state.messages.append(message)
137
  st.markdown(res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  st.session_state.ex_prompt = None
139
  st.session_state.prompt = None
140
  st.session_state.first_turn = False
@@ -150,6 +209,13 @@ def launch_bot():
150
  if len(st.session_state.log_messages) > 0:
151
  st.button("Show Logs", on_click=toggle_logs)
152
 
 
 
 
 
 
 
 
153
  sys.stdout.flush()
154
 
155
  if __name__ == "__main__":
 
1
  from PIL import Image
2
  import sys
3
+ import os
4
+ import requests
5
+ import json
6
+ import uuid
7
 
8
  import streamlit as st
9
  from streamlit_pills import pills
10
+ from streamlit_feedback import streamlit_feedback
11
 
12
  from vectara_agent.agent import AgentStatusType
13
 
 
15
 
16
  initial_prompt = "How can I help you today?"
17
 
18
+ # Setup for HTTP API Calls to Amplitude Analytics
19
+ if 'device_id' not in st.session_state:
20
+ st.session_state.device_id = str(uuid.uuid4())
21
+
22
+ headers = {
23
+ 'Content-Type': 'application/json',
24
+ 'Accept': '*/*'
25
+ }
26
+ amp_api_key = os.getenv('AMPLITUDE_TOKEN')
27
+
28
+ def thumbs_feedback(feedback, **kwargs):
29
+ """
30
+ Sends feedback to Amplitude Analytics
31
+ """
32
+ data = {
33
+ "api_key": amp_api_key,
34
+ "events": [{
35
+ "device_id": st.session_state.device_id,
36
+ "event_type": "provided_feedback",
37
+ "event_properties": {
38
+ "Space Name": kwargs.get("demo_name", "Unknown"),
39
+ "query": kwargs.get("prompt", "No user input"),
40
+ "response": kwargs.get("response", "No chat response"),
41
+ "feedback": feedback["score"]
42
+ }
43
+ }]
44
+ }
45
+ response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
46
+ if response.status_code != 200:
47
+ print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
48
+
49
+ st.session_state.feedback_key += 1
50
+
51
+ if "feedback_key" not in st.session_state:
52
+ st.session_state.feedback_key = 0
53
+
54
  def toggle_logs():
55
  st.session_state.show_logs = not st.session_state.show_logs
56
 
 
176
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
177
  st.session_state.messages.append(message)
178
  st.markdown(res)
179
+
180
+ # Send query and response to Amplitude Analytics
181
+ data = {
182
+ "api_key": amp_api_key,
183
+ "events": [{
184
+ "device_id": st.session_state.device_id,
185
+ "event_type": "submitted_query",
186
+ "event_properties": {
187
+ "Space Name": cfg['demo_name'],
188
+ "query": st.session_state.messages[-2]["content"],
189
+ "response": st.session_state.messages[-1]["content"]
190
+ }
191
+ }]
192
+ }
193
+ response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
194
+ if response.status_code != 200:
195
+ print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
196
+
197
  st.session_state.ex_prompt = None
198
  st.session_state.prompt = None
199
  st.session_state.first_turn = False
 
209
  if len(st.session_state.log_messages) > 0:
210
  st.button("Show Logs", on_click=toggle_logs)
211
 
212
+ # Record user feedback
213
+ if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How can I help you today?"):
214
+ streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
215
+ kwargs = {"prompt": st.session_state.messages[-2]["content"],
216
+ "response": st.session_state.messages[-1]["content"],
217
+ "demo_name": cfg["demo_name"]})
218
+
219
  sys.stdout.flush()
220
 
221
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -3,4 +3,6 @@ pydantic==1.10.15
3
  python-dotenv==1.0.1
4
  streamlit==1.32.2
5
  streamlit_pills==0.3.0
 
 
6
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git
 
3
  python-dotenv==1.0.1
4
  streamlit==1.32.2
5
  streamlit_pills==0.3.0
6
+ streamlit_feedback==0.1.3
7
+ uuid==1.30
8
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git