david-oplatka commited on
Commit
31be0ff
Β·
1 Parent(s): 6723b04

Add Feedback Tracking

Browse files
Files changed (3) hide show
  1. agent.py +1 -0
  2. app.py +54 -24
  3. requirements.txt +1 -0
agent.py CHANGED
@@ -107,6 +107,7 @@ def get_agent_config() -> OmegaConf:
107
  'corpus_ids': str(os.environ['VECTARA_CORPUS_IDS']).split(','),
108
  'api_keys': str(os.environ['VECTARA_API_KEYS']).split(','),
109
  'examples': os.environ.get('QUERY_EXAMPLES', None),
 
110
  'demo_welcome': "Welcome to the EV Assistant demo.",
111
  'demo_description': "This assistant can help you learn about electric vehicles in the United States, including how they work, the advantages of purchasing them, and reviews on the top choices.",
112
  })
 
107
  'corpus_ids': str(os.environ['VECTARA_CORPUS_IDS']).split(','),
108
  'api_keys': str(os.environ['VECTARA_API_KEYS']).split(','),
109
  'examples': os.environ.get('QUERY_EXAMPLES', None),
110
+ 'demo_name': "ev-assistant",
111
  'demo_welcome': "Welcome to the EV Assistant demo.",
112
  'demo_description': "This assistant can help you learn about electric vehicles in the United States, including how they work, the advantages of purchasing them, and reviews on the top choices.",
113
  })
app.py CHANGED
@@ -7,6 +7,7 @@ import uuid
7
 
8
  import streamlit as st
9
  from streamlit_pills import pills
 
10
 
11
  import sqlite3
12
  import pandas as pd
@@ -15,10 +16,9 @@ from datasets import load_dataset
15
  from vectara_agent.agent import AgentStatusType
16
  from agent import initialize_agent, get_agent_config
17
 
18
- # Setup for HTTP API Call to Amplitude Analytics
19
  if 'device_id' not in st.session_state:
20
  st.session_state.device_id = str(uuid.uuid4())
21
- print(f"DEBUG: Created new device id: {st.session_state.device_id}")
22
 
23
  headers = {
24
  'Content-Type': 'application/json',
@@ -26,6 +26,34 @@ headers = {
26
  }
27
  amp_api_key = os.getenv('AMPLITUDE_TOKEN')
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  initial_prompt = "How can I help you today?"
30
 
31
  def toggle_logs():
@@ -111,28 +139,6 @@ def launch_bot():
111
  if prompt:
112
  st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
113
  st.session_state.prompt = prompt # Save the prompt in session state
114
-
115
- # Send query to Amplitude Analytics
116
- data = {
117
- "api_key": amp_api_key,
118
- "events": [{
119
- "device_id": st.session_state.device_id,
120
- "event_type": "submitted_query",
121
- "event_properties": {
122
- "query": prompt,
123
- "Space Name": "ev-assistant"
124
- }
125
- }]
126
- }
127
-
128
- response = requests.post('https://api2.amplitude.com/2/httpapi',
129
- headers=headers, data=json.dumps(data))
130
-
131
- if response.status_code == 200:
132
- print(f"DEBUG: Request successfully sent: {response.json()}")
133
- else:
134
- print(f"DEBUG: Request failed with status code {response.status_code}. Response Text: {response.text}")
135
-
136
  st.session_state.log_messages = []
137
  st.session_state.show_logs = False
138
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
@@ -149,11 +155,35 @@ def launch_bot():
149
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
150
  st.session_state.messages.append(message)
151
  st.markdown(res)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  st.session_state.ex_prompt = None
153
  st.session_state.prompt = None
154
  st.session_state.first_turn = False
155
  st.rerun()
156
 
 
 
 
 
157
  log_placeholder = st.empty()
158
  with log_placeholder.container():
159
  if st.session_state.show_logs:
 
7
 
8
  import streamlit as st
9
  from streamlit_pills import pills
10
+ from streamlit_feedback import streamlit_feedback
11
 
12
  import sqlite3
13
  import pandas as pd
 
16
  from vectara_agent.agent import AgentStatusType
17
  from agent import initialize_agent, get_agent_config
18
 
19
+ # Setup for HTTP API Calls to Amplitude Analytics
20
  if 'device_id' not in st.session_state:
21
  st.session_state.device_id = str(uuid.uuid4())
 
22
 
23
  headers = {
24
  'Content-Type': 'application/json',
 
26
  }
27
  amp_api_key = os.getenv('AMPLITUDE_TOKEN')
28
 
29
+ def thumbs_feedback(feedback, **kwargs):
30
+ print(f'Debug: Feedback Received {feedback["score"]} FROM user question {kwargs.get("prompt", "No user input")} AND chat response {kwargs.get("response", "No chat response")}')
31
+
32
+ # Send feedback to Amplitude Analytics
33
+ data = {
34
+ "api_key": amp_api_key,
35
+ "events": [{
36
+ "device_id": st.session_state.device_id,
37
+ "event_type": "provided_feedback",
38
+ "event_properties": {
39
+ "Space Name": cfg["demo_name"],
40
+ "query": kwargs.get("prompt", "No user input"),
41
+ "response": kwargs.get("response", "No chat response"),
42
+ "feedback": feedback["score"]
43
+ }
44
+ }]
45
+ }
46
+ response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
47
+ if response.status_code == 200:
48
+ print(f"DEBUG: Request successfully sent: {response.json()}")
49
+ else:
50
+ print(f"DEBUG: Request failed with status code {response.status_code}. Response Text: {response.text}")
51
+
52
+ st.session_state.feedback_key += 1
53
+
54
+ if "feedback_key" not in st.session_state:
55
+ st.session_state.feedback_key = 0
56
+
57
  initial_prompt = "How can I help you today?"
58
 
59
  def toggle_logs():
 
139
  if prompt:
140
  st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
141
  st.session_state.prompt = prompt # Save the prompt in session state
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
142
  st.session_state.log_messages = []
143
  st.session_state.show_logs = False
144
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
 
155
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
156
  st.session_state.messages.append(message)
157
  st.markdown(res)
158
+
159
+ # Send query and response to Amplitude Analytics
160
+ data = {
161
+ "api_key": amp_api_key,
162
+ "events": [{
163
+ "device_id": st.session_state.device_id,
164
+ "event_type": "submitted_query",
165
+ "event_properties": {
166
+ "Space Name": cfg['demo_name'],
167
+ "query": st.session_state.messages[-2]["content"],
168
+ "response": st.session_state.messages[-1]["content"]
169
+ }
170
+ }]
171
+ }
172
+ response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
173
+ if response.status_code == 200:
174
+ print(f"DEBUG: Request successfully sent: {response.json()}")
175
+ else:
176
+ print(f"DEBUG: Request failed with status code {response.status_code}. Response Text: {response.text}")
177
+
178
  st.session_state.ex_prompt = None
179
  st.session_state.prompt = None
180
  st.session_state.first_turn = False
181
  st.rerun()
182
 
183
+ if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How may I help you?"):
184
+ streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
185
+ kwargs = {"prompt": st.session_state.messages[-2]["content"], "response": st.session_state.messages[-1]["content"]})
186
+
187
  log_placeholder = st.empty()
188
  with log_placeholder.container():
189
  if st.session_state.show_logs:
requirements.txt CHANGED
@@ -3,6 +3,7 @@ pydantic==1.10.15
3
  python-dotenv==1.0.1
4
  streamlit==1.32.2
5
  streamlit_pills==0.3.0
 
6
  datasets==2.14.7
7
  uuid==1.30
8
  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
  datasets==2.14.7
8
  uuid==1.30
9
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git