Spaces:
Running
Running
david-oplatka
commited on
Commit
·
b8958bb
1
Parent(s):
152dfa1
Add Amplitude Analytics
Browse files- agent.py +1 -0
- app.py +66 -0
- requirements.txt +2 -0
agent.py
CHANGED
@@ -137,6 +137,7 @@ def get_agent_config() -> OmegaConf:
|
|
137 |
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
138 |
'api_key': str(os.environ['VECTARA_API_KEY']),
|
139 |
'examples': os.environ.get('QUERY_EXAMPLES', None),
|
|
|
140 |
'demo_welcome': "Welcome to the Financial Assistant demo.",
|
141 |
'demo_description': f"This assistant can help you with any questions about the financials of several companies:\n\n **{companies}**.\n"
|
142 |
})
|
|
|
137 |
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
138 |
'api_key': str(os.environ['VECTARA_API_KEY']),
|
139 |
'examples': os.environ.get('QUERY_EXAMPLES', None),
|
140 |
+
'demo_name': "finance-chat",
|
141 |
'demo_welcome': "Welcome to the Financial Assistant demo.",
|
142 |
'demo_description': f"This assistant can help you with any questions about the financials of several companies:\n\n **{companies}**.\n"
|
143 |
})
|
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
|
|
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 |
|
@@ -109,6 +150,24 @@ def launch_bot():
|
|
109 |
message = {"role": "assistant", "content": res, "avatar": '🤖'}
|
110 |
st.session_state.messages.append(message)
|
111 |
st.markdown(res)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
112 |
st.session_state.ex_prompt = None
|
113 |
st.session_state.prompt = None
|
114 |
st.session_state.first_turn = False
|
@@ -124,6 +183,13 @@ def launch_bot():
|
|
124 |
if len(st.session_state.log_messages) > 0:
|
125 |
st.button("Show Logs", on_click=toggle_logs)
|
126 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
sys.stdout.flush()
|
128 |
|
129 |
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 |
|
|
|
150 |
message = {"role": "assistant", "content": res, "avatar": '🤖'}
|
151 |
st.session_state.messages.append(message)
|
152 |
st.markdown(res)
|
153 |
+
|
154 |
+
# Send query and response to Amplitude Analytics
|
155 |
+
data = {
|
156 |
+
"api_key": amp_api_key,
|
157 |
+
"events": [{
|
158 |
+
"device_id": st.session_state.device_id,
|
159 |
+
"event_type": "submitted_query",
|
160 |
+
"event_properties": {
|
161 |
+
"Space Name": cfg['demo_name'],
|
162 |
+
"query": st.session_state.messages[-2]["content"],
|
163 |
+
"response": st.session_state.messages[-1]["content"]
|
164 |
+
}
|
165 |
+
}]
|
166 |
+
}
|
167 |
+
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
168 |
+
if response.status_code != 200:
|
169 |
+
print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
|
170 |
+
|
171 |
st.session_state.ex_prompt = None
|
172 |
st.session_state.prompt = None
|
173 |
st.session_state.first_turn = False
|
|
|
183 |
if len(st.session_state.log_messages) > 0:
|
184 |
st.button("Show Logs", on_click=toggle_logs)
|
185 |
|
186 |
+
# Record user feedback
|
187 |
+
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How can I help you today?"):
|
188 |
+
streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
|
189 |
+
kwargs = {"prompt": st.session_state.messages[-2]["content"],
|
190 |
+
"response": st.session_state.messages[-1]["content"],
|
191 |
+
"demo_name": cfg["demo_name"]})
|
192 |
+
|
193 |
sys.stdout.flush()
|
194 |
|
195 |
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
|