File size: 4,313 Bytes
e51386e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
580bcf5
 
 
8ac5ef4
 
580bcf5
e51386e
 
 
 
 
 
 
 
8ac5ef4
580bcf5
 
e51386e
 
 
 
 
8ac5ef4
580bcf5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e51386e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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:
        @classmethod
        def get_questions(cls):
            return [{"task_id": "fallback", "question": "What is 2+2?"}]
        
        @classmethod
        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 = []