Spaces:
Sleeping
Sleeping
Upload 4 files
Browse filesAdded some more tools.
- tools/currency_tool.py +31 -0
- tools/math_tool.py +16 -0
- tools/news_tool.py +119 -0
- tools/password_tool.py +33 -0
tools/currency_tool.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import requests
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
|
| 6 |
+
"""Convert an amount from one currency to another
|
| 7 |
+
Args:
|
| 8 |
+
amount: Amount to convert
|
| 9 |
+
from_currency: Source currency code (e.g., USD)
|
| 10 |
+
to_currency: Target currency code (e.g., EUR)
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
# Using ExchangeRate-API
|
| 14 |
+
base_url = "https://api.exchangerate-api.com/v4/latest"
|
| 15 |
+
|
| 16 |
+
# Get exchange rates for the base currency
|
| 17 |
+
response = requests.get(f"{base_url}/{from_currency.upper()}")
|
| 18 |
+
data = response.json()
|
| 19 |
+
|
| 20 |
+
if response.status_code == 200:
|
| 21 |
+
# Get the exchange rate for the target currency
|
| 22 |
+
if to_currency.upper() in data['rates']:
|
| 23 |
+
rate = data['rates'][to_currency.upper()]
|
| 24 |
+
converted_amount = amount * rate
|
| 25 |
+
return f"{amount} {from_currency.upper()} = {converted_amount:.2f} {to_currency.upper()} (Rate: 1 {from_currency.upper()} = {rate:.4f} {to_currency.upper()})"
|
| 26 |
+
else:
|
| 27 |
+
return f"Error: Target currency {to_currency.upper()} not found"
|
| 28 |
+
else:
|
| 29 |
+
return f"Error: {data.get('error', 'Failed to fetch exchange rates')}"
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error converting currency: {str(e)}"
|
tools/math_tool.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import sympy
|
| 3 |
+
|
| 4 |
+
@tool
|
| 5 |
+
def solve_math_expression(expression: str) -> str:
|
| 6 |
+
"""Solve a mathematical expression
|
| 7 |
+
Args:
|
| 8 |
+
expression: Mathematical expression as string
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
# Convert string to sympy expression
|
| 12 |
+
expr = sympy.sympify(expression)
|
| 13 |
+
result = expr.evalf()
|
| 14 |
+
return f"Result: {result}"
|
| 15 |
+
except Exception as e:
|
| 16 |
+
return f"Error solving expression: {str(e)}"
|
tools/news_tool.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import os
|
| 3 |
+
from newsapi import NewsApiClient
|
| 4 |
+
import datetime
|
| 5 |
+
|
| 6 |
+
@tool
|
| 7 |
+
def get_news_headlines(topic: str, count: int = 5) -> str:
|
| 8 |
+
"""Get latest news headlines for a specific topic
|
| 9 |
+
Args:
|
| 10 |
+
topic: Topic to search news for
|
| 11 |
+
count: Number of headlines to return (default: 5)
|
| 12 |
+
"""
|
| 13 |
+
API_KEY = os.getenv("NEWSAPI_KEY")
|
| 14 |
+
if not API_KEY:
|
| 15 |
+
return "Error: NewsAPI key not found in environment variables"
|
| 16 |
+
|
| 17 |
+
newsapi = NewsApiClient(api_key=API_KEY)
|
| 18 |
+
|
| 19 |
+
try:
|
| 20 |
+
# Define search strategies with different parameters
|
| 21 |
+
search_strategies = [
|
| 22 |
+
{
|
| 23 |
+
'query': f'"{topic}"', # Exact phrase match
|
| 24 |
+
'relevance': 'high'
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
'query': topic, # Normal search
|
| 28 |
+
'relevance': 'high'
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
'query': f"{topic} latest", # Latest news
|
| 32 |
+
'relevance': 'medium'
|
| 33 |
+
}
|
| 34 |
+
]
|
| 35 |
+
|
| 36 |
+
relevant_articles = [] # Store only relevant articles
|
| 37 |
+
seen_titles = set()
|
| 38 |
+
required_keywords = set(topic.lower().split())
|
| 39 |
+
|
| 40 |
+
# Function to check article relevance
|
| 41 |
+
def is_relevant(article, required_words, relevance_level):
|
| 42 |
+
title = article['title'].lower()
|
| 43 |
+
description = (article.get('description') or '').lower()
|
| 44 |
+
content = (article.get('content') or '').lower()
|
| 45 |
+
|
| 46 |
+
# Count how many required words appear in the article
|
| 47 |
+
title_matches = sum(1 for word in required_words if word in title)
|
| 48 |
+
desc_matches = sum(1 for word in required_words if word in description)
|
| 49 |
+
content_matches = sum(1 for word in required_words if word in content)
|
| 50 |
+
|
| 51 |
+
# Calculate relevance score
|
| 52 |
+
total_score = (title_matches * 3) + (desc_matches * 2) + content_matches
|
| 53 |
+
|
| 54 |
+
# For exact phrase matching
|
| 55 |
+
if relevance_level == 'high':
|
| 56 |
+
# Check if the exact topic phrase appears
|
| 57 |
+
if topic.lower() in title or topic.lower() in description:
|
| 58 |
+
return True
|
| 59 |
+
return total_score >= len(required_words) * 2
|
| 60 |
+
else:
|
| 61 |
+
return total_score >= len(required_words)
|
| 62 |
+
|
| 63 |
+
for strategy in search_strategies:
|
| 64 |
+
if len(relevant_articles) >= count:
|
| 65 |
+
break
|
| 66 |
+
|
| 67 |
+
# Calculate how many more articles we need
|
| 68 |
+
remaining_count = count - len(relevant_articles)
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
news = newsapi.get_everything(
|
| 72 |
+
q=strategy['query'],
|
| 73 |
+
language='en',
|
| 74 |
+
sort_by='relevancy', # Changed to relevancy sort
|
| 75 |
+
page_size=min(50, remaining_count * 5) # Request more articles to filter through
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
if news['articles']:
|
| 79 |
+
for article in news['articles']:
|
| 80 |
+
# Skip if we've seen this title or have enough articles
|
| 81 |
+
if article['title'] in seen_titles:
|
| 82 |
+
continue
|
| 83 |
+
|
| 84 |
+
# Check if article is relevant enough
|
| 85 |
+
if is_relevant(article, required_keywords, strategy['relevance']):
|
| 86 |
+
seen_titles.add(article['title'])
|
| 87 |
+
pub_date = datetime.datetime.strptime(article['publishedAt'], '%Y-%m-%dT%H:%M:%SZ')
|
| 88 |
+
relevant_articles.append({
|
| 89 |
+
'title': article['title'],
|
| 90 |
+
'source': article['source']['name'],
|
| 91 |
+
'date': pub_date,
|
| 92 |
+
'url': article['url'],
|
| 93 |
+
'relevance': strategy['relevance']
|
| 94 |
+
})
|
| 95 |
+
|
| 96 |
+
# Break if we have enough relevant articles
|
| 97 |
+
if len(relevant_articles) >= count:
|
| 98 |
+
break
|
| 99 |
+
except Exception as e:
|
| 100 |
+
continue # If one strategy fails, try the next one
|
| 101 |
+
|
| 102 |
+
# Sort by date (newest first)
|
| 103 |
+
relevant_articles.sort(key=lambda x: x['date'], reverse=True)
|
| 104 |
+
|
| 105 |
+
if relevant_articles:
|
| 106 |
+
headlines = []
|
| 107 |
+
for idx, article in enumerate(relevant_articles, 1):
|
| 108 |
+
date_str = article['date'].strftime('%Y-%m-%d %H:%M UTC')
|
| 109 |
+
relevance_indicator = "🎯" if article['relevance'] == 'high' else "✓"
|
| 110 |
+
headlines.append(f"{idx}. {relevance_indicator} [{date_str}] {article['title']} ({article['source']})")
|
| 111 |
+
|
| 112 |
+
# Add a summary of how many relevant articles were found
|
| 113 |
+
found_count = len(relevant_articles)
|
| 114 |
+
summary = f"Found {found_count} relevant {'article' if found_count == 1 else 'articles'} out of {count} requested.\n\n"
|
| 115 |
+
return summary + "\n".join(headlines)
|
| 116 |
+
|
| 117 |
+
return f"No relevant news found for topic: {topic}"
|
| 118 |
+
except Exception as e:
|
| 119 |
+
return f"Error fetching news: {str(e)}"
|
tools/password_tool.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import tool
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
+
|
| 5 |
+
@tool
|
| 6 |
+
def generate_password(length: int = 12, include_special: bool = True) -> str:
|
| 7 |
+
"""Generate a secure random password
|
| 8 |
+
Args:
|
| 9 |
+
length: Length of password (default: 12)
|
| 10 |
+
include_special: Include special characters (default: True)
|
| 11 |
+
"""
|
| 12 |
+
try:
|
| 13 |
+
if length < 8:
|
| 14 |
+
return "Password length must be at least 8 characters"
|
| 15 |
+
|
| 16 |
+
chars = string.ascii_letters + string.digits
|
| 17 |
+
if include_special:
|
| 18 |
+
chars += string.punctuation
|
| 19 |
+
|
| 20 |
+
password = ''.join(random.choice(chars) for _ in range(length))
|
| 21 |
+
|
| 22 |
+
# Ensure password contains at least one of each required type
|
| 23 |
+
if not any(c.isupper() for c in password):
|
| 24 |
+
password = random.choice(string.ascii_uppercase) + password[1:]
|
| 25 |
+
if not any(c.islower() for c in password):
|
| 26 |
+
password = password[:-1] + random.choice(string.ascii_lowercase)
|
| 27 |
+
if not any(c.isdigit() for c in password):
|
| 28 |
+
pos = random.randint(1, len(password)-2)
|
| 29 |
+
password = password[:pos] + random.choice(string.digits) + password[pos+1:]
|
| 30 |
+
|
| 31 |
+
return f"Generated password: {password}"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Error generating password: {str(e)}"
|