Spaces:
Running
Running
UI upgrade
Browse files- agent.py +2 -3
- requirements.txt +2 -2
- st_app.py +40 -44
agent.py
CHANGED
@@ -23,7 +23,6 @@ get_headers = {
|
|
23 |
"Connection": "keep-alive",
|
24 |
}
|
25 |
|
26 |
-
|
27 |
def create_assistant_tools(cfg):
|
28 |
|
29 |
class QueryHackerNews(BaseModel):
|
@@ -144,7 +143,7 @@ def create_assistant_tools(cfg):
|
|
144 |
tools_factory.standard_tools()
|
145 |
)
|
146 |
|
147 |
-
def initialize_agent(_cfg,
|
148 |
bot_instructions = """
|
149 |
- You are a helpful assistant, with expertise in answering user questions based on Hacker News stories and comments.
|
150 |
- Always call the ask_hackernews tool first as your primary source of information. Then try other tools.
|
@@ -160,7 +159,7 @@ def initialize_agent(_cfg, update_func = None):
|
|
160 |
tools=create_assistant_tools(_cfg),
|
161 |
topic="hacker news",
|
162 |
custom_instructions=bot_instructions,
|
163 |
-
|
164 |
)
|
165 |
agent.report()
|
166 |
return agent
|
|
|
23 |
"Connection": "keep-alive",
|
24 |
}
|
25 |
|
|
|
26 |
def create_assistant_tools(cfg):
|
27 |
|
28 |
class QueryHackerNews(BaseModel):
|
|
|
143 |
tools_factory.standard_tools()
|
144 |
)
|
145 |
|
146 |
+
def initialize_agent(_cfg, agent_progress_callback = None):
|
147 |
bot_instructions = """
|
148 |
- You are a helpful assistant, with expertise in answering user questions based on Hacker News stories and comments.
|
149 |
- Always call the ask_hackernews tool first as your primary source of information. Then try other tools.
|
|
|
159 |
tools=create_assistant_tools(_cfg),
|
160 |
topic="hacker news",
|
161 |
custom_instructions=bot_instructions,
|
162 |
+
agent_progress_callback=agent_progress_callback
|
163 |
)
|
164 |
agent.report()
|
165 |
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,6 +1,6 @@
|
|
1 |
from PIL import Image
|
2 |
import sys
|
3 |
-
import
|
4 |
|
5 |
import streamlit as st
|
6 |
from streamlit_pills import pills
|
@@ -13,9 +13,6 @@ from agent import initialize_agent, get_agent_config
|
|
13 |
|
14 |
initial_prompt = "How can I help you today?"
|
15 |
|
16 |
-
def toggle_logs():
|
17 |
-
st.session_state.show_logs = not st.session_state.show_logs
|
18 |
-
|
19 |
def show_example_questions():
|
20 |
if len(st.session_state.example_messages) > 0 and st.session_state.first_turn:
|
21 |
selected_example = pills("Queries to Try:", st.session_state.example_messages, index=None)
|
@@ -25,23 +22,41 @@ def show_example_questions():
|
|
25 |
return True
|
26 |
return False
|
27 |
|
28 |
-
def
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
async def launch_bot():
|
34 |
def reset():
|
35 |
st.session_state.messages = [{"role": "assistant", "content": initial_prompt, "avatar": "π¦"}]
|
36 |
-
st.session_state.thinking_message = "Agent at work..."
|
37 |
st.session_state.log_messages = []
|
38 |
st.session_state.prompt = None
|
39 |
st.session_state.ex_prompt = None
|
40 |
st.session_state.first_turn = True
|
41 |
-
st.session_state.logs_enabled = False
|
42 |
st.session_state.show_logs = False
|
43 |
if 'agent' not in st.session_state:
|
44 |
-
st.session_state.agent = initialize_agent(cfg,
|
45 |
else:
|
46 |
st.session_state.agent.clear_memory()
|
47 |
|
@@ -68,15 +83,9 @@ async def launch_bot():
|
|
68 |
if st.button('Start Over'):
|
69 |
reset()
|
70 |
st.rerun()
|
71 |
-
with bc2:
|
72 |
-
if st.
|
73 |
-
|
74 |
-
st.session_state.logs_enabled = False
|
75 |
-
st.rerun()
|
76 |
-
else:
|
77 |
-
if st.button('Enable Logs', key='enable_logs'):
|
78 |
-
st.session_state.logs_enabled = True
|
79 |
-
st.rerun()
|
80 |
|
81 |
st.divider()
|
82 |
st.markdown(
|
@@ -107,7 +116,7 @@ async def launch_bot():
|
|
107 |
prompt = st.chat_input()
|
108 |
if prompt:
|
109 |
st.session_state.messages.append({"role": "user", "content": prompt, "avatar": 'π§βπ»'})
|
110 |
-
st.session_state.prompt = prompt
|
111 |
st.session_state.log_messages = []
|
112 |
st.session_state.show_logs = False
|
113 |
with st.chat_message("user", avatar='π§βπ»'):
|
@@ -118,9 +127,9 @@ async def launch_bot():
|
|
118 |
# Generate a new response if last message is not from assistant
|
119 |
if st.session_state.prompt:
|
120 |
with st.chat_message("assistant", avatar='π€'):
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
message = {"role": "assistant", "content": res, "avatar": 'π€'}
|
125 |
st.session_state.messages.append(message)
|
126 |
st.markdown(res)
|
@@ -138,27 +147,14 @@ async def launch_bot():
|
|
138 |
|
139 |
# Record user feedback
|
140 |
if (st.session_state.messages[-1]["role"] == "assistant") & (st.session_state.messages[-1]["content"] != initial_prompt):
|
141 |
-
if
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
147 |
)
|
148 |
|
149 |
-
log_placeholder = st.empty()
|
150 |
-
with log_placeholder.container():
|
151 |
-
if st.session_state.logs_enabled: # Show logs button only if log toggle is enabled
|
152 |
-
if st.session_state.show_logs:
|
153 |
-
st.button("Hide Logs", on_click=toggle_logs)
|
154 |
-
for msg in st.session_state.log_messages:
|
155 |
-
if len(msg) > 100: # Use text_area for longer messages
|
156 |
-
st.text_area(label="Log", value=msg, height=100, disabled=True)
|
157 |
-
else:
|
158 |
-
st.text(msg)
|
159 |
-
else:
|
160 |
-
if len(st.session_state.log_messages) > 0:
|
161 |
-
st.button("Show Logs", on_click=toggle_logs)
|
162 |
-
|
163 |
|
164 |
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()
|