🤗Hugging Face Transformers Agent | by Sophia Yang | Could, 2023

Comparisons with 🦜🔗LangChain Agent

Simply two days in the past, 🤗Hugging Face launched Transformers Agent — an agent that leverages pure language to decide on a software from a curated assortment of instruments and achieve varied duties. Does it sound acquainted? Sure, it does as a result of it’s lots like 🦜🔗LangChain Instruments and Brokers. On this weblog publish, I’ll cowl what Transformers Agent is and its comparisons with 🦜🔗LangChain Agent.

You may check out the code in this colab (offered by Hugging Face).

In brief, it supplies a pure language API on prime of transformers: we outline a set of curated instruments and design an agent to interpret pure language and to make use of these instruments.

I can think about engineers at HuggingFace be like: We now have so many superb fashions hosted on HuggingFace. Can we combine these with LLMs? Can we use LLMs to resolve which mannequin to make use of, write code, run code, and generate outcomes? Primarily, no one must study all of the difficult task-specific fashions anymore. Simply give it a job, LLMs (brokers) will do all the pieces for us.

Listed below are the steps:

  • Instruction: the immediate customers present
  • Immediate: a immediate template with the precise instruction added, the place it lists a number of instruments to make use of.
  • Instruments: a curated record of transformers fashions, e.g., Flan-T5 for query answering,
  • Agent: an LLM that interprets the query, decides which instruments to make use of, and generates code to carry out the duty with the instruments.
  • Restricted Python interpreter: execute Python code.

Step 1: Instantiate an agent.

Step 1 is to instantiate an agent. An agent is simply an LLM, which will be an OpenAI mannequin, a StarCoder mannequin, or an OpenAssistant mannequin.

The OpenAI mannequin wants the OpenAI API key and the utilization shouldn’t be free. We load the StarCoder mannequin and the OpenAssistant mannequin from the HuggingFace Hub, which requires HuggingFace Hub API key and it’s free to make use of.

from transformers import HfAgent

# OpenAI
agent = OpenAiAgent(mannequin="text-davinci-003", api_key="<your_api_key>")

from transformers import OpenAiAgent
from huggingface_hub import login

# Starcoder
agent = HfAgent("")

# OpenAssistant
agent = HfAgent(url_endpoint="")

Step 2: Run the agent. is a single execution technique and selects the software for the duty robotically, e.g., choose the picture generator software to create a picture. retains the chat historical past. For instance, right here it is aware of we generated an image earlier and it might remodel a picture.

Transformers Agent continues to be experimental. It’s lots smaller scope and fewer versatile. The primary focus of Transformers Agent proper now could be for utilizing Transformer fashions and executing Python code, whereas LangChain Agent does “virtually” all the pieces. Let be break it down to check completely different elements between Transformers and LangChain Brokers:


  • 🤗Hugging Face Transfomers Agent has a tremendous record of instruments, every powered by transformer fashions. These instruments supply three important benefits: 1) Though Transformers Agent can solely work together with few instruments presently, it has the potential to speak with over 100,000 Hugging Face mannequin. It possesses full multimodal capabilities, encompassing textual content, photographs, video, audio, and paperwork.; 2) Since these fashions are purpose-built for particular duties, using them will be extra easy and yield extra correct outcomes in comparison with relying solely on LLMs. For instance, as an alternative of designing the prompts for the LLM to carry out textual content classification, we will merely deploy BART that’s designed for textual content classification; 3) These instruments unlocked capabilities that LLMs alone can’t accomplish. Take BLIP, for instance, which permits us to generate charming picture captions — a job past the scope of LLMs.
  • 🦜🔗LangChain instruments are all exterior APIs, resembling Google Search, Python REPL. Actually, LangChain helps HuggingFace Instruments by way of the load_huggingface_tool operate. LangChain can probably do lots of issues Transformers Agent can do already. However, Transformers Brokers can probably incorporate all of the LangChain instruments as properly.
  • In each circumstances, every software is only a Python file. Yow will discover the recordsdata of 🤗Hugging Face Transformers Agent instruments here and 🦜🔗LangChain instruments here. As you may see, every Python file comprises one class indicating one software.


  • 🤗Hugging Face Transformers Agent makes use of this prompt template to find out which software to make use of primarily based on the software’s description. It asks the LLM to offer an explanations and it supplies some few-shots studying examples within the immediate.
  • 🦜🔗LangChain by default makes use of the ReAct framework to find out which software to make use of primarily based on the software’s description. The ReAct framework is described on this paper. It doesn’t solely act on a choice but in addition supplies ideas and reasoning, which has similarities to the explanations Transformers Agent makes use of. As well as, 🦜🔗LangChain has 4 agent types.

Customized Agent

Making a customized agent shouldn’t be too troublesome in each circumstances:

  • See the HuggingFace Transformer Agent instance in the direction of the top of this colab.
  • See the LangChain example right here.


  • 🤗Hugging Face Transformers Agent contains “code-execution” as one of many steps after the LLM selects the instruments and generates the code. This restricts the Transformers Agent’s objective to execute Python code.
  • 🦜🔗LangChain contains “code-execution” as one in every of its instruments, which implies that executing code shouldn’t be the final step of the entire course of. This supplies much more flexibility on what the duty objective is: it might be executing Python code, or it is also one thing else like doing a Google Search and returning search outcomes.

On this weblog publish, we explored the performance of 🤗Hugging Face Transformers Brokers and in contrast it to 🦜🔗LangChain Brokers. I sit up for witnessing additional developments and developments in Transformers Agent.

. . .

By Sophia Yang on Could 12, 2023

Sophia Yang is a Senior Information Scientist. Join with me on LinkedIn, Twitter, and YouTube and be part of the DS/ML Book Club ❤️

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button