KoRiF commited on
Commit
d9bf9a4
·
verified ·
1 Parent(s): ae7a494

Customize Agent Tools

Browse files

Added:
search_tool,
get_current_time_in_timezone,
native_language_translator,
weather_history,
weather_forecast,
suissknife_advisor_tool

Files changed (1) hide show
  1. app.py +106 -1
app.py CHANGED
@@ -18,6 +18,22 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
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.
@@ -34,6 +50,86 @@ def get_current_time_in_timezone(timezone: str) -> str:
34
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
 
36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  final_answer = FinalAnswerTool()
38
 
39
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
@@ -43,6 +139,7 @@ model = HfApiModel(
43
  max_tokens=2096,
44
  temperature=0.5,
45
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
 
46
  custom_role_conversions=None,
47
  )
48
 
@@ -55,7 +152,15 @@ with open("prompts.yaml", 'r') as stream:
55
 
56
  agent = CodeAgent(
57
  model=model,
58
- tools=[final_answer], ## add your tools here (don't remove final answer)
 
 
 
 
 
 
 
 
59
  max_steps=6,
60
  verbosity_level=1,
61
  grammar=None,
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ @tool
22
+ def suissknife_advisor_tool()->str:
23
+ """A tool that returns an indication of what to do at the next step - it is useful in cases where there is no other suitable tool at the user's disposal and the user does not know what he wants
24
+ May be used prior final answer to remember about additional steps
25
+ Args: (no args)
26
+ """
27
+ advice = '''
28
+ Try to undertand from context what is country or region user from.
29
+ Translate your answer to his state or region language (if this is not clear then use `be` translation as a target translation for your answer.
30
+ If you do not understand for sure what exacly user wants then try to search for a funny or encourage story about Mother Language Day
31
+ Duplicate your final answer with `en` and `bn` language translation for the case when user mentonied any stuff outside Europe
32
+ Use `native_language_translator` to translate your final answer to the target language
33
+
34
+ '''
35
+ return advice
36
+
37
  @tool
38
  def get_current_time_in_timezone(timezone: str) -> str:
39
  """A tool that fetches the current local time in a specified timezone.
 
50
  return f"Error fetching time for timezone '{timezone}': {str(e)}"
51
 
52
 
53
+ @tool
54
+ def weather_history(lat: float, lon: float, start: str, end: str) -> str:
55
+ """
56
+ Fetches historical weather data for a given location and date range using the Meteostat library.
57
+
58
+ Args:
59
+ lat: Latitude of the location.
60
+ lon: Longitude of the location.
61
+ start: Start date in YYYY-MM-DD format.
62
+ end: End date in YYYY-MM-DD format.
63
+
64
+ Returns:
65
+ A JSON string containing the historical weather data or an error message.
66
+ """
67
+ from meteostat import Point, Daily
68
+ try:
69
+ # Parse the date strings into datetime objects
70
+ start_date = datetime.strptime(start, "%Y-%m-%d")
71
+ end_date = datetime.strptime(end, "%Y-%m-%d")
72
+
73
+ # Define the location using latitude and longitude
74
+ location = Point(lat, lon)
75
+
76
+ # Fetch daily historical data for the location between start_date and end_date
77
+ data = Daily(location, start_date, end_date)
78
+ data = data.fetch()
79
+
80
+ # Return the data as a JSON string
81
+ return data.to_json()
82
+ except Exception as e:
83
+ return f"Error fetching weather history: {str(e)}"
84
+
85
+
86
+ @tool
87
+ def weather_forecast(lat: float, lon: float) -> str:
88
+ """
89
+ Fetches weather forecast data for the next 7 days for a given location using the Open-Meteo API.
90
+
91
+ Args:
92
+ lat: Latitude of the location.
93
+ lon: Longitude of the location.
94
+
95
+ Returns:
96
+ A JSON string containing the weather forecast data or an error message.
97
+ """
98
+ try:
99
+ # Construct the API URL with daily forecast for max and min temperatures.
100
+ url = (
101
+ f"https://api.open-meteo.com/v1/forecast?"
102
+ f"latitude={lat}&longitude={lon}"
103
+ f"&daily=temperature_2m_max,temperature_2m_min,precipitation_sum"
104
+ f"&timezone=auto"
105
+ )
106
+
107
+ # Request the forecast data
108
+ response = requests.get(url)
109
+ if response.status_code == 200:
110
+ return response.text
111
+ else:
112
+ return f"Error fetching forecast: {response.status_code}"
113
+ except Exception as e:
114
+ return f"Error fetching forecast: {str(e)}"
115
+
116
+
117
+ @tool
118
+ def native_language_translator(text: str, target_lang: str) -> str:
119
+ """Translates text to a specified native language
120
+ Args:
121
+ text: Input text to translate
122
+ target_lang: Target language code (e.g., 'be', 'es', 'fr', 'zh')
123
+ """
124
+ try:
125
+ from googletrans import Translator
126
+ translator = Translator()
127
+ translation = translator.translate(text, dest=target_lang)
128
+ return f"Translated to {target_lang}: {translation.text}"
129
+ except Exception as e:
130
+ return f"Translation failed: {str(e)}"
131
+
132
+ search_tool = DuckDuckGoSearchTool()
133
  final_answer = FinalAnswerTool()
134
 
135
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
 
139
  max_tokens=2096,
140
  temperature=0.5,
141
  model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
142
+ fallback_model_id='meta-llama/Meta-Llama-3-70B-Instruct', # Fallback model
143
  custom_role_conversions=None,
144
  )
145
 
 
152
 
153
  agent = CodeAgent(
154
  model=model,
155
+ tools=[
156
+ search_tool,
157
+ get_current_time_in_timezone,
158
+ native_language_translator,
159
+ weather_history,
160
+ weather_forecast,
161
+ suissknife_advisor_tool,
162
+ final_answer,
163
+ ], ## add your tools here (don't remove final answer)
164
  max_steps=6,
165
  verbosity_level=1,
166
  grammar=None,