File size: 13,404 Bytes
19ecb6a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import os
import re
import json
import requests
import pandas as pd
from pathlib import Path
from typing import Optional, Union, Dict, Any, List
from dotenv import load_dotenv

load_dotenv()

# Simple tool-based agent without LangGraph for now
class SimpleAgent:
    """Simple agent with tool capabilities"""
    
    def __init__(self, llm):
        self.llm = llm
        self.tools = {
            'search_web': self.search_web,
            'search_wikipedia': self.search_wikipedia,
            'execute_python': self.execute_python,
            'read_excel_file': self.read_excel_file,
            'read_text_file': self.read_text_file,
        }
    
    def search_web(self, query: str) -> str:
        """Search the web using DuckDuckGo for current information."""
        try:
            search_url = f"https://api.duckduckgo.com/?q={query}&format=json&no_html=1&skip_disambig=1"
            response = requests.get(search_url, timeout=10)
            
            if response.status_code == 200:
                data = response.json()
                results = []
                if data.get("AbstractText"):
                    results.append(f"Abstract: {data['AbstractText']}")
                
                if data.get("RelatedTopics"):
                    for topic in data["RelatedTopics"][:3]:
                        if isinstance(topic, dict) and topic.get("Text"):
                            results.append(f"Related: {topic['Text']}")
                
                if results:
                    return "\n".join(results)
                else:
                    return f"Search performed for '{query}' but no specific results found."
            else:
                return f"Search failed with status code {response.status_code}"
        except Exception as e:
            return f"Search error: {str(e)}"

    def search_wikipedia(self, query: str) -> str:
        """Search Wikipedia for factual information."""
        try:
            search_url = "https://en.wikipedia.org/api/rest_v1/page/summary/" + query.replace(" ", "_")
            response = requests.get(search_url, timeout=10)
            
            if response.status_code == 200:
                data = response.json()
                extract = data.get("extract", "")
                if extract:
                    return f"Wikipedia: {extract[:500]}..."
                else:
                    return f"Wikipedia page found for '{query}' but no extract available."
            else:
                return f"Wikipedia search failed for '{query}'"
        except Exception as e:
            return f"Wikipedia search error: {str(e)}"

    def execute_python(self, code: str) -> str:
        """Execute Python code and return the result."""
        try:
            import io
            import sys
            
            safe_globals = {
                '__builtins__': {
                    'print': print, 'len': len, 'str': str, 'int': int, 'float': float,
                    'bool': bool, 'list': list, 'dict': dict, 'tuple': tuple, 'set': set,
                    'range': range, 'sum': sum, 'max': max, 'min': min, 'abs': abs,
                    'round': round, 'sorted': sorted, 'enumerate': enumerate, 'zip': zip,
                },
                'math': __import__('math'),
                'json': __import__('json'),
            }
            
            old_stdout = sys.stdout
            sys.stdout = mystdout = io.StringIO()
            
            try:
                exec(code, safe_globals)
                output = mystdout.getvalue()
            finally:
                sys.stdout = old_stdout
            
            return output if output else "Code executed successfully (no output)"
        except Exception as e:
            return f"Python execution error: {str(e)}"

    def read_excel_file(self, file_path: str, sheet_name: Optional[str] = None) -> str:
        """Read an Excel file and return its contents."""
        try:
            file_path_obj = Path(file_path)
            if not file_path_obj.exists():
                return f"Error: File not found at {file_path}"
            
            if sheet_name and sheet_name.isdigit():
                sheet_name = int(sheet_name)
            elif sheet_name is None:
                sheet_name = 0
                
            df = pd.read_excel(file_path, sheet_name=sheet_name)
            
            if len(df) > 20:
                result = f"Excel file with {len(df)} rows and {len(df.columns)} columns:\n\n"
                result += "First 10 rows:\n" + df.head(10).to_string(index=False)
                result += f"\n\n... ({len(df) - 20} rows omitted) ...\n\n"
                result += "Last 10 rows:\n" + df.tail(10).to_string(index=False)
            else:
                result = f"Excel file with {len(df)} rows and {len(df.columns)} columns:\n\n"
                result += df.to_string(index=False)
                
            return result
        except Exception as e:
            return f"Error reading Excel file: {str(e)}"

    def read_text_file(self, file_path: str) -> str:
        """Read a text file and return its contents."""
        try:
            file_path_obj = Path(file_path)
            if not file_path_obj.exists():
                return f"Error: File not found at {file_path}"
            
            encodings = ['utf-8', 'utf-16', 'iso-8859-1', 'cp1252']
            
            for encoding in encodings:
                try:
                    with open(file_path_obj, 'r', encoding=encoding) as f:
                        content = f.read()
                    return f"File content ({encoding} encoding):\n\n{content}"
                except UnicodeDecodeError:
                    continue
            
            return f"Error: Could not decode file with any standard encoding"
        except Exception as e:
            return f"Error reading file: {str(e)}"

    def run(self, question: str) -> str:
        """Run the agent with tool usage"""
        # First, try to answer directly
        direct_response = self.llm(f"""
Question: {question}
Think step by step. If this question requires:
- Web search for current information, say "NEED_SEARCH: <search query>"
- Mathematical calculation, say "NEED_PYTHON: <python code>"
- Wikipedia lookup, say "NEED_WIKI: <search term>"
- File analysis (if file path mentioned), say "NEED_FILE: <file_path>"
Otherwise, provide a direct answer.
Your response:""")

        # Check if tools are needed
        if "NEED_SEARCH:" in direct_response:
            search_query = direct_response.split("NEED_SEARCH:")[1].strip()
            search_result = self.search_web(search_query)
            return self.llm(f"Question: {question}\n\nSearch results: {search_result}\n\nFinal answer:")
        
        elif "NEED_PYTHON:" in direct_response:
            code = direct_response.split("NEED_PYTHON:")[1].strip()
            exec_result = self.execute_python(code)
            return self.llm(f"Question: {question}\n\nCalculation result: {exec_result}\n\nFinal answer:")
        
        elif "NEED_WIKI:" in direct_response:
            wiki_query = direct_response.split("NEED_WIKI:")[1].strip()
            wiki_result = self.search_wikipedia(wiki_query)
            return self.llm(f"Question: {question}\n\nWikipedia info: {wiki_result}\n\nFinal answer:")
        
        elif "NEED_FILE:" in direct_response:
            file_path = direct_response.split("NEED_FILE:")[1].strip()
            if file_path.endswith(('.xlsx', '.xls')):
                file_content = self.read_excel_file(file_path)
            else:
                file_content = self.read_text_file(file_path)
            return self.llm(f"Question: {question}\n\nFile content: {file_content}\n\nFinal answer:")
        
        else:
            return direct_response
class OpenRouterLLM:
    """Simple OpenRouter LLM wrapper"""
    
    def __init__(self, model: str = "deepseek/deepseek-v3.1-terminus"):
        self.api_key = os.getenv("OPENROUTER_API_KEY") or os.getenv("my_key")
        self.model = model
        self.base_url = "https://openrouter.ai/api/v1/chat/completions"
    
    def __call__(self, prompt: str, max_tokens: int = 1500, temperature: float = 0.1) -> str:
        """Make API call to OpenRouter"""
        
        if not self.api_key or not self.api_key.startswith('sk-or-v1-'):
            return "Error: Invalid OpenRouter API key"
        
        headers = {
            "Authorization": f"Bearer {self.api_key}",
            "Content-Type": "application/json",
        }
        
        payload = {
            "model": self.model,
            "messages": [
                {
                    "role": "system",
                    "content": "You are a helpful AI assistant. Provide direct, accurate answers. For GAIA evaluation, be precise and concise."
                },
                {
                    "role": "user", 
                    "content": prompt
                }
            ],
            "temperature": temperature,
            "max_tokens": max_tokens,
        }
        
        try:
            response = requests.post(self.base_url, headers=headers, json=payload, timeout=30)
            
            if response.status_code != 200:
                return f"API Error: {response.status_code}"
            
            result = response.json()
            
            if "choices" in result and len(result["choices"]) > 0:
                answer = result["choices"][0]["message"]["content"].strip()
                return self._clean_answer(answer)
            else:
                return "Error: No response content received"
                
        except Exception as e:
            return f"Error: {str(e)}"
    
    def _clean_answer(self, answer: str) -> str:
        """Clean the answer for GAIA evaluation"""
        answer = answer.strip()
        
        # Remove common prefixes
        prefixes = [
            "Answer:", "The answer is:", "Final answer:", "Result:", 
            "Solution:", "Based on", "Therefore", "In conclusion"
        ]
        
        for prefix in prefixes:
            if answer.lower().startswith(prefix.lower()):
                answer = answer[len(prefix):].strip()
                if answer.startswith(':'):
                    answer = answer[1:].strip()
                break
        
        # Remove quotes and periods from short answers
        if len(answer.split()) <= 3:
            answer = answer.strip('"\'.')
        
        return answer


class GaiaAgent:
    """Simple tool-based agent for GAIA tasks"""
    
    def __init__(self):
        print("Initializing GaiaAgent with OpenRouter DeepSeek...")
        
        # Initialize the LLM
        self.llm = OpenRouterLLM(model="deepseek/deepseek-v3.1-terminus")
        
        # Initialize the agent with tools
        self.agent = SimpleAgent(self.llm)
        
        print("GaiaAgent initialized successfully!")
    
    def __call__(self, task_id: str, question: str) -> str:
        """Process a question and return the answer"""
        try:
            print(f"Processing task {task_id}: {question[:100]}...")
            
            # Check if there are file references in the question
            enhanced_question = self._enhance_question_with_file_analysis(question)
            
            # Run the agent
            answer = self.agent.run(enhanced_question)
            
            # Clean up the answer
            clean_answer = self._clean_final_answer(answer)
            
            print(f"Agent answer for {task_id}: {clean_answer}")
            return clean_answer
            
        except Exception as e:
            error_msg = f"Agent error: {str(e)}"
            print(f"Error processing task {task_id}: {error_msg}")
            return error_msg
    
    def _enhance_question_with_file_analysis(self, question: str) -> str:
        """Check if question mentions files and enhance accordingly"""
        # Look for file path mentions in the question
        file_patterns = [
            r'/tmp/gaia_cached_files/[^\s]+',
            r'saved locally at:\s*([^\s]+)',
            r'file.*?\.xlsx?',
            r'file.*?\.csv',
            r'file.*?\.txt'
        ]
        
        for pattern in file_patterns:
            matches = re.findall(pattern, question, re.IGNORECASE)
            if matches:
                # File found, the agent will handle it automatically
                break
        
        return question
    
    def _clean_final_answer(self, answer: str) -> str:
        """Final cleaning of the answer"""
        answer = answer.strip()
        
        # Look for final answer pattern
        if "final answer:" in answer.lower():
            parts = answer.lower().split("final answer:")
            if len(parts) > 1:
                answer = answer.split(":")[-1].strip()
        
        # Remove common unnecessary phrases
        cleanup_phrases = [
            "based on the", "according to", "the answer is", "therefore",
            "in conclusion", "as a result", "so the answer is"
        ]
        
        for phrase in cleanup_phrases:
            if answer.lower().startswith(phrase):
                answer = answer[len(phrase):].strip()
                break
        
        # Clean up formatting
        answer = answer.strip('.,;:"\'')
        
        return answer