Spaces:
Running
Running
Commit
·
43a369b
1
Parent(s):
b4e2dd9
Add Tracking and Feedback
Browse files- agent.py +1 -0
- app.py +66 -0
- requirements.txt +1 -0
agent.py
CHANGED
@@ -187,6 +187,7 @@ def get_agent_config() -> OmegaConf:
|
|
187 |
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
188 |
'api_key': str(os.environ['VECTARA_API_KEY']),
|
189 |
'examples': os.environ.get('QUERY_EXAMPLES', None),
|
|
|
190 |
'demo_welcome': "Welcome to the Legal Assistant demo.",
|
191 |
'demo_description': "This demo can help you prepare for a court case by providing you information about past court cases in Alaska.",
|
192 |
})
|
|
|
187 |
'corpus_id': str(os.environ['VECTARA_CORPUS_ID']),
|
188 |
'api_key': str(os.environ['VECTARA_API_KEY']),
|
189 |
'examples': os.environ.get('QUERY_EXAMPLES', None),
|
190 |
+
'demo_name': "legal-agent",
|
191 |
'demo_welcome': "Welcome to the Legal Assistant demo.",
|
192 |
'demo_description': "This demo can help you prepare for a court case by providing you information about past court cases in Alaska.",
|
193 |
})
|
app.py
CHANGED
@@ -1,13 +1,54 @@
|
|
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 |
|
9 |
from agent import initialize_agent, get_agent_config
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
initial_prompt = "How can I help you today?"
|
12 |
|
13 |
def toggle_logs():
|
@@ -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
|
@@ -126,6 +185,13 @@ def launch_bot():
|
|
126 |
|
127 |
sys.stdout.flush()
|
128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
if __name__ == "__main__":
|
130 |
st.set_page_config(page_title="Legal Assistant", layout="wide")
|
131 |
launch_bot()
|
|
|
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 |
|
14 |
from agent import initialize_agent, get_agent_config
|
15 |
|
16 |
+
# Setup for HTTP API Calls to Amplitude Analytics
|
17 |
+
if 'device_id' not in st.session_state:
|
18 |
+
st.session_state.device_id = str(uuid.uuid4())
|
19 |
+
|
20 |
+
headers = {
|
21 |
+
'Content-Type': 'application/json',
|
22 |
+
'Accept': '*/*'
|
23 |
+
}
|
24 |
+
amp_api_key = os.getenv('AMPLITUDE_TOKEN')
|
25 |
+
|
26 |
+
def thumbs_feedback(feedback, **kwargs):
|
27 |
+
"""
|
28 |
+
Sends feedback to Amplitude Analytics
|
29 |
+
"""
|
30 |
+
data = {
|
31 |
+
"api_key": amp_api_key,
|
32 |
+
"events": [{
|
33 |
+
"device_id": st.session_state.device_id,
|
34 |
+
"event_type": "provided_feedback",
|
35 |
+
"event_properties": {
|
36 |
+
"Space Name": kwargs.get("demo_name", "Unknown"),
|
37 |
+
"query": kwargs.get("prompt", "No user input"),
|
38 |
+
"response": kwargs.get("response", "No chat response"),
|
39 |
+
"feedback": feedback["score"]
|
40 |
+
}
|
41 |
+
}]
|
42 |
+
}
|
43 |
+
response = requests.post('https://api2.amplitude.com/2/httpapi', headers=headers, data=json.dumps(data))
|
44 |
+
if response.status_code != 200:
|
45 |
+
print(f"Request failed with status code {response.status_code}. Response Text: {response.text}")
|
46 |
+
|
47 |
+
st.session_state.feedback_key += 1
|
48 |
+
|
49 |
+
if "feedback_key" not in st.session_state:
|
50 |
+
st.session_state.feedback_key = 0
|
51 |
+
|
52 |
initial_prompt = "How can I help you today?"
|
53 |
|
54 |
def toggle_logs():
|
|
|
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
|
|
|
185 |
|
186 |
sys.stdout.flush()
|
187 |
|
188 |
+
# Record user feedback
|
189 |
+
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != "How can I help you today?"):
|
190 |
+
streamlit_feedback(feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
|
191 |
+
kwargs = {"prompt": st.session_state.messages[-2]["content"],
|
192 |
+
"response": st.session_state.messages[-1]["content"],
|
193 |
+
"demo_name": cfg["demo_name"]})
|
194 |
+
|
195 |
if __name__ == "__main__":
|
196 |
st.set_page_config(page_title="Legal Assistant", layout="wide")
|
197 |
launch_bot()
|
requirements.txt
CHANGED
@@ -3,4 +3,5 @@ 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 |
git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git
|