Commit ·
c82d598
1
Parent(s): 97fc18c
refactor(agent): remove unused websearch_agent and update supervisor instructions
Browse filesRemove the unused `websearch_agent` import and tool from supervisor agent configuration, keeping only `web_search_agents`. Update system prompt to instruct the agent to respond immediately once answer is found and avoid unnecessary searching to prevent exceeding action step limits. Also apply code formatting improvements to app.py.
- agent/agent.py +11 -5
- app.py +52 -28
agent/agent.py
CHANGED
|
@@ -6,7 +6,8 @@ from langchain_core.messages import HumanMessage
|
|
| 6 |
from agent.tools.math_solver import math_solver
|
| 7 |
|
| 8 |
from agent.agents.websearchagents import web_search_agents
|
| 9 |
-
|
|
|
|
| 10 |
from agent.agents.answer_extractor import extract_answer
|
| 11 |
|
| 12 |
load_dotenv()
|
|
@@ -16,15 +17,19 @@ def supervisor_agent():
|
|
| 16 |
"""Return a supervisor agent instance with math_solver and websearch_agent."""
|
| 17 |
return create_agent(
|
| 18 |
model="google_genai:gemini-3-flash-preview",
|
| 19 |
-
tools=[math_solver, websearch_agent, web_search_agents],
|
|
|
|
| 20 |
system_prompt=(
|
| 21 |
f"You are a supervisor agent. "
|
| 22 |
f"Current time is: {datetime.now(timezone.utc).isoformat()}. "
|
| 23 |
f"Your memory are out of date. "
|
| 24 |
f"For any math or calculation questions, use the math_solver tool for check, "
|
| 25 |
f"the accurate is the most important."
|
| 26 |
-
f"All questions that need real-time, must use the
|
| 27 |
-
f"to get a concise and accurate final answer."
|
|
|
|
|
|
|
|
|
|
| 28 |
),
|
| 29 |
)
|
| 30 |
|
|
@@ -35,7 +40,8 @@ def run(query: str, max_retries: int = 3) -> str:
|
|
| 35 |
|
| 36 |
for attempt in range(1, max_retries + 1):
|
| 37 |
print(
|
| 38 |
-
f"{Fore.CYAN}[Supervisor] Processing query (attempt {attempt}/{max_retries})...
|
|
|
|
| 39 |
)
|
| 40 |
agent = supervisor_agent()
|
| 41 |
|
|
|
|
| 6 |
from agent.tools.math_solver import math_solver
|
| 7 |
|
| 8 |
from agent.agents.websearchagents import web_search_agents
|
| 9 |
+
|
| 10 |
+
# from agent.agents.websearchagent import websearch_agent
|
| 11 |
from agent.agents.answer_extractor import extract_answer
|
| 12 |
|
| 13 |
load_dotenv()
|
|
|
|
| 17 |
"""Return a supervisor agent instance with math_solver and websearch_agent."""
|
| 18 |
return create_agent(
|
| 19 |
model="google_genai:gemini-3-flash-preview",
|
| 20 |
+
# tools=[math_solver, websearch_agent, web_search_agents],
|
| 21 |
+
tools=[math_solver, web_search_agents],
|
| 22 |
system_prompt=(
|
| 23 |
f"You are a supervisor agent. "
|
| 24 |
f"Current time is: {datetime.now(timezone.utc).isoformat()}. "
|
| 25 |
f"Your memory are out of date. "
|
| 26 |
f"For any math or calculation questions, use the math_solver tool for check, "
|
| 27 |
f"the accurate is the most important."
|
| 28 |
+
f"All questions that need real-time, must use the web_search_agents tool "
|
| 29 |
+
f"to get a concise and accurate final answer. "
|
| 30 |
+
f"Once you have found the answer, respond immediately. "
|
| 31 |
+
f"Do NOT continue searching or verifying unnecessarily — "
|
| 32 |
+
f"you have a limited number of action steps and must avoid exceeding them."
|
| 33 |
),
|
| 34 |
)
|
| 35 |
|
|
|
|
| 40 |
|
| 41 |
for attempt in range(1, max_retries + 1):
|
| 42 |
print(
|
| 43 |
+
f"{Fore.CYAN}[Supervisor] Processing query (attempt {attempt}/{max_retries})...\n"
|
| 44 |
+
f"[Supervisor] Query: {query}{Style.RESET_ALL}"
|
| 45 |
)
|
| 46 |
agent = supervisor_agent()
|
| 47 |
|
app.py
CHANGED
|
@@ -1,8 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
-
import requests
|
| 4 |
import inspect
|
| 5 |
-
import pandas as pd
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
@@ -11,25 +11,28 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
|
| 11 |
# --- Agent Definition ---
|
| 12 |
from agent.agent import run as agent_run
|
| 13 |
|
|
|
|
| 14 |
class BasicAgent:
|
| 15 |
def __init__(self):
|
| 16 |
print("BasicAgent initialized.")
|
|
|
|
| 17 |
def __call__(self, question: str) -> str:
|
| 18 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 19 |
answer = agent_run(question)
|
| 20 |
print(f"Agent returning answer (first 50 chars): {answer[:50]}...")
|
| 21 |
return answer
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
"""
|
| 25 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 26 |
and displays the results.
|
| 27 |
"""
|
| 28 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 29 |
-
space_id = os.getenv("SPACE_ID")
|
| 30 |
|
| 31 |
if profile:
|
| 32 |
-
username= f"{profile.username}"
|
| 33 |
print(f"User logged in: {username}")
|
| 34 |
else:
|
| 35 |
print("User not logged in.")
|
|
@@ -56,16 +59,16 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 56 |
response.raise_for_status()
|
| 57 |
questions_data = response.json()
|
| 58 |
if not questions_data:
|
| 59 |
-
|
| 60 |
-
|
| 61 |
print(f"Fetched {len(questions_data)} questions.")
|
| 62 |
except requests.exceptions.RequestException as e:
|
| 63 |
print(f"Error fetching questions: {e}")
|
| 64 |
return f"Error fetching questions: {e}", None
|
| 65 |
except requests.exceptions.JSONDecodeError as e:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
except Exception as e:
|
| 70 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 71 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
@@ -82,18 +85,36 @@ def run_and_submit_all( profile: gr.OAuthProfile | None):
|
|
| 82 |
continue
|
| 83 |
try:
|
| 84 |
submitted_answer = agent(question_text)
|
| 85 |
-
answers_payload.append(
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
if not answers_payload:
|
| 92 |
print("Agent did not produce any answers to submit.")
|
| 93 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 94 |
|
| 95 |
-
# 4. Prepare Submission
|
| 96 |
-
submission_data = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 98 |
print(status_update)
|
| 99 |
|
|
@@ -163,20 +184,19 @@ with gr.Blocks() as demo:
|
|
| 163 |
|
| 164 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 165 |
|
| 166 |
-
status_output = gr.Textbox(
|
|
|
|
|
|
|
| 167 |
# Removed max_rows=10 from DataFrame constructor
|
| 168 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 169 |
|
| 170 |
-
run_button.click(
|
| 171 |
-
fn=run_and_submit_all,
|
| 172 |
-
outputs=[status_output, results_table]
|
| 173 |
-
)
|
| 174 |
|
| 175 |
if __name__ == "__main__":
|
| 176 |
-
print("\n" + "-"*30 + " App Starting " + "-"*30)
|
| 177 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 178 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 179 |
-
space_id_startup = os.getenv("SPACE_ID")
|
| 180 |
|
| 181 |
if space_host_startup:
|
| 182 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
@@ -184,14 +204,18 @@ if __name__ == "__main__":
|
|
| 184 |
else:
|
| 185 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 186 |
|
| 187 |
-
if space_id_startup:
|
| 188 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 189 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 190 |
-
print(
|
|
|
|
|
|
|
| 191 |
else:
|
| 192 |
-
print(
|
|
|
|
|
|
|
| 193 |
|
| 194 |
-
print("-"*(60 + len(" App Starting ")) + "\n")
|
| 195 |
|
| 196 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 197 |
-
demo.launch(debug=True, share=False)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
import requests # type: ignore[import]
|
| 4 |
import inspect
|
| 5 |
+
import pandas as pd # type: ignore[import]
|
| 6 |
|
| 7 |
# (Keep Constants as is)
|
| 8 |
# --- Constants ---
|
|
|
|
| 11 |
# --- Agent Definition ---
|
| 12 |
from agent.agent import run as agent_run
|
| 13 |
|
| 14 |
+
|
| 15 |
class BasicAgent:
|
| 16 |
def __init__(self):
|
| 17 |
print("BasicAgent initialized.")
|
| 18 |
+
|
| 19 |
def __call__(self, question: str) -> str:
|
| 20 |
print(f"Agent received question (first 50 chars): {question[:50]}...")
|
| 21 |
answer = agent_run(question)
|
| 22 |
print(f"Agent returning answer (first 50 chars): {answer[:50]}...")
|
| 23 |
return answer
|
| 24 |
|
| 25 |
+
|
| 26 |
+
def run_and_submit_all(profile: gr.OAuthProfile | None):
|
| 27 |
"""
|
| 28 |
Fetches all questions, runs the BasicAgent on them, submits all answers,
|
| 29 |
and displays the results.
|
| 30 |
"""
|
| 31 |
# --- Determine HF Space Runtime URL and Repo URL ---
|
| 32 |
+
space_id = os.getenv("SPACE_ID") # Get the SPACE_ID for sending link to the code
|
| 33 |
|
| 34 |
if profile:
|
| 35 |
+
username = f"{profile.username}"
|
| 36 |
print(f"User logged in: {username}")
|
| 37 |
else:
|
| 38 |
print("User not logged in.")
|
|
|
|
| 59 |
response.raise_for_status()
|
| 60 |
questions_data = response.json()
|
| 61 |
if not questions_data:
|
| 62 |
+
print("Fetched questions list is empty.")
|
| 63 |
+
return "Fetched questions list is empty or invalid format.", None
|
| 64 |
print(f"Fetched {len(questions_data)} questions.")
|
| 65 |
except requests.exceptions.RequestException as e:
|
| 66 |
print(f"Error fetching questions: {e}")
|
| 67 |
return f"Error fetching questions: {e}", None
|
| 68 |
except requests.exceptions.JSONDecodeError as e:
|
| 69 |
+
print(f"Error decoding JSON response from questions endpoint: {e}")
|
| 70 |
+
print(f"Response text: {response.text[:500]}")
|
| 71 |
+
return f"Error decoding server response for questions: {e}", None
|
| 72 |
except Exception as e:
|
| 73 |
print(f"An unexpected error occurred fetching questions: {e}")
|
| 74 |
return f"An unexpected error occurred fetching questions: {e}", None
|
|
|
|
| 85 |
continue
|
| 86 |
try:
|
| 87 |
submitted_answer = agent(question_text)
|
| 88 |
+
answers_payload.append(
|
| 89 |
+
{"task_id": task_id, "submitted_answer": submitted_answer}
|
| 90 |
+
)
|
| 91 |
+
results_log.append(
|
| 92 |
+
{
|
| 93 |
+
"Task ID": task_id,
|
| 94 |
+
"Question": question_text,
|
| 95 |
+
"Submitted Answer": submitted_answer,
|
| 96 |
+
}
|
| 97 |
+
)
|
| 98 |
except Exception as e:
|
| 99 |
+
print(f"Error running agent on task {task_id}: {e}")
|
| 100 |
+
results_log.append(
|
| 101 |
+
{
|
| 102 |
+
"Task ID": task_id,
|
| 103 |
+
"Question": question_text,
|
| 104 |
+
"Submitted Answer": f"AGENT ERROR: {e}",
|
| 105 |
+
}
|
| 106 |
+
)
|
| 107 |
|
| 108 |
if not answers_payload:
|
| 109 |
print("Agent did not produce any answers to submit.")
|
| 110 |
return "Agent did not produce any answers to submit.", pd.DataFrame(results_log)
|
| 111 |
|
| 112 |
+
# 4. Prepare Submission
|
| 113 |
+
submission_data = {
|
| 114 |
+
"username": username.strip(),
|
| 115 |
+
"agent_code": agent_code,
|
| 116 |
+
"answers": answers_payload,
|
| 117 |
+
}
|
| 118 |
status_update = f"Agent finished. Submitting {len(answers_payload)} answers for user '{username}'..."
|
| 119 |
print(status_update)
|
| 120 |
|
|
|
|
| 184 |
|
| 185 |
run_button = gr.Button("Run Evaluation & Submit All Answers")
|
| 186 |
|
| 187 |
+
status_output = gr.Textbox(
|
| 188 |
+
label="Run Status / Submission Result", lines=5, interactive=False
|
| 189 |
+
)
|
| 190 |
# Removed max_rows=10 from DataFrame constructor
|
| 191 |
results_table = gr.DataFrame(label="Questions and Agent Answers", wrap=True)
|
| 192 |
|
| 193 |
+
run_button.click(fn=run_and_submit_all, outputs=[status_output, results_table])
|
|
|
|
|
|
|
|
|
|
| 194 |
|
| 195 |
if __name__ == "__main__":
|
| 196 |
+
print("\n" + "-" * 30 + " App Starting " + "-" * 30)
|
| 197 |
# Check for SPACE_HOST and SPACE_ID at startup for information
|
| 198 |
space_host_startup = os.getenv("SPACE_HOST")
|
| 199 |
+
space_id_startup = os.getenv("SPACE_ID") # Get SPACE_ID at startup
|
| 200 |
|
| 201 |
if space_host_startup:
|
| 202 |
print(f"✅ SPACE_HOST found: {space_host_startup}")
|
|
|
|
| 204 |
else:
|
| 205 |
print("ℹ️ SPACE_HOST environment variable not found (running locally?).")
|
| 206 |
|
| 207 |
+
if space_id_startup: # Print repo URLs if SPACE_ID is found
|
| 208 |
print(f"✅ SPACE_ID found: {space_id_startup}")
|
| 209 |
print(f" Repo URL: https://huggingface.co/spaces/{space_id_startup}")
|
| 210 |
+
print(
|
| 211 |
+
f" Repo Tree URL: https://huggingface.co/spaces/{space_id_startup}/tree/main"
|
| 212 |
+
)
|
| 213 |
else:
|
| 214 |
+
print(
|
| 215 |
+
"ℹ️ SPACE_ID environment variable not found (running locally?). Repo URL cannot be determined."
|
| 216 |
+
)
|
| 217 |
|
| 218 |
+
print("-" * (60 + len(" App Starting ")) + "\n")
|
| 219 |
|
| 220 |
print("Launching Gradio Interface for Basic Agent Evaluation...")
|
| 221 |
+
demo.launch(debug=True, share=False)
|