Spaces:
Running
Running
UI improvements
Browse files- agent.py +2 -2
- requirements.txt +2 -2
- st_app.py +40 -43
agent.py
CHANGED
@@ -109,7 +109,7 @@ def create_assistant_tools(cfg):
|
|
109 |
[ask_transcripts]
|
110 |
)
|
111 |
|
112 |
-
def initialize_agent(_cfg,
|
113 |
financial_bot_instructions = """
|
114 |
- You are a helpful financial assistant, with expertise in financial reporting, in conversation with a user.
|
115 |
- Use the ask_transcripts tool to answer most questions about the company's financial performance, risks, opportunities, strategy, competitors, and more.
|
@@ -127,7 +127,7 @@ def initialize_agent(_cfg, update_func=None):
|
|
127 |
tools=create_assistant_tools(_cfg),
|
128 |
topic="Financial data, annual reports and 10-K filings",
|
129 |
custom_instructions=financial_bot_instructions,
|
130 |
-
|
131 |
)
|
132 |
agent.report()
|
133 |
return agent
|
|
|
109 |
[ask_transcripts]
|
110 |
)
|
111 |
|
112 |
+
def initialize_agent(_cfg, agent_progress_callback=None):
|
113 |
financial_bot_instructions = """
|
114 |
- You are a helpful financial assistant, with expertise in financial reporting, in conversation with a user.
|
115 |
- Use the ask_transcripts tool to answer most questions about the company's financial performance, risks, opportunities, strategy, competitors, and more.
|
|
|
127 |
tools=create_assistant_tools(_cfg),
|
128 |
topic="Financial data, annual reports and 10-K filings",
|
129 |
custom_instructions=financial_bot_instructions,
|
130 |
+
agent_progress_callback=agent_progress_callback,
|
131 |
)
|
132 |
agent.report()
|
133 |
return agent
|
requirements.txt
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
omegaconf==2.3.0
|
2 |
python-dotenv==1.0.1
|
3 |
-
streamlit==1.
|
4 |
streamlit_pills==0.3.0
|
5 |
streamlit_feedback==0.1.3
|
6 |
uuid==1.30
|
7 |
langdetect==1.0.9
|
8 |
langcodes==3.4.0
|
9 |
-
vectara-agentic==0.1.
|
|
|
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 |
uuid==1.30
|
7 |
langdetect==1.0.9
|
8 |
langcodes==3.4.0
|
9 |
+
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
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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,
|
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:
|
71 |
-
if st.
|
72 |
-
|
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
|
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 |
-
|
121 |
-
|
122 |
-
|
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
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
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()
|