ArdaKaratas commited on
Commit
9d357da
·
verified ·
1 Parent(s): f00e317

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +91 -72
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
- print("⚠️ WARNING: OPENROUTER_API_KEY not found!")
16
- print("Get FREE API key: https://openrouter.ai/keys")
17
- openrouter_api_key = "dummy"
18
-
19
- # Initialize the model
20
- model = LiteLLMModel(
21
- model_id="openrouter/meta-llama/llama-3.3-70b-instruct:free",
22
- api_key=openrouter_api_key,
23
- temperature=0.5,
24
- )
25
-
26
- # Initialize tools
27
- search_tool = DuckDuckGoSearchTool()
28
- weather_info_tool = WeatherInfoTool()
29
- hub_stats_tool = HubStatsTool()
30
- code_interpreter_tool = CodeInterpreterTool()
31
- image_processing_tool = ImageProcessingTool()
32
-
33
- # Create GAIA Agent with all tools
34
- gaia_agent = CodeAgent(
35
- tools=[
36
- search_tool,
37
- weather_info_tool,
38
- hub_stats_tool,
39
- code_interpreter_tool,
40
- image_processing_tool,
41
- ],
42
- model=model,
43
- add_base_tools=True,
44
- planning_interval=3,
45
- )
46
-
47
- def run_agent(question: str) -> str:
48
- """
49
- Run the GAIA agent on a question and return the answer.
50
-
51
- Args:
52
- question: The GAIA benchmark question to answer
53
-
54
- Returns:
55
- The agent's answer as a string
56
- """
57
- try:
58
- response = gaia_agent.run(question)
59
- return response
60
- except Exception as e:
61
- return f"Error processing question: {str(e)}"
62
-
63
- if __name__ == "__main__":
64
- # Example usage
65
- test_question = "What is the capital of France?"
66
- print("=" * 80)
67
- print("GAIA Agent Test")
68
- print("=" * 80)
69
- print(f"Question: {test_question}")
70
- answer = run_agent(test_question)
71
- print(f"Answer: {answer}")
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
+