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: README.md
+17-4Lines changed: 17 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -29,14 +29,15 @@ limitations under the License.
29
29
30
30
`smolagents` is a library that enables you to run powerful agents in a few lines of code. It offers:
31
31
32
-
✨ **Simplicity**: the logic for agents fits in ~thousand lines of code. We kept abstractions to their minimal shape above raw code!
32
+
✨ **Simplicity**: the logic for agents fits in ~thousand lines of code (see [agents.py](https://github.com/huggingface/smolagents/blob/main/src/smolagents/agents.py)). We kept abstractions to their minimal shape above raw code!
33
33
34
-
🌐 **Support for any LLM**: it supports models hosted on the Hub loaded in their `transformers` version or through our inference API, but also models from OpenAI, Anthropic, and many more through our LiteLLM integration.
35
-
36
-
🧑💻 **First-class support for Code Agents**, i.e. agents that write their actions in code (as opposed to "agents being used to write code"), [read more here](https://huggingface.co/docs/smolagents/tutorials/secure_code_execution).
34
+
🧑💻 **First-class support for Code Agents**, i.e. agents that write their actions in code (as opposed to "agents being used to write code"). To make it secure, we support executing in sandboxed environments via [E2B](https://e2b.dev/).
35
+
- On top of this [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent) class, we still support the standard [`ToolCallingAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.ToolCallingAgent) that writes actions as JSON/text blobs.
37
36
38
37
🤗 **Hub integrations**: you can share and load tools to/from the Hub, and more is to come!
39
38
39
+
🌐 **Support for any LLM**: it supports models hosted on the Hub loaded in their `transformers` version or through our inference API, but also supports models from OpenAI, Anthropic and many others via our [LiteLLM](https://www.litellm.ai/) integration.
40
+
40
41
## Quick demo
41
42
42
43
First install the package.
@@ -70,6 +71,18 @@ Still, we implement several types of agents: `CodeAgent` writes its actions as P
70
71
71
72
By the way, why use a framework at all? Well, because a big part of this stuff is non-trivial. For instance, the code agent has to keep a consistent format for code throughout its system prompt, its parser, the execution. So our framework handles this complexity for you. But of course we still encourage you to hack into the source code and use only the bits that you need, to the exclusion of everything else!
72
73
74
+
## How strong are open models for agentic workflows?
75
+
76
+
We've created [`CodeAgent`](https://huggingface.co/docs/smolagents/reference/agents#smolagents.CodeAgent) instances with some leading models, and compared them on [this benchmark](https://huggingface.co/datasets/m-ric/agents_medium_benchmark_2) that gathers questions from a few different benchmarks to propose a varied blend of challenges.
77
+
78
+
[Find the benchmark here](https://github.com/huggingface/smolagents/blob/main/examples/benchmark.ipynb) for more detail on the agentic setup used, and see a comparison of code agents versus tool calling agents (spoilers: code works better).
79
+
80
+
<palign="center">
81
+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/blog/smolagents/benchmark_code_agents.png" alt="benchmark of different models on agentic workflows" width=70%>
82
+
</p>
83
+
84
+
This comparison shows that open source models can now take on the best closed models!
85
+
73
86
## Citing smolagents
74
87
75
88
If you use `smolagents` in your publication, please cite it by using the following BibTeX entry.
Copy file name to clipboardExpand all lines: docs/source/en/conceptual_guides/intro_agents.md
+26-29Lines changed: 26 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -15,7 +15,7 @@ rendered properly in your Markdown viewer.
15
15
-->
16
16
# Introduction to Agents
17
17
18
-
###🤔 What are agents?
18
+
## 🤔 What are agents?
19
19
20
20
Any efficient system using AI will need to provide LLMs some kind of access to the real world: for instance the possibility to call a search tool to get external information, or to act on certain programs in order to solve a task. In other words, LLMs should have ***agency***. Agentic programs are the gateway to the outside world for LLMs.
21
21
@@ -31,7 +31,7 @@ See in the table below how agency can vary across systems:
31
31
| Agency Level | Description | How that's called | Example Pattern |
| ★★★ | LLM output controls iteration and program continuation | Multi-step Agent |`while llm_should_continue(): execute_next_step()`|
37
37
| ★★★ | One agentic workflow can start another agentic workflow | Multi-Agent |`if llm_trigger(): execute_agent()`|
@@ -53,35 +53,32 @@ This agentic system runs in a loop, executing a new action at each step (the act
53
53
</div>
54
54
55
55
56
-
### When to use an agentic system ?
57
-
58
-
Agents are useful when you need an LLM to determine the workflow of an app.
59
-
60
-
The question to ask is: "Do I really need flexibility in the workflow to efficiently solve the task at hand?"
61
-
62
-
If a fixed workflow can work, you might as well build it all in good old no-AI code for 100% robustness. For the sake of simplicity and robstness, it's advised to regularize towards not using any agentic behaviour. On the opposite, agents are useful when the fixed workflow is not sufficient.
56
+
## ✅ When to use agents / ⛔ when to avoid them
63
57
58
+
Agents are useful when you need an LLM to determine the workflow of an app. But they’re often overkill. The question is: do I really need flexibility in the workflow to efficiently solve the task at hand?
59
+
If the pre-determined workflow falls short too often, that means you need more flexibility.
64
60
Let's take an example: say you're making an app that handles customer requests on a surfing trip website.
65
61
66
-
You could know in advance that the requests will have to be classified in either of 2 buckets according to deterministic criteria, and you have a predefined workflow for each of these 2 cases.
67
-
For instance, this is if you let the user click a button to determine their query, and it goes into either of these buckets:
62
+
You could know in advance that the requests will can belong to either of 2 buckets (based on user choice), and you have a predefined workflow for each of these 2 cases.
68
63
69
-
1. Want some knowledge on the trips? ⇒ Then you give them access to a search bar to search your knowledge base
70
-
2. Wants to talk to sales? ⇒ Then you let them type in a contact form.
64
+
1. Want some knowledge on the trips? ⇒ give them access to a search bar to search your knowledge base
65
+
2. Wants to talk to sales? ⇒ let them type in a contact form.
71
66
72
-
If that deterministic workflow fits all queries, by all means just code everything! This will give you a 100% reliable system with no risk of error introduced by letting unpredictable LLMs meddle in your workflow.
67
+
If that deterministic workflow fits all queries, by all means just code everything! This will give you a 100% reliable system with no risk of error introduced by letting unpredictable LLMs meddle in your workflow. For the sake of simplicity and robustness, it's advised to regularize towards not using any agentic behaviour.
73
68
74
-
But what if the workflow can't be determined that well in advance? Say, 20% or 40% of users requests do not fit properly into your rigid categories, and are thus not handled properly by your program?
69
+
But what if the workflow can't be determined that well in advance?
75
70
76
-
For instance, a user wants to ask : "I can come on Monday, but I forgot my passport so risk being delayed to Wednesday, is it possible to take me and my stuff to surf on Tuesday morning, with a cancellation insurance?" This question hinges on many factors, and probably none of the predetermined criteria above won't be sufficient for this request.
71
+
For instance, a user wants to ask : `"I can come on Monday, but I forgot my passport so risk being delayed to Wednesday, is it possible to take me and my stuff to surf on Tuesday morning, with a cancellation insurance?"` This question hinges on many factors, and probably none of the predetermined criteria above will suffice for this request.
77
72
78
-
If the pre-determined workflow falls short too often, that means you need more flexibility, which is just what an agentic setup provides. In the above example, you could just make a multi-step agent that has access to a weather API tool, a google maps API to compute travel distance, an employee availability dashboard and a RAG system on your knowledge base.
73
+
If the pre-determined workflow falls short too often, that means you need more flexibility.
79
74
80
-
Until recently, computer programs were restricted to pre-determined workflows (with possible piles of if/else switches), thus focused on extremely narrow tasks, like "compute the sum of these numbers" or "find the shortest path in this graph".
75
+
That is where an agentic setup helps.
81
76
82
-
But actually, most real-life tasks are like our trip example above, they do not fit in pre-determined workflows. Agentic systems open up the vast world of real-world tasks to programs!
77
+
In the above example, you could just make a multi-step agent that has access to a weather API for weather forecasts, Google Maps API to compute travel distance, an employee availability dashboard and a RAG system on your knowledge base.
83
78
84
-
### Why `smolagents`?
79
+
Until recently, computer programs were restricted to pre-determined workflows, trying to handle complexity by piling up if/else switches. They focused on extremely narrow tasks, like "compute the sum of these numbers" or "find the shortest path in this graph". But actually, most real-life tasks, like our trip example above, do not fit in pre-determined workflows. Agentic systems open up the vast world of real-world tasks to programs!
80
+
81
+
## Why `smolagents`?
85
82
86
83
For some low-level agentic use cases, like chains or routers, you can write all the code yourself. You'll be much better that way, since it will let you control and understand your system better.
87
84
@@ -101,21 +98,21 @@ But wait, since we give room to LLMs in decisions, surely they will make mistake
101
98
102
99
All these elements need tight coupling to make a well-functioning system. That's why we decided we needed to make basic building blocks to make all this stuff work together.
103
100
104
-
### Code agents
101
+
## Code agents
102
+
103
+
In a multi-step agent, at each step, the LLM can write an action, in the form of some calls to external tools. A common format (used by Anthropic, OpenAI, and many others) for writing these actions is generally different shades of "writing actions as a JSON of tools names and arguments to use, which you then parse to know which tool to execute and with which arguments".
104
+
105
+
[Multiple](https://huggingface.co/papers/2402.01030)[research](https://huggingface.co/papers/2411.01747)[papers](https://huggingface.co/papers/2401.00812) have shown that having the tool calling LLMs in code is much better.
105
106
106
-
[Multiple](https://huggingface.co/papers/2402.01030)[research](https://huggingface.co/papers/2411.01747)[papers](https://huggingface.co/papers/2401.00812) have shown that having the LLM write its actions (the tool calls) in code is much better than the current standard format for tool calling, which is across the industry different shades of "writing actions as a JSON of tools names and arguments to use, which you then parse to know which tool to execute and with which arguments".
107
+
The reason for this simply that *we crafted our code languages specifically to be the best possible way to express actions performed by a computer*. If JSON snippets were a better expression, JSON would be the top programming language and programming would be hell on earth.
107
108
108
-
Why is code better? Well, because we crafted our code languages specifically to be great at expressing actions performed by a computer. If JSON snippets were a better way, JSON would be the top programming language and programming would be hell on earth.
109
+
The figure below, taken from [Executable Code Actions Elicit Better LLM Agents](https://huggingface.co/papers/2402.01030), illustrate some advantages of writing actions in code:
Writing actions in code rather than JSON-like snippets provides better:
111
114
112
115
-**Composability:** could you nest JSON actions within each other, or define a set of JSON actions to re-use later, the same way you could just define a python function?
113
116
-**Object management:** how do you store the output of an action like `generate_image` in JSON?
114
117
-**Generality:** code is built to express simply anything you can have a computer do.
115
118
-**Representation in LLM training data:** plenty of quality code actions is already included in LLMs’ training data which means they’re already trained for this!
116
-
117
-
This is illustrated on the figure below, taken from [Executable Code Actions Elicit Better LLM Agents](https://huggingface.co/papers/2402.01030).
Copy file name to clipboardExpand all lines: docs/source/en/tutorials/building_good_agents.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ How to build into this latter category?
22
22
In this guide, we're going to see best practices for building agents.
23
23
24
24
> [!TIP]
25
-
> If you're new to building agents, make sure to first read the [intro to agents](./intro_agents) and the [guided tour of smolagents](../guided_tour).
25
+
> If you're new to building agents, make sure to first read the [intro to agents](../conceptual_guides/intro_agents) and the [guided tour of smolagents](../guided_tour).
26
26
27
27
### The best agentic systems are the simplest: simplify the workflow as much as you can
Copy file name to clipboardExpand all lines: docs/source/en/tutorials/secure_code_execution.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,7 +18,7 @@ rendered properly in your Markdown viewer.
18
18
[[open-in-colab]]
19
19
20
20
> [!TIP]
21
-
> If you're new to building agents, make sure to first read the [intro to agents](./intro_agents) and the [guided tour of smolagents](../guided_tour).
21
+
> If you're new to building agents, make sure to first read the [intro to agents](../conceptual_guides/intro_agents) and the [guided tour of smolagents](../guided_tour).
Copy file name to clipboardExpand all lines: docs/source/en/tutorials/tools.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -20,7 +20,7 @@ rendered properly in your Markdown viewer.
20
20
Here, we're going to see advanced tool usage.
21
21
22
22
> [!TIP]
23
-
> If you're new to building agents, make sure to first read the [intro to agents](./intro_agents) and the [guided tour of smolagents](../guided_tour).
23
+
> If you're new to building agents, make sure to first read the [intro to agents](../conceptual_guides/intro_agents) and the [guided tour of smolagents](../guided_tour).
0 commit comments