Files changed (1) hide show
  1. app.py +42 -25
app.py CHANGED
@@ -7,31 +7,48 @@ from tools.final_answer import FinalAnswerTool
7
 
8
  from Gradio_UI import GradioUI
9
 
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
- @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
-
21
- @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
- Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
- """
27
- try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
- except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()