benjipeng commited on
Commit
61e2bd8
·
verified ·
1 Parent(s): 804bc89

Create tools.py

Browse files
Files changed (1) hide show
  1. tools.py +73 -0
tools.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ from duckduckgo_search import DDGS
4
+
5
+ # This is the base URL for the competition API, used to fetch files.
6
+ GAIA_API_URL = "https://agents-course-unit4-scoring.hf.space"
7
+
8
+ def web_search(query: str) -> str:
9
+ """
10
+ Performs a web search using the DuckDuckGo search engine and returns the top results.
11
+ Use this to find current information, facts, or to answer general knowledge questions.
12
+
13
+ Args:
14
+ query (str): The search query.
15
+
16
+ Returns:
17
+ str: A formatted string of the search results.
18
+ """
19
+ print(f"Tool: Performing web search for '{query}'...")
20
+ try:
21
+ with DDGS() as ddgs:
22
+ results = [r for r in ddgs.text(query, max_results=5)]
23
+ return "\n".join([f"[{i+1}] {r['title']}: {r['body']}" for i, r in enumerate(results)]) if results else "No results found."
24
+ except Exception as e:
25
+ return f"Error during web search: {e}"
26
+
27
+ def read_file_from_api(task_id: str) -> str:
28
+ """
29
+ Downloads and reads the content of a file associated with a specific task_id from the GAIA competition API.
30
+ Only use this tool when the user's question explicitly mentions a file or attachment.
31
+
32
+ Args:
33
+ task_id (str): The task ID associated with the file to download.
34
+
35
+ Returns:
36
+ str: The content of the file as a string, or an error message.
37
+ """
38
+ print(f"Tool: Reading file for task_id '{task_id}'...")
39
+ file_url = f"{GAIA_API_URL}/files/{task_id}"
40
+ try:
41
+ response = requests.get(file_url, timeout=10)
42
+ response.raise_for_status()
43
+ # We assume the content is text-based (txt, csv, json, etc.)
44
+ return response.text
45
+ except requests.exceptions.RequestException as e:
46
+ return f"Error reading file from API: {e}"
47
+
48
+ def python_interpreter(code: str) -> str:
49
+ """
50
+ Executes a given string of Python code and returns its standard output.
51
+ This tool is highly useful for calculations, data manipulation, or any complex logic.
52
+ The code runs in a restricted environment. Only print the final result.
53
+
54
+ Args:
55
+ code (str): A string containing valid Python code.
56
+
57
+ Returns:
58
+ str: The captured stdout from the executed code, or the error.
59
+ """
60
+ print(f"Tool: Executing Python code:\n---\n{code}\n---")
61
+ # WARNING: Executing arbitrary code is a security risk.
62
+ # In a real-world application, this should be done in a sandboxed environment.
63
+ import io
64
+ import sys
65
+ from contextlib import redirect_stdout
66
+
67
+ try:
68
+ f = io.StringIO()
69
+ with redirect_stdout(f):
70
+ exec(code, {})
71
+ return f.getvalue()
72
+ except Exception as e:
73
+ return f"Error executing Python code: {type(e).__name__}: {e}"