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: docs/source/en/agents_advanced.md
+1-243
Original file line number
Diff line number
Diff line change
@@ -15,247 +15,5 @@ rendered properly in your Markdown viewer.
15
15
-->
16
16
# Agents, supercharged - Multi-agents, External tools, and more
17
17
18
-
[[open-in-colab]]
19
-
20
-
### What is an agent?
21
-
22
-
> [!TIP]
23
-
> If you're new to `transformers.agents`, make sure to first read the main [agents documentation](./agents).
24
-
25
-
In this page we're going to highlight several advanced uses of `transformers.agents`.
26
-
27
-
## Multi-agents
28
-
29
-
Multi-agent has been introduced in Microsoft's framework [Autogen](https://huggingface.co/papers/2308.08155).
30
-
It simply means having several agents working together to solve your task instead of only one.
31
-
It empirically yields better performance on most benchmarks. The reason for this better performance is conceptually simple: for many tasks, rather than using a do-it-all system, you would prefer to specialize units on sub-tasks. Here, having agents with separate tool sets and memories allows to achieve efficient specialization.
32
-
33
-
You can easily build hierarchical multi-agent systems with `transformers.agents`.
34
-
35
-
To do so, encapsulate the agent in a [`ManagedAgent`] object. This object needs arguments `agent`, `name`, and a `description`, which will then be embedded in the manager agent's system prompt to let it know how to call this managed agent, as we also do for tools.
36
-
37
-
Here's an example of making an agent that managed a specific web search agent using our [`DuckDuckGoSearchTool`]:
38
-
39
-
```py
40
-
from transformers.agents import ReactCodeAgent, HfApiEngine, DuckDuckGoSearchTool, ManagedAgent
manager_agent.run("Who is the CEO of Hugging Face?")
57
-
```
58
-
59
-
> [!TIP]
60
-
> For an in-depth example of an efficient multi-agent implementation, see [how we pushed our multi-agent system to the top of the GAIA leaderboard](https://huggingface.co/blog/beating-gaia).
61
-
62
-
63
-
## Advanced tool usage
64
-
65
-
### Directly define a tool by subclassing Tool, and share it to the Hub
66
-
67
-
Let's take again the tool example from main documentation, for which we had implemented a `tool` decorator.
68
-
69
-
If you need to add variation, like custom attributes for your tool, you can build your tool following the fine-grained method: building a class that inherits from the [`Tool`] superclass.
70
-
71
-
The custom tool needs:
72
-
- An attribute `name`, which corresponds to the name of the tool itself. The name usually describes what the tool does. Since the code returns the model with the most downloads for a task, let's name it `model_download_counter`.
73
-
- An attribute `description` is used to populate the agent's system prompt.
74
-
- An `inputs` attribute, which is a dictionary with keys `"type"` and `"description"`. It contains information that helps the Python interpreter make educated choices about the input.
75
-
- An `output_type` attribute, which specifies the output type.
76
-
- A `forward` method which contains the inference code to be executed.
77
-
78
-
The types for both `inputs` and `output_type` should be amongst [Pydantic formats](https://docs.pydantic.dev/latest/concepts/json_schema/#generating-json-schema).
79
-
80
-
```python
81
-
from transformers import Tool
82
-
from huggingface_hub import list_models
83
-
84
-
classHFModelDownloadsTool(Tool):
85
-
name ="model_download_counter"
86
-
description ="""
87
-
This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub.
88
-
It returns the name of the checkpoint."""
89
-
90
-
inputs = {
91
-
"task": {
92
-
"type": "string",
93
-
"description": "the task category (such as text-classification, depth-estimation, etc)",
94
-
}
95
-
}
96
-
output_type ="string"
97
-
98
-
defforward(self, task: str):
99
-
model =next(iter(list_models(filter=task, sort="downloads", direction=-1)))
100
-
return model.id
101
-
```
102
-
103
-
Now that the custom `HfModelDownloadsTool` class is ready, you can save it to a file named `model_downloads.py` and import it for use.
104
-
105
-
106
-
```python
107
-
from model_downloads import HFModelDownloadsTool
108
-
109
-
tool = HFModelDownloadsTool()
110
-
```
111
-
112
-
You can also share your custom tool to the Hub by calling [`~Tool.push_to_hub`] on the tool. Make sure you've created a repository for it on the Hub and are using a token with read access.
You can directly import a Space from the Hub as a tool using the [`Tool.from_space`] method!
129
-
130
-
You only need to provide the id of the Space on the Hub, its name, and a description that will help you agent understand what the tool does. Under the hood, this will use [`gradio-client`](https://pypi.org/project/gradio-client/) library to call the Space.
131
-
132
-
For instance, let's import the [FLUX.1-dev](https://huggingface.co/black-forest-labs/FLUX.1-dev) Space from the Hub and use it to generate an image.
Then you can use this tool just like any other tool. For example, let's improve the prompt `a rabbit wearing a space suit` and generate an image of it.
"Improve this prompt, then generate an image of it.", prompt='A rabbit wearing a space suit'
157
-
)
158
-
```
159
-
160
-
```text
161
-
=== Agent thoughts:
162
-
improved_prompt could be "A bright blue space suit wearing rabbit, on the surface of the moon, under a bright orange sunset, with the Earth visible in the background"
163
-
164
-
Now that I have improved the prompt, I can use the image generator tool to generate an image based on this prompt.
165
-
=== Agent is executing the code below:
166
-
image = image_generator(prompt="A bright blue space suit wearing rabbit, on the surface of the moon, under a bright orange sunset, with the Earth visible in the background")
[gradio-tools](https://github.com/freddyaboulton/gradio-tools) is a powerful library that allows using Hugging
177
-
Face Spaces as tools. It supports many existing Spaces as well as custom Spaces.
178
-
179
-
Transformers supports `gradio_tools` with the [`Tool.from_gradio`] method. For example, let's use the [`StableDiffusionPromptGeneratorTool`](https://github.com/freddyaboulton/gradio-tools/blob/main/gradio_tools/tools/prompt_generator.py) from `gradio-tools` toolkit for improving prompts to generate better images.
180
-
181
-
Import and instantiate the tool, then pass it to the `Tool.from_gradio` method:
182
-
183
-
```python
184
-
from gradio_tools import StableDiffusionPromptGeneratorTool
185
-
from transformers import Tool, load_tool, CodeAgent
> gradio-tools require *textual* inputs and outputs even when working with different modalities like image and audio objects. Image and audio inputs and outputs are currently incompatible.
193
-
194
-
### Use LangChain tools
195
-
196
-
We love Langchain and think it has a very compelling suite of tools.
197
-
To import a tool from LangChain, use the `from_langchain()` method.
198
-
199
-
Here is how you can use it to recreate the intro's search result using a LangChain web search tool.
200
-
This tool will need `pip install google-search-results` to work properly.
agent.run("How many more blocks (also denoted as layers) are in BERT base encoder compared to the encoder from the architecture proposed in Attention is All You Need?")
210
-
```
211
-
212
-
## Display your agent run in a cool Gradio interface
213
-
214
-
You can leverage `gradio.Chatbot` to display your agent's thoughts using `stream_to_gradio`, here is an example:
> This subpackage will soon be deprecated, since it has ben spun off into [smolagents](https://huggingface.co/docs/smolagents/index). Smolagents has extended functionality, and a similar API.
0 commit comments