First_agent_template / test_agent.py
Ark4u's picture
Deploy ArkAssistant smolagent (Hugging Face Agents Course Unit 1)
2acf84f verified
Raw History Blame Contribute Delete
3.23 kB
#!/usr/bin/env python3
"""CLI test runner for ArkAssistant.
Usage:
# Run a test query against the manager agent:
.venv/bin/python test_agent.py "What is the capital of France and what is the current time there?"
# Sanity check all tools without calling the LLM:
.venv/bin/python test_agent.py --test-tools
The project uses the Hugging Face Inference API and requires HF_TOKEN in .env.
"""
import sys
from tools.get_current_time_in_timezone import get_current_time_in_timezone
from tools.get_weather import get_weather
from tools.search_wikipedia import search_wikipedia
from tools.visit_webpage import VisitWebpageTool
from tools.web_search import DuckDuckGoSearchTool
def _require_result(result: str) -> str:
text = str(result).strip()
if not text:
raise RuntimeError("tool returned an empty result")
if text.lower().startswith("error"):
raise RuntimeError(text)
return text
def run_tools_sanity() -> int:
print("=" * 60)
print("🔬 Running Sanity Check on ArkAssistant Tools (No LLM Calls)")
print("=" * 60)
visit_webpage = VisitWebpageTool()
web_search = DuckDuckGoSearchTool()
failures: list[str] = []
checks = [
("Weather", lambda: get_weather("London")),
("Timezone", lambda: get_current_time_in_timezone("UTC")),
("Wikipedia", lambda: search_wikipedia("Alan Turing")),
("Visit Webpage", lambda: visit_webpage("https://httpbin.org/html")),
("Web Search", lambda: web_search("smolagents Hugging Face")),
]
for name, check in checks:
try:
result = _require_result(check())
preview = result.replace("\n", " ")[:100]
print(f"✅ {name} Tool: {preview}")
except Exception as exc:
failures.append(f"{name}: {exc}")
print(f"❌ {name} Tool Failed: {exc}")
print("=" * 60)
if failures:
print(f"❌ {len(failures)} tool check(s) failed.")
for failure in failures:
print(f" - {failure}")
return 1
print("✅ All tool sanity checks passed.")
return 0
def main() -> int:
args = sys.argv[1:]
if "--test-tools" in args:
return run_tools_sanity()
prompt = " ".join(arg for arg in args if not arg.startswith("--"))
if not prompt:
prompt = "What is the current time in UTC and what is the weather in Paris?"
print(f"No prompt provided. Using default test prompt:\n '{prompt}'\n")
print("🚀 Initializing ArkAssistant Agent...")
try:
from app import agent
print(f"🤖 Running Agent Task: \"{prompt}\"")
print("-" * 60)
response = agent.run(prompt)
print("-" * 60)
print(f"\n🎯 Agent Final Answer:\n{response}")
return 0
except Exception as exc:
message = str(exc)
print(f"\n❌ Error during agent execution: {message}")
if "402" in message or "Payment Required" in message:
print(
"Hint: Hugging Face Inference has no available credits. "
"Add credits/PRO or configure another cloud provider in .env."
)
return 1
if __name__ == "__main__":
sys.exit(main())