Spaces:
Sleeping
Sleeping
# File: agent/tools.py | |
try: | |
from llama_index.core.tools import FunctionTool | |
import sys | |
import os | |
# Add the parent directory to the path so we can import utils | |
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
from utils.gaia_api import GaiaAPI | |
except ImportError as e: | |
print(f"Import error in tools.py: {e}") | |
# Create a fallback GaiaAPI class if import fails | |
class GaiaAPI: | |
def get_questions(cls): | |
return [{"task_id": "fallback", "question": "What is 2+2?"}] | |
def get_random_question(cls): | |
return {"task_id": "fallback", "question": "What is 2+2?"} | |
import requests | |
from typing import Optional | |
import json | |
def get_gaia_questions() -> str: | |
"""Fetch all GAIA benchmark questions for reference""" | |
try: | |
questions = GaiaAPI.get_questions() | |
result = "Available GAIA Questions:\n" | |
for q in questions[:5]: # Show first 5 questions | |
result += f"ID: {q['task_id']} - {q['question'][:100]}...\n" | |
return result | |
except Exception as e: | |
return f"Error fetching questions: {str(e)}" | |
def get_random_gaia_question() -> str: | |
"""Get a single random GAIA question to work on""" | |
try: | |
question = GaiaAPI.get_random_question() | |
return f"Task ID: {question['task_id']}\nQuestion: {question['question']}" | |
except Exception as e: | |
return f"Error getting random question: {str(e)}" | |
def search_web(query: str) -> str: | |
"""Search the web for information (mock implementation)""" | |
try: | |
# This is a simplified web search - you might want to integrate real search API | |
# For now, return a mock response | |
return f"Search results for '{query}': This is a mock search result. In a real implementation, this would search the web and return relevant information." | |
except Exception as e: | |
return f"Search failed: {str(e)}" | |
def calculate(expression: str) -> str: | |
"""Safely evaluate mathematical expressions""" | |
try: | |
# Only allow safe mathematical operations | |
allowed_chars = set('0123456789+-*/.() ') | |
if not all(c in allowed_chars for c in expression): | |
return "Error: Invalid characters in expression" | |
result = eval(expression) | |
return str(result) | |
except Exception as e: | |
return f"Calculation error: {str(e)}" | |
def read_file_content(file_path: str) -> str: | |
"""Read content from a file (for GAIA tasks that include files)""" | |
try: | |
with open(file_path, 'r', encoding='utf-8') as f: | |
content = f.read() | |
return content[:1000] # Limit content length | |
except Exception as e: | |
return f"Error reading file: {str(e)}" | |
def get_current_info(topic: str) -> str: | |
"""Get current information about a topic""" | |
return f"Current information about '{topic}': This is a mock response. In a real implementation, this would fetch current information from reliable sources." | |
# Create the tools list for the agent | |
try: | |
gaia_tools = [ | |
FunctionTool.from_defaults( | |
fn=get_gaia_questions, | |
name="get_gaia_questions", | |
description="Fetch all available GAIA benchmark questions" | |
), | |
FunctionTool.from_defaults( | |
fn=get_random_gaia_question, | |
name="get_random_question", | |
description="Get a single random GAIA question to work on" | |
), | |
FunctionTool.from_defaults( | |
fn=search_web, | |
name="search_web", | |
description="Search the web for information about a topic" | |
), | |
FunctionTool.from_defaults( | |
fn=calculate, | |
name="calculate", | |
description="Perform mathematical calculations safely" | |
), | |
FunctionTool.from_defaults( | |
fn=read_file_content, | |
name="read_file", | |
description="Read content from a file associated with GAIA tasks" | |
), | |
FunctionTool.from_defaults( | |
fn=get_current_info, | |
name="get_current_info", | |
description="Get current information about a specific topic" | |
) | |
] | |
except Exception as e: | |
print(f"Error creating tools: {e}") | |
gaia_tools = [] |