File size: 3,686 Bytes
8712cc0
9b5b26a
 
 
c19d193
6aae614
9b5b26a
 
8712cc0
1c90da8
183cdef
 
 
8712cc0
 
 
183cdef
1c90da8
 
 
 
 
 
 
9e46e89
8712cc0
1c90da8
 
9ae337d
 
 
 
1c90da8
 
 
 
 
 
 
 
9ae337d
 
 
 
1c90da8
 
9ae337d
1c90da8
9ae337d
1c90da8
9b5b26a
 
8712cc0
9b5b26a
8712cc0
9b5b26a
 
 
 
 
 
8712cc0
8c01ffb
8712cc0
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
6aae614
ae7a494
e121372
8712cc0
 
 
 
13d500a
8c01ffb
8712cc0
9b5b26a
8c01ffb
8712cc0
 
 
 
75e86c2
 
33e67fa
75e86c2
8712cc0
75e86c2
 
 
 
 
 
8c01ffb
8fe992b
75e86c2
8712cc0
8c01ffb
 
 
 
 
8712cc0
8fe992b
 
8c01ffb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
import datetime
import requests
import pytz
import yaml
from tools.final_answer import FinalAnswerTool
from Gradio_UI import GradioUI

# Custom tools with complete docstrings
@tool
def currency_converter(amount: float, from_currency: str, to_currency: str) -> str:
    """Convert between currencies using real-time exchange rates
    Args:
        amount: The amount to convert (positive number)
        from_currency: Currency code to convert from (3-letter ISO code like USD)
        to_currency: Currency code to convert to (3-letter ISO code like EUR)
    """
    try:
        url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
        response = requests.get(url)
        rates = response.json()["rates"]
        converted = amount * rates[to_currency]
        return f"Convertion of {amount} {from_currency} = {converted:.2f} {to_currency}"
    except Exception as e:
        return f"Error converting {amount} {from_currency} to {to_currency}"

@tool
def website_availability(url: str) -> str:
    """Check if a website is online and responsive
    Args:
        url: The full URL of the website to check (including protocol like https://)
    """
    try:
        response = requests.get(url, timeout=5)
        return f"Website {url} is online (Status: {response.status_code})"
    except requests.exceptions.RequestException:
        return f"Website {url} is unreachable"

@tool
def text_summarizer(long_text: str) -> str:
    """Summarize long text documents
    Args:
        long_text: The text content to summarize (at least 3 sentences)
    """
    try:
        sentences = long_text.split('.')
        return '. '.join(sentences[:3]) + '...'
    except Exception as e:
        return f"Error summarizing text"

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """Get current time in a specific timezone
    Args:
        timezone: Valid timezone name (e.g. America/New_York or Asia/Tokyo)
    """
    try:
        tz = pytz.timezone(timezone)
        local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
        return f"The current local time in {timezone} is: {local_time}"
    except Exception as e:
        return f"Error fetching time: {str(e)}"

@tool
def get_current_weather(city: str) -> str:
    """Get current weather for a city
    Args:
        city: City name (e.g. Paris, London, Tokyo)
    """
    try:
        # Using a free weather API (no key required)
        url = f"https://wttr.in/{city}?format=%C+%t+%w"
        response = requests.get(url)
        return f"Weather in {city}: {response.text.strip()}"
    except Exception as e:
        return f"Weather lookup failed: {str(e)}"

final_answer = FinalAnswerTool()

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

# Load only working tools from Hub
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)
    
# Build tools list
all_tools = [
    final_answer,
    DuckDuckGoSearchTool(),  # Built-in search
    image_generation_tool,
    get_current_weather,     # Our custom weather tool
    get_current_time_in_timezone,
    currency_converter,
    website_availability,
    text_summarizer
]

agent = CodeAgent(
    model=model,
    tools=all_tools,
    max_steps=12,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

GradioUI(agent).launch()