Transformers
  • 🌍GET STARTED
    • Transformers
    • Quick tour
    • Installation
  • 🌍TUTORIALS
    • Run inference with pipelines
    • Write portable code with AutoClass
    • Preprocess data
    • Fine-tune a pretrained model
    • Train with a script
    • Set up distributed training with BOINC AI Accelerate
    • Load and train adapters with BOINC AI PEFT
    • Share your model
    • Agents
    • Generation with LLMs
  • 🌍TASK GUIDES
    • 🌍NATURAL LANGUAGE PROCESSING
      • Text classification
      • Token classification
      • Question answering
      • Causal language modeling
      • Masked language modeling
      • Translation
      • Summarization
      • Multiple choice
    • 🌍AUDIO
      • Audio classification
      • Automatic speech recognition
    • 🌍COMPUTER VISION
      • Image classification
      • Semantic segmentation
      • Video classification
      • Object detection
      • Zero-shot object detection
      • Zero-shot image classification
      • Depth estimation
    • 🌍MULTIMODAL
      • Image captioning
      • Document Question Answering
      • Visual Question Answering
      • Text to speech
    • 🌍GENERATION
      • Customize the generation strategy
    • 🌍PROMPTING
      • Image tasks with IDEFICS
  • 🌍DEVELOPER GUIDES
    • Use fast tokenizers from BOINC AI Tokenizers
    • Run inference with multilingual models
    • Use model-specific APIs
    • Share a custom model
    • Templates for chat models
    • Run training on Amazon SageMaker
    • Export to ONNX
    • Export to TFLite
    • Export to TorchScript
    • Benchmarks
    • Notebooks with examples
    • Community resources
    • Custom Tools and Prompts
    • Troubleshoot
  • 🌍PERFORMANCE AND SCALABILITY
    • Overview
    • 🌍EFFICIENT TRAINING TECHNIQUES
      • Methods and tools for efficient training on a single GPU
      • Multiple GPUs and parallelism
      • Efficient training on CPU
      • Distributed CPU training
      • Training on TPUs
      • Training on TPU with TensorFlow
      • Training on Specialized Hardware
      • Custom hardware for training
      • Hyperparameter Search using Trainer API
    • 🌍OPTIMIZING INFERENCE
      • Inference on CPU
      • Inference on one GPU
      • Inference on many GPUs
      • Inference on Specialized Hardware
    • Instantiating a big model
    • Troubleshooting
    • XLA Integration for TensorFlow Models
    • Optimize inference using `torch.compile()`
  • 🌍CONTRIBUTE
    • How to contribute to transformers?
    • How to add a model to BOINC AI Transformers?
    • How to convert a BOINC AI Transformers model to TensorFlow?
    • How to add a pipeline to BOINC AI Transformers?
    • Testing
    • Checks on a Pull Request
  • 🌍CONCEPTUAL GUIDES
    • Philosophy
    • Glossary
    • What BOINC AI Transformers can do
    • How BOINC AI Transformers solve tasks
    • The Transformer model family
    • Summary of the tokenizers
    • Attention mechanisms
    • Padding and truncation
    • BERTology
    • Perplexity of fixed-length models
    • Pipelines for webserver inference
    • Model training anatomy
  • 🌍API
    • 🌍MAIN CLASSES
      • Agents and Tools
      • 🌍Auto Classes
        • Extending the Auto Classes
        • AutoConfig
        • AutoTokenizer
        • AutoFeatureExtractor
        • AutoImageProcessor
        • AutoProcessor
        • Generic model classes
          • AutoModel
          • TFAutoModel
          • FlaxAutoModel
        • Generic pretraining classes
          • AutoModelForPreTraining
          • TFAutoModelForPreTraining
          • FlaxAutoModelForPreTraining
        • Natural Language Processing
          • AutoModelForCausalLM
          • TFAutoModelForCausalLM
          • FlaxAutoModelForCausalLM
          • AutoModelForMaskedLM
          • TFAutoModelForMaskedLM
          • FlaxAutoModelForMaskedLM
          • AutoModelForMaskGenerationge
          • TFAutoModelForMaskGeneration
          • AutoModelForSeq2SeqLM
          • TFAutoModelForSeq2SeqLM
          • FlaxAutoModelForSeq2SeqLM
          • AutoModelForSequenceClassification
          • TFAutoModelForSequenceClassification
          • FlaxAutoModelForSequenceClassification
          • AutoModelForMultipleChoice
          • TFAutoModelForMultipleChoice
          • FlaxAutoModelForMultipleChoice
          • AutoModelForNextSentencePrediction
          • TFAutoModelForNextSentencePrediction
          • FlaxAutoModelForNextSentencePrediction
          • AutoModelForTokenClassification
          • TFAutoModelForTokenClassification
          • FlaxAutoModelForTokenClassification
          • AutoModelForQuestionAnswering
          • TFAutoModelForQuestionAnswering
          • FlaxAutoModelForQuestionAnswering
          • AutoModelForTextEncoding
          • TFAutoModelForTextEncoding
        • Computer vision
          • AutoModelForDepthEstimation
          • AutoModelForImageClassification
          • TFAutoModelForImageClassification
          • FlaxAutoModelForImageClassification
          • AutoModelForVideoClassification
          • AutoModelForMaskedImageModeling
          • TFAutoModelForMaskedImageModeling
          • AutoModelForObjectDetection
          • AutoModelForImageSegmentation
          • AutoModelForImageToImage
          • AutoModelForSemanticSegmentation
          • TFAutoModelForSemanticSegmentation
          • AutoModelForInstanceSegmentation
          • AutoModelForUniversalSegmentation
          • AutoModelForZeroShotImageClassification
          • TFAutoModelForZeroShotImageClassification
          • AutoModelForZeroShotObjectDetection
        • Audio
          • AutoModelForAudioClassification
          • AutoModelForAudioFrameClassification
          • TFAutoModelForAudioFrameClassification
          • AutoModelForCTC
          • AutoModelForSpeechSeq2Seq
          • TFAutoModelForSpeechSeq2Seq
          • FlaxAutoModelForSpeechSeq2Seq
          • AutoModelForAudioXVector
          • AutoModelForTextToSpectrogram
          • AutoModelForTextToWaveform
        • Multimodal
          • AutoModelForTableQuestionAnswering
          • TFAutoModelForTableQuestionAnswering
          • AutoModelForDocumentQuestionAnswering
          • TFAutoModelForDocumentQuestionAnswering
          • AutoModelForVisualQuestionAnswering
          • AutoModelForVision2Seq
          • TFAutoModelForVision2Seq
          • FlaxAutoModelForVision2Seq
      • Callbacks
      • Configuration
      • Data Collator
      • Keras callbacks
      • Logging
      • Models
      • Text Generation
      • ONNX
      • Optimization
      • Model outputs
      • Pipelines
      • Processors
      • Quantization
      • Tokenizer
      • Trainer
      • DeepSpeed Integration
      • Feature Extractor
      • Image Processor
    • 🌍MODELS
      • 🌍TEXT MODELS
        • ALBERT
        • BART
        • BARThez
        • BARTpho
        • BERT
        • BertGeneration
        • BertJapanese
        • Bertweet
        • BigBird
        • BigBirdPegasus
        • BioGpt
        • Blenderbot
        • Blenderbot Small
        • BLOOM
        • BORT
        • ByT5
        • CamemBERT
        • CANINE
        • CodeGen
        • CodeLlama
        • ConvBERT
        • CPM
        • CPMANT
        • CTRL
        • DeBERTa
        • DeBERTa-v2
        • DialoGPT
        • DistilBERT
        • DPR
        • ELECTRA
        • Encoder Decoder Models
        • ERNIE
        • ErnieM
        • ESM
        • Falcon
        • FLAN-T5
        • FLAN-UL2
        • FlauBERT
        • FNet
        • FSMT
        • Funnel Transformer
        • GPT
        • GPT Neo
        • GPT NeoX
        • GPT NeoX Japanese
        • GPT-J
        • GPT2
        • GPTBigCode
        • GPTSAN Japanese
        • GPTSw3
        • HerBERT
        • I-BERT
        • Jukebox
        • LED
        • LLaMA
        • LLama2
        • Longformer
        • LongT5
        • LUKE
        • M2M100
        • MarianMT
        • MarkupLM
        • MBart and MBart-50
        • MEGA
        • MegatronBERT
        • MegatronGPT2
        • Mistral
        • mLUKE
        • MobileBERT
        • MPNet
        • MPT
        • MRA
        • MT5
        • MVP
        • NEZHA
        • NLLB
        • NLLB-MoE
        • Nyströmformer
        • Open-Llama
        • OPT
        • Pegasus
        • PEGASUS-X
        • Persimmon
        • PhoBERT
        • PLBart
        • ProphetNet
        • QDQBert
        • RAG
        • REALM
        • Reformer
        • RemBERT
        • RetriBERT
        • RoBERTa
        • RoBERTa-PreLayerNorm
        • RoCBert
        • RoFormer
        • RWKV
        • Splinter
        • SqueezeBERT
        • SwitchTransformers
        • T5
        • T5v1.1
        • TAPEX
        • Transformer XL
        • UL2
        • UMT5
        • X-MOD
        • XGLM
        • XLM
        • XLM-ProphetNet
        • XLM-RoBERTa
        • XLM-RoBERTa-XL
        • XLM-V
        • XLNet
        • YOSO
      • 🌍VISION MODELS
        • BEiT
        • BiT
        • Conditional DETR
        • ConvNeXT
        • ConvNeXTV2
        • CvT
        • Deformable DETR
        • DeiT
        • DETA
        • DETR
        • DiNAT
        • DINO V2
        • DiT
        • DPT
        • EfficientFormer
        • EfficientNet
        • FocalNet
        • GLPN
        • ImageGPT
        • LeViT
        • Mask2Former
        • MaskFormer
        • MobileNetV1
        • MobileNetV2
        • MobileViT
        • MobileViTV2
        • NAT
        • PoolFormer
        • Pyramid Vision Transformer (PVT)
        • RegNet
        • ResNet
        • SegFormer
        • SwiftFormer
        • Swin Transformer
        • Swin Transformer V2
        • Swin2SR
        • Table Transformer
        • TimeSformer
        • UperNet
        • VAN
        • VideoMAE
        • Vision Transformer (ViT)
        • ViT Hybrid
        • ViTDet
        • ViTMAE
        • ViTMatte
        • ViTMSN
        • ViViT
        • YOLOS
      • 🌍AUDIO MODELS
        • Audio Spectrogram Transformer
        • Bark
        • CLAP
        • EnCodec
        • Hubert
        • MCTCT
        • MMS
        • MusicGen
        • Pop2Piano
        • SEW
        • SEW-D
        • Speech2Text
        • Speech2Text2
        • SpeechT5
        • UniSpeech
        • UniSpeech-SAT
        • VITS
        • Wav2Vec2
        • Wav2Vec2-Conformer
        • Wav2Vec2Phoneme
        • WavLM
        • Whisper
        • XLS-R
        • XLSR-Wav2Vec2
      • 🌍MULTIMODAL MODELS
        • ALIGN
        • AltCLIP
        • BLIP
        • BLIP-2
        • BridgeTower
        • BROS
        • Chinese-CLIP
        • CLIP
        • CLIPSeg
        • Data2Vec
        • DePlot
        • Donut
        • FLAVA
        • GIT
        • GroupViT
        • IDEFICS
        • InstructBLIP
        • LayoutLM
        • LayoutLMV2
        • LayoutLMV3
        • LayoutXLM
        • LiLT
        • LXMERT
        • MatCha
        • MGP-STR
        • Nougat
        • OneFormer
        • OWL-ViT
        • Perceiver
        • Pix2Struct
        • Segment Anything
        • Speech Encoder Decoder Models
        • TAPAS
        • TrOCR
        • TVLT
        • ViLT
        • Vision Encoder Decoder Models
        • Vision Text Dual Encoder
        • VisualBERT
        • X-CLIP
      • 🌍REINFORCEMENT LEARNING MODELS
        • Decision Transformer
        • Trajectory Transformer
      • 🌍TIME SERIES MODELS
        • Autoformer
        • Informer
        • Time Series Transformer
      • 🌍GRAPH MODELS
        • Graphormer
  • 🌍INTERNAL HELPERS
    • Custom Layers and Utilities
    • Utilities for pipelines
    • Utilities for Tokenizers
    • Utilities for Trainer
    • Utilities for Generation
    • Utilities for Image Processors
    • Utilities for Audio processing
    • General Utilities
    • Utilities for Time Series
Powered by GitBook
On this page
  • Agents & Tools
  • Agents
  • Tools
  • Agent Types
  1. API
  2. MAIN CLASSES

Agents and Tools

PreviousMAIN CLASSESNextAuto Classes

Last updated 1 year ago

Agents & Tools

Transformers Agents is an experimental API which is subject to change at any time. Results returned by the agents can vary as the APIs or underlying models are prone to change.

To learn more about agents and tools make sure to read the . This page contains the API docs for the underlying classes.

Agents

We provide three types of agents: uses inference endpoints for opensource models, uses a model of your choice locally and uses OpenAI closed models.

HfAgent

class transformers.HfAgent

( url_endpointtoken = Nonechat_prompt_template = Nonerun_prompt_template = Noneadditional_tools = None )

Parameters

  • url_endpoint (str) — The name of the url endpoint to use.

  • token (str, optional) — The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • chat_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the chat method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named chat_prompt_template.txt in this repo in this case.

  • run_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the run method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named run_prompt_template.txt in this repo in this case.

  • additional_tools (, list of tools or dictionary with tool values, optional) — Any additional tools to include on top of the default ones. If you pass along a tool with the same name as one of the default tools, that default tool will be overridden.

Agent that uses an inference endpoint to generate code.

Example:

Copied

from transformers import HfAgent

agent = HfAgent("https://api-inference.boincai.com/models/bigcode/starcoder")
agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!")

LocalAgent

class transformers.LocalAgent

( modeltokenizerchat_prompt_template = Nonerun_prompt_template = Noneadditional_tools = None )

Parameters

  • chat_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the chat method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named chat_prompt_template.txt in this repo in this case.

  • run_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the run method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named run_prompt_template.txt in this repo in this case.

Agent that uses a local model and tokenizer to generate code.

Example:

Copied

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, LocalAgent

checkpoint = "bigcode/starcoder"
model = AutoModelForCausalLM.from_pretrained(checkpoint, device_map="auto", torch_dtype=torch.bfloat16)
tokenizer = AutoTokenizer.from_pretrained(checkpoint)

agent = LocalAgent(model, tokenizer)
agent.run("Draw me a picture of rivers and lakes.")

from_pretrained

( pretrained_model_name_or_path**kwargs )

Parameters

  • pretrained_model_name_or_path (str or os.PathLike) — The name of a repo on the Hub or a local path to a folder containing both model and tokenizer.

Convenience method to build a LocalAgent from a pretrained checkpoint.

Example:

Copied

import torch
from transformers import LocalAgent

agent = LocalAgent.from_pretrained("bigcode/starcoder", device_map="auto", torch_dtype=torch.bfloat16)
agent.run("Draw me a picture of rivers and lakes.")

OpenAiAgent

class transformers.OpenAiAgent

( model = 'text-davinci-003'api_key = Nonechat_prompt_template = Nonerun_prompt_template = Noneadditional_tools = None )

Parameters

  • model (str, optional, defaults to "text-davinci-003") — The name of the OpenAI model to use.

  • api_key (str, optional) — The API key to use. If unset, will look for the environment variable "OPENAI_API_KEY".

  • chat_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the chat method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named chat_prompt_template.txt in this repo in this case.

  • run_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the run method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named run_prompt_template.txt in this repo in this case.

Agent that uses the openai API to generate code.

The openAI models are used in generation mode, so even for the chat() API, it’s better to use models like "text-davinci-003" over the chat-GPT variant. Proper support for chat-GPT models will come in a next version.

Example:

Copied

from transformers import OpenAiAgent

agent = OpenAiAgent(model="text-davinci-003", api_key=xxx)
agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!")

AzureOpenAiAgent

class transformers.AzureOpenAiAgent

( deployment_idapi_key = Noneresource_name = Noneapi_version = '2022-12-01'is_chat_model = Nonechat_prompt_template = Nonerun_prompt_template = Noneadditional_tools = None )

Expand 8 parameters

Parameters

  • deployment_id (str) — The name of the deployed Azure openAI model to use.

  • api_key (str, optional) — The API key to use. If unset, will look for the environment variable "AZURE_OPENAI_API_KEY".

  • resource_name (str, optional) — The name of your Azure OpenAI Resource. If unset, will look for the environment variable "AZURE_OPENAI_RESOURCE_NAME".

  • api_version (str, optional, default to "2022-12-01") — The API version to use for this agent.

  • is_chat_mode (bool, optional) — Whether you are using a completion model or a chat model (see note above, chat models won’t be as efficient). Will default to gpt being in the deployment_id or not.

  • chat_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the chat method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named chat_prompt_template.txt in this repo in this case.

  • run_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the run method. Can be the actual prompt template or a repo ID (on the BOINC AIHub). The prompt should be in a file named run_prompt_template.txt in this repo in this case.

The openAI models are used in generation mode, so even for the chat() API, it’s better to use models like "text-davinci-003" over the chat-GPT variant. Proper support for chat-GPT models will come in a next version.

Example:

Copied

from transformers import AzureOpenAiAgent

agent = AzureAiAgent(deployment_id="Davinci-003", api_key=xxx, resource_name=yyy)
agent.run("Is the following `text` (in Spanish) positive or negative?", text="¡Este es un API muy agradable!")

Agent

class transformers.Agent

( chat_prompt_template = Nonerun_prompt_template = Noneadditional_tools = None )

Parameters

  • chat_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the chat method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named chat_prompt_template.txt in this repo in this case.

  • run_prompt_template (str, optional) — Pass along your own prompt if you want to override the default template for the run method. Can be the actual prompt template or a repo ID (on the BOINC AI Hub). The prompt should be in a file named run_prompt_template.txt in this repo in this case.

Base class for all agents which contains the main API methods.

chat

( taskreturn_code = Falseremote = False**kwargs )

Parameters

  • task (str) — The task to perform

  • return_code (bool, optional, defaults to False) — Whether to just return code and not evaluate it.

  • remote (bool, optional, defaults to False) — Whether or not to use remote tools (inference endpoints) instead of local ones.

  • kwargs (additional keyword arguments, optional) — Any keyword argument to send to the agent when evaluating the code.

Sends a new request to the agent in a chat. Will use the previous ones in its history.

Example:

Copied

from transformers import HfAgent

agent = HfAgent("https://api-inference.boincai.com/models/bigcode/starcoder")
agent.chat("Draw me a picture of rivers and lakes")

agent.chat("Transform the picture so that there is a rock in there")

run

( taskreturn_code = Falseremote = False**kwargs )

Parameters

  • task (str) — The task to perform

  • return_code (bool, optional, defaults to False) — Whether to just return code and not evaluate it.

  • remote (bool, optional, defaults to False) — Whether or not to use remote tools (inference endpoints) instead of local ones.

  • kwargs (additional keyword arguments, optional) — Any keyword argument to send to the agent when evaluating the code.

Sends a request to the agent.

Example:

Copied

from transformers import HfAgent

agent = HfAgent("https://api-inference.boincai.com/models/bigcode/starcoder")
agent.run("Draw me a picture of rivers and lakes")

prepare_for_new_chat

( )

Tools

load_tool

transformers.load_tool

( task_or_repo_idmodel_repo_id = Noneremote = Falsetoken = None**kwargs )

Expand 5 parameters

Parameters

  • task_or_repo_id (str) — The task for which to load the tool or a repo ID of a tool on the Hub. Tasks implemented in Transformers are:

    • "document-question-answering"

    • "image-captioning"

    • "image-question-answering"

    • "image-segmentation"

    • "speech-to-text"

    • "summarization"

    • "text-classification"

    • "text-question-answering"

    • "text-to-speech"

    • "translation"

  • model_repo_id (str, optional) — Use this argument to use a different model than the default one for the tool you selected.

  • remote (bool, optional, defaults to False) — Whether to use your tool by downloading the model or (if it is available) with an inference endpoint.

  • token (str, optional) — The token to identify you on hf.co. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • kwargs (additional keyword arguments, optional) — Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as cache_dir, revision, subfolder) will be used when downloading the files for your tool, and the others will be passed along to its init.

Main function to quickly load a tool, be it on the Hub or in the Transformers library.

Tool

class transformers.Tool

( *args**kwargs )

A base class for the functions used by the agent. Subclass this and implement the __call__ method as well as the following class attributes:

  • description (str) — A short description of what your tool does, the inputs it expects and the output(s) it will return. For instance ‘This is a tool that downloads a file from a url. It takes the url as input, and returns the text contained in the file’.

  • name (str) — A performative name that will be used for your tool in the prompt to the agent. For instance "text-classifier" or "image_generator".

  • inputs (List[str]) — The list of modalities expected for the inputs (in the same order as in the call). Modalitiies should be "text", "image" or "audio". This is only used by launch_gradio_demo or to make a nice space from your tool.

  • outputs (List[str]) — The list of modalities returned but the tool (in the same order as the return of the call method). Modalitiies should be "text", "image" or "audio". This is only used by launch_gradio_demo or to make a nice space from your tool.

from_gradio

( gradio_tool )

from_hub

( repo_id: strmodel_repo_id: typing.Optional[str] = Nonetoken: typing.Optional[str] = Noneremote: bool = False**kwargs )

Parameters

  • repo_id (str) — The name of the repo on the Hub where your tool is defined.

  • model_repo_id (str, optional) — If your tool uses a model and you want to use a different model than the default, you can pass a second repo ID or an endpoint url to this argument.

  • token (str, optional) — The token to identify you on hf.co. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • remote (bool, optional, defaults to False) — Whether to use your tool by downloading the model or (if it is available) with an inference endpoint.

  • kwargs (additional keyword arguments, optional) — Additional keyword arguments that will be split in two: all arguments relevant to the Hub (such as cache_dir, revision, subfolder) will be used when downloading the files for your tool, and the others will be passed along to its init.

Loads a tool defined on the Hub.

push_to_hub

( repo_id: strcommit_message: str = 'Upload tool'private: typing.Optional[bool] = Nonetoken: typing.Union[bool, str, NoneType] = Nonecreate_pr: bool = False )

Parameters

  • repo_id (str) — The name of the repository you want to push your tool to. It should contain your organization name when pushing to a given organization.

  • commit_message (str, optional, defaults to "Upload tool") — Message to commit while pushing.

  • private (bool, optional) — Whether or not the repository created should be private.

  • token (bool or str, optional) — The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • create_pr (bool, optional, defaults to False) — Whether or not to create a PR with the uploaded files or directly commit.

Upload the tool to the Hub.

save

( output_dir )

Parameters

  • output_dir (str) — The folder in which you want to save your tool.

Saves the relevant code files for your tool so it can be pushed to the Hub. This will copy the code of your tool in output_dir as well as autogenerate:

  • a config file named tool_config.json

  • an app.py file so that your tool can be converted to a space

  • a requirements.txt containing the names of the module used by your tool (as detected when inspecting its code)

You should only use this method to save tools that are defined in a separate module (not __main__).

setup

( )

Overwrite this method here for any operation that is expensive and needs to be executed before you start using your tool. Such as loading a big model.

PipelineTool

class transformers.PipelineTool

( model = Nonepre_processor = Nonepost_processor = Nonedevice = Nonedevice_map = Nonemodel_kwargs = Nonetoken = None**hub_kwargs )

Expand 8 parameters

Parameters

  • pre_processor (str or Any, optional) — The name of the checkpoint to use for the pre-processor, or the instantiated pre-processor (can be a tokenizer, an image processor, a feature extractor or a processor). Will default to the value of model if unset.

  • post_processor (str or Any, optional) — The name of the checkpoint to use for the post-processor, or the instantiated pre-processor (can be a tokenizer, an image processor, a feature extractor or a processor). Will default to the pre_processor if unset.

  • device (int, str or torch.device, optional) — The device on which to execute the model. Will default to any accelerator available (GPU, MPS etc…), the CPU otherwise.

  • device_map (str or dict, optional) — If passed along, will be used to instantiate the model.

  • model_kwargs (dict, optional) — Any keyword argument to send to the model instantiation.

  • token (str, optional) — The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • hub_kwargs (additional keyword arguments, optional) — Any additional keyword argument to send to the methods that will load the data from the Hub.

  • model_class (type) — The class to use to load the model in this tool.

  • default_checkpoint (str) — The default checkpoint that should be used when the user doesn’t specify one.

decode

( outputs )

Uses the post_processor to decode the model output.

encode

( raw_inputs )

Uses the pre_processor to prepare the inputs for the model.

forward

( inputs )

Sends the inputs through the model.

setup

( )

Instantiates the pre_processor, model and post_processor if necessary.

RemoteTool

class transformers.RemoteTool

( endpoint_url = Nonetoken = Nonetool_class = None )

Parameters

  • endpoint_url (str) — The url of the endpoint to use.

  • token (str, optional) — The token to use as HTTP bearer authorization for remote files. If unset, will use the token generated when running boincai-cli login (stored in ~/.boincai).

  • tool_class (type, optional) — The corresponding tool_class if this is a remote version of an existing tool. Will help determine when the output should be converted to another type (like images).

extract_outputs

( outputs )

prepare_inputs

( *args**kwargs )

Prepare the inputs received for the HTTP client sending data to the endpoint. Positional arguments will be matched with the signature of the tool_class if it was provided at instantation. Images will be encoded into bytes.

launch_gradio_demo

transformers.launch_gradio_demo

( tool_class: Tool )

Parameters

  • tool_class (type) — The class of the tool for which to launch the demo.

Launches a gradio demo for a tool. The corresponding tool class needs to properly implement the class attributes inputs and outputs.

Agent Types

Agents can handle any type of object in-between tools; tools, being completely multimodal, can accept and return text, image, audio, video, among other types. In order to increase compatibility between tools, as well as to correctly render these returns in ipython (jupyter, colab, ipython notebooks, …), we implement wrapper classes around these types.

The wrapped objects should continue behaving as initially; a text object should still behave as a string, an image object should still behave as a PIL.Image.

These types have three specific purposes:

  • Calling to_raw on the type should return the underlying object

  • Calling to_string on the type should return the object as a string: that can be the string in case of an AgentText but will be the path of the serialized version of the object in other instances

  • Displaying it in an ipython kernel should display the object correctly

AgentText

class transformers.tools.agent_types.AgentText

( value )

Text type returned by the agent. Behaves as a string.

AgentImage

class transformers.tools.agent_types.AgentImage

( value )

Image type returned by the agent. Behaves as a PIL.Image.

to_raw

( )

Returns the “raw” version of that object. In the case of an AgentImage, it is a PIL.Image.

to_string

( )

Returns the stringified version of that object. In the case of an AgentImage, it is a path to the serialized version of the image.

AgentAudio

class transformers.tools.agent_types.AgentAudio

( valuesamplerate = 16000 )

Audio type returned by the agent.

to_raw

( )

Returns the “raw” version of that object. It is a torch.Tensor object.

to_string

( )

Returns the stringified version of that object. In the case of an AgentAudio, it is a path to the serialized version of the audio.

model () — The model to use for the agent.

tokenizer () — The tokenizer to use for the agent.

additional_tools (, list of tools or dictionary with tool values, optional) — Any additional tools to include on top of the default ones. If you pass along a tool with the same name as one of the default tools, that default tool will be overridden.

kwargs (Dict[str, Any], optional) — Keyword arguments passed along to .

additional_tools (, list of tools or dictionary with tool values, optional) — Any additional tools to include on top of the default ones. If you pass along a tool with the same name as one of the default tools, that default tool will be overridden.

additional_tools (, list of tools or dictionary with tool values, optional) — Any additional tools to include on top of the default ones. If you pass along a tool with the same name as one of the default tools, that default tool will be overridden.

Agent that uses Azure OpenAI to generate code. See the to learn how to deploy an openAI model on Azure

additional_tools (, list of tools or dictionary with tool values, optional) — Any additional tools to include on top of the default ones. If you pass along a tool with the same name as one of the default tools, that default tool will be overridden.

Clears the history of prior calls to .

You can also override the method if your tool as an expensive operation to perform before being usable (such as loading a model). will be called the first time you use your tool, but not at instantiation.

Creates a from a gradio tool.

model (str or , optional) — The name of the checkpoint to use for the model, or the instantiated model. If unset, will default to the value of the class attribute default_checkpoint.

A tailored towards Transformer models. On top of the class attributes of the base class , you will need to specify:

pre_processor_class (type, optional, defaults to ) — The class to use to load the pre-processor

post_processor_class (type, optional, defaults to ) — The class to use to load the post-processor (when different from the pre-processor).

A that will make requests to an inference endpoint.

You can override this method in your custom class of to apply some custom post-processing of the outputs of the endpoint.

You can override this method in your custom class of .

🌍
🌍
introductory guide
HfAgent
LocalAgent
OpenAiAgent
<source>
Tool
<source>
PreTrainedModel
PreTrainedTokenizer
Tool
<source>
from_pretrained()
<source>
Tool
<source>
Tool
official documentation
<source>
Tool
<source>
<source>
<source>
chat()
<source>
<source>
setup()
setup()
<source>
Tool
<source>
<source>
<source>
<source>
<source>
PreTrainedModel
Tool
Tool
AutoProcessor
AutoProcessor
<source>
<source>
<source>
<source>
<source>
Tool
<source>
RemoteTool
<source>
RemoteTool
<source>
<source>
<source>
<source>
<source>
<source>
<source>
<source>