cjb97 commited on
Commit
4c7943e
·
1 Parent(s): bd6d853

Update .gitignore and remove instructions file

Browse files
Files changed (2) hide show
  1. .gitignore +2 -0
  2. instructions +0 -202
.gitignore CHANGED
@@ -52,3 +52,5 @@ logs/
52
  # OS-specific
53
  .DS_Store
54
  Thumbs.db
 
 
 
52
  # OS-specific
53
  .DS_Store
54
  Thumbs.db
55
+
56
+ instructions.txt
instructions DELETED
@@ -1,202 +0,0 @@
1
- Let’s Create Our First Agent Using smolagents
2
- In the last section, we learned how we can create Agents from scratch using Python code, and we saw just how tedious that process can be. Fortunately, many Agent libraries simplify this work by handling much of the heavy lifting for you.
3
-
4
- In this tutorial, you’ll create your very first Agent capable of performing actions such as image generation, web search, time zone checking and much more!
5
-
6
- You will also publish your agent on a Hugging Face Space so you can share it with friends and colleagues.
7
-
8
- Let’s get started!
9
-
10
- What is smolagents?
11
- smolagents
12
- To make this Agent, we’re going to use smolagents, a library that provides a framework for developing your agents with ease.
13
-
14
- This lightweight library is designed for simplicity, but it abstracts away much of the complexity of building an Agent, allowing you to focus on designing your agent’s behavior.
15
-
16
- We’re going to get deeper into smolagents in the next Unit. Meanwhile, you can also check this blog post or the library’s repo in GitHub.
17
-
18
- In short, smolagents is a library that focuses on codeAgent, a kind of agent that performs “Actions” through code blocks, and then “Observes” results by executing the code.
19
-
20
- Here is an example of what we’ll build!
21
-
22
- We provided our agent with an Image generation tool and asked it to generate an image of a cat.
23
-
24
- The agent inside smolagents is going to have the same behaviors as the custom one we built previously: it’s going to think, act and observe in cycle until it reaches a final answer:
25
-
26
-
27
- Exciting, right?
28
-
29
- Let’s build our Agent!
30
- To start, duplicate this Space: https://huggingface.co/spaces/agents-course/First_agent_template
31
-
32
- Thanks to Aymeric for this template! 🙌
33
-
34
- Duplicating this space means creating a local copy on your own profile:
35
-
36
- Duplicate
37
- Throughout this lesson, the only file you will need to modify is the (currently incomplete) “app.py”. You can see here the original one in the template. To find yours, go to your copy of the space, then click the Files tab and then on app.py in the directory listing.
38
-
39
- Let’s break down the code together:
40
-
41
- The file begins with some simple but necessary library imports
42
- Copied
43
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
44
- import datetime
45
- import requests
46
- import pytz
47
- import yaml
48
- from tools.final_answer import FinalAnswerTool
49
- As outlined earlier, we will directly use the CodeAgent class from smolagents.
50
-
51
- The Tools
52
- Now let’s get into the tools! If you want a refresher about tools, don’t hesitate to go back to the Tools section of the course.
53
-
54
- Copied
55
-
56
- def my_custom_tool(arg1:str, arg2:int)-> str: # it's important to specify the return type
57
- # Keep this format for the tool description / args description but feel free to modify the tool
58
- """A tool that does nothing yet
59
- Args:
60
- arg1: the first argument
61
- arg2: the second argument
62
- """
63
- return "What magic will you build ?"
64
-
65
-
66
- def get_current_time_in_timezone(timezone: str) -> str:
67
- """A tool that fetches the current local time in a specified timezone.
68
- Args:
69
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
70
- """
71
- try:
72
- # Create timezone object
73
- tz = pytz.timezone(timezone)
74
- # Get current time in that timezone
75
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
76
- return f"The current local time in {timezone} is: {local_time}"
77
- except Exception as e:
78
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
79
- The Tools are what we are encouraging you to build in this section! We give you two examples:
80
-
81
- A non-working dummy Tool that you can modify to make something useful.
82
- An actually working Tool that gets the current time somewhere in the world.
83
- To define your tool it is important to:
84
-
85
- Provide input and output types for your function, like in get_current_time_in_timezone(timezone: str) -> str:
86
- A well formatted docstring. smolagents is expecting all the arguments to have a textual description in the docstring.
87
- The Agent
88
- It uses Qwen/Qwen2.5-Coder-32B-Instruct as the LLM engine. This is a very capable model that we’ll access via the serverless API.
89
-
90
- Copied
91
- final_answer = FinalAnswerTool()
92
- model = HfApiModel(
93
- max_tokens=2096,
94
- temperature=0.5,
95
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
96
- custom_role_conversions=None,
97
- )
98
-
99
- with open("prompts.yaml", 'r') as stream:
100
- prompt_templates = yaml.safe_load(stream)
101
-
102
- # We're creating our CodeAgent
103
- agent = CodeAgent(
104
- model=model,
105
- tools=[final_answer], # add your tools here (don't remove final_answer)
106
- max_steps=6,
107
- verbosity_level=1,
108
- grammar=None,
109
- planning_interval=None,
110
- name=None,
111
- description=None,
112
- prompt_templates=prompt_templates
113
- )
114
-
115
- GradioUI(agent).launch()
116
- This Agent still uses the InferenceClient we saw in an earlier section behind the HfApiModel class!
117
-
118
- We will give more in-depth examples when we present the framework in Unit 2. For now, you need to focus on adding new tools to the list of tools using the tools parameter of your Agent.
119
-
120
- For example, you could use the DuckDuckGoSearchTool that was imported in the first line of the code, or you can examine the image_generation_tool that is loaded from the Hub later in the code.
121
-
122
- Adding tools will give your agent new capabilities, try to be creative here!
123
-
124
- The complete “app.py”:
125
-
126
- Copied
127
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
128
- import datetime
129
- import requests
130
- import pytz
131
- import yaml
132
- from tools.final_answer import FinalAnswerTool
133
-
134
- from Gradio_UI import GradioUI
135
-
136
- # Below is an example of a tool that does nothing. Amaze us with your creativity!
137
-
138
- def my_custom_tool(arg1:str, arg2:int)-> str: # it's important to specify the return type
139
- # Keep this format for the tool description / args description but feel free to modify the tool
140
- """A tool that does nothing yet
141
- Args:
142
- arg1: the first argument
143
- arg2: the second argument
144
- """
145
- return "What magic will you build ?"
146
-
147
-
148
- def get_current_time_in_timezone(timezone: str) -> str:
149
- """A tool that fetches the current local time in a specified timezone.
150
- Args:
151
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
152
- """
153
- try:
154
- # Create timezone object
155
- tz = pytz.timezone(timezone)
156
- # Get current time in that timezone
157
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
158
- return f"The current local time in {timezone} is: {local_time}"
159
- except Exception as e:
160
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
161
-
162
-
163
- final_answer = FinalAnswerTool()
164
- model = HfApiModel(
165
- max_tokens=2096,
166
- temperature=0.5,
167
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
168
- custom_role_conversions=None,
169
- )
170
-
171
-
172
- # Import tool from Hub
173
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
174
-
175
- with open("prompts.yaml", 'r') as stream:
176
- prompt_templates = yaml.safe_load(stream)
177
-
178
- agent = CodeAgent(
179
- model=model,
180
- tools=[final_answer], # add your tools here (don't remove final_answer)
181
- max_steps=6,
182
- verbosity_level=1,
183
- grammar=None,
184
- planning_interval=None,
185
- name=None,
186
- description=None,
187
- prompt_templates=prompt_templates
188
- )
189
-
190
-
191
- GradioUI(agent).launch()
192
- Your Goal is to get familiar with the Space and the Agent.
193
-
194
- Currently, the agent in the template does not use any tools, so try to provide it with some of the pre-made ones or even make some new tools yourself!
195
-
196
- We are eagerly waiting for your amazing agents output in the discord channel #agents-course-showcase!
197
-
198
- Congratulations, you’ve built your first Agent! Don’t hesitate to share it with your friends and colleagues.
199
-
200
- Since this is your first try, it’s perfectly normal if it’s a little buggy or slow. In future units, we’ll learn how to build even better Agents.
201
-
202
- The best way to learn is to try, so don’t hesitate to update it, add more tools, try with another model, etc.