Spaces:
Runtime error
Runtime error
Update app.py
#2
by
wt002
- opened
app.py
CHANGED
@@ -7,31 +7,48 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
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 |
final_answer = FinalAnswerTool()
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
import requests
|
11 |
+
|
12 |
+
class FinalAnswerTool:
|
13 |
+
def __call__(self, result: str):
|
14 |
+
return f"\n Final Answer:\n{result}"
|
15 |
+
|
16 |
+
class ToolCallingAgent:
|
17 |
+
def __init__(self, tools, final_tool, model, max_steps=10):
|
18 |
+
self.tools = tools
|
19 |
+
self.final_tool = final_tool
|
20 |
+
self.model = model
|
21 |
+
self.max_steps = max_steps
|
22 |
+
|
23 |
+
def run(self, input_text):
|
24 |
+
print(f"\n Agent received: {input_text}")
|
25 |
+
for tool in self.tools:
|
26 |
+
try:
|
27 |
+
result = tool(input_text)
|
28 |
+
if result:
|
29 |
+
final_output = self.final_tool(str(result))
|
30 |
+
print(final_output)
|
31 |
+
return final_output # Stop after first useful tool response
|
32 |
+
except Exception as e:
|
33 |
+
print(f"\n[{tool.__class__.__name__} Error]: {str(e)}")
|
34 |
+
|
35 |
+
# Initialize all tools
|
36 |
+
weather_tool = WeatherTool(api_key="YOUR_API_KEY_HERE")
|
37 |
+
visit_webpage = VisitWebpageTool()
|
38 |
+
duckduckgo_tool = CustomDuckDuckGoSearchTool()
|
39 |
+
final_answer = FinalAnswerTool()
|
40 |
+
model = DummyModel()
|
41 |
+
|
42 |
+
# Initialize the agent with the final answer tool
|
43 |
+
web_agent = ToolCallingAgent(
|
44 |
+
tools=[duckduckgo_tool, visit_webpage, weather_tool],
|
45 |
+
final_tool=final_answer,
|
46 |
+
model=model,
|
47 |
+
max_steps=10
|
48 |
+
)
|
49 |
+
|
50 |
+
# Run with a query
|
51 |
+
web_agent.run("Tokyo") # Works for weather, search, or URL
|
52 |
|
53 |
|
54 |
final_answer = FinalAnswerTool()
|