Spaces:
Build error
Build error
added required packages
Browse files
agent.py
CHANGED
@@ -1,11 +1,15 @@
|
|
1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
|
2 |
from smolagents import tool
|
|
|
3 |
import os
|
4 |
|
5 |
-
# Initialize model
|
6 |
-
model = InferenceClientModel(
|
|
|
|
|
|
|
7 |
|
8 |
-
#
|
9 |
@tool
|
10 |
def add(a: int, b: int) -> int:
|
11 |
"""Add two numbers.
|
@@ -37,7 +41,7 @@ def subtract(a: int, b: int) -> int:
|
|
37 |
return a - b
|
38 |
|
39 |
@tool
|
40 |
-
def divide(a: int, b: int) ->
|
41 |
"""Divide two numbers.
|
42 |
|
43 |
Args:
|
@@ -58,15 +62,37 @@ def modulus(a: int, b: int) -> int:
|
|
58 |
"""
|
59 |
return a % b
|
60 |
|
61 |
-
# Create agent with tools
|
62 |
agent = CodeAgent(
|
63 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
model=model,
|
|
|
|
|
|
|
65 |
)
|
66 |
|
|
|
67 |
def answer_question(question: str) -> str:
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from smolagents import CodeAgent, DuckDuckGoSearchTool, InferenceClientModel
|
2 |
from smolagents import tool
|
3 |
+
from smolagents.tools import WikipediaTool, ArxivTool, ImageReaderTool, PythonREPLTool
|
4 |
import os
|
5 |
|
6 |
+
# Initialize model with token
|
7 |
+
model = InferenceClientModel(
|
8 |
+
model_id="Qwen/Qwen2.5-72B-Instruct",
|
9 |
+
token=os.environ.get("HF_TOKEN")
|
10 |
+
)
|
11 |
|
12 |
+
# Define math tools with detailed docstrings
|
13 |
@tool
|
14 |
def add(a: int, b: int) -> int:
|
15 |
"""Add two numbers.
|
|
|
41 |
return a - b
|
42 |
|
43 |
@tool
|
44 |
+
def divide(a: int, b: int) -> float:
|
45 |
"""Divide two numbers.
|
46 |
|
47 |
Args:
|
|
|
62 |
"""
|
63 |
return a % b
|
64 |
|
65 |
+
# Create the agent with all required tools
|
66 |
agent = CodeAgent(
|
67 |
+
tools=[
|
68 |
+
DuckDuckGoSearchTool(),
|
69 |
+
WikipediaTool(),
|
70 |
+
ArxivTool(),
|
71 |
+
ImageReaderTool(),
|
72 |
+
PythonREPLTool(),
|
73 |
+
add,
|
74 |
+
multiply,
|
75 |
+
subtract,
|
76 |
+
divide,
|
77 |
+
modulus
|
78 |
+
],
|
79 |
model=model,
|
80 |
+
planning_interval=3, # Enable planning every 3 steps
|
81 |
+
max_steps=15, # Limit max steps to prevent infinite loops
|
82 |
+
verbosity_level=1 # Enable some logging for debugging
|
83 |
)
|
84 |
|
85 |
+
# Function to run the agent and ensure correct output format
|
86 |
def answer_question(question: str) -> str:
|
87 |
+
try:
|
88 |
+
result = agent.run(question)
|
89 |
+
if not result.strip().startswith("FINAL ANSWER:"):
|
90 |
+
result = f"FINAL ANSWER: {result.strip()}"
|
91 |
+
return result
|
92 |
+
except Exception as e:
|
93 |
+
return f"FINAL ANSWER: [AGENT ERROR: {str(e)}]"
|
94 |
+
|
95 |
+
# Example test
|
96 |
+
if __name__ == "__main__":
|
97 |
+
test_question = "What is the capital of France?"
|
98 |
+
print(answer_question(test_question))
|