File size: 6,383 Bytes
17973dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#Building and Integrating Tools for Your Agent


#---------------------------------#
# Give Your Agent Access to the Web
#---------------------------------#
from smolagents import DuckDuckGoSearchTool
from typing import List


#Initalize the DuckDuckgo search tool
search_tool = DuckDuckGoSearchTool()

#Exsample usage:
results = search_tool("Who's the current president of France?")
print(results)
# Expected output: The current President of France in Emmanuel Macron.


#-----------------------------------------------------------------------------------------#
# Creating a Custom Tool that can be used to get the latest news about a specific topic.
#-----------------------------------------------------------------------------------------#
from newsapi import NewsApiClient
class GetLatestNewsTool(Tool):
    name="Latest_news"
    description="""Fetch the latest breaking headline news worldwide. supports filtering by keyword, country, category, or specific sources.
                Supports filtering by keyword, country, category, or specific sources.
                **Note:** you cannot use 'sources' together with 'country' or 'category';
                choose either sources OR country/category filters.
                """
    inputs = {
        "Query": {
            "type": "string",
            "description": "keywords or phrase to search for in headlines."
        },
        "Country": {
            "type": "string",
            "descripton": "2-letter country code (e.g., 'us', 'gb'). optional",
            "required": False
        },
        "category": {
            "type": "string",
            "description": "News category (e.g, 'buisness', 'sports'). optional",
            "required": False
        },
        "sources": {
            "type": "string",
            "description": "Comma-seperated list of news source ID'S to get headliens from. Optional",
            "required": False
        }
    }
    output = {
        "articles": {
            "type": "list",
            "description": "List of matching news articles. Each article contains: "
                            "`source` (ID and name), `author`, `title`, `description`,"
                            "`url`, `urlToImage`, `PublishedAt`, and `content`."
        }
    }
    
    def __init__(self, api_key):
        self.newsapi = NewsApiClient(api_key=api_key)

    def run(self, Query=None, Country=None, Category=None, sources= None):
        """
        Run the tool: Call NewsApi with the provided filters.
        """
        if sources and (Country or Category):   
            return "You cannot use `sources` together with 'country' or 'category'"
        

        response = self.newsapi.get_top_headlines(
            q=Query,
            country=Country,
            category=Category,
            sources=sources
        )

        return response








#------------------------------------------------------------------------------#
# Creating a Custom Tool for Weather Information to Schedule the Fireworks
#------------------------------------------------------------------------------#
from smolagents import Tool
import random 

class weatherinfoTool(Tool):
    name = "weather_info"
    description ="Fetches dummy weather information for a given location."
    inputs = {
        "location": {
            "type": "string",
            "description": "The location to get weather information for."
        }
    }
    output_type = "string"

    def forward(self, location: str):
        # Dummy weather data
        weather_conditions = [
            {"condition": "Rainy", "temp_c": 15},
            {"condition": "Clear", "temp_c": 25},
            {"condition": "Windy", "temp_c": 20}
        ]
        #Randomaly select a weather condition
        data = random.choice(weather_conditions)
        return f"Weather in {location}: {data['condition']}, {data['temp_c']}°C"

#Initalize the tool
weather_info_tool = weatherinfoTool()


#--------------------------------------------------------#
# Creating a Hub Stats Tool for Influential AI Builders
#--------------------------------------------------------#
from smolagents import Tool
from huggingface_hub import list_models

class HubStatsTool(Tool):
    name = "hub_stats"
    description = "Fetches the most downloaded model from a specific author on the Hugging Face Hub."
    inputs = {
        "author": {
            "type": "string",
            "description": "The username of the model author/organization to find models from."
        }
    }
    output_type = "string"

    def forward(self, author: str):
        try:
            models = List(list_models(author=author, sort="downloads", direction=-1, limit=1))
            if models:
                model = models[0]
                return f"The most downloaded model by {author} is {model.id} with {model.downloads:,} downloads"
            else:
                return f"No models found for author: {author}."
        except Exception as e:
            print(f"Error fetching model for {author}: {str(e)}")

#Initalize the tool
hub_stats_tool = HubStatsTool()

#Exsample usage
print(hub_stats_tool("facebook"))
#Expected output: The most downloaded model by facebook is facebook/esmfold_v1 with 12,544,550 downloads.

#--------------------------------------------------------#
# Integrating Tools with Alfred
#--------------------------------------------------------#

from smolagents import CodeAgent, InferenceClientModel

model = InferenceClientModel()

alfred = CodeAgent(
    tools=[search_tool, weather_info_tool, hub_stats_tool,GetLatestNewsTool()],
    model=model
)

#Exsample query Alfred might recieve during the gala 
response = alfred.run("what is facebook and what's their most popular model?")
print("🎩 Alfred's Response:")
print(response)
#Expected output: 🎩 Alfred's Response: Facebook is a social networking website where users can connect, share information, and interact with others. The most downloaded model by Facebook on the Hugging Face Hub is ESMFold_v1.


#--------------------------------------------------------#
# Conclusion
#--------------------------------------------------------#
#By integrating these tools, Alfred is now equipped to handle a variety of tasks, from web searches to weather updates and model statistics. This ensures he remains the most informed and engaging host at the gala.