Update agent.py
Browse files
agent.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
"""
|
2 |
-
agent.py -
|
3 |
-----------------------------------------------------------
|
4 |
A simplified implementation with direct litellm access to Anthropic's Claude
|
5 |
"""
|
@@ -114,12 +114,18 @@ All answers are graded by exact string match, so format carefully!"""
|
|
114 |
raise
|
115 |
|
116 |
# --------------------------------------------------------------------------- #
|
117 |
-
#
|
118 |
# --------------------------------------------------------------------------- #
|
119 |
@tool
|
120 |
def gaia_file_reader(file_id: str) -> str:
|
121 |
"""
|
122 |
-
Download a GAIA attachment and return its contents
|
|
|
|
|
|
|
|
|
|
|
|
|
123 |
"""
|
124 |
try:
|
125 |
raw = _download_file(file_id)
|
@@ -130,12 +136,18 @@ def gaia_file_reader(file_id: str) -> str:
|
|
130 |
except Exception as exc:
|
131 |
return f"ERROR downloading {file_id}: {exc}"
|
132 |
|
133 |
-
# --------------------------------------------------------------------------- #
|
134 |
-
# Additional tools
|
135 |
-
# --------------------------------------------------------------------------- #
|
136 |
@tool
|
137 |
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
138 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
temp_dir = tempfile.gettempdir()
|
140 |
if filename is None:
|
141 |
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
@@ -150,7 +162,16 @@ def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
|
150 |
|
151 |
@tool
|
152 |
def analyze_csv_file(file_path: str, query: str) -> str:
|
153 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
154 |
try:
|
155 |
import pandas as pd
|
156 |
df = pd.read_csv(file_path)
|
@@ -168,7 +189,16 @@ def analyze_csv_file(file_path: str, query: str) -> str:
|
|
168 |
|
169 |
@tool
|
170 |
def analyze_excel_file(file_path: str, query: str) -> str:
|
171 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
172 |
try:
|
173 |
import pandas as pd
|
174 |
df = pd.read_excel(file_path)
|
@@ -184,6 +214,73 @@ def analyze_excel_file(file_path: str, query: str) -> str:
|
|
184 |
except Exception as e:
|
185 |
return f"Error analyzing Excel file: {str(e)}"
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
# --------------------------------------------------------------------------- #
|
188 |
# ClaudeAgent - Main class for GAIA challenge
|
189 |
# --------------------------------------------------------------------------- #
|
@@ -210,7 +307,9 @@ class ClaudeAgent:
|
|
210 |
save_and_read_file,
|
211 |
analyze_csv_file,
|
212 |
analyze_excel_file,
|
213 |
-
gaia_file_reader
|
|
|
|
|
214 |
]
|
215 |
|
216 |
# Create the CodeAgent
|
|
|
1 |
"""
|
2 |
+
agent.py - Claude implementation for GAIA challenge
|
3 |
-----------------------------------------------------------
|
4 |
A simplified implementation with direct litellm access to Anthropic's Claude
|
5 |
"""
|
|
|
114 |
raise
|
115 |
|
116 |
# --------------------------------------------------------------------------- #
|
117 |
+
# Tools section - All tools used by the agent
|
118 |
# --------------------------------------------------------------------------- #
|
119 |
@tool
|
120 |
def gaia_file_reader(file_id: str) -> str:
|
121 |
"""
|
122 |
+
Download a GAIA attachment and return its contents.
|
123 |
+
|
124 |
+
Args:
|
125 |
+
file_id: The identifier of the file to download from GAIA API.
|
126 |
+
|
127 |
+
Returns:
|
128 |
+
The content of the file as a string (text files) or base64-encoded (binary files).
|
129 |
"""
|
130 |
try:
|
131 |
raw = _download_file(file_id)
|
|
|
136 |
except Exception as exc:
|
137 |
return f"ERROR downloading {file_id}: {exc}"
|
138 |
|
|
|
|
|
|
|
139 |
@tool
|
140 |
def save_and_read_file(content: str, filename: Optional[str] = None) -> str:
|
141 |
+
"""
|
142 |
+
Save content to a temporary file and return the path.
|
143 |
+
|
144 |
+
Args:
|
145 |
+
content: The content to save to the file.
|
146 |
+
filename: Optional filename, will generate a random name if not provided.
|
147 |
+
|
148 |
+
Returns:
|
149 |
+
Path to the saved file.
|
150 |
+
"""
|
151 |
temp_dir = tempfile.gettempdir()
|
152 |
if filename is None:
|
153 |
temp_file = tempfile.NamedTemporaryFile(delete=False)
|
|
|
162 |
|
163 |
@tool
|
164 |
def analyze_csv_file(file_path: str, query: str) -> str:
|
165 |
+
"""
|
166 |
+
Analyze a CSV file using pandas and answer questions about it.
|
167 |
+
|
168 |
+
Args:
|
169 |
+
file_path: Path to the CSV file to analyze.
|
170 |
+
query: A question or instruction about what to analyze in the file.
|
171 |
+
|
172 |
+
Returns:
|
173 |
+
Analysis results as text.
|
174 |
+
"""
|
175 |
try:
|
176 |
import pandas as pd
|
177 |
df = pd.read_csv(file_path)
|
|
|
189 |
|
190 |
@tool
|
191 |
def analyze_excel_file(file_path: str, query: str) -> str:
|
192 |
+
"""
|
193 |
+
Analyze an Excel file using pandas and answer questions about it.
|
194 |
+
|
195 |
+
Args:
|
196 |
+
file_path: Path to the Excel file to analyze.
|
197 |
+
query: A question or instruction about what to analyze in the file.
|
198 |
+
|
199 |
+
Returns:
|
200 |
+
Analysis results as text.
|
201 |
+
"""
|
202 |
try:
|
203 |
import pandas as pd
|
204 |
df = pd.read_excel(file_path)
|
|
|
214 |
except Exception as e:
|
215 |
return f"Error analyzing Excel file: {str(e)}"
|
216 |
|
217 |
+
@tool
|
218 |
+
def download_file_from_url(url: str, filename: Optional[str] = None) -> str:
|
219 |
+
"""
|
220 |
+
Download a file from a URL and save it to a temporary location.
|
221 |
+
|
222 |
+
Args:
|
223 |
+
url: The URL to download from.
|
224 |
+
filename: Optional filename, will generate one based on URL if not provided.
|
225 |
+
|
226 |
+
Returns:
|
227 |
+
Path to the downloaded file.
|
228 |
+
"""
|
229 |
+
try:
|
230 |
+
# Parse URL to get filename if not provided
|
231 |
+
if not filename:
|
232 |
+
path = urlparse(url).path
|
233 |
+
filename = os.path.basename(path)
|
234 |
+
if not filename:
|
235 |
+
# Generate a random name if we couldn't extract one
|
236 |
+
import uuid
|
237 |
+
filename = f"downloaded_{uuid.uuid4().hex[:8]}"
|
238 |
+
|
239 |
+
# Create temporary file
|
240 |
+
temp_dir = tempfile.gettempdir()
|
241 |
+
filepath = os.path.join(temp_dir, filename)
|
242 |
+
|
243 |
+
# Download the file
|
244 |
+
response = requests.get(url, stream=True)
|
245 |
+
response.raise_for_status()
|
246 |
+
|
247 |
+
# Save the file
|
248 |
+
with open(filepath, 'wb') as f:
|
249 |
+
for chunk in response.iter_content(chunk_size=8192):
|
250 |
+
f.write(chunk)
|
251 |
+
|
252 |
+
return f"File downloaded to {filepath}. You can now process this file."
|
253 |
+
except Exception as e:
|
254 |
+
return f"Error downloading file: {str(e)}"
|
255 |
+
|
256 |
+
@tool
|
257 |
+
def extract_text_from_image(image_path: str) -> str:
|
258 |
+
"""
|
259 |
+
Extract text from an image using pytesseract (if available).
|
260 |
+
|
261 |
+
Args:
|
262 |
+
image_path: Path to the image file to extract text from.
|
263 |
+
|
264 |
+
Returns:
|
265 |
+
Extracted text from the image.
|
266 |
+
"""
|
267 |
+
try:
|
268 |
+
# Try to import pytesseract
|
269 |
+
import pytesseract
|
270 |
+
from PIL import Image
|
271 |
+
|
272 |
+
# Open the image
|
273 |
+
image = Image.open(image_path)
|
274 |
+
|
275 |
+
# Extract text
|
276 |
+
text = pytesseract.image_to_string(image)
|
277 |
+
|
278 |
+
return f"Extracted text from image:\n\n{text}"
|
279 |
+
except ImportError:
|
280 |
+
return "Error: pytesseract is not installed. Please install it with 'pip install pytesseract' and ensure Tesseract OCR is installed on your system."
|
281 |
+
except Exception as e:
|
282 |
+
return f"Error extracting text from image: {str(e)}"
|
283 |
+
|
284 |
# --------------------------------------------------------------------------- #
|
285 |
# ClaudeAgent - Main class for GAIA challenge
|
286 |
# --------------------------------------------------------------------------- #
|
|
|
307 |
save_and_read_file,
|
308 |
analyze_csv_file,
|
309 |
analyze_excel_file,
|
310 |
+
gaia_file_reader,
|
311 |
+
download_file_from_url,
|
312 |
+
extract_text_from_image
|
313 |
]
|
314 |
|
315 |
# Create the CodeAgent
|