Search

Search papers and lessons

HuggingGPT: Solving AI Tasks with ChatGPT and its Friends in Hugging Face

Yongliang Shen, Kaitao Song, Xu Tan et al. · 2023

HuggingGPT lets a chat model plan a multi-step AI task, pick a specialist model from Hugging Face for each step, run them, and write the answer.

On this page

  • Colour = topic mix
  • Neural network basics
  • Calculus
  • Discrete math
  • Language & LLMs
  • Linear algebra
  • Probability
  • ReadReady to readNot yet

TL;DR

HuggingGPT is an agent built around a large language model, ChatGPT, that does not try to solve every AI task itself. It reads the user's request and writes a plan of sub-tasks with their dependencies. For each sub-task it picks a specialist model from the Hugging Face hub using the model's text description, runs that model, and then writes the final answer from the results. The paper's evaluation centres on the planning step, which it identifies as the main weakness of the design.

Builds on: LLM Agent, Chain-of-Thought Prompting, In-Context Learning · Leads to: Tool Use

Why it mattered

The paper proposed a simple design for AI systems that handle many kinds of input, such as text, images and audio. Instead of training one large model for every task, a language model acts as a controller. The paper's Figure 1 describes language as the interface between the controller and the models: each specialist model is described in words, and the controller routes work to them through those words. Adding a new model means adding a description, not retraining the controller.

The paper's own related-work section places it among systems that give a language model access to outside tools, such as Toolformer, which inserts calls to external APIs into text, and Visual ChatGPT, which combines image models with a chat model. HuggingGPT's contribution was to route each step to a model chosen from a public hub at run time. The agent pattern it uses is plan, then act, then answer.

The problem

A real request often has several parts. "Describe this photo, count the people in it, and read the caption aloud" needs an image captioner, an object detector and a speech model, and the second step may need the first step's output. A single language model cannot do all three. Earlier work showed language models could call tools, but the paper points out two difficulties.

The first is choosing the tools. Writing a prompt that lists all useful models with careful instructions for each one takes heavy manual effort, and the list grows without bound as new models appear. The second is dependencies. A later step may need a file produced by an earlier step, and that file does not exist until the earlier step runs. The system must therefore know the order of the steps and wire each output into the next input.

The paper's answer is to let the language model do the planning and selection, with model descriptions as the only interface, and to keep the planning output in a structured format the program can parse.

How it works

The workflow has four stages. Take the paper's own tiny request: "Can you tell me how many objects in e1.jpg?"

  1. Read the request and plan the tasks. The controller model receives the request, a list of the task types the system supports, and a few worked examples. It must answer with a list of tasks in a fixed JSON form. For this request the plan is one task, object detection, with no dependency, applied to the image file.
[{"task": "object-detection", "id": 0, "dep": [-1], "args": {"image": "e1.jpg"}}]

Check: the list has one task, and "dep": [-1] means it depends on nothing.

  1. Filter the candidate models. The system keeps only the models on the hub whose task type is object detection. Those models are ranked by number of downloads, and the top few are kept as candidates. The ranking is a proxy for popularity and quality, which the paper states directly. Check: a model for image captioning is removed at this step, because its task type does not match.

  2. Let the controller choose one model. The controller sees the request and the candidate descriptions and returns a model id with a short reason in JSON. The paper's Figure 1 example uses the object detector facebook/detr-resnet-101 for the counting part of a similar request. Check: the choice is one model id from the candidate list, not a free-form guess.

  3. Run the model and collect the result. The chosen model runs on e1.jpg and returns structured output, such as bounding boxes with confidence scores. The system sends this back to the controller. Check: the output is a list of boxes; counting the boxes gives the answer.

Now take the paper's dependent example, a request with two steps: "First generate a HED image of e3.jpg, then based on the HED image and a text 'a girl reading a book', create a new image." The plan is two tasks.

[{"task": "pose-detection", "id": 0, "dep": [-1], "args": {"image": "e3.jpg"}},
 {"task": "pose-text-to-image", "id": 1, "dep": [0], "args": {"text": "a girl reading a book", "image": "<resource>-0"}}]

Task 1 cannot start until task 0 has produced its output. The placeholder <resource>-0 marks the image input of task 1 as the output of task 0. When task 0 finishes, the system replaces the placeholder with the real file and then runs task 1.

Here is the whole flow for a request with several steps.

flowchart LR
  U["User request"] --> P["1 Task planning: LLM writes task list and dependencies"]
  P --> S["2 Model selection: LLM picks a hub model per task"]
  S --> E["3 Task execution: run independent tasks in parallel, substitute resources"]
  E --> R["4 Response generation: LLM writes the answer from structured results"]

Check: stages 2 and 3 repeat for each task in the list, not once per request.

The math

The paper is mostly about system design and is not quantitative, so this section only writes down the two rules that the system's behaviour depends on.

Let T={t1,…,tn}T = \{t_1, \dots, t_n\} be the tasks in the plan. Each task has a dependency set D(t)D(t), the ids of the tasks whose outputs it needs. The plan is a directed acyclic graph: an edge runs from each dependency to the task that uses it. A task is ready to run when every task in its dependency set has finished. In the paper's words, tasks without dependencies can run "simultaneously".

For the model choice, let MM be the set of all models on the hub, and let type(m)\mathrm{type}(m) be the task type a model serves. The candidate set for task tt is

C(t)=TopK⁡({ m∈M:type(m)=type(t) }, by downloads),C(t) = \operatorname{TopK}\bigl(\{\, m \in M : \mathrm{type}(m) = \mathrm{type}(t) \,\}, \ \text{by downloads}\bigr),

where KK is a small fixed number chosen to fit the controller's context length. The controller then chooses one m∈C(t)m \in C(t) from the descriptions in its prompt. Filtering before ranking keeps the prompt short, which is the reason the paper gives for the step.

The paper's stated reason for filtering is the context limit: a full list of models will not fit in one prompt. The paper lists the length limit among its own limitations.

Results

The paper evaluates the controller's planning, not the quality of the specialist models. It sorts requests into three types: single tasks, sequential tasks (a chain), and graph tasks (a general directed graph). It reports accuracy and F1 for single tasks, edit distance and F1 for sequential tasks, and a GPT-4 judgement score for graph tasks.

On the GPT-4 annotated set, GPT-3.5 gets 52.62 accuracy and 54.45 F1 on single tasks (Table 3). On sequential tasks its normalised edit distance is 0.54 and its F1 is 51.92 (Table 4). On graph tasks its GPT-4-judged score is 50.48 (Table 5). The open 7-billion-parameter models Alpaca-7b and Vicuna-7b score much lower on all three.

On a smaller set of 46 requests labelled by people, GPT-4 reaches 41.36 accuracy on sequential tasks and 58.33 accuracy on graph tasks (Table 6). The paper notes that even GPT-4 falls well short of the human labels, which it takes as evidence that planning is the bottleneck.

The ablation study varies the number of task types shown in the demonstrations. With 10 task types, GPT-4 reaches 67.52 accuracy on single tasks, up from 65.59 with 2 types (Table 7). The paper finds that more variety helps moderately and that gains flatten beyond about four demonstrations.

The limits the paper states are these. Planning depends entirely on the controller, so a plan is not guaranteed to be feasible or optimal. Each step needs a round trip to the controller, which costs time. The model descriptions must fit in the context window. And the controller can fail to follow the format, which breaks the program's workflow. The results come from a small set of requests and a handful of language models, so they should be read as a measurement of this design, not as a general benchmark.

Reading guide

  • Section 1, Introduction needs LLM Agent and the idea of In-Context Learning: the controller is shaped by examples in its prompt.
  • Section 2, Related Works assumes the reader knows that language models can call outside tools, the topic of Tool Use.
  • Section 3.1, Task Planning uses Chain-of-Thought Prompting in spirit: the plan is written out as structured steps before anything runs.
  • Section 3.2, Model Selection uses the descriptions of Object Detection and Image Captioning as examples of task types.
  • Section 3.3, Task Execution needs no new ideas beyond the dependency graph above.
  • Section 4, Experiments reports accuracy, F1 and edit distance; these are ordinary classification and string-matching measures, so no new maths is needed.
  • Section 5, Limitations is short and worth reading in full.

Check yourself

QuestionIn one sentence, what does the controller model do in HuggingGPT?Show answer

It plans the request as a list of tasks with dependencies, chooses a specialist model from the hub for each task using text descriptions, and writes the final answer from the results. It does not run the specialist models itself.

QuestionA plan has task 0 (object detection on e1.jpg, no dependency) and task 1 (caption the detected objects, dep [0]). In what order can they run?Show answer

Task 0 must run first, because task 1 lists it as a dependency. Task 1 can start only after task 0 produces its output, and the system substitutes that output into task 1's input before running it.

QuestionWhy does the paper rank candidate models by downloads and keep only the top few?Show answer

The controller has a maximum context length, so it cannot read the descriptions of every model on the hub in one prompt. Filtering by task type and keeping the top few by downloads shortens the prompt. Downloads are used as a rough proxy for popularity and quality, not as a measure of accuracy.

QuestionOn the GPT-4 annotated set, GPT-3.5 scores 52.62 accuracy on single tasks, and GPT-4 scores 41.36 accuracy on sequential tasks in the human-labelled set. Why should these two numbers not be compared directly?Show answer

They come from different tables and different test sets: the first is a single-task score on the GPT-4 annotated set, and the second is a sequential-task score on the 46-request human-labelled set. The task types and label sources differ, so the numbers measure different things.

Sources

Read the full paper

Yongliang Shen, Kaitao Song, Xu Tan et al., 2023. arXiv:2303.17580