david-oplatka commited on
Commit
0f3a499
β€’
1 Parent(s): 7d28805

Change Analytics to HTTP API

Browse files
Files changed (2) hide show
  1. app.py +29 -10
  2. requirements.txt +1 -1
app.py CHANGED
@@ -1,6 +1,9 @@
1
  from PIL import Image
2
  import sys
3
  import os
 
 
 
4
 
5
  import streamlit as st
6
  from streamlit_pills import pills
@@ -12,10 +15,14 @@ from datasets import load_dataset
12
  from vectara_agent.agent import AgentStatusType
13
  from agent import initialize_agent, get_agent_config
14
 
15
- from amplitude import Amplitude, BaseEvent
 
 
 
 
 
16
 
17
  initial_prompt = "How can I help you today?"
18
- client = Amplitude(os.getenv('AMPLITUDE_TOKEN'))
19
 
20
  def toggle_logs():
21
  st.session_state.show_logs = not st.session_state.show_logs
@@ -100,15 +107,27 @@ def launch_bot():
100
  if prompt:
101
  st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
102
  st.session_state.prompt = prompt # Save the prompt in session state
103
- client.track(
104
- BaseEvent(
105
- event_type="submitted_query",
106
- event_properties={
 
 
 
 
107
  "query": prompt
108
- }
109
- )
110
- )
111
- client.flush() # Send user query to Amplitude Analytics
 
 
 
 
 
 
 
 
112
  st.session_state.log_messages = []
113
  st.session_state.show_logs = False
114
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
 
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
 
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
+ device_id = str(uuid.uuid4())
20
+ headers = {
21
+ 'Content-Type': 'application/json',
22
+ 'Accept': '*/*'
23
+ }
24
 
25
  initial_prompt = "How can I help you today?"
 
26
 
27
  def toggle_logs():
28
  st.session_state.show_logs = not st.session_state.show_logs
 
107
  if prompt:
108
  st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
109
  st.session_state.prompt = prompt # Save the prompt in session state
110
+
111
+ # Send query to Amplitude Analytics
112
+ data = {
113
+ "api_key": "2a706490a2e8d62cdabf007d25408d34",
114
+ "events": [{
115
+ "device_id": device_id,
116
+ "event_type": "submitted_query",
117
+ "event_properties": {
118
  "query": prompt
119
+ }
120
+ }]
121
+ }
122
+
123
+ response = requests.post('https://api2.amplitude.com/2/httpapi',
124
+ headers=headers, data=json.dumps(data))
125
+
126
+ if response.status_code == 200:
127
+ print(f"DEBUG: Request successfully sent: {response.json()}")
128
+ else:
129
+ print(f"DEBUG: Request failed with status code {response.status_code}. Response Text: {response.text}")
130
+
131
  st.session_state.log_messages = []
132
  st.session_state.show_logs = False
133
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
requirements.txt CHANGED
@@ -4,5 +4,5 @@ python-dotenv==1.0.1
4
  streamlit==1.32.2
5
  streamlit_pills==0.3.0
6
  datasets==2.14.7
7
- amplitude-analytics==1.1.4
8
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git
 
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