fix bug
Browse files
app.py
CHANGED
|
@@ -9,9 +9,8 @@ import time
|
|
| 9 |
from typing import List, Dict, Any, Optional, Union, Tuple
|
| 10 |
|
| 11 |
# --- Import necessary libraries ---
|
| 12 |
-
from smolagents import CodeAgent
|
| 13 |
from smolagents.models import LiteLLMModel
|
| 14 |
-
from llama_index.core.tools import FunctionTool
|
| 15 |
from langgraph.graph import StateGraph, END
|
| 16 |
|
| 17 |
# --- Constants ---
|
|
@@ -257,33 +256,61 @@ class GAIAAgent:
|
|
| 257 |
|
| 258 |
def setup_tools(self):
|
| 259 |
"""Set up tools for the agent"""
|
| 260 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
self.tools = [
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
FunctionTool.from_defaults(
|
| 268 |
-
name="search_web",
|
| 269 |
-
description="Search for information related to a query",
|
| 270 |
-
fn=GAIAToolkit.search_web
|
| 271 |
-
),
|
| 272 |
-
FunctionTool.from_defaults(
|
| 273 |
-
name="file_reader",
|
| 274 |
-
description="Read file content given a file ID",
|
| 275 |
-
fn=GAIAToolkit.file_reader
|
| 276 |
-
),
|
| 277 |
-
FunctionTool.from_defaults(
|
| 278 |
-
name="analyze_text",
|
| 279 |
-
description="Analyze text to extract statistics and key information",
|
| 280 |
-
fn=GAIAToolkit.analyze_text
|
| 281 |
-
),
|
| 282 |
-
FunctionTool.from_defaults(
|
| 283 |
-
name="extract_answer",
|
| 284 |
-
description="Extract the final answer from reasoning",
|
| 285 |
-
fn=GAIAToolkit.extract_answer
|
| 286 |
-
)
|
| 287 |
]
|
| 288 |
|
| 289 |
def create_system_prompt(self) -> str:
|
|
@@ -356,32 +383,24 @@ class GAIAAgent:
|
|
| 356 |
def analyze_and_plan(self, question: str):
|
| 357 |
"""Analyze the question and plan approach"""
|
| 358 |
analyze_prompt = f"""Analyze the following question:
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
Provide only a concise analysis, don't attempt to answer the question.
|
| 368 |
-
"""
|
| 369 |
-
|
| 370 |
analysis = self.model.generate(analyze_prompt).strip()
|
| 371 |
self.workflow_states["analysis"] = analysis
|
| 372 |
|
| 373 |
plan_prompt = f"""Based on the question analysis:
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
Use available tools: calculator, search_web, file_reader, analyze_text.
|
| 382 |
-
List specific steps, don't attempt to answer the question.
|
| 383 |
-
"""
|
| 384 |
-
|
| 385 |
plan = self.model.generate(plan_prompt).strip()
|
| 386 |
self.workflow_states["plan"] = plan
|
| 387 |
|
|
|
|
| 9 |
from typing import List, Dict, Any, Optional, Union, Tuple
|
| 10 |
|
| 11 |
# --- Import necessary libraries ---
|
| 12 |
+
from smolagents import CodeAgent, tool
|
| 13 |
from smolagents.models import LiteLLMModel
|
|
|
|
| 14 |
from langgraph.graph import StateGraph, END
|
| 15 |
|
| 16 |
# --- Constants ---
|
|
|
|
| 256 |
|
| 257 |
def setup_tools(self):
|
| 258 |
"""Set up tools for the agent"""
|
| 259 |
+
# Create tools using smolagents @tool decorator
|
| 260 |
+
|
| 261 |
+
@tool
|
| 262 |
+
def calculator(expression: str) -> str:
|
| 263 |
+
"""Calculate mathematical expressions like '2 + 2' or '(15 * 3) / 2'
|
| 264 |
+
|
| 265 |
+
Args:
|
| 266 |
+
expression: The mathematical expression to calculate
|
| 267 |
+
"""
|
| 268 |
+
return GAIAToolkit.calculator(expression)
|
| 269 |
+
|
| 270 |
+
@tool
|
| 271 |
+
def search_web(query: str) -> str:
|
| 272 |
+
"""Search for information related to a query
|
| 273 |
+
|
| 274 |
+
Args:
|
| 275 |
+
query: The search query
|
| 276 |
+
"""
|
| 277 |
+
return GAIAToolkit.search_web(query)
|
| 278 |
+
|
| 279 |
+
@tool
|
| 280 |
+
def file_reader(file_id: str) -> str:
|
| 281 |
+
"""Read file content given a file ID
|
| 282 |
+
|
| 283 |
+
Args:
|
| 284 |
+
file_id: The ID of the file to read
|
| 285 |
+
"""
|
| 286 |
+
return GAIAToolkit.file_reader(file_id)
|
| 287 |
+
|
| 288 |
+
@tool
|
| 289 |
+
def analyze_text(text: str) -> str:
|
| 290 |
+
"""Analyze text to extract statistics and key information
|
| 291 |
+
|
| 292 |
+
Args:
|
| 293 |
+
text: The text to analyze
|
| 294 |
+
"""
|
| 295 |
+
result = GAIAToolkit.analyze_text(text)
|
| 296 |
+
return str(result)
|
| 297 |
+
|
| 298 |
+
@tool
|
| 299 |
+
def extract_answer(reasoning: str) -> str:
|
| 300 |
+
"""Extract the final answer from reasoning
|
| 301 |
+
|
| 302 |
+
Args:
|
| 303 |
+
reasoning: The reasoning text to extract the answer from
|
| 304 |
+
"""
|
| 305 |
+
return GAIAToolkit.extract_answer(reasoning)
|
| 306 |
+
|
| 307 |
+
# Assign the tools to the agent
|
| 308 |
self.tools = [
|
| 309 |
+
calculator,
|
| 310 |
+
search_web,
|
| 311 |
+
file_reader,
|
| 312 |
+
analyze_text,
|
| 313 |
+
extract_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
]
|
| 315 |
|
| 316 |
def create_system_prompt(self) -> str:
|
|
|
|
| 383 |
def analyze_and_plan(self, question: str):
|
| 384 |
"""Analyze the question and plan approach"""
|
| 385 |
analyze_prompt = f"""Analyze the following question:
|
| 386 |
+
{question}
|
| 387 |
+
Identify:
|
| 388 |
+
1. Question type (calculation, information retrieval, text analysis, etc.)
|
| 389 |
+
2. Key tools needed
|
| 390 |
+
3. Solution steps
|
| 391 |
+
Provide only a concise analysis, don't attempt to answer the question.
|
| 392 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
analysis = self.model.generate(analyze_prompt).strip()
|
| 394 |
self.workflow_states["analysis"] = analysis
|
| 395 |
|
| 396 |
plan_prompt = f"""Based on the question analysis:
|
| 397 |
+
{analysis}
|
| 398 |
+
Formulate a concise step-by-step plan to answer the question:
|
| 399 |
+
{question}
|
| 400 |
+
Use available tools: calculator, search_web, file_reader, analyze_text.
|
| 401 |
+
List specific steps, don't attempt to answer the question.
|
| 402 |
+
"""
|
| 403 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 404 |
plan = self.model.generate(plan_prompt).strip()
|
| 405 |
self.workflow_states["plan"] = plan
|
| 406 |
|