LamiaYT's picture
Complete GAIA agent with LlamaIndex - fixed all issues
580bcf5
raw
history blame
3.27 kB
# agent/tools.py
from llama_index.core.tools import FunctionTool
from utils.gaia_api import GaiaAPI
import requests
from typing import Optional
import json
def get_gaia_questions() -> str:
"""Fetch all GAIA benchmark questions for reference"""
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
def get_random_gaia_question() -> str:
"""Get a single random GAIA question to work on"""
question = GaiaAPI.get_random_question()
return f"Task ID: {question['task_id']}\nQuestion: {question['question']}"
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
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"
)
]