ofermend commited on
Commit
2a73f32
·
1 Parent(s): 4f043ae

added amplitude and bug fixes

Browse files
Files changed (2) hide show
  1. app.py +18 -58
  2. requirements.txt +2 -0
app.py CHANGED
@@ -1,10 +1,8 @@
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
@@ -12,7 +10,7 @@ from streamlit_feedback import streamlit_feedback
12
  from vectara_agent.agent import AgentStatusType
13
 
14
  from agent import initialize_agent, get_agent_config
15
-
16
 
17
  initial_prompt = "How can I help you today?"
18
 
@@ -20,35 +18,6 @@ initial_prompt = "How can I help you today?"
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',
25
- 'Accept': '*/*'
26
- }
27
- amp_api_key = os.getenv('AMPLITUDE_TOKEN')
28
-
29
- def thumbs_feedback(feedback, **kwargs):
30
- """
31
- Sends feedback to Amplitude Analytics
32
- """
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": kwargs.get("demo_name", "Unknown"),
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"Request failed with status code {response.status_code}. Response Text: {response.text}")
49
-
50
- st.session_state.feedback_key += 1
51
-
52
  if "feedback_key" not in st.session_state:
53
  st.session_state.feedback_key = 0
54
 
@@ -105,7 +74,7 @@ def launch_bot():
105
  reset()
106
  st.rerun()
107
 
108
- st.markdown("---")
109
  st.markdown(
110
  "## How this works?\n"
111
  "This app was built with [Vectara](https://vectara.com).\n\n"
@@ -147,33 +116,31 @@ def launch_bot():
147
  with st.chat_message("assistant", avatar='🤖'):
148
  with st.spinner(st.session_state.thinking_message):
149
  res = st.session_state.agent.chat(st.session_state.prompt)
150
- res = res.replace('$', '\\$') # escape dollar sign for markdown
151
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
152
  st.session_state.messages.append(message)
153
  st.markdown(res)
154
 
155
- # Send query and response to Amplitude Analytics
156
- data = {
157
- "api_key": amp_api_key,
158
- "events": [{
159
- "device_id": st.session_state.device_id,
160
- "event_type": "submitted_query",
161
- "event_properties": {
162
- "Space Name": cfg['demo_name'],
163
- "query": st.session_state.messages[-2]["content"],
164
- "response": st.session_state.messages[-1]["content"]
165
- }
166
- }]
167
- }
168
- response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
169
- if response.status_code != 200:
170
- print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
171
 
172
  st.session_state.ex_prompt = None
173
  st.session_state.prompt = None
174
  st.session_state.first_turn = False
175
  st.rerun()
176
 
 
 
 
 
 
 
 
 
 
177
  log_placeholder = st.empty()
178
  with log_placeholder.container():
179
  if st.session_state.show_logs:
@@ -186,13 +153,6 @@ def launch_bot():
186
 
187
  sys.stdout.flush()
188
 
189
- # Record user feedback
190
- if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How can I help you today?"):
191
- streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
192
- kwargs = {"prompt": st.session_state.messages[-2]["content"],
193
- "response": st.session_state.messages[-1]["content"],
194
- "demo_name": cfg["demo_name"]})
195
-
196
  if __name__ == "__main__":
197
  st.set_page_config(page_title="Legal Assistant", layout="wide")
198
  launch_bot()
 
1
  from PIL import Image
2
  import sys
 
 
 
3
  import uuid
4
 
5
+
6
  import streamlit as st
7
  from streamlit_pills import pills
8
  from streamlit_feedback import streamlit_feedback
 
10
  from vectara_agent.agent import AgentStatusType
11
 
12
  from agent import initialize_agent, get_agent_config
13
+ from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
14
 
15
  initial_prompt = "How can I help you today?"
16
 
 
18
  if 'device_id' not in st.session_state:
19
  st.session_state.device_id = str(uuid.uuid4())
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  if "feedback_key" not in st.session_state:
22
  st.session_state.feedback_key = 0
23
 
 
74
  reset()
75
  st.rerun()
76
 
77
+ st.divider()
78
  st.markdown(
79
  "## How this works?\n"
80
  "This app was built with [Vectara](https://vectara.com).\n\n"
 
116
  with st.chat_message("assistant", avatar='🤖'):
117
  with st.spinner(st.session_state.thinking_message):
118
  res = st.session_state.agent.chat(st.session_state.prompt)
119
+ res = escape_dollars_outside_latex(res)
120
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
121
  st.session_state.messages.append(message)
122
  st.markdown(res)
123
 
124
+ send_amplitude_data(
125
+ user_query=st.session_state.messages[-2]["content"],
126
+ bot_response=st.session_state.messages[-1]["content"],
127
+ demo_name=cfg['demo_name']
128
+ )
 
 
 
 
 
 
 
 
 
 
 
129
 
130
  st.session_state.ex_prompt = None
131
  st.session_state.prompt = None
132
  st.session_state.first_turn = False
133
  st.rerun()
134
 
135
+ # Record user feedback
136
+ if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How can I help you today?"):
137
+ streamlit_feedback(
138
+ feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
139
+ kwargs = {"user_query": st.session_state.messages[-2]["content"],
140
+ "bot_response": st.session_state.messages[-1]["content"],
141
+ "demo_name": cfg["demo_name"]}
142
+ )
143
+
144
  log_placeholder = st.empty()
145
  with log_placeholder.container():
146
  if st.session_state.show_logs:
 
153
 
154
  sys.stdout.flush()
155
 
 
 
 
 
 
 
 
156
  if __name__ == "__main__":
157
  st.set_page_config(page_title="Legal Assistant", layout="wide")
158
  launch_bot()
requirements.txt CHANGED
@@ -5,4 +5,6 @@ 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
 
5
  streamlit_pills==0.3.0
6
  streamlit-feedback==0.1.3
7
  uuid==1.30
8
+ langdetect==1.0.9
9
+ langcodes==3.4.0
10
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git