Move examples away
This commit is contained in:
parent
5231162274
commit
1751bf03ac
|
@ -1,19 +0,0 @@
|
|||
# Minimal makefile for Sphinx documentation
|
||||
#
|
||||
|
||||
# You can set these variables from the command line.
|
||||
SPHINXOPTS =
|
||||
SPHINXBUILD = sphinx-build
|
||||
SOURCEDIR = source
|
||||
BUILDDIR = _build
|
||||
|
||||
# Put it first so that "make" without argument is like "make help".
|
||||
help:
|
||||
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
||||
|
||||
.PHONY: help Makefile
|
||||
|
||||
# Catch-all target: route all unknown targets to Sphinx using the new
|
||||
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
|
||||
%: Makefile
|
||||
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
|
137
docs/README.md
137
docs/README.md
|
@ -16,17 +16,21 @@ limitations under the License.
|
|||
|
||||
# Generating the documentation
|
||||
|
||||
To generate the documentation, you first have to build it. Several packages are necessary to build the doc,
|
||||
you can install them with the following command, at the root of the code repository:
|
||||
To generate the documentation, you have to build it. Several packages are necessary to build the doc.
|
||||
|
||||
First, you need to install the project itself by running the following command at the root of the code repository:
|
||||
|
||||
```bash
|
||||
pip install -e ".[docs]"
|
||||
pip install -e .
|
||||
```
|
||||
|
||||
Then you need to install our special tool that builds the documentation:
|
||||
You also need to install 2 extra packages:
|
||||
|
||||
```bash
|
||||
pip install git+https://github.com/huggingface/doc-builder
|
||||
# `hf-doc-builder` to build the docs
|
||||
pip install git+https://github.com/huggingface/doc-builder@main
|
||||
# `watchdog` for live reloads
|
||||
pip install watchdog
|
||||
```
|
||||
|
||||
---
|
||||
|
@ -39,11 +43,11 @@ check how they look before committing for instance). You don't have to commit th
|
|||
|
||||
## Building the documentation
|
||||
|
||||
Once you have setup the `doc-builder` and additional packages, you can generate the documentation by
|
||||
typing the following command:
|
||||
Once you have setup the `doc-builder` and additional packages with the pip install command above,
|
||||
you can generate the documentation by typing the following command:
|
||||
|
||||
```bash
|
||||
doc-builder build accelerate docs/source/ --build_dir ~/tmp/test-build
|
||||
doc-builder build agents docs/source/ --build_dir ~/tmp/test-build
|
||||
```
|
||||
|
||||
You can adapt the `--build_dir` to set any temporary folder that you prefer. This command will create it and generate
|
||||
|
@ -52,30 +56,20 @@ Markdown editor.
|
|||
|
||||
## Previewing the documentation
|
||||
|
||||
To preview the docs, first install the `watchdog` module with:
|
||||
To preview the docs, run the following command:
|
||||
|
||||
```bash
|
||||
pip install watchdog
|
||||
doc-builder preview agents docs/source/
|
||||
```
|
||||
|
||||
Then run the following command:
|
||||
|
||||
```bash
|
||||
doc-builder preview {package_name} {path_to_docs}
|
||||
```
|
||||
|
||||
For example:
|
||||
|
||||
```bash
|
||||
doc-builder preview accelerate docs/source/
|
||||
```
|
||||
|
||||
The docs will be viewable at [http://localhost:3000](http://localhost:3000). You can also preview the docs once you have opened a PR. You will see a bot add a comment to a link where the documentation with your changes lives.
|
||||
The docs will be viewable at [http://localhost:5173](http://localhost:5173). You can also preview the docs once you
|
||||
have opened a PR. You will see a bot add a comment to a link where the documentation with your changes lives.
|
||||
|
||||
---
|
||||
**NOTE**
|
||||
|
||||
The `preview` command only works with existing doc files. When you add a completely new file, you need to update `_toctree.yml` & restart `preview` command (`ctrl-c` to stop it & call `doc-builder preview ...` again).
|
||||
The `preview` command only works with existing doc files. When you add a completely new file, you need to update
|
||||
`_toctree.yml` & restart `preview` command (`ctrl-c` to stop it & call `doc-builder preview ...` again).
|
||||
|
||||
---
|
||||
|
||||
|
@ -84,7 +78,7 @@ The `preview` command only works with existing doc files. When you add a complet
|
|||
Accepted files are Markdown (.md).
|
||||
|
||||
Create a file with its extension and put it in the source directory. You can then link it to the toc-tree by putting
|
||||
the filename without the extension in the [`_toctree.yml`](https://github.com/huggingface/accelerate/blob/main/docs/source/_toctree.yml) file.
|
||||
the filename without the extension in the [`_toctree.yml`](https://github.com/huggingface/agents/blob/main/docs/source/_toctree.yml) file.
|
||||
|
||||
## Renaming section headers and moving sections
|
||||
|
||||
|
@ -109,10 +103,12 @@ Sections that were moved:
|
|||
|
||||
Use the relative style to link to the new file so that the versioned docs continue to work.
|
||||
|
||||
For an example of a rich moved section set please see the very end of [the transformers Trainer doc](https://github.com/huggingface/transformers/blob/main/docs/source/en/main_classes/trainer.md).
|
||||
|
||||
|
||||
## Writing Documentation - Specification
|
||||
|
||||
The `huggingface/accelerate` documentation follows the
|
||||
The `huggingface/agents` documentation follows the
|
||||
[Google documentation](https://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html) style for docstrings,
|
||||
although we can write them directly in Markdown.
|
||||
|
||||
|
@ -120,12 +116,14 @@ although we can write them directly in Markdown.
|
|||
|
||||
Adding a new tutorial or section is done in two steps:
|
||||
|
||||
- Add a new file under `./source`. This file can either be ReStructuredText (.rst) or Markdown (.md).
|
||||
- Add a new Markdown (.md) file under `./source`.
|
||||
- Link that file in `./source/_toctree.yml` on the correct toc-tree.
|
||||
|
||||
Make sure to put your new file under the proper section. It's unlikely to go in the first section (*Get Started*), so
|
||||
depending on the intended targets (beginners, more advanced users, or researchers) it should go in sections two, three, or
|
||||
four.
|
||||
Make sure to put your new file under the proper section. If you have a doubt, feel free to ask in a Github Issue or PR.
|
||||
|
||||
### Translating
|
||||
|
||||
When translating, refer to the guide at [./TRANSLATING.md](https://github.com/huggingface/agents/blob/main/docs/TRANSLATING.md).
|
||||
|
||||
### Writing source documentation
|
||||
|
||||
|
@ -137,9 +135,9 @@ adds a link to its documentation with this syntax: \[\`XXXClass\`\] or \[\`funct
|
|||
function to be in the main package.
|
||||
|
||||
If you want to create a link to some internal class or function, you need to
|
||||
provide its path. For instance: \[\`utils.gather\`\]. This will be converted into a link with
|
||||
`utils.gather` in the description. To get rid of the path and only keep the name of the object you are
|
||||
linking to in the description, add a ~: \[\`~utils.gather\`\] will generate a link with `gather` in the description.
|
||||
provide its path. For instance: \[\`utils.ModelOutput\`\]. This will be converted into a link with
|
||||
`utils.ModelOutput` in the description. To get rid of the path and only keep the name of the object you are
|
||||
linking to in the description, add a ~: \[\`~utils.ModelOutput\`\] will generate a link with `ModelOutput` in the description.
|
||||
|
||||
The same works for methods so you can either use \[\`XXXClass.method\`\] or \[~\`XXXClass.method\`\].
|
||||
|
||||
|
@ -154,20 +152,20 @@ description:
|
|||
n_layers (`int`): The number of layers of the model.
|
||||
```
|
||||
|
||||
If the description is too long to fit in one line (more than 119 characters in total), another indentation is necessary
|
||||
before writing the description after the argument.
|
||||
|
||||
Finally, to maintain uniformity if any *one* description is too long to fit on one line, the
|
||||
rest of the parameters should follow suit and have an indention before their description.
|
||||
If the description is too long to fit in one line, another indentation is necessary before writing the description
|
||||
after the argument.
|
||||
|
||||
Here's an example showcasing everything so far:
|
||||
|
||||
```
|
||||
Args:
|
||||
gradient_accumulation_steps (`int`, *optional*, default to 1):
|
||||
The number of steps that should pass before gradients are accumulated. A number > 1 should be combined with `Accelerator.accumulate`.
|
||||
cpu (`bool`, *optional*):
|
||||
Whether or not to force the script to execute on CPU. Will ignore GPU available if set to `True` and force the execution on one process only.
|
||||
input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`):
|
||||
Indices of input sequence tokens in the vocabulary.
|
||||
|
||||
Indices can be obtained using [`AlbertTokenizer`]. See [`~PreTrainedTokenizer.encode`] and
|
||||
[`~PreTrainedTokenizer.__call__`] for details.
|
||||
|
||||
[What are input IDs?](../glossary#input-ids)
|
||||
```
|
||||
|
||||
For optional arguments or arguments with defaults we follow the following syntax: imagine we have a function with the
|
||||
|
@ -182,9 +180,9 @@ then its documentation should look like this:
|
|||
```
|
||||
Args:
|
||||
x (`str`, *optional*):
|
||||
This argument controls ... and has a description longer than 119 chars.
|
||||
This argument controls ...
|
||||
a (`float`, *optional*, defaults to 1):
|
||||
This argument is used to ... and has a description longer than 119 chars.
|
||||
This argument is used to ...
|
||||
```
|
||||
|
||||
Note that we always omit the "defaults to \`None\`" when None is the default for any argument. Also note that even
|
||||
|
@ -197,7 +195,7 @@ Multi-line code blocks can be useful for displaying examples. They are done betw
|
|||
|
||||
|
||||
````
|
||||
```python
|
||||
```
|
||||
# first line of code
|
||||
# second line
|
||||
# etc
|
||||
|
@ -228,17 +226,15 @@ Here's an example of a tuple return, comprising several objects:
|
|||
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
|
||||
```
|
||||
|
||||
## Styling the docstring
|
||||
#### Adding an image
|
||||
|
||||
We have an automatic script running with the `make style` comment that will make sure that:
|
||||
- the docstrings fully take advantage of the line width
|
||||
- all code examples are formatted using black, like the code of the Transformers library
|
||||
Due to the rapidly growing repository, it is important to make sure that no files that would significantly weigh down the repository are added. This includes images, videos, and other non-text files. We prefer to leverage a hf.co hosted `dataset` like
|
||||
the ones hosted on [`hf-internal-testing`](https://huggingface.co/hf-internal-testing) in which to place these files and reference
|
||||
them by URL. We recommend putting them in the following dataset: [huggingface/documentation-images](https://huggingface.co/datasets/huggingface/documentation-images).
|
||||
If an external contribution, feel free to add the images to your PR and ask a Hugging Face member to migrate your images
|
||||
to this dataset.
|
||||
|
||||
This script may have some weird failures if you made a syntax mistake or if you uncover a bug. Therefore, it's
|
||||
recommended to commit your changes before running `make style`, so you can revert the changes done by that script
|
||||
easily.
|
||||
|
||||
## Writing documentation examples
|
||||
#### Writing documentation examples
|
||||
|
||||
The syntax for Example docstrings can look as follows:
|
||||
|
||||
|
@ -246,20 +242,31 @@ The syntax for Example docstrings can look as follows:
|
|||
Example:
|
||||
|
||||
```python
|
||||
>>> import time
|
||||
>>> from accelerate import Accelerator
|
||||
>>> accelerator = Accelerator()
|
||||
>>> if accelerator.is_main_process:
|
||||
... time.sleep(2)
|
||||
>>> else:
|
||||
... print("I'm waiting for the main process to finish its sleep...")
|
||||
>>> accelerator.wait_for_everyone()
|
||||
>>> # Should print on every process at the same time
|
||||
>>> print("Everyone is here")
|
||||
>>> from transformers import Wav2Vec2Processor, Wav2Vec2ForCTC
|
||||
>>> from datasets import load_dataset
|
||||
>>> import torch
|
||||
|
||||
>>> dataset = load_dataset("hf-internal-testing/librispeech_asr_demo", "clean", split="validation")
|
||||
>>> dataset = dataset.sort("id")
|
||||
>>> sampling_rate = dataset.features["audio"].sampling_rate
|
||||
|
||||
>>> processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
|
||||
>>> model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
|
||||
|
||||
>>> # audio file is decoded on the fly
|
||||
>>> inputs = processor(dataset[0]["audio"]["array"], sampling_rate=sampling_rate, return_tensors="pt")
|
||||
>>> with torch.no_grad():
|
||||
... logits = model(**inputs).logits
|
||||
>>> predicted_ids = torch.argmax(logits, dim=-1)
|
||||
|
||||
>>> # transcribe speech
|
||||
>>> transcription = processor.batch_decode(predicted_ids)
|
||||
>>> transcription[0]
|
||||
'MISTER QUILTER IS THE APOSTLE OF THE MIDDLE CLASSES AND WE ARE GLAD TO WELCOME HIS GOSPEL'
|
||||
```
|
||||
```
|
||||
|
||||
The docstring should give a minimal, clear example of how the respective function
|
||||
The docstring should give a minimal, clear example of how the respective model
|
||||
is to be used in inference and also include the expected (ideally sensible)
|
||||
output.
|
||||
Often, readers will try out the example before even going through the function
|
||||
|
|
|
@ -114,8 +114,7 @@ Now let’s build our tool. It needs the following: (read the documentation for
|
|||
```py
|
||||
from transformers.agents import tool
|
||||
|
||||
|
||||
|
||||
@tool
|
||||
def sql_engine(query: str) -> str:
|
||||
"""
|
||||
Allows you to perform SQL queries on the table. Returns a string representation of the result.
|
||||
|
@ -212,8 +211,8 @@ agent.run("Which waiter got more total money from tips?")
|
|||
It directly works! The setup was surprisingly simple, wasn’t it?
|
||||
|
||||
This example is done! We've touched upon these concepts:
|
||||
- building new tools
|
||||
- updating tool description
|
||||
- switching to a stronger LLM helps agent reasoning.
|
||||
- Building new tools.
|
||||
- Updating a tool's description.
|
||||
- Switching to a stronger LLM helps agent reasoning.
|
||||
|
||||
✅ Now you can go build this text-to-SQL system you’ve always dreamt of! ✨
|
|
@ -15,60 +15,29 @@ rendered properly in your Markdown viewer.
|
|||
|
||||
# Agents
|
||||
|
||||
Accelerate is a library that enables the same PyTorch code to be run across any distributed configuration by adding just four lines of code! In short, training and inference at scale made simple, efficient and adaptable.
|
||||
|
||||
```diff
|
||||
+ from accelerate import Accelerator
|
||||
+ accelerator = Accelerator()
|
||||
|
||||
+ model, optimizer, training_dataloader, scheduler = accelerator.prepare(
|
||||
+ model, optimizer, training_dataloader, scheduler
|
||||
+ )
|
||||
|
||||
for batch in training_dataloader:
|
||||
optimizer.zero_grad()
|
||||
inputs, targets = batch
|
||||
inputs = inputs.to(device)
|
||||
targets = targets.to(device)
|
||||
outputs = model(inputs)
|
||||
loss = loss_function(outputs, targets)
|
||||
+ accelerator.backward(loss)
|
||||
optimizer.step()
|
||||
scheduler.step()
|
||||
```
|
||||
|
||||
Built on `torch_xla` and `torch.distributed`, Accelerate takes care of the heavy lifting, so you don't have to write any custom code to adapt to these platforms.
|
||||
Convert existing codebases to utilize [DeepSpeed](usage_guides/deepspeed), perform [fully sharded data parallelism](usage_guides/fsdp), and have automatic support for mixed-precision training!
|
||||
|
||||
<Tip>
|
||||
|
||||
To get a better idea of this process, make sure to check out the [Tutorials](basic_tutorials/overview)!
|
||||
|
||||
</Tip>
|
||||
|
||||
|
||||
This code can then be launched on any system through Accelerate's CLI interface:
|
||||
```bash
|
||||
accelerate launch {my_script.py}
|
||||
```
|
||||
Agents is a library that enables you to run powerful agents in a few lines of code!
|
||||
It is:
|
||||
- lightweight
|
||||
- understandable (we kept abstractions to the minimum)
|
||||
- the only library with first-class support for Code Agents, i.e. agents that write their actions in code! Head to [./conceptual_guides/intro_agents.md] to learn more.
|
||||
|
||||
<div class="mt-10">
|
||||
<div class="w-full flex flex-col space-y-4 md:space-y-0 md:grid md:grid-cols-2 md:gap-y-4 md:gap-x-5">
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./basic_tutorials/overview"
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./tutorials/tools"
|
||||
><div class="w-full text-center bg-gradient-to-br from-blue-400 to-blue-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">Tutorials</div>
|
||||
<p class="text-gray-700">Learn the basics and become familiar with using Accelerate. Start here if you are using Accelerate for the first time!</p>
|
||||
<p class="text-gray-700">Learn the basics and become familiar with using Agents. Start here if you are using Agents for the first time!</p>
|
||||
</a>
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./usage_guides/explore"
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./examples/text_to_sql"
|
||||
><div class="w-full text-center bg-gradient-to-br from-indigo-400 to-indigo-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">How-to guides</div>
|
||||
<p class="text-gray-700">Practical guides to help you achieve a specific goal. Take a look at these guides to learn how to use Accelerate to solve real-world problems.</p>
|
||||
<p class="text-gray-700">Practical guides to help you achieve a specific goal: create an agent to generate and test SQL queries!</p>
|
||||
</a>
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./concept_guides/gradient_synchronization"
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./conceptual_guides/intro_agents"
|
||||
><div class="w-full text-center bg-gradient-to-br from-pink-400 to-pink-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">Conceptual guides</div>
|
||||
<p class="text-gray-700">High-level explanations for building a better understanding of important topics such as avoiding subtle nuances and pitfalls in distributed training and DeepSpeed.</p>
|
||||
<p class="text-gray-700">High-level explanations for building a better understanding of important topics to build better functioning agents.</p>
|
||||
</a>
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./package_reference/accelerator"
|
||||
<a class="!no-underline border dark:border-gray-700 p-5 rounded-lg shadow hover:shadow-lg" href="./tutorials/building_good_agents"
|
||||
><div class="w-full text-center bg-gradient-to-br from-purple-400 to-purple-500 rounded-lg py-1.5 font-semibold mb-5 text-white text-lg leading-relaxed">Reference</div>
|
||||
<p class="text-gray-700">Technical descriptions of how Accelerate classes and methods work.</p>
|
||||
<p class="text-gray-700">General, horizontal tutorials.</p>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -13,7 +13,6 @@ specific language governing permissions and limitations under the License.
|
|||
rendered properly in your Markdown viewer.
|
||||
|
||||
-->
|
||||
|
||||
# Agents & Tools
|
||||
|
||||
<Tip warning={true}>
|
||||
|
|
|
@ -72,8 +72,8 @@ def get_weather_api(location: str, date_time: str) -> str:
|
|||
Returns the weather report.
|
||||
|
||||
Args:
|
||||
- location (`str`): the name of the place that you want the weather for.
|
||||
- date_time (`str`): the date and time for which you want the report.
|
||||
location: the name of the place that you want the weather for.
|
||||
date_time: the date and time for which you want the report.
|
||||
"""
|
||||
lon, lat = convert_location_to_coordinates(location)
|
||||
date_time = datetime.strptime(date_time)
|
||||
|
@ -96,8 +96,8 @@ def get_weather_api(location: str, date_time: str) -> str:
|
|||
Returns the weather report.
|
||||
|
||||
Args:
|
||||
- location (`str`): the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
|
||||
- date_time (`str`): the date and time for which you want the report, formatted as '%m/%d/%y %H:%M:%S'.
|
||||
location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like "Anchor Point, Taghazout, Morocco".
|
||||
date_time: the date and time for which you want the report, formatted as '%m/%d/%y %H:%M:%S'.
|
||||
"""
|
||||
lon, lat = convert_location_to_coordinates(location)
|
||||
try:
|
||||
|
|
|
@ -1,276 +0,0 @@
|
|||
<!---
|
||||
Copyright 2021 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.
|
||||
-->
|
||||
|
||||
# In this folder we showcase various full examples using 🤗 Accelerate
|
||||
|
||||
## Simple NLP example
|
||||
|
||||
The [nlp_example.py](./nlp_example.py) script is a simple example to train a Bert model on a classification task ([GLUE's MRPC](https://www.microsoft.com/en-us/download/details.aspx?id=52398)).
|
||||
|
||||
Prior to running it you should install 🤗 Dataset and 🤗 Transformers:
|
||||
|
||||
```bash
|
||||
pip install datasets evaluate transformers
|
||||
```
|
||||
|
||||
The same script can be run in any of the following configurations:
|
||||
- single CPU or single GPU
|
||||
- multi CPUs
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
- (multi) TPUs
|
||||
- fp16 (mixed-precision) or fp32 (normal precision)
|
||||
|
||||
To run it in each of these various modes, use the following commands:
|
||||
- single CPU:
|
||||
* from a server without GPU
|
||||
```bash
|
||||
python ./nlp_example.py
|
||||
```
|
||||
* from any server by passing `cpu=True` to the `Accelerator`.
|
||||
```bash
|
||||
python ./nlp_example.py --cpu
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --cpu ./nlp_example.py
|
||||
```
|
||||
- single GPU:
|
||||
```bash
|
||||
python ./nlp_example.py # from a server with a GPU
|
||||
```
|
||||
- with fp16 (mixed-precision)
|
||||
* from any server by passing `mixed_precison=fp16` to the `Accelerator`.
|
||||
```bash
|
||||
python ./nlp_example.py --mixed_precision fp16
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --mixed_precision fp16 ./nlp_example.py
|
||||
- multi CPUs (requires Open MPI, Intel MPI, or MVAPICH)
|
||||
* With Accelerate config and launcher, execute the following from node 0:
|
||||
```bash
|
||||
accelerate config # Select to have accelerate launch mpirun
|
||||
accelerate launch ./nlp_example.py # This will run the script on each server
|
||||
```
|
||||
* With Intel MPI:
|
||||
```bash
|
||||
export CCL_WORKER_COUNT=1
|
||||
export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
|
||||
mpirun -f hostfile -n 16 -ppn 4 python ./nlp_example.py
|
||||
```
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
accelerate config # This will create a config file on your server
|
||||
accelerate launch ./nlp_example.py # This will run the script on your server
|
||||
```
|
||||
* With traditional PyTorch launcher (`python -m torch.distributed.run` can be used instead of `torchrun`)
|
||||
```bash
|
||||
torchrun --nproc_per_node 2 ./nlp_example.py
|
||||
```
|
||||
- multi GPUs, multi node (several machines, using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher, on each machine:
|
||||
```bash
|
||||
accelerate config # This will create a config file on each server
|
||||
accelerate launch ./nlp_example.py # This will run the script on each server
|
||||
```
|
||||
* With PyTorch launcher only (`python -m torch.distributed.run` can be used instead of `torchrun`). Run this command on each node:
|
||||
```bash
|
||||
torchrun \ # python -m torch.distributed.run
|
||||
--nproc_per_node 2 \
|
||||
--nnodes 2 \
|
||||
--rdzv_id 2299 \ # A unique job id
|
||||
--rdzv_backend c10d \
|
||||
--rdzv_endpoint master_node_ip_address:29500 \
|
||||
./nlp_example.py
|
||||
```
|
||||
- (multi) TPUs
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
accelerate config # This will create a config file on your TPU server
|
||||
accelerate launch ./nlp_example.py # This will run the script on each server
|
||||
```
|
||||
* In PyTorch:
|
||||
Add an `xmp.spawn` line in your script as you usually do.
|
||||
|
||||
|
||||
## Simple vision example
|
||||
|
||||
The [cv_example.py](./cv_example.py) script is a simple example to fine-tune a ResNet-50 on a classification task ([Ofxord-IIT Pet Dataset](https://www.robots.ox.ac.uk/~vgg/data/pets/)).
|
||||
|
||||
The same script can be run in any of the following configurations:
|
||||
- single CPU or single GPU
|
||||
- multi CPUs
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
- (multi) TPUs
|
||||
- fp16 (mixed-precision) or fp32 (normal precision)
|
||||
|
||||
Prior to running it you should install timm and torchvision:
|
||||
|
||||
```bash
|
||||
pip install timm torchvision
|
||||
```
|
||||
|
||||
and you should download the data with the following commands:
|
||||
|
||||
```bash
|
||||
wget https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz
|
||||
tar -xzf images.tar.gz
|
||||
```
|
||||
|
||||
To run it in each of these various modes, use the following commands:
|
||||
- single CPU:
|
||||
* from a server without GPU
|
||||
```bash
|
||||
python ./cv_example.py --data_dir path_to_data
|
||||
```
|
||||
* from any server by passing `cpu=True` to the `Accelerator`.
|
||||
```bash
|
||||
python ./cv_example.py --data_dir path_to_data --cpu
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --cpu ./cv_example.py --data_dir path_to_data
|
||||
```
|
||||
- single GPU:
|
||||
```bash
|
||||
python ./cv_example.py # from a server with a GPU
|
||||
```
|
||||
- with fp16 (mixed-precision)
|
||||
* from any server by passing `mixed_precison=fp16` to the `Accelerator`.
|
||||
```bash
|
||||
python ./cv_example.py --data_dir path_to_data --mixed_precison fp16
|
||||
```
|
||||
* from any server with Accelerate launcher
|
||||
```bash
|
||||
accelerate launch --mixed_precison fp16 ./cv_example.py --data_dir path_to_data
|
||||
- multi CPUs (requires Open MPI, Intel MPI, or MVAPICH)
|
||||
* With Accelerate config and launcher, run the following from node 0:
|
||||
```bash
|
||||
accelerate config --config_file config.yaml # Select to have accelerate launch mpirun
|
||||
accelerate launch ./cv_example.py --data_dir path_to_data # This will run the script on each server
|
||||
```
|
||||
* With Intel MPI, execute mpirun from node 0:
|
||||
```bash
|
||||
export CCL_WORKER_COUNT=1
|
||||
export MASTER_ADDR=xxx.xxx.xxx.xxx #node0 ip
|
||||
mpirun -f hostfile -n 16 -ppn 4 python ./cv_example.py --data_dir path_to_data
|
||||
```
|
||||
- multi GPUs (using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
|
||||
accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on your server
|
||||
```
|
||||
* With traditional PyTorch launcher (`python -m torch.distributed.run` can be used instead of `torchrun`)
|
||||
```bash
|
||||
torchrun --nproc_per_node 2 ./cv_example.py --data_dir path_to_data
|
||||
```
|
||||
- multi GPUs, multi node (several machines, using PyTorch distributed mode)
|
||||
* With Accelerate config and launcher, on each machine:
|
||||
```bash
|
||||
accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
|
||||
accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on each server
|
||||
```
|
||||
* With PyTorch launcher only (`python -m torch.distributed.run` can be used instead of `torchrun`). Run this command on each node:
|
||||
```bash
|
||||
torchrun \ # python -m torch.distributed.run
|
||||
--nproc_per_node 2 \
|
||||
--nnodes 2 \
|
||||
--rdzv_id 2299 \ # A unique job id
|
||||
--rdzv_backend c10d \
|
||||
--rdzv_endpoint master_node_ip_address:29500 \
|
||||
./cv_example.py --data_dir path_to_data
|
||||
```
|
||||
- (multi) TPUs
|
||||
* With Accelerate config and launcher
|
||||
```bash
|
||||
accelerate config --config_file config.yaml # This will create a config file on your server to `config.yaml`
|
||||
accelerate launch --config_file config.yaml ./cv_example.py --data_dir path_to_data # This will run the script on each server
|
||||
```
|
||||
* In PyTorch:
|
||||
Add an `xmp.spawn` line in your script as you usually do.
|
||||
|
||||
### Simple vision example (GANs)
|
||||
|
||||
- [huggan project](https://github.com/huggingface/community-events/tree/main/huggan)
|
||||
|
||||
|
||||
### Using AWS SageMaker integration
|
||||
- [Examples showcasing AWS SageMaker integration of 🤗 Accelerate.](https://github.com/pacman100/accelerate-aws-sagemaker)
|
||||
|
||||
## Configuration zoo
|
||||
In [/config_yaml_templates](./config_yaml_templates/) we have a variety of *minimal* `config.yaml` templates and examples to help you learn
|
||||
how to create your own configuration files depending on the scenario.
|
||||
|
||||
## SLURM Scripts
|
||||
In [/slurm/submit_multigpu.sh](./slurm/submit_multigpu.sh) and [/slurm/submit_multinode.sh](./slurm/submit_multinode.sh) we present two scripts for running the examples on a machine with [SLURM](https://slurm.schedmd.com/documentation.html) workload manager.
|
||||
|
||||
In [/slurm/submit_multigpu.sh](./slurm/submit_multigpu.sh) the only parameter in the launcher that needs to be modified is `--num_processes`, which determines the number of GPUs we will use. In this case, using the environment variable `$SLURM_GPUS`, we indicate that we want to utilize all the GPUs available on the node we have requested.
|
||||
|
||||
In [/slurm/submit_multinode.sh](./slurm/submit_multinode.sh) we must specify the number of nodes that will be part of the training (`--num_machines`), how many GPUs we will use in total (`--num_processes`), the [`backend`](https://pytorch.org/docs/stable/elastic/run.html#note-on-rendezvous-backend), `--main_process_ip` which will be the address the master node and the `--main_process_port`.
|
||||
|
||||
In [/slurm/submit_multicpu.sh](./slurm/submit_multicpu.sh) we must specify the number of nodes that will be part of the training (`--num_machines`), how many CPU processes we will use in total (`--num_processes`), the [`backend`](https://pytorch.org/docs/stable/elastic/run.html#note-on-rendezvous-backend), `--main_process_ip` which will be the address the master node and the `--main_process_port`. `mpirun_hostfile` specifies to run the job using MPIRun.
|
||||
|
||||
In both scripts, we run `activateEnviroment.sh` at the beginning. This script should contain the necessary instructions to initialize the environment for execution. Below, we show an example that loads the necessary libraries ([Environment modules](https://github.com/cea-hpc/modules)), activates the Python environment, and sets up various environment variables, most of them to run the scripts in offline mode in case we don't have internet connection from the cluster.
|
||||
|
||||
```bash
|
||||
# activateEnvironment.sh
|
||||
module purge
|
||||
module load anaconda3/2020.02 cuda/10.2 cudnn/8.0.5 nccl/2.9.9 arrow/7.0.0 openmpi
|
||||
source activate /home/nct01/nct01328/pytorch_antoni_local
|
||||
|
||||
export HF_HOME=/gpfs/projects/nct01/nct01328/
|
||||
export HF_LOCAL_HOME=/gpfs/projects/nct01/nct01328/HF_LOCAL
|
||||
export HF_DATASETS_OFFLINE=1
|
||||
export TRANSFORMERS_OFFLINE=1
|
||||
export PYTHONPATH=/home/nct01/nct01328/transformers-in-supercomputers:$PYTHONPATH
|
||||
export GPUS_PER_NODE=4
|
||||
```
|
||||
|
||||
## Simple Multi-GPU Hardware Launcher (using an external platform)
|
||||
|
||||
[multigpu_remote_launcher.py](./multigpu_remote_launcher.py) is a minimal script that demonstrates launching accelerate
|
||||
on multiple remote GPUs, and with automatic hardware environment and dependency setup for reproducibility. You can
|
||||
easily customize the training function used, training arguments, hyperparameters, and type of compute hardware, and then
|
||||
run the script to automatically launch multi GPU training on remote hardware.
|
||||
|
||||
This script uses [Runhouse](https://github.com/run-house/runhouse) to launch on self-hosted hardware (e.g. in your own
|
||||
cloud account or on-premise cluster) but there are other options for running remotely as well. Runhouse can be installed
|
||||
with `pip install runhouse`, and you can refer to
|
||||
[hardware setup](https://runhouse-docs.readthedocs-hosted.com/en/latest/api/python/cluster.html#hardware-setup)
|
||||
for hardware setup instructions, or this
|
||||
[Colab tutorial](https://colab.research.google.com/drive/1qVwYyLTCPYPSdz9ZX7BZl9Qm0A3j7RJe) for a more in-depth walkthrough.
|
||||
|
||||
## Finer Examples
|
||||
|
||||
While the first two scripts are extremely barebones when it comes to what you can do with accelerate, more advanced features are documented in two other locations.
|
||||
|
||||
### `by_feature` examples
|
||||
|
||||
These scripts are *individual* examples highlighting one particular feature or use-case within Accelerate. They all stem from the [nlp_example.py](./nlp_example.py) script, and any changes or modifications is denoted with a `# New Code #` comment.
|
||||
|
||||
Read the README.md file located in the `by_feature` folder for more information.
|
||||
|
||||
### `complete_*` examples
|
||||
|
||||
These two scripts contain *every* single feature currently available in Accelerate in one place, as one giant script.
|
||||
|
||||
New arguments that can be passed include:
|
||||
|
||||
- `checkpointing_steps`, whether the various states should be saved at the end of every `n` steps, or `"epoch"` for each epoch. States are then saved to folders named `step_{n}` or `epoch_{n}`
|
||||
- `resume_from_checkpoint`, should be used if you want to resume training off of a previous call to the script and passed a `checkpointing_steps` to it.
|
||||
- `with_tracking`, should be used if you want to log the training run using all available experiment trackers in your environment. Currently supported trackers include TensorBoard, Weights and Biases, and CometML.
|
|
@ -1,40 +0,0 @@
|
|||
from agents import load_tool, CodeAgent, JsonAgent, HfApiEngine
|
||||
from agents.default_tools import PythonInterpreterTool
|
||||
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
# Import tool from Hub
|
||||
image_generation_tool = load_tool("m-ric/text-to-image", cache=False)
|
||||
|
||||
from agents.search import DuckDuckGoSearchTool
|
||||
|
||||
search_tool = DuckDuckGoSearchTool()
|
||||
|
||||
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
||||
|
||||
agent = CodeAgent(tools=[search_tool], llm_engine=llm_engine, planning_interval=3)
|
||||
|
||||
# Run it!
|
||||
print("Let's run the Code agent:")
|
||||
|
||||
result = agent.run(
|
||||
"How long would a cheetah at full speed take to run the length of Pont Alexandre III?",
|
||||
)
|
||||
|
||||
print("RESULT:", result)
|
||||
|
||||
|
||||
code_tool = PythonInterpreterTool()
|
||||
|
||||
agent = JsonAgent(tools=[search_tool, code_tool], llm_engine=llm_engine, planning_interval=3)
|
||||
|
||||
print("====================")
|
||||
print("====================")
|
||||
print("Now let's run the JSON agent:")
|
||||
result = agent.run(
|
||||
"How long would a cheetah at full speed take to run the length of Pont Alexandre III?",
|
||||
)
|
||||
|
||||
print("RESULT:", result)
|
|
@ -1,20 +0,0 @@
|
|||
from agents import load_tool, CodeAgent, HfApiEngine
|
||||
from agents.search import DuckDuckGoSearchTool
|
||||
|
||||
# Import tool from Hub
|
||||
image_generation_tool = load_tool("m-ric/text-to-image", cache=False)
|
||||
|
||||
# Import tool from LangChain
|
||||
|
||||
search_tool = DuckDuckGoSearchTool()
|
||||
|
||||
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
||||
# Initialize the agent with both tools
|
||||
agent = CodeAgent(tools=[image_generation_tool, search_tool], llm_engine=llm_engine)
|
||||
|
||||
# Run it!
|
||||
result = agent.run(
|
||||
"Return me a photo of the car that James bond drove in the latest movie.",
|
||||
)
|
||||
|
||||
print(result)
|
|
@ -1,27 +0,0 @@
|
|||
from agents import stream_to_gradio, HfApiEngine, load_tool, CodeAgent
|
||||
import gradio as gr
|
||||
|
||||
image_generation_tool = load_tool("m-ric/text-to-image")
|
||||
|
||||
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
||||
|
||||
agent = CodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)
|
||||
|
||||
def interact_with_agent(prompt, messages):
|
||||
messages.append(gr.ChatMessage(role="user", content=prompt))
|
||||
yield messages
|
||||
for msg in stream_to_gradio(agent, task=prompt, reset_agent_memory=False):
|
||||
messages.append(msg)
|
||||
yield messages
|
||||
yield messages
|
||||
|
||||
|
||||
with gr.Blocks() as demo:
|
||||
stored_message = gr.State([])
|
||||
chatbot = gr.Chatbot(label="Agent",
|
||||
type="messages",
|
||||
avatar_images=(None, "https://em-content.zobj.net/source/twitter/53/robot-face_1f916.png"))
|
||||
text_input = gr.Textbox(lines=1, label="Chat Message")
|
||||
text_input.submit(lambda s: (s, ""), [text_input], [stored_message, text_input]).then(interact_with_agent, [stored_message, chatbot], [chatbot])
|
||||
|
||||
demo.launch()
|
|
@ -1,149 +0,0 @@
|
|||
from agents import JsonAgent
|
||||
from agents import tool
|
||||
import webbrowser
|
||||
import requests
|
||||
from datetime import datetime
|
||||
import random
|
||||
from llama_cpp import Llama
|
||||
from agents import tool
|
||||
import webbrowser
|
||||
from typing import List, Generator, Dict, Any
|
||||
import json
|
||||
import re
|
||||
|
||||
model_repo="andito/SmolLM2-1.7B-Instruct-F16-GGUF"
|
||||
model_filename="smollm2-1.7b-8k-dpo-f16.gguf"
|
||||
|
||||
|
||||
model = Llama.from_pretrained(
|
||||
repo_id=model_repo,
|
||||
filename=model_filename,
|
||||
n_ctx=8192,
|
||||
verbose=False
|
||||
)
|
||||
print("Model initialized")
|
||||
|
||||
def llm_engine(messages, stop_sequences=["Task", "<|endoftext|>"]) -> str:
|
||||
output = ""
|
||||
for chunk in model.create_chat_completion(
|
||||
messages=messages,
|
||||
max_tokens=2048,
|
||||
temperature=0.0,
|
||||
top_p=1.0,
|
||||
top_k=50,
|
||||
repeat_penalty=1.0,
|
||||
stream=True
|
||||
):
|
||||
content = chunk['choices'][0]['delta'].get('content')
|
||||
if content:
|
||||
if content in ["<end_action>", "<|endoftext|>"]:
|
||||
break
|
||||
output += content
|
||||
return output
|
||||
|
||||
system_prompt = """You are an expert in composing functions. You are given a question and a set of possible functions.
|
||||
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
||||
If none of the functions can be used, point it out and refuse to answer.
|
||||
If the given question lacks the parameters required by the function, also point it out.
|
||||
|
||||
You have access to the following tools:
|
||||
{{tool_descriptions}}
|
||||
|
||||
<<managed_agents_descriptions>>
|
||||
|
||||
You can use imports in your code, but only from the following list of modules: <<authorized_imports>>
|
||||
|
||||
The output MUST strictly adhere to the following format, and NO other text MUST be included.
|
||||
The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make the tool calls an empty list '[]'.
|
||||
<tool_call>[
|
||||
{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
|
||||
... (more tool calls as required)
|
||||
]</tool_call>"""
|
||||
|
||||
@tool
|
||||
def get_random_number_between(min: int, max: int) -> int:
|
||||
"""
|
||||
Gets a random number between min and max.
|
||||
|
||||
Args:
|
||||
min: The minimum number.
|
||||
max: The maximum number.
|
||||
|
||||
Returns:
|
||||
A random number between min and max.
|
||||
"""
|
||||
return random.randint(min, max)
|
||||
|
||||
|
||||
@tool
|
||||
def get_weather(city: str) -> str:
|
||||
"""
|
||||
Returns the weather forecast for a given city.
|
||||
|
||||
Args:
|
||||
city: The name of the city.
|
||||
|
||||
Returns:
|
||||
A string with a mock weather forecast.
|
||||
"""
|
||||
url = 'https://wttr.in/{}?format=+%C,+%t'.format(city)
|
||||
res = requests.get(url).text
|
||||
|
||||
return f"The weather in {city} is {res.split(',')[0]} with a high of {res.split(',')[1][:-2]} degrees Celsius."
|
||||
|
||||
@tool
|
||||
def get_current_time() -> str:
|
||||
"""
|
||||
This is a tool that returns the current time.
|
||||
It returns the current time as HH:MM.
|
||||
"""
|
||||
return f"The current time is {datetime.now().hour}:{datetime.now().minute}."
|
||||
|
||||
@tool
|
||||
def open_webbrowser(url: str) -> str:
|
||||
"""
|
||||
This is a tool that opens a web browser to the given website.
|
||||
If the user asks to open a website or a browser, you should use this tool.
|
||||
|
||||
Args:
|
||||
url: The url to open.
|
||||
"""
|
||||
webbrowser.open(url)
|
||||
return f"I opened {url.replace('https://', '').replace('www.', '')} in the browser."
|
||||
|
||||
‹
|
||||
def _parse_response(self, text: str) -> List[Dict[str, Any]]:
|
||||
pattern = r"<tool_call>(.*?)</tool_call>"
|
||||
matches = re.findall(pattern, text, re.DOTALL)
|
||||
if matches:
|
||||
return json.loads(matches[0])
|
||||
return text
|
||||
|
||||
def _call_tools(self, tool_calls: List[Dict[str, Any]]) -> List[str]:
|
||||
tool_responses = []
|
||||
for tool_call in tool_calls:
|
||||
if tool_call["name"] in self.toolbox:
|
||||
tool_responses.append(
|
||||
self.toolbox[tool_call["name"]](**tool_call["arguments"])
|
||||
)
|
||||
else:
|
||||
tool_responses.append(f"Tool {tool_call['name']} not found.")
|
||||
return tool_responses
|
||||
|
||||
def process(self, text: str) -> Generator[str, None, None]:
|
||||
response = self.json_code_agent.run(text, return_generated_code=True)
|
||||
# Parse and execute the tool calls
|
||||
try:
|
||||
tool_calls = self._parse_response(response)
|
||||
if tool_calls in [response, [], ""]:
|
||||
yield response
|
||||
return
|
||||
tool_responses = self._call_tools(tool_calls)
|
||||
except Exception as e:
|
||||
print("error", e)
|
||||
yield response
|
||||
return
|
||||
|
||||
agent = JsonAgent(llm_engine = llm_engine, tools=[get_current_time, open_webbrowser, get_random_number_between, get_weather])
|
||||
print("Agent initialized!")
|
||||
agent.run("What's the weather like in London?")
|
|
@ -1,26 +0,0 @@
|
|||
from agents import load_tool, CodeAgent, HfApiEngine
|
||||
from agents.prompts import ONESHOT_CODE_SYSTEM_PROMPT
|
||||
|
||||
# Import tool from Hub
|
||||
image_generation_tool = load_tool("m-ric/text-to-image", cache=False)
|
||||
|
||||
# Import tool from LangChain
|
||||
from agents.search import DuckDuckGoSearchTool
|
||||
|
||||
search_tool = DuckDuckGoSearchTool()
|
||||
|
||||
llm_engine = HfApiEngine("Qwen/Qwen2.5-Coder-32B-Instruct")
|
||||
# Initialize the agent with both tools
|
||||
agent = CodeAgent(
|
||||
tools=[image_generation_tool, search_tool],
|
||||
llm_engine=llm_engine,
|
||||
system_prompt=ONESHOT_CODE_SYSTEM_PROMPT,
|
||||
verbose=True
|
||||
)
|
||||
|
||||
# Run it!
|
||||
result = agent.run(
|
||||
"When was Llama 3 first released?"
|
||||
)
|
||||
|
||||
print(result)
|
|
@ -1,73 +0,0 @@
|
|||
import re
|
||||
import requests
|
||||
from markdownify import markdownify as md
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
from agents import (
|
||||
tool,
|
||||
CodeAgent,
|
||||
JsonAgent,
|
||||
HfApiEngine,
|
||||
ManagedAgent,
|
||||
)
|
||||
from agents.default_tools import UserInputTool
|
||||
from agents.search import DuckDuckGoSearchTool
|
||||
from agents.utils import console
|
||||
|
||||
model = "Qwen/Qwen2.5-72B-Instruct"
|
||||
|
||||
@tool
|
||||
def visit_webpage(url: str) -> str:
|
||||
"""Visits a webpage at the given URL and returns its content as a markdown string.
|
||||
|
||||
Args:
|
||||
url: The URL of the webpage to visit.
|
||||
|
||||
Returns:
|
||||
The content of the webpage converted to Markdown, or an error message if the request fails.
|
||||
"""
|
||||
try:
|
||||
# Send a GET request to the URL
|
||||
response = requests.get(url)
|
||||
response.raise_for_status() # Raise an exception for bad status codes
|
||||
|
||||
# Convert the HTML content to Markdown
|
||||
markdown_content = md(response.text).strip()
|
||||
|
||||
# Remove multiple line breaks
|
||||
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
||||
|
||||
return markdown_content
|
||||
|
||||
except RequestException as e:
|
||||
return f"Error fetching the webpage: {str(e)}"
|
||||
except Exception as e:
|
||||
return f"An unexpected error occurred: {str(e)}"
|
||||
|
||||
|
||||
llm_engine = HfApiEngine(model)
|
||||
|
||||
web_agent = CodeAgent(
|
||||
tools=[DuckDuckGoSearchTool(), visit_webpage],
|
||||
llm_engine=llm_engine,
|
||||
max_iterations=10,
|
||||
)
|
||||
|
||||
managed_web_agent = ManagedAgent(
|
||||
agent=web_agent,
|
||||
name="search",
|
||||
description="Runs web searches for you. Give it your query as an argument.",
|
||||
)
|
||||
|
||||
manager_agent = CodeAgent(
|
||||
tools=[UserInputTool()],
|
||||
llm_engine=llm_engine,
|
||||
managed_agents=[managed_web_agent],
|
||||
additional_authorized_imports=["time", "datetime"],
|
||||
)
|
||||
|
||||
with console.status(
|
||||
"Agent is running...", spinner="aesthetic"
|
||||
):
|
||||
manager_agent.run("""How many years ago was Stripe founded?
|
||||
You should ask for user input on wether the answer is correct before returning your final answer.""")
|
|
@ -1,10 +0,0 @@
|
|||
from agents.gradio_ui import GradioUI
|
||||
from agents import HfApiEngine, load_tool, CodeAgent
|
||||
|
||||
image_generation_tool = load_tool("m-ric/text-to-image")
|
||||
|
||||
llm_engine = HfApiEngine("Qwen/Qwen2.5-72B-Instruct")
|
||||
|
||||
agent = CodeAgent(tools=[image_generation_tool], llm_engine=llm_engine)
|
||||
|
||||
GradioUI(agent).run()
|
|
@ -1,6 +0,0 @@
|
|||
accelerate # used to be installed in Amazon SageMaker environment
|
||||
evaluate
|
||||
datasets==2.3.2
|
||||
schedulefree
|
||||
huggingface_hub>=0.20.0
|
||||
duckduckgo-search
|
|
@ -1,30 +0,0 @@
|
|||
from agents import OpenAIEngine, AnthropicEngine, HfApiEngine, CodeAgent
|
||||
from dotenv import load_dotenv
|
||||
|
||||
load_dotenv()
|
||||
|
||||
openai_engine = OpenAIEngine(model_name="gpt-4o")
|
||||
|
||||
agent = CodeAgent([], llm_engine=openai_engine)
|
||||
|
||||
print("\n\n##############")
|
||||
print("Running OpenAI agent:")
|
||||
agent.run("What is the 10th Fibonacci Number?")
|
||||
|
||||
|
||||
anthropic_engine = AnthropicEngine()
|
||||
|
||||
agent = CodeAgent([], llm_engine=anthropic_engine)
|
||||
|
||||
print("\n\n##############")
|
||||
print("Running Anthropic agent:")
|
||||
agent.run("What is the 10th Fibonacci Number?")
|
||||
|
||||
# Here, our token stored as HF_TOKEN environment variable has accesses 'Make calls to the serverless Inference API' and 'Read access to contents of all public gated repos you can access'
|
||||
llama_engine = HfApiEngine(model="meta-llama/Llama-3.3-70B-Instruct")
|
||||
|
||||
agent = CodeAgent([], llm_engine=llama_engine)
|
||||
|
||||
print("\n\n##############")
|
||||
print("Running Llama3.3-70B agent:")
|
||||
agent.run("What is the 10th Fibonacci Number?")
|
|
@ -14,6 +14,8 @@
|
|||
# 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.
|
||||
__version__ = "0.1.0"
|
||||
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from transformers.utils import (
|
||||
|
|
|
@ -347,7 +347,7 @@ class Tool:
|
|||
# Save tool file
|
||||
forward_source_code = inspect.getsource(self.forward)
|
||||
validate_args_are_self_contained(forward_source_code)
|
||||
tool_code = textwrap.dedent(f"""
|
||||
tool_code = f"""
|
||||
from agents import Tool
|
||||
|
||||
class {class_name}(Tool):
|
||||
|
@ -355,8 +355,7 @@ class Tool:
|
|||
description = \"\"\"{self.description}\"\"\"
|
||||
inputs = {json.dumps(self.inputs, separators=(',', ':'))}
|
||||
output_type = "{self.output_type}"
|
||||
"""
|
||||
).strip()
|
||||
""".strip()
|
||||
|
||||
def add_self_argument(source_code: str) -> str:
|
||||
"""Add 'self' as first argument to a function definition if not present."""
|
||||
|
|
|
@ -102,7 +102,7 @@ class TestDocs:
|
|||
def teardown_class(cls):
|
||||
shutil.rmtree(cls._tmpdir)
|
||||
|
||||
@pytest.mark.timeout(2)
|
||||
@pytest.mark.timeout(100)
|
||||
def test_single_doc(self, doc_path: Path):
|
||||
"""Test a single documentation file."""
|
||||
with open(doc_path, "r", encoding="utf-8") as f:
|
||||
|
|
|
@ -1,96 +0,0 @@
|
|||
# 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.
|
||||
|
||||
import ast
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest import mock, skip
|
||||
from typing import List
|
||||
|
||||
from .test_utils import slow, skip, get_launch_command, TempDirTestCase
|
||||
|
||||
# DataLoaders built from `test_samples/MRPC` for quick testing
|
||||
# Should mock `{script_name}.get_dataloaders` via:
|
||||
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)
|
||||
|
||||
EXCLUDE_EXAMPLES = [
|
||||
"cross_validation.py",
|
||||
"checkpointing.py",
|
||||
"gradient_accumulation.py",
|
||||
"local_sgd.py",
|
||||
"multi_process_metrics.py",
|
||||
"memory.py",
|
||||
"schedule_free.py",
|
||||
"tracking.py",
|
||||
"automatic_gradient_accumulation.py",
|
||||
"fsdp_with_peak_mem_tracking.py",
|
||||
"deepspeed_with_config_support.py",
|
||||
"megatron_lm_gpt_pretraining.py",
|
||||
"early_stopping.py",
|
||||
"ddp_comm_hook.py",
|
||||
"profiler.py",
|
||||
]
|
||||
|
||||
import subprocess
|
||||
|
||||
|
||||
class SubprocessCallException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def run_command(command: List[str], return_stdout=False, env=None):
|
||||
"""
|
||||
Runs `command` with `subprocess.check_output` and will potentially return the `stdout`. Will also properly capture
|
||||
if an error occured while running `command`
|
||||
"""
|
||||
# Cast every path in `command` to a string
|
||||
for i, c in enumerate(command):
|
||||
if isinstance(c, Path):
|
||||
command[i] = str(c)
|
||||
if env is None:
|
||||
env = os.environ.copy()
|
||||
try:
|
||||
output = subprocess.check_output(command, stderr=subprocess.STDOUT, env=env)
|
||||
if return_stdout:
|
||||
if hasattr(output, "decode"):
|
||||
output = output.decode("utf-8")
|
||||
return output
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise SubprocessCallException(
|
||||
f"Command `{' '.join(command)}` failed with the following error:\n\n{e.output.decode()}"
|
||||
) from e
|
||||
|
||||
|
||||
class ExamplesTests(TempDirTestCase):
|
||||
clear_on_setup = False
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
super().setUpClass()
|
||||
cls._tmpdir = tempfile.mkdtemp()
|
||||
cls.launch_args = ["python3"]
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
super().tearDownClass()
|
||||
shutil.rmtree(cls._tmpdir)
|
||||
|
||||
|
||||
def test_oneshot(self):
|
||||
testargs = ["examples/oneshot.py"]
|
||||
run_command(self.launch_args + testargs)
|
Loading…
Reference in New Issue