Spaces:
Sleeping
Sleeping
Update agent.py
Browse files
agent.py
CHANGED
|
@@ -1,72 +1,91 @@
|
|
| 1 |
-
"""
|
| 2 |
-
GAIA Agent - Main Agent Implementation
|
| 3 |
-
A sophisticated agent for solving GAIA benchmark questions using multiple tools.
|
| 4 |
-
"""
|
| 5 |
-
|
| 6 |
-
import os
|
| 7 |
-
from smolagents import CodeAgent, LiteLLMModel
|
| 8 |
-
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 9 |
-
from code_interpreter import CodeInterpreterTool
|
| 10 |
-
from image_processing import ImageProcessingTool
|
| 11 |
-
|
| 12 |
-
# Get API key from environment
|
| 13 |
-
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 14 |
-
if not openrouter_api_key:
|
| 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 |
-
The
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
GAIA Agent - Main Agent Implementation
|
| 3 |
+
A sophisticated agent for solving GAIA benchmark questions using multiple tools.
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import os
|
| 7 |
+
from smolagents import CodeAgent, LiteLLMModel
|
| 8 |
+
from tools import DuckDuckGoSearchTool, WeatherInfoTool, HubStatsTool
|
| 9 |
+
from code_interpreter import CodeInterpreterTool
|
| 10 |
+
from image_processing import ImageProcessingTool
|
| 11 |
+
|
| 12 |
+
# Get API key from environment
|
| 13 |
+
openrouter_api_key = os.environ.get("OPENROUTER_API_KEY")
|
| 14 |
+
if not openrouter_api_key or openrouter_api_key == "dummy":
|
| 15 |
+
raise ValueError(
|
| 16 |
+
"⚠️ OPENROUTER_API_KEY not found or invalid!\n"
|
| 17 |
+
"Please set OPENROUTER_API_KEY environment variable.\n"
|
| 18 |
+
"Get FREE API key: https://openrouter.ai/keys\n"
|
| 19 |
+
"In Hugging Face Space: Settings → Repository secrets → Add secret"
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Initialize the model
|
| 23 |
+
model = LiteLLMModel(
|
| 24 |
+
model_id="openrouter/meta-llama/llama-3.3-70b-instruct:free",
|
| 25 |
+
api_key=openrouter_api_key,
|
| 26 |
+
temperature=0.5,
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
# Initialize tools
|
| 30 |
+
search_tool = DuckDuckGoSearchTool()
|
| 31 |
+
weather_info_tool = WeatherInfoTool()
|
| 32 |
+
hub_stats_tool = HubStatsTool()
|
| 33 |
+
code_interpreter_tool = CodeInterpreterTool()
|
| 34 |
+
image_processing_tool = ImageProcessingTool()
|
| 35 |
+
|
| 36 |
+
# Create GAIA Agent with all tools
|
| 37 |
+
gaia_agent = CodeAgent(
|
| 38 |
+
tools=[
|
| 39 |
+
search_tool,
|
| 40 |
+
weather_info_tool,
|
| 41 |
+
hub_stats_tool,
|
| 42 |
+
code_interpreter_tool,
|
| 43 |
+
image_processing_tool,
|
| 44 |
+
],
|
| 45 |
+
model=model,
|
| 46 |
+
add_base_tools=True,
|
| 47 |
+
planning_interval=3,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
def run_agent(question: str) -> str:
|
| 51 |
+
"""
|
| 52 |
+
Run the GAIA agent on a question and return the answer.
|
| 53 |
+
|
| 54 |
+
Args:
|
| 55 |
+
question: The GAIA benchmark question to answer
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
The agent's answer as a string
|
| 59 |
+
"""
|
| 60 |
+
try:
|
| 61 |
+
if not question or not question.strip():
|
| 62 |
+
return "Error: Empty question provided"
|
| 63 |
+
|
| 64 |
+
response = gaia_agent.run(question)
|
| 65 |
+
|
| 66 |
+
# Ensure we return a string
|
| 67 |
+
if response is None:
|
| 68 |
+
return "Agent returned None"
|
| 69 |
+
|
| 70 |
+
response_str = str(response).strip()
|
| 71 |
+
if not response_str:
|
| 72 |
+
return "Agent returned empty response"
|
| 73 |
+
|
| 74 |
+
return response_str
|
| 75 |
+
except Exception as e:
|
| 76 |
+
error_msg = f"Error processing question: {str(e)}"
|
| 77 |
+
print(error_msg)
|
| 78 |
+
import traceback
|
| 79 |
+
traceback.print_exc()
|
| 80 |
+
return error_msg
|
| 81 |
+
|
| 82 |
+
if __name__ == "__main__":
|
| 83 |
+
# Example usage
|
| 84 |
+
test_question = "What is the capital of France?"
|
| 85 |
+
print("=" * 80)
|
| 86 |
+
print("GAIA Agent Test")
|
| 87 |
+
print("=" * 80)
|
| 88 |
+
print(f"Question: {test_question}")
|
| 89 |
+
answer = run_agent(test_question)
|
| 90 |
+
print(f"Answer: {answer}")
|
| 91 |
+
|