File size: 11,171 Bytes
dfd19f5
06293e9
c1d3919
06293e9
dfd19f5
 
 
 
c1d3919
 
09c80e8
34606bb
66a0e23
06293e9
c1d3919
09c80e8
06293e9
66a0e23
dfd19f5
 
66a0e23
dfd19f5
 
 
 
c1d3919
dfd19f5
 
 
 
 
 
 
 
34606bb
06293e9
66a0e23
06293e9
66a0e23
06293e9
 
fa599aa
 
 
06293e9
fa599aa
06293e9
fa599aa
06293e9
 
 
 
 
fa599aa
06293e9
fa599aa
06293e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa599aa
 
06293e9
 
 
 
 
 
fa599aa
06293e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa599aa
dfd19f5
06293e9
dfd19f5
b9a4880
dfd19f5
 
06293e9
dfd19f5
 
 
 
 
 
 
09c80e8
dfd19f5
 
 
06293e9
dfd19f5
09c80e8
 
06293e9
09c80e8
 
 
 
 
 
 
 
 
 
06293e9
d367dae
09c80e8
 
06293e9
09c80e8
 
 
 
 
 
 
 
 
 
 
06293e9
09c80e8
 
 
 
 
06293e9
09c80e8
 
 
 
 
 
 
 
 
 
 
06293e9
09c80e8
 
 
 
06293e9
09c80e8
 
06293e9
09c80e8
 
06293e9
09c80e8
 
 
 
 
 
06293e9
 
 
 
 
 
 
 
 
 
 
 
 
 
09c80e8
06293e9
 
 
 
 
 
 
09c80e8
06293e9
 
 
09c80e8
06293e9
09c80e8
 
dfd19f5
06293e9
09c80e8
06293e9
34606bb
06293e9
 
09c80e8
06293e9
09c80e8
 
 
06293e9
09c80e8
06293e9
dfd19f5
09c80e8
 
 
 
 
 
 
 
 
 
06293e9
 
 
 
 
 
 
09c80e8
06293e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09c80e8
 
06293e9
 
09c80e8
06293e9
 
 
 
09c80e8
06293e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
09c80e8
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
"""
agent.py - Minimal Claude implementation for GAIA challenge
-----------------------------------------------------------
A simplified implementation with direct litellm access to Anthropic's Claude
"""

import base64
import mimetypes
import os
import re
import tempfile
import time
import random
from typing import List, Dict, Any, Optional
import requests
from urllib.parse import urlparse

from smolagents import CodeAgent, DuckDuckGoSearchTool, PythonInterpreterTool, tool

# --------------------------------------------------------------------------- #
# Constants & helpers
# --------------------------------------------------------------------------- #
DEFAULT_API_URL = os.getenv(
    "GAIA_API_URL", "https://agents-course-unit4-scoring.hf.space"
)
FILE_TAG = re.compile(r"<file:([^>]+)>")  # <file:xyz>

def _download_file(file_id: str) -> bytes:
    """Download the attachment for a GAIA task."""
    url = f"{DEFAULT_API_URL}/files/{file_id}"
    resp = requests.get(url, timeout=30)
    resp.raise_for_status()
    return resp.content

# --------------------------------------------------------------------------- #
# Direct Claude model implementation with litellm
# --------------------------------------------------------------------------- #
class DirectClaudeModel:
    """
    Direct interface to Claude via litellm that works with smolagents
    This avoids the message format issues by keeping things very simple
    """
    
    def __init__(
        self, 
        api_key: Optional[str] = None,
        temperature: float = 0.1
    ):
        """Initialize the Claude model"""
        self.api_key = api_key or os.getenv("ANTHROPIC_API_KEY")
        if not self.api_key:
            raise ValueError("No Anthropic API key provided")
            
        self.temperature = temperature
        self.model_name = "anthropic/claude-3-5-sonnet-20240620"
        
        print(f"Initialized DirectClaudeModel with {self.model_name}")
        
        # Sleep random amount to avoid race conditions with many queries
        time.sleep(random.uniform(1, 3))
        
    def __call__(self, prompt: str, **kwargs) -> str:
        """
        Simple call method that works with smolagents
        
        Args:
            prompt: The user prompt
            **kwargs: Additional parameters (ignored)
            
        Returns:
            Claude's response as a string
        """
        # Import here to avoid any circular imports
        from litellm import completion
        
        # Use a simple format: system message + user message
        messages = [
            {
                "role": "system",
                "content": """You are a concise, highly accurate assistant specialized in solving challenges.
Your answers should be precise, direct, and exactly match the expected format.
All answers are graded by exact string match, so format carefully!"""
            },
            {
                "role": "user",
                "content": prompt
            }
        ]
        
        # Add delay to avoid rate limits
        time.sleep(random.uniform(0.5, 2.0))
        
        try:
            # Make API call with simple format
            response = completion(
                model=self.model_name,
                messages=messages,
                temperature=self.temperature,
                max_tokens=1024,
                api_key=self.api_key
            )
            
            # Extract and return the text content only
            return response.choices[0].message.content
        
        except Exception as e:
            # If it's a rate limit error, wait and retry
            if "rate_limit" in str(e).lower():
                print(f"Rate limit hit, waiting 30 seconds: {e}")
                time.sleep(30)
                return self.__call__(prompt, **kwargs)
            else:
                print(f"Error: {str(e)}")
                raise

# --------------------------------------------------------------------------- #
# Custom tool: fetch GAIA attachments
# --------------------------------------------------------------------------- #
@tool
def gaia_file_reader(file_id: str) -> str:
    """
    Download a GAIA attachment and return its contents
    """
    try:
        raw = _download_file(file_id)
        mime = mimetypes.guess_type(file_id)[0] or "application/octet-stream"
        if mime.startswith("text") or mime in ("application/json",):
            return raw.decode(errors="ignore")
        return base64.b64encode(raw).decode()
    except Exception as exc:
        return f"ERROR downloading {file_id}: {exc}"

# --------------------------------------------------------------------------- #
# Additional tools
# --------------------------------------------------------------------------- #
@tool
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
    """Save content to a file and return the path"""
    temp_dir = tempfile.gettempdir()
    if filename is None:
        temp_file = tempfile.NamedTemporaryFile(delete=False)
        filepath = temp_file.name
    else:
        filepath = os.path.join(temp_dir, filename)
    
    with open(filepath, 'w') as f:
        f.write(content)
    
    return f"File saved to {filepath}."

@tool
def analyze_csv_file(file_path: str, query: str) -> str:
    """Analyze a CSV file with pandas"""
    try:
        import pandas as pd
        df = pd.read_csv(file_path)
        
        result = f"CSV file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
        result += f"Columns: {', '.join(df.columns)}\n\n"
        result += "Summary statistics:\n"
        result += str(df.describe())
        
        return result
    except ImportError:
        return "Error: pandas is not installed."
    except Exception as e:
        return f"Error analyzing CSV file: {str(e)}"

@tool
def analyze_excel_file(file_path: str, query: str) -> str:
    """Analyze an Excel file with pandas"""
    try:
        import pandas as pd
        df = pd.read_excel(file_path)
        
        result = f"Excel file loaded with {len(df)} rows and {len(df.columns)} columns.\n"
        result += f"Columns: {', '.join(df.columns)}\n\n"
        result += "Summary statistics:\n"
        result += str(df.describe())
        
        return result
    except ImportError:
        return "Error: pandas and openpyxl are not installed."
    except Exception as e:
        return f"Error analyzing Excel file: {str(e)}"

# --------------------------------------------------------------------------- #
# ClaudeAgent - Main class for GAIA challenge
# --------------------------------------------------------------------------- #
class ClaudeAgent:
    """A simplified Claude agent for the GAIA challenge"""
    
    def __init__(self):
        """Initialize the agent with Claude"""
        try:
            # Get API key
            api_key = os.getenv("ANTHROPIC_API_KEY")
            if not api_key:
                raise ValueError("ANTHROPIC_API_KEY environment variable not found")
                
            print("βœ… Initializing ClaudeAgent")
            
            # Create the model with direct implementation
            model = DirectClaudeModel(api_key=api_key, temperature=0.1)
            
            # Set up tools
            tools = [
                DuckDuckGoSearchTool(),
                PythonInterpreterTool(),
                save_and_read_file,
                analyze_csv_file,
                analyze_excel_file,
                gaia_file_reader
            ]
            
            # Create the CodeAgent
            self.agent = CodeAgent(
                tools=tools,
                model=model,
                additional_authorized_imports=["pandas", "numpy", "json", "re", "math"],
                executor_type="local",
                verbosity_level=2
            )
            
            print("Agent initialized successfully")
            
        except Exception as e:
            print(f"Error initializing ClaudeAgent: {e}")
            raise
    
    def __call__(self, question: str) -> str:
        """Process a question and return the answer"""
        try:
            print(f"Processing question: {question[:100]}..." if len(question) > 100 else question)
            
            # Add a small delay between questions
            time.sleep(random.uniform(1.0, 3.0))
            
            # Handle file references
            file_match = re.search(r"<file:([^>]+)>", question)
            if file_match:
                file_id = file_match.group(1)
                print(f"Detected file: {file_id}")
                
                # Download file
                try:
                    file_content = _download_file(file_id)
                    temp_dir = tempfile.gettempdir()
                    file_path = os.path.join(temp_dir, file_id)
                    
                    with open(file_path, 'wb') as f:
                        f.write(file_content)
                    
                    # Remove file tag from question
                    clean_question = re.sub(r"<file:[^>]+>", "", question).strip()
                    
                    # Build prompt with file context
                    prompt = f"""
Question: {clean_question}
There is a file available at path: {file_path}
Use appropriate tools to analyze this file if needed.
Answer the question directly and precisely.
"""
                except Exception as e:
                    print(f"Error downloading file: {e}")
                    prompt = question
            else:
                # Handle reversed text separately
                if question.startswith(".") or ".rewsna eht sa" in question:
                    prompt = f"""
This question is in reversed text. Here's the normal version:
{question[::-1]}
Answer the question directly and precisely.
"""
                else:
                    prompt = question
            
            # Execute agent with prompt
            answer = self.agent.run(prompt)
            
            # Clean up response
            answer = self._clean_answer(answer)
            
            print(f"Generated answer: {answer}")
            return answer
            
        except Exception as e:
            print(f"Error: {str(e)}")
            return f"Error processing question: {str(e)}"
    
    def _clean_answer(self, answer: any) -> str:
        """Clean up the answer for exact matching"""
        if not isinstance(answer, str):
            return str(answer)
        
        # Normalize spacing
        answer = answer.strip()
        
        # Remove common prefixes
        prefixes = [
            "The answer is ", "Answer: ", "Final answer: ", 
            "The result is ", "Based on the information provided, "
        ]
        
        for prefix in prefixes:
            if answer.startswith(prefix):
                answer = answer[len(prefix):].strip()
        
        # Remove quotes
        if (answer.startswith('"') and answer.endswith('"')) or (
            answer.startswith("'") and answer.endswith("'")
        ):
            answer = answer[1:-1].strip()
            
        return answer