ofermend commited on
Commit
84357ab
β€’
1 Parent(s): e6d4ed8
Files changed (4) hide show
  1. agent.py +2 -2
  2. app.py +0 -3
  3. requirements.txt +2 -2
  4. st_app.py +40 -43
agent.py CHANGED
@@ -81,7 +81,7 @@ def create_assistant_tools(cfg):
81
  [ask_vehicles, ask_policies]
82
  )
83
 
84
- def initialize_agent(_cfg, update_func=None):
85
  electric_vehicle_bot_instructions = """
86
  - You are a helpful research assistant, with expertise in electric vehicles, in conversation with a user.
87
  - For a query with multiple sub-questions, break down the query into the sub-questions,
@@ -94,7 +94,7 @@ def initialize_agent(_cfg, update_func=None):
94
  tools=create_assistant_tools(_cfg),
95
  topic="Electric vehicles in the United States",
96
  custom_instructions=electric_vehicle_bot_instructions,
97
- update_func=update_func
98
  )
99
  agent.report()
100
  return agent
 
81
  [ask_vehicles, ask_policies]
82
  )
83
 
84
+ def initialize_agent(_cfg, agent_progress_callback=None):
85
  electric_vehicle_bot_instructions = """
86
  - You are a helpful research assistant, with expertise in electric vehicles, in conversation with a user.
87
  - For a query with multiple sub-questions, break down the query into the sub-questions,
 
94
  tools=create_assistant_tools(_cfg),
95
  topic="Electric vehicles in the United States",
96
  custom_instructions=electric_vehicle_bot_instructions,
97
+ agent_progress_callback=agent_progress_callback
98
  )
99
  agent.report()
100
  return agent
app.py CHANGED
@@ -14,9 +14,6 @@ from datasets import load_dataset
14
  if 'device_id' not in st.session_state:
15
  st.session_state.device_id = str(uuid.uuid4())
16
 
17
- if "feedback_key" not in st.session_state:
18
- st.session_state.feedback_key = 0
19
-
20
  def setup_db():
21
  db_path = 'ev_database.db'
22
  conn = sqlite3.connect(db_path)
 
14
  if 'device_id' not in st.session_state:
15
  st.session_state.device_id = str(uuid.uuid4())
16
 
 
 
 
17
  def setup_db():
18
  db_path = 'ev_database.db'
19
  conn = sqlite3.connect(db_path)
requirements.txt CHANGED
@@ -1,10 +1,10 @@
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
- streamlit==1.32.2
4
  streamlit_pills==0.3.0
5
  streamlit-feedback==0.1.3
6
  langdetect==1.0.9
7
  langcodes==3.4.0
8
  datasets==2.19.2
9
  uuid==1.30
10
- vectara-agentic==0.1.16
 
1
  omegaconf==2.3.0
2
  python-dotenv==1.0.1
3
+ streamlit==1.39.0
4
  streamlit_pills==0.3.0
5
  streamlit-feedback==0.1.3
6
  langdetect==1.0.9
7
  langcodes==3.4.0
8
  datasets==2.19.2
9
  uuid==1.30
10
+ vectara-agentic==0.1.18
st_app.py CHANGED
@@ -1,5 +1,6 @@
1
  from PIL import Image
2
  import sys
 
3
 
4
  import streamlit as st
5
  from streamlit_pills import pills
@@ -12,9 +13,6 @@ from agent import initialize_agent, get_agent_config
12
 
13
  initial_prompt = "How can I help you today?"
14
 
15
- def toggle_logs():
16
- st.session_state.show_logs = not st.session_state.show_logs
17
-
18
  def show_example_questions():
19
  if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
20
  selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
@@ -24,23 +22,41 @@ def show_example_questions():
24
  return True
25
  return False
26
 
27
- def update_func(status_type: AgentStatusType, msg: str):
28
- if status_type != AgentStatusType.AGENT_UPDATE:
29
- output = f"{status_type.value} - {msg}"
30
- st.session_state.log_messages.append(output)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  async def launch_bot():
33
  def reset():
34
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "πŸ¦–"}]
35
- st.session_state.thinking_message = "Agent at work..."
36
  st.session_state.log_messages = []
37
  st.session_state.prompt = None
38
  st.session_state.ex_prompt = None
39
  st.session_state.first_turn = True
40
- st.session_state.logs_enabled = False
41
  st.session_state.show_logs = False
42
  if 'agent' not in st.session_state:
43
- st.session_state.agent = initialize_agent(cfg, update_func=update_func)
44
  else:
45
  st.session_state.agent.clear_memory()
46
 
@@ -67,15 +83,9 @@ async def launch_bot():
67
  if st.button('Start Over'):
68
  reset()
69
  st.rerun()
70
- with bc2: # Updated button for enabling/disabling logs
71
- if st.session_state.logs_enabled:
72
- if st.button('Disable Logs', key='disable_logs'):
73
- st.session_state.logs_enabled = False
74
- st.rerun()
75
- else:
76
- if st.button('Enable Logs', key='enable_logs'):
77
- st.session_state.logs_enabled = True
78
- st.rerun()
79
 
80
  st.divider()
81
  st.markdown(
@@ -106,7 +116,7 @@ async def launch_bot():
106
  prompt = st.chat_input()
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
  st.session_state.log_messages = []
111
  st.session_state.show_logs = False
112
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
@@ -117,9 +127,9 @@ async def launch_bot():
117
  # Generate a new response if last message is not from assistant
118
  if st.session_state.prompt:
119
  with st.chat_message("assistant", avatar='πŸ€–'):
120
- with st.spinner(st.session_state.thinking_message):
121
- res = st.session_state.agent.chat(st.session_state.prompt)
122
- res = escape_dollars_outside_latex(res)
123
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
124
  st.session_state.messages.append(message)
125
  st.markdown(res)
@@ -137,27 +147,14 @@ async def launch_bot():
137
 
138
  # Record user feedback
139
  if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
140
- if st.session_state.show_logs and st.session_state.logs_enabled: # Only show logs if enabled
141
- streamlit_feedback(
142
- feedback_type="thumbs", on_submit=thumbs_feedback, key=st.session_state.feedback_key,
143
- kwargs={"user_query": st.session_state.messages[-2]["content"],
144
- "bot_response": st.session_state.messages[-1]["content"],
145
- "demo_name": cfg["demo_name"]}
 
146
  )
147
 
148
- log_placeholder = st.empty()
149
- with log_placeholder.container():
150
- if st.session_state.logs_enabled: # Show logs button only if log toggle is enabled
151
- if st.session_state.show_logs:
152
- st.button("Hide Logs", on_click=toggle_logs)
153
- for msg in st.session_state.log_messages:
154
- if len(msg) > 100: # Use text_area for longer messages
155
- st.text_area(label="Log", value=msg, height=100, disabled=True)
156
- else:
157
- st.text(msg)
158
- else:
159
- if len(st.session_state.log_messages) > 0:
160
- st.button("Show Logs", on_click=toggle_logs)
161
-
162
 
163
  sys.stdout.flush()
 
1
  from PIL import Image
2
  import sys
3
+ import re
4
 
5
  import streamlit as st
6
  from streamlit_pills import pills
 
13
 
14
  initial_prompt = "How can I help you today?"
15
 
 
 
 
16
  def show_example_questions():
17
  if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
18
  selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
 
22
  return True
23
  return False
24
 
25
+ def agent_progress_callback(status_type: AgentStatusType, msg: str):
26
+ output = f'<span style="color:blue;">{status_type.value}</span>: {msg}'
27
+ st.session_state.log_messages.append(output)
28
+ if 'status' in st.session_state:
29
+ latest_message = ''
30
+ if status_type == AgentStatusType.TOOL_CALL:
31
+ match = re.search(r"'([^']*)'", msg)
32
+ tool_name = match.group(1) if match else "Unknown tool"
33
+ latest_message = f"Calling tool {tool_name}..."
34
+ elif status_type == AgentStatusType.TOOL_OUTPUT:
35
+ latest_message = "Analyzing tool output..."
36
+ else:
37
+ return
38
+
39
+ st.session_state.status.update(label=latest_message)
40
+ max_log_msg_size = 200
41
+ with st.session_state.status:
42
+ for log_msg in st.session_state.log_messages:
43
+ st.markdown(log_msg[:max_log_msg_size]+'...', unsafe_allow_html=True)
44
+
45
+ @st.dialog(title="Agent logs", width='large')
46
+ def show_modal():
47
+ for log_msg in st.session_state.log_messages:
48
+ st.write(log_msg, unsafe_allow_html=True)
49
 
50
  async def launch_bot():
51
  def reset():
52
  st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "πŸ¦–"}]
 
53
  st.session_state.log_messages = []
54
  st.session_state.prompt = None
55
  st.session_state.ex_prompt = None
56
  st.session_state.first_turn = True
 
57
  st.session_state.show_logs = False
58
  if 'agent' not in st.session_state:
59
+ st.session_state.agent = initialize_agent(cfg, agent_progress_callback=agent_progress_callback)
60
  else:
61
  st.session_state.agent.clear_memory()
62
 
 
83
  if st.button('Start Over'):
84
  reset()
85
  st.rerun()
86
+ with bc2:
87
+ if st.button('Show Logs'):
88
+ show_modal()
 
 
 
 
 
 
89
 
90
  st.divider()
91
  st.markdown(
 
116
  prompt = st.chat_input()
117
  if prompt:
118
  st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'πŸ§‘β€πŸ’»'})
119
+ st.session_state.prompt = prompt
120
  st.session_state.log_messages = []
121
  st.session_state.show_logs = False
122
  with st.chat_message("user", avatar='πŸ§‘β€πŸ’»'):
 
127
  # Generate a new response if last message is not from assistant
128
  if st.session_state.prompt:
129
  with st.chat_message("assistant", avatar='πŸ€–'):
130
+ st.session_state.status = st.status('Processing...', expanded=False)
131
+ res = st.session_state.agent.chat(st.session_state.prompt)
132
+ res = escape_dollars_outside_latex(res)
133
  message = {"role": "assistant", "content": res, "avatar": 'πŸ€–'}
134
  st.session_state.messages.append(message)
135
  st.markdown(res)
 
147
 
148
  # Record user feedback
149
  if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
150
+ if "feedback_key" not in st.session_state:
151
+ st.session_state.feedback_key = 0
152
+ streamlit_feedback(
153
+ feedback_type="thumbs", on_submit=thumbs_feedback, key=str(st.session_state.feedback_key),
154
+ kwargs={"user_query": st.session_state.messages[-2]["content"],
155
+ "bot_response": st.session_state.messages[-1]["content"],
156
+ "demo_name": cfg["demo_name"]}
157
  )
158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  sys.stdout.flush()