File size: 22,218 Bytes
bd73133 e7b4937 bd73133 1041734 bd73133 1041734 4eed151 41ac444 bd73133 e7b4937 4eed151 1041734 bd73133 5890f66 e7b4937 5890f66 e7b4937 5890f66 e7b4937 bd73133 4eed151 bd73133 4eed151 4c0b7eb 4eed151 41ac444 4eed151 41ac444 4eed151 41ac444 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 41ac444 e7b4937 41ac444 e7b4937 4eed151 41ac444 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 e7b4937 4eed151 bd73133 4eed151 bd73133 1041734 4c0b7eb bd73133 4c0b7eb 4eed151 4c0b7eb fed2514 4c0b7eb fed2514 4eed151 4c0b7eb bd73133 fed2514 4c0b7eb 59f3cce 4c0b7eb 59f3cce 40db96d 4c0b7eb 4eed151 4c0b7eb 41ac444 4c0b7eb 4eed151 fed2514 41ac444 4eed151 fed2514 e7b4937 41ac444 4eed151 fed2514 4c0b7eb 4eed151 4c0b7eb fed2514 4c0b7eb e7b4937 4c0b7eb 41ac444 e7b4937 41ac444 4c0b7eb 9c1dff0 e7b4937 fed2514 5890f66 fed2514 4eed151 fed2514 4c0b7eb fed2514 5890f66 e7b4937 fed2514 5890f66 fed2514 bd73133 4eed151 41ac444 4eed151 59f3cce 4eed151 59f3cce 40db96d 4eed151 e7b4937 41ac444 e7b4937 41ac444 fed2514 4eed151 fed2514 4eed151 fed2514 4eed151 bd73133 fed2514 4eed151 fed2514 4c0b7eb e7b4937 fed2514 4c0b7eb 4eed151 4c0b7eb fed2514 bd73133 4c0b7eb fed2514 4eed151 e7b4937 bd73133 4eed151 bd73133 4eed151 bd73133 4eed151 e7b4937 4eed151 bd73133 4eed151 bd73133 41ac444 bd73133 41ac444 bd73133 41ac444 bd73133 41ac444 bd73133 41ac444 bd73133 4c0b7eb bd73133 4eed151 bd73133 4eed151 bd73133 |
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 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
"""
LangGraph Agent Core - StateGraph Definition
Author: @mangubee
Date: 2026-01-01
Stage 1: Skeleton with placeholder nodes
Stage 2: Tool integration (CURRENT)
Stage 3: Planning and reasoning logic implementation
Based on:
- Level 3: Sequential workflow with dynamic planning
- Level 4: Goal-based reasoning, coarse-grained generalist
- Level 6: LangGraph framework
"""
import logging
import os
from pathlib import Path
from typing import TypedDict, List, Optional
from langgraph.graph import StateGraph, END
from src.config import Settings
from src.tools import (
TOOLS,
search,
parse_file,
safe_eval,
analyze_image,
youtube_transcript,
transcribe_audio,
)
from src.agent.llm_client import (
plan_question,
select_tools_with_function_calling,
synthesize_answer,
)
# ============================================================================
# Logging Setup
# ============================================================================
logger = logging.getLogger(__name__)
# ============================================================================
# Helper Functions
# ============================================================================
def is_vision_question(question: str) -> bool:
"""
Detect if question requires vision analysis tool.
Vision questions typically contain keywords about visual content like images, videos, or YouTube links.
Args:
question: GAIA question text
Returns:
True if question likely requires vision tool, False otherwise
"""
vision_keywords = [
"image",
"video",
"youtube",
"photo",
"picture",
"watch",
"screenshot",
"visual",
]
return any(keyword in question.lower() for keyword in vision_keywords)
# ============================================================================
# Agent State Definition
# ============================================================================
class AgentState(TypedDict):
"""
State structure for GAIA agent workflow.
Tracks question processing from input through planning, execution, to final answer.
"""
question: str # Input question from GAIA
file_paths: Optional[List[str]] # Optional file paths for file-based questions
plan: Optional[str] # Generated execution plan (Stage 3)
tool_calls: List[dict] # Tool invocation tracking (Stage 3)
tool_results: List[dict] # Tool execution results (Stage 3)
evidence: List[str] # Evidence collected from tools (Stage 3)
answer: Optional[str] # Final factoid answer
errors: List[str] # Error messages from failures
# ============================================================================
# Environment Validation
# ============================================================================
def validate_environment() -> List[str]:
"""
Check which API keys are available at startup.
Returns:
List of missing API key names (empty if all present)
"""
missing = []
if not os.getenv("GOOGLE_API_KEY"):
missing.append("GOOGLE_API_KEY (Gemini)")
if not os.getenv("HF_TOKEN"):
missing.append("HF_TOKEN (HuggingFace)")
if not os.getenv("ANTHROPIC_API_KEY"):
missing.append("ANTHROPIC_API_KEY (Claude)")
if not os.getenv("TAVILY_API_KEY"):
missing.append("TAVILY_API_KEY (Search)")
return missing
# ============================================================================
# Helper Functions
# ============================================================================
def fallback_tool_selection(
question: str, plan: str, file_paths: Optional[List[str]] = None
) -> List[dict]:
"""
MVP Fallback: Simple keyword-based tool selection when LLM fails.
Enhanced to use actual file paths when available.
This is a temporary hack to get basic functionality working.
Uses simple keyword matching to select tools.
Args:
question: The user question
plan: The execution plan
file_paths: Optional list of downloaded file paths
Returns:
List of tool calls with basic parameters
"""
logger.info(
"[fallback_tool_selection] Using keyword-based fallback for tool selection"
)
tool_calls = []
question_lower = question.lower()
plan_lower = plan.lower()
combined = f"{question_lower} {plan_lower}"
# Search tool: keywords like "search", "find", "look up", "who", "what", "when", "where"
search_keywords = [
"search",
"find",
"look up",
"who is",
"what is",
"when",
"where",
"google",
]
if any(keyword in combined for keyword in search_keywords):
# Extract search query - use first sentence or full question
query = question.split(".")[0] if "." in question else question
tool_calls.append({"tool": "web_search", "params": {"query": query}})
logger.info(
f"[fallback_tool_selection] Added web_search tool with query: {query}"
)
# Math tool: keywords like "calculate", "compute", "+", "-", "*", "/", "="
math_keywords = [
"calculate",
"compute",
"math",
"sum",
"multiply",
"divide",
"+",
"-",
"*",
"/",
"=",
]
if any(keyword in combined for keyword in math_keywords):
# Try to extract expression - look for patterns with numbers and operators
import re
# Look for mathematical expressions
expr_match = re.search(r"[\d\s\+\-\*/\(\)\.]+", question)
if expr_match:
expression = expr_match.group().strip()
tool_calls.append(
{"tool": "calculator", "params": {"expression": expression}}
)
logger.info(
f"[fallback_tool_selection] Added calculator tool with expression: {expression}"
)
# File tool: if file_paths available, use them
if file_paths:
for file_path in file_paths:
# Determine file type and appropriate tool
file_ext = Path(file_path).suffix.lower()
if file_ext in [".png", ".jpg", ".jpeg"]:
tool_calls.append(
{"tool": "vision", "params": {"image_path": file_path}}
)
logger.info(
f"[fallback_tool_selection] Added vision tool for image: {file_path}"
)
elif file_ext in [
".pdf",
".xlsx",
".xls",
".csv",
".json",
".txt",
".docx",
".doc",
]:
tool_calls.append(
{"tool": "parse_file", "params": {"file_path": file_path}}
)
logger.info(
f"[fallback_tool_selection] Added parse_file tool for: {file_path}"
)
else:
# Keyword-based file detection (legacy)
file_keywords = ["file", "parse", "read", "csv", "json", "txt", "document"]
if any(keyword in combined for keyword in file_keywords):
logger.warning(
"[fallback_tool_selection] File operation detected but no file_paths available"
)
# Image tool: keywords like "image", "picture", "photo", "analyze", "vision"
image_keywords = ["image", "picture", "photo", "analyze image", "vision"]
if any(keyword in combined for keyword in image_keywords):
if file_paths:
# Already handled above in file_paths check
pass
else:
logger.warning(
"[fallback_tool_selection] Image operation detected but no file_paths available"
)
if not tool_calls:
logger.warning(
"[fallback_tool_selection] No tools selected by fallback - adding default search"
)
# Default: just search the question
tool_calls.append({"tool": "web_search", "params": {"query": question}})
logger.info(
f"[fallback_tool_selection] Fallback selected {len(tool_calls)} tool(s)"
)
return tool_calls
# ============================================================================
# Graph Node Functions (Placeholders for Stage 1)
# ============================================================================
def plan_node(state: AgentState) -> AgentState:
"""
Planning node: Analyze question and generate execution plan.
Stage 3: Dynamic planning with LLM
- LLM analyzes question and available tools
- Generates step-by-step execution plan
- Identifies which tools to use and in what order
Args:
state: Current agent state with question
Returns:
Updated state with execution plan
"""
try:
plan = plan_question(
question=state["question"],
available_tools=TOOLS,
file_paths=state.get("file_paths"),
)
state["plan"] = plan
logger.info(f"[plan] β {len(plan)} chars")
except Exception as e:
logger.error(f"[plan] β {type(e).__name__}: {str(e)}")
state["errors"].append(f"Planning error: {type(e).__name__}: {str(e)}")
state["plan"] = "Error: Unable to create plan"
return state
def execute_node(state: AgentState) -> AgentState:
"""Execution node: Execute tools based on plan."""
# Map tool names to actual functions
TOOL_FUNCTIONS = {
"web_search": search,
"parse_file": parse_file,
"calculator": safe_eval,
"vision": analyze_image,
"youtube_transcript": youtube_transcript,
"transcribe_audio": transcribe_audio,
}
tool_results = []
evidence = []
tool_calls = []
try:
tool_calls = select_tools_with_function_calling(
question=state["question"],
plan=state["plan"],
available_tools=TOOLS,
file_paths=state.get("file_paths"),
)
# Validate tool_calls result
if not tool_calls:
logger.warning("[execute] No tools selected, using fallback")
state["errors"].append("Tool selection returned no tools - using fallback")
tool_calls = fallback_tool_selection(
state["question"], state["plan"], state.get("file_paths")
)
elif not isinstance(tool_calls, list):
logger.error(f"[execute] Invalid type: {type(tool_calls)}, using fallback")
state["errors"].append(
f"Tool selection returned invalid type: {type(tool_calls)}"
)
tool_calls = fallback_tool_selection(
state["question"], state["plan"], state.get("file_paths")
)
else:
logger.info(f"[execute] {len(tool_calls)} tool(s) selected")
# Execute each tool call
for idx, tool_call in enumerate(tool_calls, 1):
tool_name = tool_call["tool"]
params = tool_call["params"]
try:
tool_func = TOOL_FUNCTIONS.get(tool_name)
if not tool_func:
raise ValueError(f"Tool '{tool_name}' not found in TOOL_FUNCTIONS")
result = tool_func(**params)
logger.info(f"[{idx}/{len(tool_calls)}] {tool_name} β")
tool_results.append(
{
"tool": tool_name,
"params": params,
"result": result,
"status": "success",
}
)
# Extract evidence - handle different result formats
if isinstance(result, dict):
# Vision tool returns {"answer": "..."}
if "answer" in result:
evidence.append(result["answer"])
# Search tools return {"results": [...], "source": "...", "query": "..."}
elif "results" in result:
# Format search results as readable text
results_list = result.get("results", [])
if results_list:
# Take first 3 results and format them
formatted = []
for r in results_list[:3]:
title = r.get("title", "")[:100]
url = r.get("url", "")[:100]
snippet = r.get("snippet", "")[:200]
formatted.append(
f"Title: {title}\nURL: {url}\nSnippet: {snippet}"
)
evidence.append("\n\n".join(formatted))
else:
evidence.append(str(result))
else:
evidence.append(str(result))
elif isinstance(result, str):
evidence.append(result)
else:
evidence.append(str(result))
except Exception as tool_error:
logger.error(f"[execute] β {tool_name}: {tool_error}")
tool_results.append(
{
"tool": tool_name,
"params": params,
"error": str(tool_error),
"status": "failed",
}
)
if tool_name == "vision" and (
"quota" in str(tool_error).lower() or "429" in str(tool_error)
):
state["errors"].append(f"Vision failed: LLM quota exhausted")
else:
state["errors"].append(f"{tool_name}: {type(tool_error).__name__}")
logger.info(f"[execute] {len(tool_results)} tools, {len(evidence)} evidence")
except Exception as e:
logger.error(f"[execute] β {type(e).__name__}: {str(e)}")
if is_vision_question(state["question"]) and (
"quota" in str(e).lower() or "429" in str(e)
):
state["errors"].append("Vision unavailable (quota exhausted)")
else:
state["errors"].append(f"Execution error: {type(e).__name__}")
# Try fallback if we don't have any tool_calls yet
if not tool_calls:
try:
tool_calls = fallback_tool_selection(
state["question"], state.get("plan", ""), state.get("file_paths")
)
TOOL_FUNCTIONS = {
"web_search": search,
"parse_file": parse_file,
"calculator": safe_eval,
"vision": analyze_image,
"youtube_transcript": youtube_transcript,
"transcribe_audio": transcribe_audio,
}
for tool_call in tool_calls:
try:
tool_name = tool_call["tool"]
params = tool_call["params"]
tool_func = TOOL_FUNCTIONS.get(tool_name)
if tool_func:
result = tool_func(**params)
tool_results.append(
{
"tool": tool_name,
"params": params,
"result": result,
"status": "success",
}
)
if isinstance(result, dict):
if "answer" in result:
evidence.append(result["answer"])
elif "results" in result:
results_list = result.get("results", [])
if results_list:
formatted = []
for r in results_list[:3]:
title = r.get("title", "")[:100]
url = r.get("url", "")[:100]
snippet = r.get("snippet", "")[:200]
formatted.append(
f"Title: {title}\nURL: {url}\nSnippet: {snippet}"
)
evidence.append("\n\n".join(formatted))
else:
evidence.append(str(result))
else:
evidence.append(str(result))
elif isinstance(result, str):
evidence.append(result)
else:
evidence.append(str(result))
logger.info(f"[execute] Fallback {tool_name} β")
except Exception as tool_error:
logger.error(f"[execute] Fallback {tool_name} β {tool_error}")
except Exception as fallback_error:
logger.error(f"[execute] Fallback failed: {fallback_error}")
# Always update state, even if there were errors
state["tool_calls"] = tool_calls
state["tool_results"] = tool_results
state["evidence"] = evidence
return state
def answer_node(state: AgentState) -> AgentState:
"""Answer synthesis node: Generate final factoid answer from evidence."""
if state["errors"]:
logger.warning(f"[answer] Errors: {state['errors']}")
try:
if not state["evidence"]:
error_summary = (
"; ".join(state["errors"]) if state["errors"] else "No errors logged"
)
state["answer"] = f"ERROR: No evidence. {error_summary}"
logger.error(f"[answer] β No evidence - {error_summary}")
return state
answer = synthesize_answer(
question=state["question"], evidence=state["evidence"]
)
state["answer"] = answer
logger.info(f"[answer] β {answer}")
except Exception as e:
logger.error(f"[answer] β {type(e).__name__}: {str(e)}")
state["errors"].append(f"Answer synthesis error: {type(e).__name__}: {str(e)}")
state["answer"] = (
f"ERROR: Answer synthesis failed - {type(e).__name__}: {str(e)}"
)
return state
# ============================================================================
# StateGraph Construction
# ============================================================================
def create_gaia_graph() -> StateGraph:
"""
Create LangGraph StateGraph for GAIA agent.
Implements sequential workflow (Level 3 decision):
question β plan β execute β answer
Returns:
Compiled StateGraph ready for execution
"""
settings = Settings()
# Initialize StateGraph with AgentState
graph = StateGraph(AgentState)
# Add nodes (placeholder implementations)
graph.add_node("plan", plan_node)
graph.add_node("execute", execute_node)
graph.add_node("answer", answer_node)
# Define sequential workflow edges
graph.set_entry_point("plan")
graph.add_edge("plan", "execute")
graph.add_edge("execute", "answer")
graph.add_edge("answer", END)
# Compile graph
compiled_graph = graph.compile()
print("[create_gaia_graph] StateGraph compiled successfully")
return compiled_graph
# ============================================================================
# Agent Wrapper Class
# ============================================================================
class GAIAAgent:
"""
GAIA Benchmark Agent - Main interface.
Wraps LangGraph StateGraph and provides simple call interface.
Compatible with existing BasicAgent interface in app.py.
"""
def __init__(self):
"""Initialize agent and compile StateGraph."""
print("GAIAAgent initializing...")
# Validate environment - check API keys
missing_keys = validate_environment()
if missing_keys:
warning_msg = f"β οΈ WARNING: Missing API keys: {', '.join(missing_keys)}"
print(warning_msg)
logger.warning(warning_msg)
print(
" Agent may fail to answer questions. Set keys in environment variables."
)
else:
print("β All API keys present")
self.graph = create_gaia_graph()
self.last_state = None # Store last execution state for diagnostics
print("GAIAAgent initialized successfully")
def __call__(self, question: str, file_path: Optional[str] = None) -> str:
"""
Process question and return answer.
Supports optional file attachment for file-based questions.
Args:
question: GAIA question text
file_path: Optional path to downloaded file attachment
Returns:
Factoid answer string
"""
print(f"GAIAAgent processing question (first 50 chars): {question[:50]}...")
if file_path:
print(f"GAIAAgent processing file: {file_path}")
# Initialize state
initial_state: AgentState = {
"question": question,
"file_paths": [file_path] if file_path else None,
"plan": None,
"tool_calls": [],
"tool_results": [],
"evidence": [],
"answer": None,
"errors": [],
}
# Invoke graph
final_state = self.graph.invoke(initial_state)
# Store state for diagnostics
self.last_state = final_state
# Extract answer
answer = final_state.get("answer", "Error: No answer generated")
print(f"GAIAAgent returning answer: {answer}")
return answer
|