[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transformers Agents #23214

Merged
merged 327 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from 250 commits
Commits
Show all changes
327 commits
Select commit Hold shift + click to select a range
1a2f127
Work
sgugger May 4, 2023
ac508c8
Clean up eval
sgugger May 4, 2023
1ee146b
Changes
sgugger May 4, 2023
08b97bd
Tools
May 4, 2023
e072f4f
Tools
May 4, 2023
ef7d989
tool
May 4, 2023
30a39d8
Fix everything
sgugger May 4, 2023
2ef47fb
Use last result/assign for evaluation
sgugger May 4, 2023
5a212fc
Prompt
May 4, 2023
d558620
Remove hardcoded selection
May 4, 2023
a56a008
Evaluation for chat agents
sgugger May 5, 2023
0ddbb67
correct some spelling
patrickvonplaten May 5, 2023
d3d0958
Small fixes
patrickvonplaten May 5, 2023
c548087
Change summarization model (#23172)
philschmid May 5, 2023
9ba0342
Fix link displayed
sgugger May 5, 2023
b89e1c1
Update description of the tool
sgugger May 5, 2023
d621762
Fixes in chat prompt
sgugger May 5, 2023
d17f559
Custom tools, custom prompt
sgugger May 5, 2023
8b2a900
Tool clean up
sgugger May 5, 2023
78d178f
save_pretrained and push_to_hub for tool
sgugger May 5, 2023
df0b4e0
Fix init
sgugger May 5, 2023
ff01bc3
Tests
May 5, 2023
36f2e55
Fix tests
May 5, 2023
4670626
Tool save/from_hub/push_to_hub and tool->load_tool
sgugger May 6, 2023
b4d0a2c
Clean push_to_hub and add app file
sgugger May 6, 2023
4705ab9
Custom inference API for endpoints too
sgugger May 6, 2023
0032ad2
Clean up
sgugger May 6, 2023
9fd81ed
old remote tool and new remote tool
sgugger May 6, 2023
f60032d
Make a requirements
sgugger May 6, 2023
44e71f6
return_code adds tool creation
sgugger May 7, 2023
61fde3b
Avoid redundancy between global variables
sgugger May 7, 2023
d5832e5
Remote tools can be loaded
sgugger May 8, 2023
c051db5
Tests
May 7, 2023
583b4eb
Text summarization tests
May 7, 2023
79f7d7d
Quality
sgugger May 8, 2023
41bc513
Properly mark tests
sgugger May 8, 2023
9617a61
Test the python interpreter
sgugger May 8, 2023
2a18e0a
And the CI shall be green.
sgugger May 8, 2023
f13a5f9
fix loading of additional tools
patrickvonplaten May 8, 2023
6690cfc
Work on RemoteTool and fix tests
sgugger May 8, 2023
81b1f71
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 8, 2023
e568348
General clean up
sgugger May 8, 2023
e420014
Guard imports
sgugger May 8, 2023
3d9ac19
Fix tools
sgugger May 8, 2023
188a8bf
docs: Fix broken link in 'How to add a model...' (#23216)
May 8, 2023
b90dc27
Get default endpoint from the Hub
sgugger May 8, 2023
2906331
Add guide
LysandreJik May 8, 2023
3240ace
Simplify tool config
sgugger May 8, 2023
f170c8f
Merge commit
sgugger May 8, 2023
2e7cca6
Docs
LysandreJik May 8, 2023
eaa542c
Some fixes
sgugger May 8, 2023
15ba913
Docs
LysandreJik May 8, 2023
70592b7
Docs
LysandreJik May 8, 2023
6487452
Docs
LysandreJik May 8, 2023
764e73f
Fix code returned by agent
sgugger May 8, 2023
27a9d2e
Try this
sgugger May 8, 2023
b1ca0eb
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 8, 2023
3f3135c
Match args with signature in remote tool
sgugger May 8, 2023
3e9c82d
Should fix python interpreter for Python 3.8
sgugger May 8, 2023
08503de
Fix push_to_hub for tools
sgugger May 8, 2023
dcdfa3c
Other fixes to push_to_hub
sgugger May 8, 2023
4cb5b82
Add API doc page
sgugger May 8, 2023
b8e8dd0
Docs
LysandreJik May 8, 2023
0d7373e
Docs
LysandreJik May 8, 2023
7422124
Custom tools
LysandreJik May 8, 2023
006da46
Pin tensorflow-probability (#23220)
sgugger May 8, 2023
a63eb35
PoC for some chaining API
sgugger Apr 18, 2023
0334ece
Text to speech
LysandreJik Apr 18, 2023
e39c5dc
J'ai pris des libertés
LysandreJik Apr 19, 2023
56afeaf
Rename
sgugger Apr 19, 2023
a036425
Basic python interpreter
sgugger Apr 20, 2023
2c0f217
Add agents
sgugger Apr 20, 2023
fbd520b
Quality
sgugger Apr 20, 2023
42a1ed4
Add translation tool
sgugger Apr 20, 2023
755f71e
temp
Apr 21, 2023
a3eb372
GenQA + LID + S2T
Apr 21, 2023
3acb2f3
Quality + word missing in translation
sgugger Apr 21, 2023
9043822
Add open assistance, support f-strings in evaluate
sgugger Apr 21, 2023
4221840
captioning + s2t fixes
Apr 24, 2023
c3c1b1f
Style
Apr 24, 2023
de1aa68
Refactor descriptions and remove chain
sgugger Apr 24, 2023
39f1acf
Support errors and rename OpenAssistantAgent
sgugger Apr 24, 2023
d237e6a
Add setup
sgugger Apr 24, 2023
dc8f442
Deal with typos + example of inference API
sgugger Apr 24, 2023
2c46e92
Some rename + README
sgugger Apr 24, 2023
56d67b1
Fixes
sgugger Apr 24, 2023
7d1bfbd
Update prompt
sgugger Apr 25, 2023
4d85d98
Unwanted change
sgugger Apr 25, 2023
a7527ce
Make sure everyone has a default
sgugger Apr 25, 2023
b7f8987
One prompt to rule them all.
sgugger Apr 25, 2023
e5e5967
SD
Apr 25, 2023
bb88ab9
Description
sgugger Apr 25, 2023
51ad30e
Clean up remote tools
sgugger Apr 25, 2023
b1c0286
More remote tools
sgugger Apr 25, 2023
4ea33de
Add option to return code and update doc
sgugger Apr 25, 2023
693a024
Image segmentation
Apr 25, 2023
2305583
ControlNet
Apr 26, 2023
70febc4
Gradio demo
Apr 26, 2023
05a5a43
Diffusers protection
Apr 26, 2023
6631fde
Lib protection
Apr 26, 2023
e6e4c04
ControlNet description
Apr 26, 2023
a7d681f
Cleanup
Apr 26, 2023
d8b7811
Style
sgugger Apr 26, 2023
c40c018
Remove accelerate and try to be reproducible
sgugger Apr 26, 2023
b7b2ff9
No randomness
sgugger Apr 26, 2023
612ee63
Male Basic optional in token
sgugger Apr 26, 2023
3bbe6ee
Clean description
sgugger Apr 26, 2023
86f5862
Better prompts
sgugger Apr 26, 2023
ecabbcf
Fix args eval in interpreter
sgugger Apr 26, 2023
e8a52e8
Add tool wrapper
sgugger Apr 26, 2023
99d09f8
Tool on the Hub
sgugger Apr 27, 2023
4ca6928
Style post-rebase
sgugger Apr 27, 2023
5d53f6b
Big refactor of descriptions, batch generation and evaluation for agents
sgugger Apr 28, 2023
f389804
Make problems easier - interface to debug
sgugger May 1, 2023
71feaf5
More problems, add python primitives
sgugger May 1, 2023
3fb21f3
Back to one prompt
sgugger May 1, 2023
133be6d
Remove dict for translation
sgugger May 1, 2023
268c859
Be consistent
sgugger May 1, 2023
83f3078
Add prompts
LysandreJik May 1, 2023
57b68f4
New version of the agent
sgugger May 1, 2023
d1a2d1e
Evaluate new agents
sgugger May 2, 2023
0328388
New endpoints agents
sgugger May 2, 2023
7ebd702
Make all tools a dict variable
sgugger May 2, 2023
3648399
Typo
sgugger May 2, 2023
e5016ac
Add problems
LysandreJik May 2, 2023
7abfbaf
Add to big prompt
sgugger May 2, 2023
01e8f9f
Harmonize
sgugger May 2, 2023
1bbe15f
Add tools
May 2, 2023
54b7e90
New evaluation
sgugger May 2, 2023
0e28346
Add more tools
May 2, 2023
df9a857
Build prompt with tools descriptions
sgugger May 2, 2023
7249413
Tools on the Hub
sgugger May 3, 2023
5ec66a6
Let's chat!
sgugger May 3, 2023
d38f741
Cleanup
May 3, 2023
caf5511
Temporary bs4 safeguard
May 3, 2023
f3ccd38
Cache agents and clean up
sgugger May 3, 2023
0c85974
Blank init
May 3, 2023
19f1560
Fix evaluation for agents
sgugger May 3, 2023
dca161c
New format for tools on the Hub
sgugger May 3, 2023
47628e5
Add method to reset state
sgugger May 3, 2023
ed5755a
Remove nestedness in tool config
sgugger May 3, 2023
8041093
Really do it
sgugger May 3, 2023
a72a812
Use remote tools descriptions
sgugger May 4, 2023
18ce516
Work
sgugger May 4, 2023
158f493
Clean up eval
sgugger May 4, 2023
b540659
Changes
sgugger May 4, 2023
6c29dd6
Tools
May 4, 2023
a3efb85
Tools
May 4, 2023
841d762
tool
May 4, 2023
9a0009f
Fix everything
sgugger May 4, 2023
df0757d
Use last result/assign for evaluation
sgugger May 4, 2023
8e33307
Prompt
May 4, 2023
134cc9d
Remove hardcoded selection
May 4, 2023
56882ce
Evaluation for chat agents
sgugger May 5, 2023
5d757e3
correct some spelling
patrickvonplaten May 5, 2023
f1ea701
Small fixes
patrickvonplaten May 5, 2023
9ae7440
Change summarization model (#23172)
philschmid May 5, 2023
133fb89
Fix link displayed
sgugger May 5, 2023
d55b11c
Update description of the tool
sgugger May 5, 2023
5ccbf3e
Fixes in chat prompt
sgugger May 5, 2023
32d7851
Custom tools, custom prompt
sgugger May 5, 2023
8e216a2
Tool clean up
sgugger May 5, 2023
c9fd2d5
save_pretrained and push_to_hub for tool
sgugger May 5, 2023
0c10487
Fix init
sgugger May 5, 2023
82c349b
Tests
May 5, 2023
3bfeb2b
Fix tests
May 5, 2023
80baba6
Tool save/from_hub/push_to_hub and tool->load_tool
sgugger May 6, 2023
10ca327
Clean push_to_hub and add app file
sgugger May 6, 2023
b66f702
Custom inference API for endpoints too
sgugger May 6, 2023
2f4ecfc
Clean up
sgugger May 6, 2023
490abe6
old remote tool and new remote tool
sgugger May 6, 2023
3f2d10d
Make a requirements
sgugger May 6, 2023
5a78c65
return_code adds tool creation
sgugger May 7, 2023
bbbf639
Avoid redundancy between global variables
sgugger May 7, 2023
c62205d
Remote tools can be loaded
sgugger May 8, 2023
875a5c5
Tests
May 7, 2023
96ca804
Text summarization tests
May 7, 2023
a6eb262
Quality
sgugger May 8, 2023
33de65a
Properly mark tests
sgugger May 8, 2023
994413d
Test the python interpreter
sgugger May 8, 2023
fd27bde
And the CI shall be green.
sgugger May 8, 2023
2da957a
Work on RemoteTool and fix tests
sgugger May 8, 2023
c026b2b
fix loading of additional tools
patrickvonplaten May 8, 2023
23092fa
General clean up
sgugger May 8, 2023
ccd4e88
Guard imports
sgugger May 8, 2023
b9b78e0
Fix tools
sgugger May 8, 2023
3e8843a
Get default endpoint from the Hub
sgugger May 8, 2023
4720199
Simplify tool config
sgugger May 8, 2023
920505e
Add guide
LysandreJik May 8, 2023
24bf8f5
Docs
LysandreJik May 8, 2023
b1ee4bc
Some fixes
sgugger May 8, 2023
2b4ef97
Docs
LysandreJik May 8, 2023
1128bdd
Docs
LysandreJik May 8, 2023
a475d9a
Fix code returned by agent
sgugger May 8, 2023
d76bbc9
Try this
sgugger May 8, 2023
520fb88
Docs
LysandreJik May 8, 2023
48c2ccb
Match args with signature in remote tool
sgugger May 8, 2023
cc65150
Should fix python interpreter for Python 3.8
sgugger May 8, 2023
89d116b
Fix push_to_hub for tools
sgugger May 8, 2023
2d1abbc
Other fixes to push_to_hub
sgugger May 8, 2023
724f587
Add API doc page
sgugger May 8, 2023
8554fa2
Fix merge
sgugger May 8, 2023
ddeef3a
Fixes
sgugger May 8, 2023
a809624
Doc fixes
sgugger May 8, 2023
006f105
Docs
LysandreJik May 8, 2023
9c46fa9
Fix audio
LysandreJik May 8, 2023
2beaded
Custom tools
LysandreJik May 9, 2023
91637f1
Audio fix
LysandreJik May 9, 2023
750235c
Improve custom tools docstring
patrickvonplaten May 9, 2023
4ee321b
Docstrings
sgugger May 9, 2023
faa0fc8
Trigger CI
sgugger May 9, 2023
7ecdaf2
Mode docstrings
sgugger May 9, 2023
0316ab7
More docstrings
sgugger May 9, 2023
dc79b2d
Improve custom tools
patrickvonplaten May 9, 2023
6271473
Fix for remote tools
sgugger May 9, 2023
7c59778
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 9, 2023
27314b1
Style
sgugger May 9, 2023
e0f5685
Fix repo consistency
sgugger May 9, 2023
625d92f
Quality
sgugger May 9, 2023
c598276
Tip
LysandreJik May 9, 2023
7ac7f59
Cleanup on doc
sgugger May 9, 2023
69fbaca
Cleanup toc
sgugger May 9, 2023
3ce9b72
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 9, 2023
4677329
Add disclaimer for starcoder vs openai
LysandreJik May 9, 2023
b0eeaa3
Remove disclaimer
LysandreJik May 9, 2023
775896f
Small fixed in the prompts
sgugger May 9, 2023
81c9866
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 9, 2023
7d8c311
4.29
LysandreJik May 9, 2023
ecb7e16
Update src/transformers/tools/agents.py
sgugger May 9, 2023
a7d2d6f
Complete documentation
LysandreJik May 9, 2023
d34fff2
Small fixes
sgugger May 9, 2023
f48f7b3
Agent evaluation
sgugger May 9, 2023
6acc104
Note about gradio-tools & LC
LysandreJik May 9, 2023
f7eb64a
Clean up agents and prompt
sgugger May 9, 2023
51e6758
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 9, 2023
34da8dd
Apply suggestions from code review
LysandreJik May 9, 2023
059d68c
Apply suggestions from code review
sgugger May 9, 2023
e25d386
Note about gradio-tools & LC
LysandreJik May 9, 2023
700c39a
Add copyrights and address review comments
sgugger May 9, 2023
a7d9b56
Merge branch 'test_composition' of github.com:huggingface/transformer…
sgugger May 9, 2023
c53168d
Quality
sgugger May 9, 2023
66d09cf
Add all language codes
sgugger May 9, 2023
f1f833e
Add remote tool tests
sgugger May 9, 2023
dbe81d8
Move custom prompts to other docs
patrickvonplaten May 9, 2023
4c355a6
Apply suggestions from code review
sgugger May 9, 2023
2ea8339
TTS tests
May 9, 2023
2d55901
Quality
sgugger May 9, 2023
c5439f1
Address review comments
sgugger May 9, 2023
ec9d168
Quality
sgugger May 9, 2023
4038274
Last fix
sgugger May 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .circleci/create_circleci_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def job_name(self):
"git lfs install",
"pip install --upgrade pip",
"pip install .[sklearn,tf-cpu,torch,testing,sentencepiece,torch-speech,vision]",
"pip install tensorflow_probability",
'pip install "tensorflow_probability<0.20"',
"pip install git+https://github.com/huggingface/accelerate",
],
marker="is_pt_tf_cross_test",
Expand Down Expand Up @@ -227,7 +227,7 @@ def job_name(self):
"sudo apt-get -y update && sudo apt-get install -y libsndfile1-dev espeak-ng cmake",
"pip install --upgrade pip",
"pip install .[sklearn,tf-cpu,testing,sentencepiece,tf-speech,vision]",
"pip install tensorflow_probability",
'pip install "tensorflow_probability<0.20"',
],
parallelism=1,
pytest_options={"rA": None},
Expand Down Expand Up @@ -266,7 +266,7 @@ def job_name(self):
"sudo apt-get -y update && sudo apt-get install -y cmake",
"pip install --upgrade pip",
"pip install .[sklearn,tf-cpu,testing,sentencepiece,vision]",
"pip install tensorflow_probability",
'pip install "tensorflow_probability<0.20"',
],
pytest_options={"rA": None},
marker="is_pipeline_test",
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def pytest_configure(config):
)
config.addinivalue_line("markers", "is_staging_test: mark test to run only in the staging environment")
config.addinivalue_line("markers", "accelerate_tests: mark test that require accelerate")
config.addinivalue_line("markers", "tool_tests: mark the tool tests that are run on their specific schedule")


def pytest_addoption(parser):
Expand Down
Binary file added docs/source/en/DO_NOT_MERGE.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 8 additions & 0 deletions docs/source/en/_toctree.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
title: Set up distributed training with 🤗 Accelerate
- local: model_sharing
title: Share your model
- local: agents_and_tools
title: Agents and Tools
- local: transformers_agents
title: Agents
title: Tutorials
- sections:
- sections:
Expand Down Expand Up @@ -99,6 +103,8 @@
title: Notebooks with examples
- local: community
title: Community resources
- local: custom_tools
title: Custom Tools
- local: troubleshooting
title: Troubleshoot
title: Developer guides
Expand Down Expand Up @@ -179,6 +185,8 @@
title: Conceptual guides
- sections:
- sections:
- local: main_classes/agent
title: Agents and Tools
- local: model_doc/auto
title: Auto Classes
- local: main_classes/callback
Expand Down
2 changes: 1 addition & 1 deletion docs/source/en/add_new_model.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ model.save_pretrained("/path/to/converted/checkpoint/folder")
**7. Implement the forward pass**

Having managed to correctly load the pretrained weights into the 🤗 Transformers implementation, you should now make
sure that the forward pass is correctly implemented. In [Get familiar with the original repository](#run-a-pretrained-checkpoint-using-the-original-repository), you have already created a script that runs a forward
sure that the forward pass is correctly implemented. In [Get familiar with the original repository](#34-run-a-pretrained-checkpoint-using-the-original-repository), you have already created a script that runs a forward
pass of the model using the original repository. Now you should write an analogous script using the 🤗 Transformers
implementation instead of the original one. It should look as follows:

Expand Down
285 changes: 285 additions & 0 deletions docs/source/en/agents_and_tools.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,285 @@
<!--Copyright 2022 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
-->

# Transformers Agent

Transformers version v4.30.0 introduces a new API, building on the concept of *tools* and *agents*.

In short, it provides a natural language API on top of transformers: we define a set of curated tools, and design an
agent to interpret natural language and to use these tools. It is extensible by design; we curated some relevant tools,
but we'll show you how the system can be extended easily to use any tool.

Let's start with a few examples of what can be achieved with this new API. It is particularly powerful when it comes
to multimodal tasks, so let's take it for a spin to generate images and read text out loud.

```py
agent.run("Caption the following image", image=image)
```

| **Input** | **Output** |
|-----------------------------------------------------------------------------------------------------------------------------|-----------------------------------|
| <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/beaver.png" width=200> | A beaver is swimming in the water |

---

```py
agent.run("Read the following text out loud", text=text)
```
| **Input** | **Output** |
|-------------------------------------------------------------------------------------------------------------------------|----------------------------------------------|
| A beaver is swimming in the water | <audio controls><source src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tts_example.wav" type="audio/wav"> Your browser does not support the audio element. </audio>

---

```py
agent.run(
"In the following `document`, where will the TRRF Scientific Advisory Council Meeting take place?",
document=document,
)
```
| **Input** | **Output** |
|-----------------------------------------------------------------------------------------------------------------------------|----------------|
| <img src="https://datasets-server.huggingface.co/assets/hf-internal-testing/example-documents/--/hf-internal-testing--example-documents/test/0/image/image.jpg" width=200> | ballroom foyer |

### Quickstart

Before being able to use `agent.run`, you will need to instantiate an agent, which is a large language model (LLM).
We recommend using the [bigcode/starcoder](https://huggingface.co/bigcode/starcoder) checkpoint as it works very well
for the task at hand and is open-source, but please find other examples below.

Start by logging-in to have access to the Inference API:

```py
from huggingface_hub import login

login("<TOKEN>")
```

Then, instantiate the agent

```py
from transformers.tools import HfAgent

starcoder = HfAgent("https://api-inference.huggingface.co/models/bigcode/starcoder")
```

You're now good to go! Let's dive into the two APIs that you now have at your disposal.

#### Single execution (run)

The single execution method is when using the `.run` method of the agent:

```py
agent.run("Draw me a picture of rivers and lakes")
```

<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200>

This is great to perform single instructions. Every `.run` operation is independent, so running it several times in a
row is unlikely to be problematic.

#### Chat-based execution (chat)

The agent also has a chat-based approach, using the `.chat` method:

```py
agent.chat("Draw me a picture of rivers and lakes")
agent.chat("Transform the picture so that there is a rock in there")
```

<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes.png" width=200> <img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/rivers_and_lakes_and_beaver.png" width=200>

<br/>

This is an interesting approach when you want to keep the state across instructions. It's better for experimentation,
but will tend to be much better at single instructions rather than complex instructions (which the `run` method is
better at handling).


### What's happening here? What are tools, and what are agents?

The "agent" here is a large language model, and we're prompting it so that it has access to a specific set of tools.

Tools are very simple: they're a single function, with a name, and a description. We then use these tools description
to prompt the agent. Through the prompt, we show the agent how it would leverage tools in order to perform what was
requests in the query.

### A curated set of tools

We identify a set of tools that can empower such agents. Here is an updated list of the tools we have integrated
in `transformers`:

- **Document question answering**: given a document (such as a PDF) in image format, answer a question on this document (Donut)
- **Text question answering**: given a long text and a question, answer the question in the text (Flan-T5)
- **Unconditional image captioning**: Caption the image! (BLIP)
- **Image question answering**: given an image, answer a question on this image (VILT)
- **Image segmentation**: given an image and a prompt, output the segmentation mask of that prompt (CLIPSeg)
- **Speech to text**: given an audio recording of a person talking, transcribe the speech into text (Whisper)
- **Text to speech**: convert text to speech (SpeechT5)
- **Zero-shot text classification**: given a text and a list of labels, identify to which label the text corresponds the most (BART)
- **Text summarization**: summarize a long text in one or a few sentences (BART)
- **Translation**: translate the text into a given language (NLLB)

These tools have an integration in transformers, and can be used manually as well, for example:

```py
from transformers import load_tool

tool = load_tool("text-to-speech")
audio = tool("This is a text to speech tool")

play(audio)
```

For the release, we offer a remote counterpart for the tools, removing the need for local execution. This demonstration
is made using inference-endpoints.

TODO [to complete/show how to use remote tools]

### Custom tools

While we identify a curated set of tools, we strongly believe that the main value provided by this implementation is
the ability to quickly create and share custom tools.

By pushing the code of a tool to a huggingface Space or a model repository, you're then able to leverage the tool
directly with the agent. For demonstration purposes (and because they're super powerful), we've pushed a few
transformers-agnostic tools in the `huggingface-tools` organization:

- **Text downloader**: to download a text from a web URL
- **Text to image**: generate an image according to a prompt, leveraging stable diffusion
- **Image transformation**: TODO
- **Image inpainting**: TODO

The text-to-image tool we have been using since the beginning is actually a remote tool that lives in
[*huggingface-tools/text-to-image*](https://huggingface.co/spaces/huggingface-tools/text-to-image)! We will
continue releasing such tools on this and other organization, to further supercharge this implementation.

We explain how to push your own tools to the Hub in order to leverage them in the following guide Using Custom Tools.

### Leveraging different agents

We showcase here how to use the [bigcode/starcoder](https://huggingface.co/bigcode/starcoder) model as an LLM, but
it isn't the only model available.
We also support the OpenAssistant model and OpenAI's davinci models (3.5 and 4).

We're planning on supporting local language models in an ulterior version.

The tools defined in this implementation are agnostic to the agent used; we are showcasing the agents that work with
our prompts below, but the tools can also be used with Langchain, Minichain, or any other Agent-based library.

#### Example code for the OpenAssistant model

```py
from transformers import HfAgent

agent = HfAgent(url_endpoint="https://open-assistant.ngrok.io", token="<HF_TOKEN>")
```

#### Example code for OpenAI models

```py
from transformers import OpenAiAgent

agent = OpenAiAgent(model="text-davinci-003", api_key="<API_KEY>")
```

### Code generation

So far we have shown how to use the agents to perform actions for you. However, the agent is really only generating code
that we then execute using a very restricted Python interpreter. In case you would like to use the code generated in
a different setting, the agent can be prompted to return the code, along with tool definition and accurate imports.

For example, the following instruction
```python
agent.run("Draw me a picture of rivers and lakes", return_code=True)
```

returns the following code

```python
from transformers import load_tool

image_generator = load_tool("huggingface-tools/text-to-image")

image = image_generator(prompt="rivers and lakes")
```

### Custom prompts

The performance of the agent is directly linked to the prompt itself. We structure the prompt so that it works well
with what we intend for the agent to do; but for maximum customization we also offer the ability to specify a different prompt when instantiating the agent.

The agent has two prompts: one for the `.run` method, and one for the `.chat` method. Both are customizable.

Here is how the two prompts are structured, and how to update them:

#### Single-execution prompt

The single-execution prompt is defined as such:
- Introduction: how the agent should behave, explanation of the concept of tools. Mentions to the agent that it should
first explain what it tries to do, before outputting the Python code needed to perform.
- Description of all the tools. This is defined by a `<<all_tools>>` token that will be manually replaced at runtime
with the tools defined/chosen by the user.
- Example of tasks and their solution
- Current example, and request for solution.

In order to specify a custom single-execution prompt, one would so the following:

```py
template = """ [...] """

agent = HfAgent(your_endpoint, run_prompt_template=template)
```

<Tip>

Please make sure to have the `<<all_tools>>` string defined somewhere in the `template` so that the agent can be aware
of the tools it has available to it.

</Tip>

#### Chat-execution prompt

The chat-execution prompt is defined as such:
- Introduction: how the agent should behave, specifically as a chat-based assistant. Explanation of the concept of
tools. Mentions to the agent that it should first explain what it tries to do, before outputting the Python code
needed to perform.
- Description of all the tools. This is defined by a `<<all_tools>>` token that will be manually replaced at
runtime with the tools defined/chosen by the user.
- Example of chat-based tasks and their solution
- Current example, and request for solution.

In order to specify a custom single-execution prompt, one would so the following:

```
template = """ [...] """

agent = HfAgent(
url_endpoint=your_endpoint,
token=your_hf_token,
chat_prompt_template=template
)
```

<Tip>

Please make sure to have the `<<all_tools>>` string defined somewhere in the `template` so that the agent can be
aware of the tools it has available to it.

</Tip>

### Using other tools

There are other libraries that provide a myriad of tools, and we aim to be compatible with them. Such examples are
[gradio-tools] and [langchain].

We provide pointers regarding how to leverage their tools in the [Custom Tools](/custom_tools) documentation page.
Loading