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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -47,17 +271,28 @@ custom_role_conversions=None,
|
|
| 47 |
|
| 48 |
# Import tool from Hub
|
| 49 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
|
| 50 |
|
| 51 |
with open("prompts.yaml", 'r') as stream:
|
| 52 |
prompt_templates = yaml.safe_load(stream)
|
| 53 |
|
| 54 |
agent = CodeAgent(
|
| 55 |
model=model,
|
| 56 |
-
tools=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
max_steps=10,
|
| 58 |
verbosity_level=4,
|
| 59 |
grammar=None,
|
| 60 |
-
planning_interval=
|
| 61 |
name=None,
|
| 62 |
description=None,
|
| 63 |
prompt_templates=prompt_templates
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
|
| 11 |
+
from typing import Optional
|
| 12 |
+
|
| 13 |
+
import requests
|
| 14 |
+
|
| 15 |
+
# from smolagents.agents import ToolCallingAgent
|
| 16 |
+
from smolagents import CodeAgent, HfApiModel, tool
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# Choose which LLM engine to use!
|
| 20 |
+
model = HfApiModel()
|
| 21 |
+
# model = TransformersModel(model_id="meta-llama/Llama-3.2-2B-Instruct")
|
| 22 |
+
|
| 23 |
+
# For anthropic: change model_id below to 'anthropic/claude-3-5-sonnet-20240620'
|
| 24 |
+
# model = LiteLLMModel(model_id="gpt-4o")
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@tool
|
| 28 |
+
def get_weather(location: str, celsius: Optional[bool] = False) -> str:
|
| 29 |
+
"""
|
| 30 |
+
Get the current weather at the given location using the WeatherStack API.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
location: The location (city name).
|
| 34 |
+
celsius: Whether to return the temperature in Celsius (default is False, which returns Fahrenheit).
|
| 35 |
+
|
| 36 |
+
Returns:
|
| 37 |
+
A string describing the current weather at the location.
|
| 38 |
+
"""
|
| 39 |
+
api_key = "your_api_key" # Replace with your API key from https://weatherstack.com/
|
| 40 |
+
units = "m" if celsius else "f" # 'm' for Celsius, 'f' for Fahrenheit
|
| 41 |
+
|
| 42 |
+
url = f"http://api.weatherstack.com/current?access_key={api_key}&query={location}&units={units}"
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
response = requests.get(url)
|
| 46 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 47 |
+
|
| 48 |
+
data = response.json()
|
| 49 |
+
|
| 50 |
+
if data.get("error"): # Check if there's an error in the response
|
| 51 |
+
return f"Error: {data['error'].get('info', 'Unable to fetch weather data.')}"
|
| 52 |
+
|
| 53 |
+
weather = data["current"]["weather_descriptions"][0]
|
| 54 |
+
temp = data["current"]["temperature"]
|
| 55 |
+
temp_unit = "°C" if celsius else "°F"
|
| 56 |
+
|
| 57 |
+
return f"The current weather in {location} is {weather} with a temperature of {temp} {temp_unit}."
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
return f"Error fetching weather data: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
@tool
|
| 64 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
| 65 |
+
"""
|
| 66 |
+
Converts a specified amount from one currency to another using the ExchangeRate-API.
|
| 67 |
+
|
| 68 |
+
Args:
|
| 69 |
+
amount: The amount of money to convert.
|
| 70 |
+
from_currency: The currency code of the currency to convert from (e.g., 'USD').
|
| 71 |
+
to_currency: The currency code of the currency to convert to (e.g., 'EUR').
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
str: A string describing the converted amount in the target currency, or an error message if the conversion fails.
|
| 75 |
+
|
| 76 |
+
Raises:
|
| 77 |
+
requests.exceptions.RequestException: If there is an issue with the HTTP request to the ExchangeRate-API.
|
| 78 |
+
"""
|
| 79 |
+
api_key = "your_api_key" # Replace with your actual API key from https://www.exchangerate-api.com/
|
| 80 |
+
url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{from_currency}"
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
response = requests.get(url)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
|
| 86 |
+
data = response.json()
|
| 87 |
+
exchange_rate = data["conversion_rates"].get(to_currency)
|
| 88 |
+
|
| 89 |
+
if not exchange_rate:
|
| 90 |
+
return f"Error: Unable to find exchange rate for {from_currency} to {to_currency}."
|
| 91 |
+
|
| 92 |
+
converted_amount = amount * exchange_rate
|
| 93 |
+
return f"{amount} {from_currency} is equal to {converted_amount} {to_currency}."
|
| 94 |
+
|
| 95 |
+
except requests.exceptions.RequestException as e:
|
| 96 |
+
return f"Error fetching conversion data: {str(e)}"
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
@tool
|
| 100 |
+
def get_news_headlines() -> str:
|
| 101 |
+
"""
|
| 102 |
+
Fetches the top news headlines from the News API for the United States.
|
| 103 |
+
This function makes a GET request to the News API to retrieve the top news headlines
|
| 104 |
+
for the United States. It returns the titles and sources of the top 5 articles as a
|
| 105 |
+
formatted string. If no articles are available, it returns a message indicating that
|
| 106 |
+
no news is available. In case of a request error, it returns an error message.
|
| 107 |
+
Returns:
|
| 108 |
+
str: A string containing the top 5 news headlines and their sources, or an error message.
|
| 109 |
+
"""
|
| 110 |
+
api_key = "your_api_key" # Replace with your actual API key from https://newsapi.org/
|
| 111 |
+
url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}"
|
| 112 |
+
|
| 113 |
+
try:
|
| 114 |
+
response = requests.get(url)
|
| 115 |
+
response.raise_for_status()
|
| 116 |
+
|
| 117 |
+
data = response.json()
|
| 118 |
+
articles = data["articles"]
|
| 119 |
+
|
| 120 |
+
if not articles:
|
| 121 |
+
return "No news available at the moment."
|
| 122 |
+
|
| 123 |
+
headlines = [f"{article['title']} - {article['source']['name']}" for article in articles[:5]]
|
| 124 |
+
return "\n".join(headlines)
|
| 125 |
+
|
| 126 |
+
except requests.exceptions.RequestException as e:
|
| 127 |
+
return f"Error fetching news data: {str(e)}"
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
@tool
|
| 131 |
+
def get_joke() -> str:
|
| 132 |
+
"""
|
| 133 |
+
Fetches a random joke from the JokeAPI.
|
| 134 |
+
This function sends a GET request to the JokeAPI to retrieve a random joke.
|
| 135 |
+
It handles both single jokes and two-part jokes (setup and delivery).
|
| 136 |
+
If the request fails or the response does not contain a joke, an error message is returned.
|
| 137 |
+
Returns:
|
| 138 |
+
str: The joke as a string, or an error message if the joke could not be fetched.
|
| 139 |
+
"""
|
| 140 |
+
url = "https://v2.jokeapi.dev/joke/Any?type=single"
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
response = requests.get(url)
|
| 144 |
+
response.raise_for_status()
|
| 145 |
+
|
| 146 |
+
data = response.json()
|
| 147 |
+
|
| 148 |
+
if "joke" in data:
|
| 149 |
+
return data["joke"]
|
| 150 |
+
elif "setup" in data and "delivery" in data:
|
| 151 |
+
return f"{data['setup']} - {data['delivery']}"
|
| 152 |
+
else:
|
| 153 |
+
return "Error: Unable to fetch joke."
|
| 154 |
+
|
| 155 |
+
except requests.exceptions.RequestException as e:
|
| 156 |
+
return f"Error fetching joke: {str(e)}"
|
| 157 |
+
|
| 158 |
+
|
| 159 |
+
@tool
|
| 160 |
+
def get_time_in_timezone(location: str) -> str:
|
| 161 |
+
"""
|
| 162 |
+
Fetches the current time for a given location using the World Time API.
|
| 163 |
+
Args:
|
| 164 |
+
location: The location for which to fetch the current time, formatted as 'Region/City'.
|
| 165 |
+
Returns:
|
| 166 |
+
str: A string indicating the current time in the specified location, or an error message if the request fails.
|
| 167 |
+
Raises:
|
| 168 |
+
requests.exceptions.RequestException: If there is an issue with the HTTP request.
|
| 169 |
+
"""
|
| 170 |
+
url = f"http://worldtimeapi.org/api/timezone/{location}.json"
|
| 171 |
+
|
| 172 |
+
try:
|
| 173 |
+
response = requests.get(url)
|
| 174 |
+
response.raise_for_status()
|
| 175 |
+
|
| 176 |
+
data = response.json()
|
| 177 |
+
current_time = data["datetime"]
|
| 178 |
+
|
| 179 |
+
return f"The current time in {location} is {current_time}."
|
| 180 |
+
|
| 181 |
+
except requests.exceptions.RequestException as e:
|
| 182 |
+
return f"Error fetching time data: {str(e)}"
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
@tool
|
| 186 |
+
def get_random_fact() -> str:
|
| 187 |
+
"""
|
| 188 |
+
Fetches a random fact from the "uselessfacts.jsph.pl" API.
|
| 189 |
+
Returns:
|
| 190 |
+
str: A string containing the random fact or an error message if the request fails.
|
| 191 |
+
"""
|
| 192 |
+
url = "https://uselessfacts.jsph.pl/random.json?language=en"
|
| 193 |
+
|
| 194 |
+
try:
|
| 195 |
+
response = requests.get(url)
|
| 196 |
+
response.raise_for_status()
|
| 197 |
+
|
| 198 |
+
data = response.json()
|
| 199 |
+
|
| 200 |
+
return f"Random Fact: {data['text']}"
|
| 201 |
+
|
| 202 |
+
except requests.exceptions.RequestException as e:
|
| 203 |
+
return f"Error fetching random fact: {str(e)}"
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
@tool
|
| 207 |
+
def search_wikipedia(query: str) -> str:
|
| 208 |
+
"""
|
| 209 |
+
Fetches a summary of a Wikipedia page for a given query.
|
| 210 |
+
Args:
|
| 211 |
+
query: The search term to look up on Wikipedia.
|
| 212 |
+
Returns:
|
| 213 |
+
str: A summary of the Wikipedia page if successful, or an error message if the request fails.
|
| 214 |
+
Raises:
|
| 215 |
+
requests.exceptions.RequestException: If there is an issue with the HTTP request.
|
| 216 |
+
"""
|
| 217 |
+
url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
|
| 218 |
+
|
| 219 |
+
try:
|
| 220 |
+
response = requests.get(url)
|
| 221 |
+
response.raise_for_status()
|
| 222 |
+
|
| 223 |
+
data = response.json()
|
| 224 |
+
title = data["title"]
|
| 225 |
+
extract = data["extract"]
|
| 226 |
+
|
| 227 |
+
return f"Summary for {title}: {extract}"
|
| 228 |
+
|
| 229 |
+
except requests.exceptions.RequestException as e:
|
| 230 |
+
return f"Error fetching Wikipedia data: {str(e)}"
|
| 231 |
+
|
| 232 |
+
|
| 233 |
+
|
| 234 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 235 |
@tool
|
| 236 |
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 271 |
|
| 272 |
# Import tool from Hub
|
| 273 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 274 |
+
search_tool = DuckDuckGoSearchTool()
|
| 275 |
|
| 276 |
with open("prompts.yaml", 'r') as stream:
|
| 277 |
prompt_templates = yaml.safe_load(stream)
|
| 278 |
|
| 279 |
agent = CodeAgent(
|
| 280 |
model=model,
|
| 281 |
+
tools=[
|
| 282 |
+
final_answer,
|
| 283 |
+
search_tool,
|
| 284 |
+
image_generation_tool,
|
| 285 |
+
convert_currency,
|
| 286 |
+
get_weather,
|
| 287 |
+
get_news_headlines,
|
| 288 |
+
get_joke,
|
| 289 |
+
get_random_fact,
|
| 290 |
+
search_wikipedia,
|
| 291 |
+
],
|
| 292 |
max_steps=10,
|
| 293 |
verbosity_level=4,
|
| 294 |
grammar=None,
|
| 295 |
+
planning_interval=3,
|
| 296 |
name=None,
|
| 297 |
description=None,
|
| 298 |
prompt_templates=prompt_templates
|