|
|
|
import math
|
|
import requests
|
|
import wikipedia
|
|
|
|
|
|
def tool_search(query: str) -> str:
|
|
"""Search the web (Wikipedia API) for the query and return a summary of results."""
|
|
try:
|
|
|
|
summary = wikipedia.summary(query, sentences=2)
|
|
return summary
|
|
except Exception as e:
|
|
return f"(Search tool failed: {e})"
|
|
|
|
|
|
def tool_calculate(expression: str) -> str:
|
|
"""Evaluate a mathematical expression and return the result as a string."""
|
|
try:
|
|
result = eval(expression, {"__builtins__": None}, {"sqrt": math.sqrt, "pow": math.pow})
|
|
return str(result)
|
|
except Exception as e:
|
|
return f"(Calculation error: {e})"
|
|
|
|
|
|
def tool_load_file(task_id: str) -> str:
|
|
"""Fetch the file for a given task (if any) and return its content or a description."""
|
|
url = f"https://agents-course-unit4-scoring.hf.space/files/{task_id}"
|
|
try:
|
|
resp = requests.get(url, timeout=10)
|
|
resp.raise_for_status()
|
|
except Exception as e:
|
|
return f"(File download error: {e})"
|
|
|
|
content_type = resp.headers.get("Content-Type", "")
|
|
if "image" in content_type:
|
|
|
|
return "[Image received from task]"
|
|
elif "text" in content_type or "json" in content_type:
|
|
text_data = resp.text[:500]
|
|
return f"[File content snippet: {text_data}]"
|
|
else:
|
|
return "(Unknown file type or binary data received)" |