You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: units/en/unit1/dummy-agent-library.mdx
+50-73Lines changed: 50 additions & 73 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,7 +12,7 @@ You probably wouldn't use these in production, but they will serve as a good **s
12
12
13
13
After this section, you'll be ready to **create a simple Agent** using `smolagents`
14
14
15
-
And in the following Units we will also use other AI Agent libraries like `LangGraph`, `LangChain`, and `LlamaIndex`.
15
+
And in the following Units we will also use other AI Agent libraries like `LangGraph`, and `LlamaIndex`.
16
16
17
17
To keep things simple we will use a simple Python function as a Tool and Agent.
18
18
@@ -29,45 +29,13 @@ import os
29
29
from huggingface_hub import InferenceClient
30
30
31
31
## You need a token from https://hf.co/settings/tokens, ensure that you select 'read' as the token type. If you run this on Google Colab, you can set it up in the "settings" tab under "secrets". Make sure to call it "HF_TOKEN"
Paris. The capital of France is Paris. Paris, the City of Light, is known for its stunning architecture, art museums, fashion, and romantic atmosphere. It's a must-visit destination for anyone interested in history, culture, and beauty. The Eiffel Tower, the Louvre Museum, and Notre-Dame Cathedral are just a few of the many iconic landmarks that make Paris a unique and unforgettable experience. Whether you're interested in exploring the city's charming neighborhoods, enjoying the local cuisine.
50
-
```
51
-
As seen in the LLM section, if we just do decoding, **the model will only stop when it predicts an EOS token**, and this does not happen here because this is a conversational (chat) model and **we didn't apply the chat template it expects**.
52
-
53
-
If we now add the special tokens related to the <ahref="https://huggingface.co/meta-llama/Llama-3.3-70B-Instruct">Llama-3.3-70B-Instruct model</a> that we're using, the behavior changes and it now produces the expected EOS.
37
+
We use the `chat` method since is a convenient and reliable way to apply chat templates:
The chat method is the RECOMMENDED method to use in order to ensure a smooth transition between models, but since this notebook is only educational, we will keep using the "text_generation" method to understand the details.
55
+
56
+
The chat method is the RECOMMENDED method to use in order to ensure a smooth transition between models.
86
57
87
58
## Dummy Agent
88
59
@@ -133,29 +104,19 @@ Final Answer: the final answer to the original input question
133
104
Now begin! Reminder to ALWAYS use the exact characters `Final Answer:` when you provide a definitive answer. """
134
105
```
135
106
136
-
Since we are running the "text_generation" method, we need to apply the prompt manually:
@@ -222,19 +185,22 @@ Final Answer: The current weather in London is partly cloudy with a temperature
222
185
````
223
186
224
187
Do you see the issue?
188
+
225
189
> At this point, the model is hallucinating, because it's producing a fabricated "Observation" -- a response that it generates on its own rather than being the result of an actual function or tool call.
226
190
> To prevent this, we stop generating right before "Observation:".
227
191
> This allows us to manually run the function (e.g., `get_weather`) and then insert the real output as the Observation.
228
192
229
193
```python
230
-
output = client.text_generation(
231
-
prompt,
232
-
max_new_tokens=200,
194
+
# The answer was hallucinated by the model. We need to stop to actually execute the function!
195
+
output = client.chat.completions.create(
196
+
messages=messages,
197
+
max_tokens=150,
233
198
stop=["Observation:"] # Let's stop before any actual function is called
234
199
)
235
200
236
-
print(output)
201
+
print(output.choices[0].message.content)
237
202
```
203
+
238
204
output:
239
205
240
206
````
@@ -249,8 +215,9 @@ Action:
249
215
Observation:
250
216
````
251
217
252
-
Much Better!
253
-
Let's now create a dummy get weather function. In a real situation, you would likely call an API.
218
+
Much Better!
219
+
220
+
Let's now create a **dummy get weather function**. In a real situation you could call an API.
254
221
255
222
```python
256
223
# Dummy function
@@ -259,23 +226,33 @@ def get_weather(location):
259
226
260
227
get_weather('London')
261
228
```
229
+
262
230
output:
231
+
263
232
```
264
233
'the weather in London is sunny with low temperatures. \n'
265
234
```
266
235
267
-
Let's concatenate the base prompt, the completion until function execution and the result of the function as an Observation and resume generation.
236
+
Let's concatenate the system prompt, the base prompt, the completion until function execution and the result of the function as an Observation and resume generation.
0 commit comments