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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# GAIA Dataset - Final Working Implementation for 300 Questions
|
| 2 |
+
# Based on actual GAIA dataset structure: 2023_all config with test split
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import numpy as np
|
| 8 |
+
import time
|
| 9 |
+
from typing import Dict, List, Tuple
|
| 10 |
+
import requests
|
| 11 |
+
from datetime import datetime
|
| 12 |
+
import matplotlib.pyplot as plt
|
| 13 |
+
import seaborn as sns
|
| 14 |
+
from tqdm import tqdm
|
| 15 |
+
import warnings
|
| 16 |
+
warnings.filterwarnings('ignore')
|
| 17 |
+
|
| 18 |
+
# Install required packages
|
| 19 |
+
print("📦 Installing required packages...")
|
| 20 |
+
import subprocess
|
| 21 |
+
import sys
|
| 22 |
+
|
| 23 |
+
packages = [
|
| 24 |
+
"langchain-community", "langchain-core", "langchain-google-genai",
|
| 25 |
+
"langchain-groq", "langchain-huggingface", "langgraph", "supabase",
|
| 26 |
+
"sentence-transformers", "tavily-python", "wikipedia", "arxiv",
|
| 27 |
+
"python-dotenv", "gradio", "datasets", "huggingface_hub"
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
+
for package in packages:
|
| 31 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", package])
|
| 32 |
+
|
| 33 |
+
print("✅ All packages installed!")
|
| 34 |
+
|
| 35 |
+
# Import libraries
|
| 36 |
+
from dotenv import load_dotenv
|
| 37 |
+
from langchain_core.messages import HumanMessage, SystemMessage
|
| 38 |
+
from langchain_groq import ChatGroq
|
| 39 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 40 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 41 |
+
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
|
| 42 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 43 |
+
from langchain_community.vectorstores import SupabaseVectorStore
|
| 44 |
+
from langchain_core.tools import tool
|
| 45 |
+
from langchain.tools.retriever import create_retriever_tool
|
| 46 |
+
from langgraph.graph import START, StateGraph, MessagesState
|
| 47 |
+
from langgraph.prebuilt import tools_condition, ToolNode
|
| 48 |
+
from supabase.client import Client, create_client
|
| 49 |
+
from datasets import load_dataset
|
| 50 |
+
from huggingface_hub import login, hf_hub_download
|
| 51 |
+
|
| 52 |
+
# ================================
|
| 53 |
+
# API KEYS SETUP
|
| 54 |
+
# ================================
|
| 55 |
+
|
| 56 |
+
print("🔑 Setting up API keys...")
|
| 57 |
+
|
| 58 |
+
API_KEYS = {
|
| 59 |
+
'GROQ_API_KEY': "",
|
| 60 |
+
'TAVILY_API_KEY': "",
|
| 61 |
+
'SUPABASE_URL': '',
|
| 62 |
+
'SUPABASE_SERVICE_KEY': '',
|
| 63 |
+
'GOOGLE_API_KEY': '',
|
| 64 |
+
'HUGGINGFACEHUB_API_TOKEN': 'h'
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
for key, value in API_KEYS.items():
|
| 68 |
+
os.environ[key] = value
|
| 69 |
+
|
| 70 |
+
print(" API keys configured!")
|
| 71 |
+
|
| 72 |
+
# HuggingFace login
|
| 73 |
+
try:
|
| 74 |
+
login(token=API_KEYS['HUGGINGFACEHUB_API_TOKEN'])
|
| 75 |
+
print("HuggingFace authentication successful!")
|
| 76 |
+
except Exception as e:
|
| 77 |
+
print(f"HuggingFace login warning: {e}")
|
| 78 |
+
|
| 79 |
+
# ================================
|
| 80 |
+
# FINAL GAIA DATASET LOADER
|
| 81 |
+
# ================================
|
| 82 |
+
|
| 83 |
+
class FinalGAIALoader:
|
| 84 |
+
"""Final GAIA dataset loader using correct configuration"""
|
| 85 |
+
|
| 86 |
+
def __init__(self):
|
| 87 |
+
self.dataset = None
|
| 88 |
+
self.test_data = []
|
| 89 |
+
|
| 90 |
+
def load_gaia_dataset(self):
|
| 91 |
+
"""Load GAIA dataset using the correct configuration"""
|
| 92 |
+
print("Loading GAIA dataset with correct configuration...")
|
| 93 |
+
|
| 94 |
+
try:
|
| 95 |
+
# Based on the dataset code, use "2023_all" config
|
| 96 |
+
print("Loading with config '2023_all'...")
|
| 97 |
+
|
| 98 |
+
self.dataset = load_dataset(
|
| 99 |
+
"gaia-benchmark/GAIA",
|
| 100 |
+
"2023_all",
|
| 101 |
+
token=True,
|
| 102 |
+
trust_remote_code=True # Important for custom dataset scripts
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
print(f"Dataset loaded successfully!")
|
| 106 |
+
print(f"Available splits: {list(self.dataset.keys())}")
|
| 107 |
+
|
| 108 |
+
if 'test' in self.dataset:
|
| 109 |
+
self.test_data = list(self.dataset['test'])
|
| 110 |
+
print(f"Loaded {len(self.test_data)} test questions")
|
| 111 |
+
|
| 112 |
+
# Analyze the data structure
|
| 113 |
+
self._analyze_data_structure()
|
| 114 |
+
return True
|
| 115 |
+
else:
|
| 116 |
+
print("No test split found")
|
| 117 |
+
return False
|
| 118 |
+
|
| 119 |
+
except Exception as e:
|
| 120 |
+
print(f"Primary method failed: {e}")
|
| 121 |
+
|
| 122 |
+
# Fallback: Try without trust_remote_code
|
| 123 |
+
try:
|
| 124 |
+
print("Trying fallback method...")
|
| 125 |
+
self.dataset = load_dataset(
|
| 126 |
+
"gaia-benchmark/GAIA",
|
| 127 |
+
"2023_all",
|
| 128 |
+
token=True
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
if 'test' in self.dataset:
|
| 132 |
+
self.test_data = list(self.dataset['test'])
|
| 133 |
+
print(f"Fallback successful! Loaded {len(self.test_data)} questions")
|
| 134 |
+
self._analyze_data_structure()
|
| 135 |
+
return True
|
| 136 |
+
|
| 137 |
+
except Exception as e2:
|
| 138 |
+
print(f"Fallback also failed: {e2}")
|
| 139 |
+
|
| 140 |
+
# Final fallback: Manual download
|
| 141 |
+
return self._manual_download_fallback()
|
| 142 |
+
|
| 143 |
+
def _manual_download_fallback(self):
|
| 144 |
+
"""Manual download fallback method"""
|
| 145 |
+
print("Attempting manual download...")
|
| 146 |
+
|
| 147 |
+
try:
|
| 148 |
+
# Download the metadata file directly
|
| 149 |
+
metadata_file = hf_hub_download(
|
| 150 |
+
repo_id="gaia-benchmark/GAIA",
|
| 151 |
+
filename="2023/test/metadata.jsonl",
|
| 152 |
+
repo_type="dataset",
|
| 153 |
+
token=API_KEYS['HUGGINGFACEHUB_API_TOKEN']
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
# Read the JSONL file
|
| 157 |
+
test_questions = []
|
| 158 |
+
with open(metadata_file, 'r', encoding='utf-8') as f:
|
| 159 |
+
for line in f:
|
| 160 |
+
if line.strip():
|
| 161 |
+
test_questions.append(json.loads(line))
|
| 162 |
+
|
| 163 |
+
self.test_data = test_questions
|
| 164 |
+
print(f"Manual download successful! Loaded {len(test_questions)} questions")
|
| 165 |
+
self._analyze_data_structure()
|
| 166 |
+
return True
|
| 167 |
+
|
| 168 |
+
except Exception as e:
|
| 169 |
+
print(f"Manual download failed: {e}")
|
| 170 |
+
return False
|
| 171 |
+
|
| 172 |
+
def _analyze_data_structure(self):
|
| 173 |
+
"""Analyze the structure of loaded GAIA data"""
|
| 174 |
+
if not self.test_data:
|
| 175 |
+
return
|
| 176 |
+
|
| 177 |
+
print(f"\n GAIA Data Analysis:")
|
| 178 |
+
print(f"Total questions: {len(self.test_data)}")
|
| 179 |
+
|
| 180 |
+
# Show sample item structure
|
| 181 |
+
if self.test_data:
|
| 182 |
+
sample = self.test_data[0]
|
| 183 |
+
print(f"Sample item keys: {list(sample.keys())}")
|
| 184 |
+
|
| 185 |
+
# Level distribution
|
| 186 |
+
levels = [item.get('Level') for item in self.test_data]
|
| 187 |
+
level_counts = pd.Series(levels).value_counts().sort_index()
|
| 188 |
+
print(f"\nLevel distribution:")
|
| 189 |
+
for level, count in level_counts.items():
|
| 190 |
+
print(f" Level {level}: {count} questions")
|
| 191 |
+
|
| 192 |
+
# Sample questions
|
| 193 |
+
print(f"\nSample questions by level:")
|
| 194 |
+
for level in sorted(level_counts.index):
|
| 195 |
+
level_questions = [item for item in self.test_data if item.get('Level') == level]
|
| 196 |
+
if level_questions:
|
| 197 |
+
sample_q = level_questions[0]['Question'][:100]
|
| 198 |
+
print(f" Level {level}: {sample_q}...")
|
| 199 |
+
|
| 200 |
+
def filter_300_questions(self, level1_ratio=0.6, level2_ratio=0.25, level3_ratio=0.15):
|
| 201 |
+
"""Filter exactly 300 questions with specified ratios"""
|
| 202 |
+
|
| 203 |
+
if not self.test_data:
|
| 204 |
+
print("No test data available")
|
| 205 |
+
return []
|
| 206 |
+
|
| 207 |
+
total_questions = 300
|
| 208 |
+
level1_count = int(total_questions * level1_ratio) # 180
|
| 209 |
+
level2_count = int(total_questions * level2_ratio) # 75
|
| 210 |
+
level3_count = total_questions - level1_count - level2_count # 45
|
| 211 |
+
|
| 212 |
+
print(f"Filtering {total_questions} questions:")
|
| 213 |
+
print(f" Level 1: {level1_count} questions ({level1_ratio*100:.0f}%)")
|
| 214 |
+
print(f" Level 2: {level2_count} questions ({level2_ratio*100:.0f}%)")
|
| 215 |
+
print(f" Level 3: {level3_count} questions ({level3_ratio*100:.0f}%)")
|
| 216 |
+
|
| 217 |
+
# Group by level
|
| 218 |
+
level_groups = {1: [], 2: [], 3: []}
|
| 219 |
+
for item in self.test_data:
|
| 220 |
+
level = item.get('Level')
|
| 221 |
+
if level in level_groups:
|
| 222 |
+
level_groups[level].append(item)
|
| 223 |
+
|
| 224 |
+
# Check availability
|
| 225 |
+
print(f"\nAvailable questions by level:")
|
| 226 |
+
for level in [1, 2, 3]:
|
| 227 |
+
available = len(level_groups[level])
|
| 228 |
+
print(f" Level {level}: {available} available")
|
| 229 |
+
|
| 230 |
+
# Sample questions
|
| 231 |
+
filtered_questions = []
|
| 232 |
+
np.random.seed(42) # For reproducibility
|
| 233 |
+
|
| 234 |
+
for level, target_count in [(1, level1_count), (2, level2_count), (3, level3_count)]:
|
| 235 |
+
available = len(level_groups[level])
|
| 236 |
+
|
| 237 |
+
if available >= target_count:
|
| 238 |
+
# Random sample
|
| 239 |
+
sampled_indices = np.random.choice(available, size=target_count, replace=False)
|
| 240 |
+
sampled = [level_groups[level][i] for i in sampled_indices]
|
| 241 |
+
filtered_questions.extend(sampled)
|
| 242 |
+
print(f"Level {level}: Selected {target_count} from {available}")
|
| 243 |
+
else:
|
| 244 |
+
# Take all available
|
| 245 |
+
filtered_questions.extend(level_groups[level])
|
| 246 |
+
print(f"Level {level}: Only {available} available (needed {target_count})")
|
| 247 |
+
|
| 248 |
+
print(f"\n📊 Total filtered: {len(filtered_questions)} questions")
|
| 249 |
+
|
| 250 |
+
# Verify final distribution
|
| 251 |
+
final_levels = [q['Level'] for q in filtered_questions]
|
| 252 |
+
final_dist = pd.Series(final_levels).value_counts().sort_index()
|
| 253 |
+
print(f"Final distribution:")
|
| 254 |
+
for level, count in final_dist.items():
|
| 255 |
+
percentage = (count / len(filtered_questions)) * 100
|
| 256 |
+
print(f" Level {level}: {count} questions ({percentage:.1f}%)")
|
| 257 |
+
|
| 258 |
+
return filtered_questions
|
| 259 |
+
|
| 260 |
+
def create_dataframe(self, questions):
|
| 261 |
+
"""Create DataFrame from filtered questions"""
|
| 262 |
+
if not questions:
|
| 263 |
+
return pd.DataFrame()
|
| 264 |
+
|
| 265 |
+
data = []
|
| 266 |
+
for i, item in enumerate(questions):
|
| 267 |
+
data.append({
|
| 268 |
+
'id': i,
|
| 269 |
+
'task_id': item.get('task_id', f'gaia_{i}'),
|
| 270 |
+
'question': item.get('Question', ''),
|
| 271 |
+
'level': item.get('Level', 1),
|
| 272 |
+
'final_answer': item.get('Final answer', ''),
|
| 273 |
+
'file_name': item.get('file_name', ''),
|
| 274 |
+
'annotator_metadata': item.get('Annotator Metadata', {})
|
| 275 |
+
})
|
| 276 |
+
|
| 277 |
+
df = pd.DataFrame(data)
|
| 278 |
+
print(f"📋 Created DataFrame with {len(df)} questions")
|
| 279 |
+
return df
|
| 280 |
+
|
| 281 |
+
# ================================
|
| 282 |
+
# ENHANCED SYSTEM PROMPT
|
| 283 |
+
# ================================
|
| 284 |
+
|
| 285 |
+
SYSTEM_PROMPT = """
|
| 286 |
+
You are an expert research assistant designed to answer complex, multi-step questions from the GAIA benchmark.
|
| 287 |
+
You have access to powerful tools for web search, Wikipedia, arXiv research, and mathematical calculations.
|
| 288 |
+
|
| 289 |
+
Key Instructions:
|
| 290 |
+
1. Read the question carefully and identify what information you need
|
| 291 |
+
2. Use tools strategically - web search for current info, Wikipedia for general knowledge, arXiv for research
|
| 292 |
+
3. For mathematical questions, use the calculation tools
|
| 293 |
+
4. Think step-by-step and show your reasoning
|
| 294 |
+
5. Be precise and accurate in your final answer
|
| 295 |
+
|
| 296 |
+
Answer Format:
|
| 297 |
+
- Provide clear reasoning and methodology
|
| 298 |
+
- Show any calculations or research steps
|
| 299 |
+
- End with: FINAL ANSWER: [exact answer]
|
| 300 |
+
- Keep the final answer concise and precise
|
| 301 |
+
- Match the format requested (number, name, list, etc.)
|
| 302 |
+
|
| 303 |
+
Examples:
|
| 304 |
+
Question: What is the population of Tokyo according to the latest census?
|
| 305 |
+
I need to search for the most recent population data for Tokyo.
|
| 306 |
+
[uses web_search("Tokyo population latest census")]
|
| 307 |
+
Based on the search results, the latest census shows...
|
| 308 |
+
FINAL ANSWER: 13960000
|
| 309 |
+
|
| 310 |
+
Question: What is 157 * 238?
|
| 311 |
+
I need to calculate 157 multiplied by 238.
|
| 312 |
+
[uses multiply(157, 238)]
|
| 313 |
+
FINAL ANSWER: 37366
|
| 314 |
+
"""
|
| 315 |
+
|
| 316 |
+
# ================================
|
| 317 |
+
# ENHANCED TOOLS
|
| 318 |
+
# ================================
|
| 319 |
+
|
| 320 |
+
@tool
|
| 321 |
+
def multiply(a: float, b: float) -> float:
|
| 322 |
+
"""Multiply two numbers."""
|
| 323 |
+
return a * b
|
| 324 |
+
|
| 325 |
+
@tool
|
| 326 |
+
def add(a: float, b: float) -> float:
|
| 327 |
+
"""Add two numbers."""
|
| 328 |
+
return a + b
|
| 329 |
+
|
| 330 |
+
@tool
|
| 331 |
+
def subtract(a: float, b: float) -> float:
|
| 332 |
+
"""Subtract two numbers."""
|
| 333 |
+
return a - b
|
| 334 |
+
|
| 335 |
+
@tool
|
| 336 |
+
def divide(a: float, b: float) -> float:
|
| 337 |
+
"""Divide two numbers."""
|
| 338 |
+
if abs(b) < 1e-10:
|
| 339 |
+
raise ValueError("Cannot divide by zero.")
|
| 340 |
+
return a / b
|
| 341 |
+
|
| 342 |
+
@tool
|
| 343 |
+
def power(a: float, b: float) -> float:
|
| 344 |
+
"""Calculate a raised to the power of b."""
|
| 345 |
+
return a ** b
|
| 346 |
+
|
| 347 |
+
@tool
|
| 348 |
+
def square_root(a: float) -> float:
|
| 349 |
+
"""Calculate the square root of a number."""
|
| 350 |
+
if a < 0:
|
| 351 |
+
raise ValueError("Cannot calculate square root of negative number.")
|
| 352 |
+
return a ** 0.5
|
| 353 |
+
|
| 354 |
+
@tool
|
| 355 |
+
def modulo(a: float, b: float) -> float:
|
| 356 |
+
"""Calculate a modulo b (remainder after division)."""
|
| 357 |
+
return a % b
|
| 358 |
+
|
| 359 |
+
@tool
|
| 360 |
+
def absolute_value(a: float) -> float:
|
| 361 |
+
"""Calculate the absolute value of a number."""
|
| 362 |
+
return abs(a)
|
| 363 |
+
|
| 364 |
+
@tool
|
| 365 |
+
def wiki_search(query: str) -> str:
|
| 366 |
+
"""Search Wikipedia for comprehensive information on a topic."""
|
| 367 |
+
try:
|
| 368 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=3).load()
|
| 369 |
+
if not search_docs:
|
| 370 |
+
return f"No Wikipedia results found for: {query}"
|
| 371 |
+
|
| 372 |
+
# Return the most relevant result with more content
|
| 373 |
+
result = search_docs[0]
|
| 374 |
+
title = result.metadata.get('title', 'Unknown')
|
| 375 |
+
content = result.page_content[:1500]
|
| 376 |
+
|
| 377 |
+
return f"Wikipedia article '{title}':\n{content}..."
|
| 378 |
+
except Exception as e:
|
| 379 |
+
return f"Wikipedia search failed: {str(e)}"
|
| 380 |
+
|
| 381 |
+
@tool
|
| 382 |
+
def web_search(query: str) -> str:
|
| 383 |
+
"""Search the web for current, factual information."""
|
| 384 |
+
try:
|
| 385 |
+
search_tool = TavilySearchResults(max_results=4)
|
| 386 |
+
results = search_tool.invoke(query)
|
| 387 |
+
|
| 388 |
+
if not results:
|
| 389 |
+
return f"No web results found for: {query}"
|
| 390 |
+
|
| 391 |
+
formatted_results = f"Web search results for '{query}':\n\n"
|
| 392 |
+
for i, result in enumerate(results, 1):
|
| 393 |
+
url = result.get('url', 'Unknown URL')
|
| 394 |
+
content = result.get('content', 'No content')[:800]
|
| 395 |
+
formatted_results += f"{i}. Source: {url}\n{content}...\n\n"
|
| 396 |
+
|
| 397 |
+
return formatted_results
|
| 398 |
+
except Exception as e:
|
| 399 |
+
return f"Web search failed: {str(e)}"
|
| 400 |
+
|
| 401 |
+
@tool
|
| 402 |
+
def arxiv_search(query: str) -> str:
|
| 403 |
+
"""Search arXiv for academic papers and research."""
|
| 404 |
+
try:
|
| 405 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 406 |
+
if not search_docs:
|
| 407 |
+
return f"No arXiv papers found for: {query}"
|
| 408 |
+
|
| 409 |
+
formatted_results = f"arXiv search results for '{query}':\n\n"
|
| 410 |
+
for i, doc in enumerate(search_docs, 1):
|
| 411 |
+
title = doc.metadata.get('Title', 'Unknown Title')
|
| 412 |
+
authors = doc.metadata.get('Authors', 'Unknown Authors')
|
| 413 |
+
content = doc.page_content[:1000]
|
| 414 |
+
formatted_results += f"{i}. Title: {title}\nAuthors: {authors}\nAbstract: {content}...\n\n"
|
| 415 |
+
|
| 416 |
+
return formatted_results
|
| 417 |
+
except Exception as e:
|
| 418 |
+
return f"arXiv search failed: {str(e)}"
|
| 419 |
+
|
| 420 |
+
# All tools
|
| 421 |
+
tools = [
|
| 422 |
+
multiply, add, subtract, divide, power, square_root, modulo, absolute_value,
|
| 423 |
+
wiki_search, web_search, arxiv_search
|
| 424 |
+
]
|
| 425 |
+
|
| 426 |
+
# ================================
|
| 427 |
+
# ENHANCED GAIA AGENT
|
| 428 |
+
# ================================
|
| 429 |
+
|
| 430 |
+
class EnhancedGAIAAgent:
|
| 431 |
+
"""Enhanced GAIA agent optimized for benchmark performance"""
|
| 432 |
+
|
| 433 |
+
def __init__(self, provider="groq", model="llama3-70b-8192"):
|
| 434 |
+
self.provider = provider
|
| 435 |
+
self.model = model
|
| 436 |
+
self.graph = self._build_graph()
|
| 437 |
+
|
| 438 |
+
def _build_graph(self):
|
| 439 |
+
"""Build optimized agent graph"""
|
| 440 |
+
print(f"Building enhanced agent: {self.provider} {self.model}")
|
| 441 |
+
|
| 442 |
+
if self.provider == "groq":
|
| 443 |
+
llm = ChatGroq(
|
| 444 |
+
model=self.model,
|
| 445 |
+
temperature=0,
|
| 446 |
+
max_retries=3,
|
| 447 |
+
timeout=120
|
| 448 |
+
)
|
| 449 |
+
elif self.provider == "google":
|
| 450 |
+
llm = ChatGoogleGenerativeAI(
|
| 451 |
+
model="gemini-pro",
|
| 452 |
+
temperature=0,
|
| 453 |
+
max_retries=3
|
| 454 |
+
)
|
| 455 |
+
else:
|
| 456 |
+
raise ValueError("Choose 'groq' or 'google'")
|
| 457 |
+
|
| 458 |
+
llm_with_tools = llm.bind_tools(tools)
|
| 459 |
+
|
| 460 |
+
def assistant_node(state: MessagesState):
|
| 461 |
+
"""Enhanced assistant node with better error handling"""
|
| 462 |
+
try:
|
| 463 |
+
messages = state["messages"]
|
| 464 |
+
|
| 465 |
+
# Add system message if not present
|
| 466 |
+
if not any(isinstance(msg, SystemMessage) for msg in messages):
|
| 467 |
+
messages = [SystemMessage(content=SYSTEM_PROMPT)] + messages
|
| 468 |
+
|
| 469 |
+
# Invoke LLM
|
| 470 |
+
response = llm_with_tools.invoke(messages)
|
| 471 |
+
return {"messages": [response]}
|
| 472 |
+
|
| 473 |
+
except Exception as e:
|
| 474 |
+
error_msg = f"Assistant error: {str(e)}"
|
| 475 |
+
print(f" {error_msg}")
|
| 476 |
+
return {"messages": [HumanMessage(content=f"Error: {error_msg}")]}
|
| 477 |
+
|
| 478 |
+
# Build graph
|
| 479 |
+
builder = StateGraph(MessagesState)
|
| 480 |
+
builder.add_node("assistant", assistant_node)
|
| 481 |
+
builder.add_node("tools", ToolNode(tools))
|
| 482 |
+
|
| 483 |
+
builder.add_edge(START, "assistant")
|
| 484 |
+
builder.add_conditional_edges("assistant", tools_condition)
|
| 485 |
+
builder.add_edge("tools", "assistant")
|
| 486 |
+
|
| 487 |
+
return builder.compile()
|
| 488 |
+
|
| 489 |
+
def process_question(self, question: str, question_id: str = None) -> Dict:
|
| 490 |
+
"""Process a single GAIA question with enhanced handling"""
|
| 491 |
+
start_time = time.time()
|
| 492 |
+
|
| 493 |
+
try:
|
| 494 |
+
# Create message and invoke graph
|
| 495 |
+
messages = [HumanMessage(content=question)]
|
| 496 |
+
result = self.graph.invoke({"messages": messages})
|
| 497 |
+
|
| 498 |
+
# Extract final response
|
| 499 |
+
final_response = result["messages"][-1].content
|
| 500 |
+
|
| 501 |
+
# Extract final answer more robustly
|
| 502 |
+
answer = self._extract_final_answer(final_response)
|
| 503 |
+
|
| 504 |
+
processing_time = time.time() - start_time
|
| 505 |
+
|
| 506 |
+
return {
|
| 507 |
+
'question_id': question_id,
|
| 508 |
+
'question': question,
|
| 509 |
+
'full_response': final_response,
|
| 510 |
+
'final_answer': answer,
|
| 511 |
+
'processing_time': processing_time,
|
| 512 |
+
'status': 'success',
|
| 513 |
+
'error': None
|
| 514 |
+
}
|
| 515 |
+
|
| 516 |
+
except Exception as e:
|
| 517 |
+
processing_time = time.time() - start_time
|
| 518 |
+
error_msg = str(e)
|
| 519 |
+
|
| 520 |
+
return {
|
| 521 |
+
'question_id': question_id,
|
| 522 |
+
'question': question,
|
| 523 |
+
'full_response': f"Error: {error_msg}",
|
| 524 |
+
'final_answer': f"ERROR: {error_msg}",
|
| 525 |
+
'processing_time': processing_time,
|
| 526 |
+
'status': 'error',
|
| 527 |
+
'error': error_msg
|
| 528 |
+
}
|
| 529 |
+
|
| 530 |
+
def _extract_final_answer(self, response: str) -> str:
|
| 531 |
+
"""Extract final answer more robustly"""
|
| 532 |
+
if "FINAL ANSWER:" in response:
|
| 533 |
+
answer = response.split("FINAL ANSWER:")[-1].strip()
|
| 534 |
+
# Clean up the answer
|
| 535 |
+
answer = answer.split('\n')[0].strip() # Take first line only
|
| 536 |
+
return answer
|
| 537 |
+
else:
|
| 538 |
+
# Fallback: take last substantial line
|
| 539 |
+
lines = [line.strip() for line in response.split('\n') if line.strip()]
|
| 540 |
+
return lines[-1] if lines else response.strip()
|
| 541 |
+
|
| 542 |
+
def evaluate_300_questions(self, questions_df: pd.DataFrame) -> pd.DataFrame:
|
| 543 |
+
"""Evaluate 300 GAIA questions with comprehensive tracking"""
|
| 544 |
+
print(f"Starting GAIA evaluation: {len(questions_df)} questions")
|
| 545 |
+
print(f" Estimated time: {len(questions_df) * 15 / 60:.0f}-{len(questions_df) * 25 / 60:.0f} minutes")
|
| 546 |
+
|
| 547 |
+
results = []
|
| 548 |
+
|
| 549 |
+
# Progress tracking
|
| 550 |
+
with tqdm(total=len(questions_df), desc="GAIA Evaluation Progress") as pbar:
|
| 551 |
+
for idx, row in questions_df.iterrows():
|
| 552 |
+
question_id = row.get('task_id', f'gaia_{idx}')
|
| 553 |
+
question = row['question']
|
| 554 |
+
level = row['level']
|
| 555 |
+
expected = row.get('final_answer', '')
|
| 556 |
+
|
| 557 |
+
print(f"\n Question {idx+1}/{len(questions_df)} - Level {level}")
|
| 558 |
+
print(f"ID: {question_id}")
|
| 559 |
+
print(f"Q: {question[:120]}...")
|
| 560 |
+
|
| 561 |
+
# Process question
|
| 562 |
+
result = self.process_question(question, question_id)
|
| 563 |
+
|
| 564 |
+
# Add metadata
|
| 565 |
+
result.update({
|
| 566 |
+
'level': level,
|
| 567 |
+
'expected_answer': expected,
|
| 568 |
+
'question_index': idx
|
| 569 |
+
})
|
| 570 |
+
|
| 571 |
+
results.append(result)
|
| 572 |
+
|
| 573 |
+
# Show result
|
| 574 |
+
status_emoji = "" if result['status'] == 'success' else ""
|
| 575 |
+
print(f"A: {result['final_answer'][:120]}...")
|
| 576 |
+
print(f"{status_emoji} Time: {result['processing_time']:.2f}s")
|
| 577 |
+
|
| 578 |
+
# Update progress bar
|
| 579 |
+
pbar.update(1)
|
| 580 |
+
pbar.set_postfix({
|
| 581 |
+
'Success Rate': f"{len([r for r in results if r['status'] == 'success'])/len(results):.1%}",
|
| 582 |
+
'Avg Time': f"{np.mean([r['processing_time'] for r in results]):.1f}s"
|
| 583 |
+
})
|
| 584 |
+
|
| 585 |
+
# Save progress every 25 questions
|
| 586 |
+
if (idx + 1) % 25 == 0:
|
| 587 |
+
temp_df = pd.DataFrame(results)
|
| 588 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 589 |
+
temp_df.to_csv(f'gaia_progress_{timestamp}.csv', index=False)
|
| 590 |
+
print(f"💾 Progress saved at question {idx+1}")
|
| 591 |
+
|
| 592 |
+
# Rate limiting
|
| 593 |
+
time.sleep(1.5)
|
| 594 |
+
|
| 595 |
+
results_df = pd.DataFrame(results)
|
| 596 |
+
print(f"\n GAIA evaluation completed!")
|
| 597 |
+
return results_df
|
| 598 |
+
|
| 599 |
+
# ================================
|
| 600 |
+
# ANALYSIS FUNCTIONS
|
| 601 |
+
# ================================
|
| 602 |
+
|
| 603 |
+
def analyze_gaia_results(results_df: pd.DataFrame) -> Dict:
|
| 604 |
+
"""Comprehensive analysis of GAIA results"""
|
| 605 |
+
|
| 606 |
+
if results_df.empty:
|
| 607 |
+
return {}
|
| 608 |
+
|
| 609 |
+
# Basic metrics
|
| 610 |
+
total = len(results_df)
|
| 611 |
+
successful = len(results_df[results_df['status'] == 'success'])
|
| 612 |
+
success_rate = successful / total
|
| 613 |
+
error_rate = 1 - success_rate
|
| 614 |
+
|
| 615 |
+
metrics = {
|
| 616 |
+
'total_questions': total,
|
| 617 |
+
'successful_runs': successful,
|
| 618 |
+
'success_rate': success_rate,
|
| 619 |
+
'error_rate': error_rate,
|
| 620 |
+
'avg_processing_time': results_df['processing_time'].mean(),
|
| 621 |
+
'median_processing_time': results_df['processing_time'].median(),
|
| 622 |
+
'total_processing_time': results_df['processing_time'].sum()
|
| 623 |
+
}
|
| 624 |
+
|
| 625 |
+
# Level-wise analysis
|
| 626 |
+
level_metrics = {}
|
| 627 |
+
for level in sorted(results_df['level'].unique()):
|
| 628 |
+
level_data = results_df[results_df['level'] == level]
|
| 629 |
+
level_success = len(level_data[level_data['status'] == 'success'])
|
| 630 |
+
|
| 631 |
+
level_metrics[f'level_{level}'] = {
|
| 632 |
+
'count': len(level_data),
|
| 633 |
+
'success_count': level_success,
|
| 634 |
+
'success_rate': level_success / len(level_data),
|
| 635 |
+
'error_rate': 1 - (level_success / len(level_data)),
|
| 636 |
+
'avg_time': level_data['processing_time'].mean()
|
| 637 |
+
}
|
| 638 |
+
|
| 639 |
+
metrics['by_level'] = level_metrics
|
| 640 |
+
|
| 641 |
+
# Print comprehensive analysis
|
| 642 |
+
print(f"\n GAIA Benchmark Results Analysis")
|
| 643 |
+
print(f"=" * 60)
|
| 644 |
+
print(f" Total Questions: {total}")
|
| 645 |
+
print(f" Successful: {successful}")
|
| 646 |
+
print(f" Overall Success Rate: {success_rate:.2%}")
|
| 647 |
+
print(f" Error Rate: {error_rate:.2%}")
|
| 648 |
+
print(f" Average Time: {metrics['avg_processing_time']:.2f}s")
|
| 649 |
+
print(f" Total Time: {metrics['total_processing_time']/60:.1f} minutes")
|
| 650 |
+
|
| 651 |
+
print(f"\n Performance by Difficulty Level:")
|
| 652 |
+
for level_key, level_data in level_metrics.items():
|
| 653 |
+
level_num = level_key.split('_')[1]
|
| 654 |
+
print(f"Level {level_num}:")
|
| 655 |
+
print(f" Questions: {level_data['count']}")
|
| 656 |
+
print(f" Success: {level_data['success_count']}/{level_data['count']} ({level_data['success_rate']:.1%})")
|
| 657 |
+
print(f" Avg Time: {level_data['avg_time']:.1f}s")
|
| 658 |
+
|
| 659 |
+
return metrics
|
| 660 |
+
|
| 661 |
+
def save_gaia_results(results_df: pd.DataFrame, metrics: Dict):
|
| 662 |
+
"""Save comprehensive GAIA results"""
|
| 663 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 664 |
+
|
| 665 |
+
# Save detailed results
|
| 666 |
+
results_filename = f'gaia_300_final_results_{timestamp}.csv'
|
| 667 |
+
results_df.to_csv(results_filename, index=False)
|
| 668 |
+
print(f" Detailed results: {results_filename}")
|
| 669 |
+
|
| 670 |
+
# Save metrics
|
| 671 |
+
metrics_filename = f'gaia_300_metrics_{timestamp}.json'
|
| 672 |
+
with open(metrics_filename, 'w') as f:
|
| 673 |
+
json.dump(metrics, f, indent=2, default=str)
|
| 674 |
+
print(f" Metrics: {metrics_filename}")
|
| 675 |
+
|
| 676 |
+
# Create summary report
|
| 677 |
+
report = f"""
|
| 678 |
+
# GAIA Benchmark - 300 Questions Evaluation Report
|
| 679 |
+
Generated: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
|
| 680 |
+
|
| 681 |
+
## Overall Performance
|
| 682 |
+
- Total Questions: {metrics['total_questions']}
|
| 683 |
+
- Success Rate: {metrics['success_rate']:.2%}
|
| 684 |
+
- Average Time: {metrics['avg_processing_time']:.2f}s
|
| 685 |
+
- Total Time: {metrics['total_processing_time']/60:.1f} minutes
|
| 686 |
+
|
| 687 |
+
## Level Performance
|
| 688 |
+
"""
|
| 689 |
+
|
| 690 |
+
for level_key, level_data in metrics['by_level'].items():
|
| 691 |
+
level_num = level_key.split('_')[1]
|
| 692 |
+
report += f"""
|
| 693 |
+
### Level {level_num}
|
| 694 |
+
- Questions: {level_data['count']}
|
| 695 |
+
- Success Rate: {level_data['success_rate']:.2%}
|
| 696 |
+
- Average Time: {level_data['avg_time']:.2f}s
|
| 697 |
+
"""
|
| 698 |
+
|
| 699 |
+
report_filename = f'gaia_300_report_{timestamp}.txt'
|
| 700 |
+
with open(report_filename, 'w') as f:
|
| 701 |
+
f.write(report)
|
| 702 |
+
print(f"💾 Report: {report_filename}")
|
| 703 |
+
|
| 704 |
+
return timestamp
|
| 705 |
+
|
| 706 |
+
# ================================
|
| 707 |
+
# MAIN EXECUTION
|
| 708 |
+
# ================================
|
| 709 |
+
|
| 710 |
+
def main_gaia_300():
|
| 711 |
+
"""Main function for 300 GAIA questions evaluation"""
|
| 712 |
+
print(" GAIA Benchmark - 300 Questions Evaluation")
|
| 713 |
+
print("=" * 60)
|
| 714 |
+
|
| 715 |
+
try:
|
| 716 |
+
# Step 1: Load GAIA dataset
|
| 717 |
+
print("\n Step 1: Loading GAIA Dataset")
|
| 718 |
+
loader = FinalGAIALoader()
|
| 719 |
+
|
| 720 |
+
if not loader.load_gaia_dataset():
|
| 721 |
+
print(" Failed to load GAIA dataset")
|
| 722 |
+
return None, None
|
| 723 |
+
|
| 724 |
+
# Step 2: Filter 300 questions
|
| 725 |
+
print(f"\n Step 2: Filtering 300 Questions")
|
| 726 |
+
filtered_questions = loader.filter_300_questions()
|
| 727 |
+
|
| 728 |
+
if len(filtered_questions) < 250: # Allow some flexibility
|
| 729 |
+
print(f" Only {len(filtered_questions)} questions available")
|
| 730 |
+
proceed = input("Proceed with available questions? (y/n): ")
|
| 731 |
+
if proceed.lower() != 'y':
|
| 732 |
+
return None, None
|
| 733 |
+
|
| 734 |
+
# Create DataFrame
|
| 735 |
+
questions_df = loader.create_dataframe(filtered_questions)
|
| 736 |
+
|
| 737 |
+
# Step 3: Confirm evaluation
|
| 738 |
+
print(f"\n Step 3: Ready for Evaluation")
|
| 739 |
+
print(f" Questions to evaluate: {len(questions_df)}")
|
| 740 |
+
|
| 741 |
+
level_dist = questions_df['level'].value_counts().sort_index()
|
| 742 |
+
for level, count in level_dist.items():
|
| 743 |
+
percentage = (count / len(questions_df)) * 100
|
| 744 |
+
print(f" Level {level}: {count} questions ({percentage:.1f}%)")
|
| 745 |
+
|
| 746 |
+
estimated_time = len(questions_df) * 18 / 60 # 18 seconds average per question
|
| 747 |
+
print(f" Estimated time: {estimated_time:.0f} minutes")
|
| 748 |
+
|
| 749 |
+
# Confirm with user
|
| 750 |
+
proceed = input(f"\nProceed with {len(questions_df)} questions evaluation? (y/n): ")
|
| 751 |
+
if proceed.lower() != 'y':
|
| 752 |
+
print("Evaluation cancelled.")
|
| 753 |
+
return None, None
|
| 754 |
+
|
| 755 |
+
# Step 4: Initialize enhanced agent
|
| 756 |
+
print(f"\n Step 4: Initializing Enhanced GAIA Agent")
|
| 757 |
+
agent = EnhancedGAIAAgent(provider="groq", model="llama3-70b-8192")
|
| 758 |
+
|
| 759 |
+
# Step 5: Run evaluation
|
| 760 |
+
print(f"\n Step 5: Running GAIA Evaluation")
|
| 761 |
+
results_df = agent.evaluate_300_questions(questions_df)
|
| 762 |
+
|
| 763 |
+
# Step 6: Analyze results
|
| 764 |
+
print(f"\n Step 6: Analyzing Results")
|
| 765 |
+
metrics = analyze_gaia_results(results_df)
|
| 766 |
+
|
| 767 |
+
# Step 7: Save results
|
| 768 |
+
print(f"\n Step 7: Saving Results")
|
| 769 |
+
timestamp = save_gaia_results(results_df, metrics)
|
| 770 |
+
|
| 771 |
+
# Final summary
|
| 772 |
+
print(f"\n GAIA Evaluation Completed Successfully!")
|
| 773 |
+
print(f" Success Rate: {metrics['success_rate']:.2%}")
|
| 774 |
+
print(f" Total Time: {metrics['total_processing_time']/60:.1f} minutes")
|
| 775 |
+
print(f" Files saved with timestamp: {timestamp}")
|
| 776 |
+
|
| 777 |
+
# Performance insights
|
| 778 |
+
best_level = max(metrics['by_level'].items(), key=lambda x: x[1]['success_rate'])
|
| 779 |
+
worst_level = min(metrics['by_level'].items(), key=lambda x: x[1]['success_rate'])
|
| 780 |
+
|
| 781 |
+
print(f"\n Key Insights:")
|
| 782 |
+
print(f" Best Performance: Level {best_level[0].split('_')[1]} ({best_level[1]['success_rate']:.1%})")
|
| 783 |
+
print(f" Most Challenge: Level {worst_level[0].split('_')[1]} ({worst_level[1]['success_rate']:.1%})")
|
| 784 |
+
|
| 785 |
+
if metrics['success_rate'] >= 0.85:
|
| 786 |
+
print(" Excellent performance on GAIA benchmark!")
|
| 787 |
+
elif metrics['success_rate'] >= 0.70:
|
| 788 |
+
print(" Good performance on GAIA benchmark!")
|
| 789 |
+
else:
|
| 790 |
+
print(" Room for improvement on GAIA benchmark.")
|
| 791 |
+
|
| 792 |
+
return results_df, metrics
|
| 793 |
+
|
| 794 |
+
except Exception as e:
|
| 795 |
+
print(f" Error in main execution: {e}")
|
| 796 |
+
import traceback
|
| 797 |
+
traceback.print_exc()
|
| 798 |
+
return None, None
|
| 799 |
+
|
| 800 |
+
def test_gaia_single():
|
| 801 |
+
"""Test with a single GAIA question"""
|
| 802 |
+
print(" Testing Single GAIA Question")
|
| 803 |
+
print("=" * 40)
|
| 804 |
+
|
| 805 |
+
try:
|
| 806 |
+
# Load dataset
|
| 807 |
+
loader = FinalGAIALoader()
|
| 808 |
+
if not loader.load_gaia_dataset():
|
| 809 |
+
print(" Failed to load dataset")
|
| 810 |
+
return None
|
| 811 |
+
|
| 812 |
+
# Get one question of each level
|
| 813 |
+
sample_questions = []
|
| 814 |
+
for level in [1, 2, 3]:
|
| 815 |
+
level_questions = [q for q in loader.test_data if q.get('Level') == level]
|
| 816 |
+
if level_questions:
|
| 817 |
+
sample_questions.append(level_questions[0])
|
| 818 |
+
|
| 819 |
+
if not sample_questions:
|
| 820 |
+
print(" No sample questions found")
|
| 821 |
+
return None
|
| 822 |
+
|
| 823 |
+
# Test with agent
|
| 824 |
+
agent = EnhancedGAIAAgent()
|
| 825 |
+
|
| 826 |
+
for i, question_data in enumerate(sample_questions):
|
| 827 |
+
question = question_data['Question']
|
| 828 |
+
level = question_data['Level']
|
| 829 |
+
|
| 830 |
+
print(f"\n Test {i+1} - Level {level}")
|
| 831 |
+
print(f"Q: {question[:150]}...")
|
| 832 |
+
|
| 833 |
+
result = agent.process_question(question, f"test_{level}")
|
| 834 |
+
|
| 835 |
+
print(f"A: {result['final_answer']}")
|
| 836 |
+
print(f" Time: {result['processing_time']:.2f}s")
|
| 837 |
+
print(f"Status: {result['status']}")
|
| 838 |
+
|
| 839 |
+
return True
|
| 840 |
+
|
| 841 |
+
except Exception as e:
|
| 842 |
+
print(f" Test failed: {e}")
|
| 843 |
+
return None
|
| 844 |
+
|
| 845 |
+
def quick_gaia_test():
|
| 846 |
+
"""Quick test with 10 questions"""
|
| 847 |
+
print(" Quick GAIA Test - 10 Questions")
|
| 848 |
+
print("=" * 40)
|
| 849 |
+
|
| 850 |
+
try:
|
| 851 |
+
# Load and filter
|
| 852 |
+
loader = FinalGAIALoader()
|
| 853 |
+
if not loader.load_gaia_dataset():
|
| 854 |
+
return None, None
|
| 855 |
+
|
| 856 |
+
# Get 10 questions (3/4/3 distribution)
|
| 857 |
+
level_groups = {1: [], 2: [], 3: []}
|
| 858 |
+
for item in loader.test_data:
|
| 859 |
+
level = item.get('Level')
|
| 860 |
+
if level in level_groups:
|
| 861 |
+
level_groups[level].append(item)
|
| 862 |
+
|
| 863 |
+
quick_questions = []
|
| 864 |
+
quick_questions.extend(level_groups[1][:3]) # 3 Level 1
|
| 865 |
+
quick_questions.extend(level_groups[2][:4]) # 4 Level 2
|
| 866 |
+
quick_questions.extend(level_groups[3][:3]) # 3 Level 3
|
| 867 |
+
|
| 868 |
+
questions_df = loader.create_dataframe(quick_questions)
|
| 869 |
+
|
| 870 |
+
# Run evaluation
|
| 871 |
+
agent = EnhancedGAIAAgent()
|
| 872 |
+
results_df = agent.evaluate_300_questions(questions_df)
|
| 873 |
+
|
| 874 |
+
# Quick analysis
|
| 875 |
+
metrics = analyze_gaia_results(results_df)
|
| 876 |
+
|
| 877 |
+
return results_df, metrics
|
| 878 |
+
|
| 879 |
+
except Exception as e:
|
| 880 |
+
print(f" Quick test failed: {e}")
|
| 881 |
+
return None, None
|
| 882 |
+
|
| 883 |
+
# ================================
|
| 884 |
+
# EXECUTION
|
| 885 |
+
# ================================
|
| 886 |
+
|
| 887 |
+
if __name__ == "__main__":
|
| 888 |
+
print(" GAIA Benchmark Evaluation System")
|
| 889 |
+
print("Using correct dataset configuration: 2023_all")
|
| 890 |
+
print("=" * 60)
|
| 891 |
+
|
| 892 |
+
# Test API connectivity
|
| 893 |
+
print("\n Testing API Connectivity...")
|
| 894 |
+
try:
|
| 895 |
+
# Test Groq
|
| 896 |
+
test_llm = ChatGroq(model="llama3-8b-8192", temperature=0)
|
| 897 |
+
test_response = test_llm.invoke([HumanMessage(content="Hello")])
|
| 898 |
+
print(" Groq API working")
|
| 899 |
+
|
| 900 |
+
# Test Tavily
|
| 901 |
+
test_search = TavilySearchResults(max_results=1)
|
| 902 |
+
test_results = test_search.invoke("test")
|
| 903 |
+
print(" Tavily API working")
|
| 904 |
+
|
| 905 |
+
except Exception as e:
|
| 906 |
+
print(f" API test warning: {e}")
|
| 907 |
+
|
| 908 |
+
# Main menu
|
| 909 |
+
print(f"\n🎮 Choose evaluation option:")
|
| 910 |
+
print("1. Test single GAIA questions (5 minutes)")
|
| 911 |
+
print("2. Quick test with 10 questions (15 minutes)")
|
| 912 |
+
print("3. Full 300 questions evaluation (75-90 minutes)")
|
| 913 |
+
print("4. Auto-run full evaluation")
|
| 914 |
+
|
| 915 |
+
choice = input("\nEnter choice (1-4): ").strip()
|
| 916 |
+
|
| 917 |
+
if choice == "1":
|
| 918 |
+
print(" Running single question tests...")
|
| 919 |
+
test_gaia_single()
|
| 920 |
+
|
| 921 |
+
elif choice == "2":
|
| 922 |
+
print(" Running quick 10-question test...")
|
| 923 |
+
results_df, metrics = quick_gaia_test()
|
| 924 |
+
if results_df is not None:
|
| 925 |
+
print(f"\n Quick Test Results:")
|
| 926 |
+
print(f" Success Rate: {metrics['success_rate']:.2%}")
|
| 927 |
+
print(f"⏱️ Average Time: {metrics['avg_processing_time']:.1f}s")
|
| 928 |
+
|
| 929 |
+
elif choice == "3" or choice == "4":
|
| 930 |
+
print(" Starting full GAIA 300 questions evaluation...")
|
| 931 |
+
results_df, metrics = main_gaia_300()
|
| 932 |
+
|
| 933 |
+
if results_df is not None:
|
| 934 |
+
print(f"\n Final GAIA Benchmark Results:")
|
| 935 |
+
print(f" Overall Success Rate: {metrics['success_rate']:.2%}")
|
| 936 |
+
print(f" Questions Completed: {len(results_df)}")
|
| 937 |
+
print(f"⏱️ Total Evaluation Time: {metrics['total_processing_time']/60:.1f} minutes")
|
| 938 |
+
|
| 939 |
+
# Level breakdown
|
| 940 |
+
for level_key, level_data in metrics['by_level'].items():
|
| 941 |
+
level_num = level_key.split('_')[1]
|
| 942 |
+
print(f" Level {level_num}: {level_data['success_rate']:.1%} success rate")
|
| 943 |
+
|
| 944 |
+
else:
|
| 945 |
+
print(" Evaluation failed")
|
| 946 |
+
|
| 947 |
+
else:
|
| 948 |
+
print("Invalid choice. Running single question test...")
|
| 949 |
+
test_gaia_single()
|
| 950 |
+
|
| 951 |
+
print("\n���� GAIA Evaluation System Complete!")
|