File size: 3,985 Bytes
df50f02
9b5b26a
 
 
c19d193
8fe992b
df50f02
9b5b26a
 
c3822aa
df50f02
c3822aa
df50f02
 
 
9b5b26a
df50f02
 
 
 
9b5b26a
df50f02
 
 
 
 
9b5b26a
c3822aa
 
df50f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9b5b26a
 
 
df50f02
 
 
9b5b26a
 
df50f02
 
 
9b5b26a
 
 
 
 
 
 
8c01ffb
df50f02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c01ffb
df50f02
6aae614
ae7a494
c3822aa
e121372
df50f02
 
c3822aa
df50f02
13d500a
8c01ffb
c3822aa
9b5b26a
8c01ffb
df50f02
861422e
 
df50f02
c3822aa
8c01ffb
8fe992b
df50f02
8c01ffb
 
 
 
 
 
861422e
8fe992b
 
df50f02
 
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
118
119
120
121
122
123
124
125
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

# Use updated API from duckduckgo_search: import DDGS instead of ddg
try:
    from duckduckgo_search import DDGS
except ImportError:
    raise ImportError("Please install duckduckgo_search using 'pip install duckduckgo_search'.")

@tool
def DuckDuckGoSearchTool(arg1: str, arg2: int) -> str:
    """
    A tool that lets you search the web using DuckDuckGo.
    
    Args:
        arg1: The search query.
        arg2: The maximum number of results to return.
    
    Returns:
        A formatted string containing the search results.
    """
    with DDGS() as ddgs:
        results = ddgs.text(arg1, max_results=arg2)
    if not results:
        return "No results found."
    
    formatted_results = []
    for idx, result in enumerate(results, start=1):
        title = result.get("title", "No title")
        href = result.get("href", "No URL")
        snippet = result.get("body", "No description available")
        formatted_results.append(
            f"Result {idx}:\n"
            f"🔍 Title: {title}\n"
            f"🌐 URL: {href}\n"
            f"📝 Snippet: {snippet}\n"
        )
    return "\n".join(formatted_results)

@tool
def get_current_time_in_timezone(timezone: str) -> str:
    """
    A tool that fetches the current local time in a specified timezone.
    
    Args:
        timezone: A string representing a valid timezone (e.g., 'America/New_York').
    
    Returns:
        A string indicating the current local time in the given timezone.
    """
    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 for timezone '{timezone}': {str(e)}"

@tool
def get_weather_info(location: str) -> str:
    """
    A tool that fetches current weather information for a given location using wttr.in.
    
    Args:
        location: The name of the location (e.g., 'New York').
    
    Returns:
        A formatted string with the current weather details.
    """
    try:
        url = f"http://wttr.in/{location}?format=j1"
        response = requests.get(url)
        data = response.json()
        current_condition = data.get("current_condition", [{}])[0]
        temp_c = current_condition.get("temp_C", "N/A")
        weather_desc = current_condition.get("weatherDesc", [{"value": "N/A"}])[0]["value"]
        humidity = current_condition.get("humidity", "N/A")
        return (
            f"Current weather in {location}:\n"
            f"🌡 Temperature: {temp_c}°C\n"
            f"🌤 Weather: {weather_desc}\n"
            f"💧 Humidity: {humidity}%"
        )
    except Exception as e:
        return f"Error fetching weather for {location}: {str(e)}"

# Initialize the final answer tool.
final_answer = FinalAnswerTool()

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

# Load an additional image generation tool from the Hub.
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)

# Load prompt templates from a YAML configuration file.
with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

# Create the CodeAgent with the specified model and tools.
agent = CodeAgent(
    model=model,
    tools=[final_answer, DuckDuckGoSearchTool, get_current_time_in_timezone, get_weather_info],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

# Launch the Gradio UI for interactive use.
GradioUI(agent).launch()