Spaces:
Runtime error
Runtime error
Update my_tools.py
Browse files- my_tools.py +0 -88
my_tools.py
CHANGED
|
@@ -85,73 +85,6 @@ def get_weather(location: str, celsius: Optional[bool] = False) -> str:
|
|
| 85 |
return f"Error fetching weather data: {str(e)}"
|
| 86 |
|
| 87 |
|
| 88 |
-
@tool
|
| 89 |
-
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
| 90 |
-
"""
|
| 91 |
-
Converts a specified amount from one currency to another using the ExchangeRate-API.
|
| 92 |
-
|
| 93 |
-
Args:
|
| 94 |
-
amount: The amount of money to convert.
|
| 95 |
-
from_currency: The currency code of the currency to convert from (e.g., 'USD').
|
| 96 |
-
to_currency: The currency code of the currency to convert to (e.g., 'EUR').
|
| 97 |
-
|
| 98 |
-
Returns:
|
| 99 |
-
str: A string describing the converted amount in the target currency, or an error message if the conversion fails.
|
| 100 |
-
|
| 101 |
-
Raises:
|
| 102 |
-
requests.exceptions.RequestException: If there is an issue with the HTTP request to the ExchangeRate-API.
|
| 103 |
-
"""
|
| 104 |
-
api_key = "your_api_key" # Replace with your actual API key from https://www.exchangerate-api.com/
|
| 105 |
-
url = f"https://v6.exchangerate-api.com/v6/{api_key}/latest/{from_currency}"
|
| 106 |
-
|
| 107 |
-
try:
|
| 108 |
-
response = requests.get(url)
|
| 109 |
-
response.raise_for_status()
|
| 110 |
-
|
| 111 |
-
data = response.json()
|
| 112 |
-
exchange_rate = data["conversion_rates"].get(to_currency)
|
| 113 |
-
|
| 114 |
-
if not exchange_rate:
|
| 115 |
-
return f"Error: Unable to find exchange rate for {from_currency} to {to_currency}."
|
| 116 |
-
|
| 117 |
-
converted_amount = amount * exchange_rate
|
| 118 |
-
return f"{amount} {from_currency} is equal to {converted_amount} {to_currency}."
|
| 119 |
-
|
| 120 |
-
except requests.exceptions.RequestException as e:
|
| 121 |
-
return f"Error fetching conversion data: {str(e)}"
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
@tool
|
| 125 |
-
def get_news_headlines() -> str:
|
| 126 |
-
"""
|
| 127 |
-
Fetches the top news headlines from the News API for the United States.
|
| 128 |
-
This function makes a GET request to the News API to retrieve the top news headlines
|
| 129 |
-
for the United States. It returns the titles and sources of the top 5 articles as a
|
| 130 |
-
formatted string. If no articles are available, it returns a message indicating that
|
| 131 |
-
no news is available. In case of a request error, it returns an error message.
|
| 132 |
-
Returns:
|
| 133 |
-
str: A string containing the top 5 news headlines and their sources, or an error message.
|
| 134 |
-
"""
|
| 135 |
-
api_key = "your_api_key" # Replace with your actual API key from https://newsapi.org/
|
| 136 |
-
url = f"https://newsapi.org/v2/top-headlines?country=us&apiKey={api_key}"
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
response = requests.get(url)
|
| 140 |
-
response.raise_for_status()
|
| 141 |
-
|
| 142 |
-
data = response.json()
|
| 143 |
-
articles = data["articles"]
|
| 144 |
-
|
| 145 |
-
if not articles:
|
| 146 |
-
return "No news available at the moment."
|
| 147 |
-
|
| 148 |
-
headlines = [f"{article['title']} - {article['source']['name']}" for article in articles[:5]]
|
| 149 |
-
return "\n".join(headlines)
|
| 150 |
-
|
| 151 |
-
except requests.exceptions.RequestException as e:
|
| 152 |
-
return f"Error fetching news data: {str(e)}"
|
| 153 |
-
|
| 154 |
-
|
| 155 |
@tool
|
| 156 |
def get_joke() -> str:
|
| 157 |
"""
|
|
@@ -207,27 +140,6 @@ def get_time_in_timezone(location: str) -> str:
|
|
| 207 |
return f"Error fetching time data: {str(e)}"
|
| 208 |
|
| 209 |
|
| 210 |
-
@tool
|
| 211 |
-
def get_random_fact() -> str:
|
| 212 |
-
"""
|
| 213 |
-
Fetches a random fact from the "uselessfacts.jsph.pl" API.
|
| 214 |
-
Returns:
|
| 215 |
-
str: A string containing the random fact or an error message if the request fails.
|
| 216 |
-
"""
|
| 217 |
-
url = "https://uselessfacts.jsph.pl/random.json?language=en"
|
| 218 |
-
|
| 219 |
-
try:
|
| 220 |
-
response = requests.get(url)
|
| 221 |
-
response.raise_for_status()
|
| 222 |
-
|
| 223 |
-
data = response.json()
|
| 224 |
-
|
| 225 |
-
return f"Random Fact: {data['text']}"
|
| 226 |
-
|
| 227 |
-
except requests.exceptions.RequestException as e:
|
| 228 |
-
return f"Error fetching random fact: {str(e)}"
|
| 229 |
-
|
| 230 |
-
|
| 231 |
@tool
|
| 232 |
def search_wikipedia(query: str) -> str:
|
| 233 |
"""
|
|
|
|
| 85 |
return f"Error fetching weather data: {str(e)}"
|
| 86 |
|
| 87 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
@tool
|
| 89 |
def get_joke() -> str:
|
| 90 |
"""
|
|
|
|
| 140 |
return f"Error fetching time data: {str(e)}"
|
| 141 |
|
| 142 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
@tool
|
| 144 |
def search_wikipedia(query: str) -> str:
|
| 145 |
"""
|