ofermend commited on
Commit
9b43a21
1 Parent(s): a9d3e7f

added amplitude

Browse files
Files changed (3) hide show
  1. agent.py +6 -2
  2. app.py +18 -49
  3. requirements.txt +2 -0
agent.py CHANGED
@@ -134,7 +134,10 @@ def create_assistant_tools(cfg):
134
  whats_new,
135
  ]
136
  ] +
137
- tools_factory.get_llama_index_tools("tavily_research", "TavilyToolSpec", api_key=cfg.tavily_api_key) +
 
 
 
138
  tools_factory.standard_tools() +
139
  tools_factory.guardrail_tools() +
140
  [ask_hackernews]
@@ -149,7 +152,8 @@ def initialize_agent(_cfg, update_func = None):
149
  Don't use text like "Source" which doesn't tell the user what the link is about.
150
  - Don't include external links in your responses unless the user asks for them.
151
  - Always call the ask_hackernews tool first as your primary source of information.
152
- you can use the tavily search tool to gain additional information if needed for follow up questions about Hacker News topic.
 
153
  """
154
 
155
  agent = Agent(
 
134
  whats_new,
135
  ]
136
  ] +
137
+ tools_factory.get_llama_index_tools(
138
+ "tavily_research", "TavilyToolSpec",
139
+ tool_name_prefix="tavily", api_key=cfg.tavily_api_key
140
+ ) +
141
  tools_factory.standard_tools() +
142
  tools_factory.guardrail_tools() +
143
  [ask_hackernews]
 
152
  Don't use text like "Source" which doesn't tell the user what the link is about.
153
  - Don't include external links in your responses unless the user asks for them.
154
  - Always call the ask_hackernews tool first as your primary source of information.
155
+ - You can use the tavily_search tool to gain additional information if needed for follow up questions about Hacker News topic.
156
+ - When including information or links provided by the tavily_search tool, make sure to notify the user in your response that this is not based on Hacker News stories.
157
  """
158
 
159
  agent = Agent(
app.py CHANGED
@@ -11,6 +11,8 @@ 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
  initial_prompt = "How can I help you today?"
@@ -23,30 +25,6 @@ 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
@@ -68,7 +46,6 @@ def update_func(status_type: AgentStatusType, msg: str):
68
  output = f"{status_type.value} - {msg}"
69
  st.session_state.log_messages.append(output)
70
 
71
-
72
  def launch_bot():
73
  def reset():
74
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
@@ -104,7 +81,7 @@ def launch_bot():
104
  reset()
105
  st.rerun()
106
 
107
- st.markdown("---")
108
  st.markdown(
109
  "## How this works?\n"
110
  "This app was built with [Vectara](https://vectara.com).\n\n"
@@ -146,33 +123,31 @@ def launch_bot():
146
  with st.chat_message("assistant", avatar='🤖'):
147
  with st.spinner(st.session_state.thinking_message):
148
  res = st.session_state.agent.chat(st.session_state.prompt)
149
- res = res.replace('$', '\\$') # escape dollar sign for markdown
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
174
  st.rerun()
175
 
 
 
 
 
 
 
 
 
 
176
  log_placeholder = st.empty()
177
  with log_placeholder.container():
178
  if st.session_state.show_logs:
@@ -183,12 +158,6 @@ def launch_bot():
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
 
 
11
 
12
  from vectara_agent.agent import AgentStatusType
13
 
14
+ from utils import thumbs_feedback, escape_dollars_outside_latex, send_amplitude_data
15
+
16
  from agent import initialize_agent, get_agent_config
17
 
18
  initial_prompt = "How can I help you today?"
 
25
  'Content-Type': 'application/json',
26
  'Accept': '*/*'
27
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
  if "feedback_key" not in st.session_state:
30
  st.session_state.feedback_key = 0
 
46
  output = f"{status_type.value} - {msg}"
47
  st.session_state.log_messages.append(output)
48
 
 
49
  def launch_bot():
50
  def reset():
51
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "🦖"}]
 
81
  reset()
82
  st.rerun()
83
 
84
+ st.divider()
85
  st.markdown(
86
  "## How this works?\n"
87
  "This app was built with [Vectara](https://vectara.com).\n\n"
 
123
  with st.chat_message("assistant", avatar='🤖'):
124
  with st.spinner(st.session_state.thinking_message):
125
  res = st.session_state.agent.chat(st.session_state.prompt)
126
+ res = escape_dollars_outside_latex(res)
127
  message = {"role": "assistant", "content": res, "avatar": '🤖'}
128
  st.session_state.messages.append(message)
129
  st.markdown(res)
130
 
131
+ send_amplitude_data(
132
+ user_query=st.session_state.messages[-2]["content"],
133
+ bot_response=st.session_state.messages[-1]["content"],
134
+ demo_name=cfg['demo_name']
135
+ )
 
 
 
 
 
 
 
 
 
 
 
136
 
137
  st.session_state.ex_prompt = None
138
  st.session_state.prompt = None
139
  st.session_state.first_turn = False
140
  st.rerun()
141
 
142
+ # Record user feedback
143
+ if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
144
+ streamlit_feedback(
145
+ feedback_type="thumbs", on_submit = thumbs_feedback, key = st.session_state.feedback_key,
146
+ kwargs = {"user_query": st.session_state.messages[-2]["content"],
147
+ "bot_response": st.session_state.messages[-1]["content"],
148
+ "demo_name": cfg["demo_name"]}
149
+ )
150
+
151
  log_placeholder = st.empty()
152
  with log_placeholder.container():
153
  if st.session_state.show_logs:
 
158
  if len(st.session_state.log_messages) > 0:
159
  st.button("Show Logs", on_click=toggle_logs)
160
 
 
 
 
 
 
 
161
 
162
  sys.stdout.flush()
163
 
requirements.txt CHANGED
@@ -4,5 +4,7 @@ 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
 
4
  streamlit==1.32.2
5
  streamlit_pills==0.3.0
6
  streamlit_feedback==0.1.3
7
+ langdetect==1.0.9
8
+ langcodes==3.4.0
9
  uuid==1.30
10
  git+https://{GITHUB_TOKEN}@github.com/vectara/vectara-agent.git