Spaces:
Sleeping
Sleeping
import os | |
import requests | |
from duckduckgo_search import DDGS | |
import pandas as pd | |
import io | |
# This is the base URL for the competition API, used to construct file URLs. | |
GAIA_API_URL = "https://agents-course-unit4-scoring.hf.space" | |
def web_search(query: str) -> str: | |
""" | |
Performs a web search using the DuckDuckGo search engine and returns the top results. | |
Use this to find current information, facts, or to answer general knowledge questions. | |
Args: | |
query (str): The search query. | |
Returns: | |
str: A formatted string of the search results. | |
""" | |
print(f"Tool: Performing web search for '{query}'...") | |
try: | |
with DDGS() as ddgs: | |
results = [r for r in ddgs.text(query, max_results=5)] | |
return "\n".join([f"[{i+1}] {r['title']}: {r['body']}" for i, r in enumerate(results)]) if results else "No results found." | |
except Exception as e: | |
return f"Error during web search: {e}" | |
def read_file_from_api(task_id: str) -> str: | |
""" | |
Downloads and reads the content of a file associated with a specific task_id from the GAIA competition API. | |
Only use this tool when the user's question explicitly mentions a file or attachment. | |
Args: | |
task_id (str): The task ID associated with the file to download. | |
Returns: | |
str: The content of the file as a string, or an error message. | |
""" | |
print(f"Tool: Reading file for task_id '{task_id}'...") | |
file_url = f"{GAIA_API_URL}/files/{task_id}" | |
try: | |
response = requests.get(file_url, timeout=10) | |
response.raise_for_status() | |
# We assume the content is text-based (txt, csv, json, etc.) for direct reading | |
return response.text | |
except requests.exceptions.RequestException as e: | |
return f"Error reading file from API: {e}" | |
def python_interpreter(code: str) -> str: | |
""" | |
Executes a given string of Python code and returns its standard output. | |
This tool is highly useful for calculations, data manipulation (using pandas), or any complex logic. | |
The code runs in a restricted environment. Only print the final result. | |
It has access to the 'pandas' library (as pd). | |
Args: | |
code (str): A string containing valid Python code. | |
Returns: | |
str: The captured stdout from the executed code, or the error. | |
""" | |
print(f"Tool: Executing Python code:\n---\n{code}\n---") | |
# WARNING: Executing arbitrary code is a security risk. | |
# In a real-world application, this should be done in a sandboxed environment. | |
local_scope = {"pd": pd, "io": io} | |
# Use a string stream to capture the output of 'print' statements | |
string_stream = io.StringIO() | |
try: | |
# Redirect stdout to our string stream | |
import sys | |
original_stdout = sys.stdout | |
sys.stdout = string_stream | |
# Execute the code | |
exec(code, {"__builtins__": __builtins__}, local_scope) | |
except Exception as e: | |
# Restore stdout and return the error | |
sys.stdout = original_stdout | |
print(f"Error executing python code: {e}") | |
return f"Error: {type(e).__name__}: {e}" | |
finally: | |
# Always restore stdout | |
sys.stdout = original_stdout | |
return string_stream.getvalue() |