All papersFoundations Classic
A model that teaches itself when to use tools Paper: Toolformer: Language Models Can Teach Themselves to Use Tools
Timo Schick, Jane Dwivedi-Yu, Roberto Dessì, Roberta Raileanu, Maria Lomeli, Luke Zettlemoyer, Nicola Cancedda, Thomas Scialom · arXiv · Posted 9 Feb 2023 · 16 min read · arXiv 2302.04761
1 The problem Show in original Language models fail at arithmetic and fact lookup . They do not know today's date. Earlier tool methods need many human labels or work for one task. Models should learn when to use a tool. 44.0 MAWPS score for Toolformer, versus 19.8 for GPT-3 (175B).
2 What they tested Show in original They gave GPT-J (6.7B) five tools: calculator, Q&A, search, translator and calendar. The model wrote its own tool calls in text. Only calls that helped predict the next words were kept. They tested math, facts, languages and dates. 3 What they found Show in original Toolformer beat models up to 25 times its size on many tasks. It kept its language skill : perplexity did not rise. Tool use only appears at about 775M parameters. It cannot chain tools or browse results. In a chart With the calculator, Toolformer beats GPT-3 on math (ASDiv and SVAMP) From Table 4 GPT-J Toolformer (disabled) Toolformer GPT-3 (175B)
0 10 20 30 40 50 Score (%) ASDiv 7.5% 14.8% 40.4% 14.0% SVAMP 5.2% 6.3% 29.4% 10.0%
Hover, tap or focus a bar to read its exact value.
Show as table Dataset GPT-J (%) Toolformer (disabled) (%) Toolformer (%) GPT-3 (175B) (%) ASDiv 7.5 14.8 40.4 14.0 SVAMP 5.2 6.3 29.4 10.0
5 Watch out Show in original It cannot chain tools or use results from one tool in another. It is sensitive to wording when deciding to call. It ignores the cost of a call. Small models gain nothing: tools help at about 775M parameters and up. Jump to A The abstract1 Introduction2 Approach3 Tools4.1 Experimental setup4.2 Downstream tasks and decoding4.2.1 LAMA4.2.2 Math datasets4.2.3 Question answering4.2.4 Multilingual question answering4.2.5 Temporal datasets4.3 Language modeling4.4 Scaling laws5 Analysis6 Related work7 Limitations8 ConclusionApp A Appendix A: API detailsApp B Appendix B: training detailsApp C Appendix C: zero-shot promptsApp D Appendix D: DATESETW Words to knowEvery detail from the paper Cards follow the paper's own order. Dotted words link to Words to know. Numbers match the paper.
A The abstract Show in original The problem Language models solve new tasks from a few examples or instructions. Yet they fail at basic things, such as arithmetic and fact lookup. Much simpler and smaller models do these well. The idea Toolformer teaches itself to call tools through simple APIs .It decides which API to call, when, and with what arguments. It also decides how to use the result. This is self-supervised . It needs only a handful of demonstrations per API. The result Tools: a calculator, a Q&A system, a search engine, a translator and a calendar. Zero-shot results improve a lot, often matching much larger models. Core language modeling does not suffer. 1 Introduction Show in original Limits of language models Large models are strong at zero-shot and few-shot tasks. But scaling alone only partly fixes some limits. They lack up-to-date information on recent events. They tend to hallucinate facts. They struggle with low-resource languages. They lack skill at exact math. They do not sense the passing of time. The obvious fix Give models tools: search engines, calculators and calendars. Existing ways need large amounts of human annotation. Or they work only for one task, which limits wide use. Two requirements Tool use must be learned in a self-supervised way, with few human labels. Humans may find different things useful than the model does. The model must keep its generality. It must decide for itself when and how to use each tool. The approach Give a model a handful of examples of how an API is used. It annotates a huge text set with possible API calls. A self-supervised loss keeps only the calls that truly help predict later tokens. Then the model is finetuned on the kept calls. Same data, same skills The same text set can be the original pretraining data. So the model keeps its generality and language skill. The base is GPT-J, with 6.7B parameters. It clearly beats much larger GPT-3 on many zero-shot tasks. 2 Approach Show in original The goal Goal: let a model M use tools through API calls. Inputs and outputs of each API must be text. Special tokens mark the start and end of each call. How a call looks A call is a pair: the API name and its input. Format without result: start token, name, input in brackets, end token. Format with result: the same, plus an arrow and the result. In practice the tokens are " [", "]" and "->". That avoids changing the model's word list. How Toolformer builds its training data From Section 2 and Figure 2 Step 1 Sample API calls Step 2 Execute the calls Step 3 Keep useful calls Finetune on the augmented text A call is kept only if the answer helps predict the next words.
Step 1: sampling API calls For each API, write a prompt that shows how to add calls to text. Example: add Question Answering calls to "Joe Biden was born in Scranton, Pennsylvania". The model gives each position a probability of starting a call. Keep positions above a sampling threshold. If there are more than k, keep the top k. At each position, sample up to m calls. Drop examples where the model never writes the end token. Step 2: executing API calls How a call runs depends on the API. It may call another neural network, run a Python script or search a corpus. Each response must be one text sequence. Step 3: filtering API calls The rule Use a weighted cross entropy loss over the next tokens. Weights are larger for tokens close to the call. Loss A: the loss with the call and its result as a prefix. Loss B: the lower of two losses: no call, or a call with no result. Keep a call if loss B minus loss A is at least a threshold. So the result must lower the loss by that much. Why prefix The result goes in as a prefix, not in the middle. The model is not yet finetuned on calls. Inserting mid-text would break the flow and raise perplexity. Finetuning and inference Finetuning Merge the kept calls for all tools into the text. The augmented text holds the exact same text as the original, plus calls. Finetune with the normal language modeling goal. Calls sit exactly where they help. So the model learns when and how to use tools. Using Toolformer at inference From Section 2 Decode normally Model writes the arrow Pause and call the API Insert result and end token Continue decoding Decoding stops when the model writes the arrow token.
4.1 Experimental setup Show in original Three questions Zero-shot tests where at least one tool should help. Check that language modeling skill does not drop (Section 4.3). Test the effect of model size (Section 4.4). Data generation Text set: a subset of CCNet. Language model: GPT-J. Heuristics select texts where calls are likelier to help. Example: calculator texts must have at least three numbers. They also drop examples where all calls were filtered out. API Threshold 0.5 Threshold 1.0 Threshold 2.0 Question Answering 51,987 18,526 5,135 Wikipedia Search 207,241 60,974 13,944 Calculator 3,680 994 138 Calendar 61,811 20,587 3,007 Machine Translation 3,156 1,034 229
Table 2: examples with API calls for different filter thresholds
Weights and training Weights fall as tokens get farther from the call, so calls sit near their use. Thresholds are set per tool to give enough examples. Finetune with batch size 128 and learning rate 1e-5. Use linear warmup for the first 10% of training. Baselines GPT-J: plain, no finetuning.GPT-J + CC: finetuned on the text set with no calls.Toolformer: finetuned on the text set with calls.Toolformer (disabled): the same, with calls turned off in decoding.Also OPT (66B ) and GPT-3 (175B ), about 10 and 25 times bigger. 4.2 Downstream tasks and decoding Show in original Zero-shot All tests are prompted zero-shot: no in-context examples. Earlier tool work gave dataset-specific examples. Zero-shot is harder, but it matches users who do not say which tool to use. Decoding change Greedy decoding, with one change. The model starts a call when the call token is among the k most likely. Plain greedy is k = 1 . They use k = 10 to make tool use likelier. At most one call per input, so the model cannot loop. 4.2.1 LAMA Show in original The task Complete a short statement with a missing fact, such as a date or place. They use the SQuAD, Google-RE and T-REx subsets. They keep examples where the blank is last. A correct word in the first five words counts. The Wikipedia Search tool is off, to be fair. Model SQuAD Google-RE T-REx GPT-J 17.8 4.9 31.9 GPT-J + CC 19.2 5.6 33.2 Toolformer (disabled) 22.1 6.3 34.9 Toolformer 33.8 11.5 53.5 OPT (66B) 21.6 2.9 30.1 GPT-3 (175B) 26.8 7.0 39.8
Table 3: LAMA subsets
Toolformer beats models up to 25 times its size on LAMA From Table 3 GPT-J Toolformer OPT (66B) GPT-3 (175B)
0 20 40 60 Score on LAMA subset (%) SQuAD 17.8% 33.8% 21.6% 26.8% Google-RE 4.9% 11.5% 2.9% 7.0% T-REx 31.9% 53.5% 30.1% 39.8%
Hover, tap or focus a bar to read its exact value.
Show as table Subset GPT-J (%) Toolformer (%) OPT (66B) (%) GPT-3 (175B) (%) SQuAD 17.8 33.8 21.6 26.8 Google-RE 4.9 11.5 2.9 7.0 T-REx 31.9 53.5 30.1 39.8
Result Toolformer beats the best baseline by 11.7 , 5.2 and 18.6 points. It beats OPT and GPT-3 despite being much smaller. It asks the Q&A tool in 98.1% of cases. It uses another tool in 0.7% and none in 1.2% . 4.2.2 Math datasets Show in original The task Tests: ASDiv, SVAMP and MAWPS. The score takes the first number in the output. If there is an equation, it takes the number after the equals sign. Model ASDiv SVAMP MAWPS GPT-J 7.5 5.2 9.9 GPT-J + CC 9.6 5.0 9.3 Toolformer (disabled) 14.8 6.3 15.0 Toolformer 40.4 29.4 44.0 OPT (66B) 6.0 4.9 7.9 GPT-3 (175B) 14.0 10.0 19.8
Table 4: math benchmarks
Result Toolformer does better than GPT-J even with calls off. They guess it learned from many calls and their results. Turning calls on more than doubles the score on all tasks. It beats OPT and GPT-3. It asks the calculator in 97.9% of examples. 4.2.3 Question answering Show in original The task Tests: Web Questions, Natural Questions and TriviaQA. A correct answer in the first 20 words counts. The Q&A tool is off, because it was trained on Natural Questions. Model WebQS NQ TriviaQA GPT-J 18.5 12.8 43.9 GPT-J + CC 18.4 12.2 45.6 Toolformer (disabled) 18.9 12.6 46.7 Toolformer 26.3 17.7 48.8 OPT (66B) 18.6 11.4 45.7 GPT-3 (175B) 29.0 22.6 65.9
Table 5: question answering
Result Toolformer beats all GPT-J models. It uses Wikipedia search in 99.3% of cases. It still trails GPT-3 (175B). The search tool is simple and often returns poor matches. The model cannot rewrite a query or browse results. 4.2.4 Multilingual question answering Show in original The task MLQA has an English context and a question in another language. Languages: Arabic, German, Spanish, Hindi, Vietnamese and Chinese. Translating the question into English may help. A correct answer in the first 10 words counts. Model Spanish German Hindi GPT-J 15.2 16.5 1.3 GPT-J + CC 15.7 14.9 0.5 Toolformer (disabled) 19.8 11.9 1.2 Toolformer 20.6 13.5 1.4 OPT (66B) 0.3 0.1 1.1 GPT-3 (175B) 3.4 1.1 0.1 GPT-J (All En) 24.3 27.0 23.9 GPT-3 (All En) 24.7 27.2 26.1
Table 6 (part 1): MLQA
Model Vietnamese Chinese Arabic GPT-J 8.2 18.2 8.2 GPT-J + CC 8.3 13.7 4.6 Toolformer (disabled) 10.1 15.0 3.1 Toolformer 10.6 16.8 3.7 OPT (66B) 0.2 0.7 0.1 GPT-3 (175B) 1.7 17.7 0.1 GPT-J (All En) 23.3 23.1 23.6 GPT-3 (All En) 24.9 23.6 24.0
Table 6 (part 2): MLQA
The All En rows give English contexts and English questions. They are an upper bound.
Result Calls help Toolformer in every language. The translation tool is used in 63.8% to 94.9% of examples. The exception is Hindi: only 7.3% . Toolformer does not always beat plain GPT-J. Finetuning on CCNet hurts some languages, likely from data shift. Why OPT and GPT-3 look weak OPT and GPT-3 score very low. They often fail to answer in English. GPT-J saw more multilingual data, including EuroParl. With English only, GPT-3 does best, so its weakness is the multilingual setup. 4.2.5 Temporal datasets Show in original The two datasets TEMPLAMA: cloze questions on facts that change over time, 2010 to 2020.DATESET: a new set of date questions.Example: "What day of the week was it 30 days ago?". The current date is needed to answer. Model TEMPLAMA DATESET GPT-J 13.7 3.9 GPT-J + CC 12.9 2.9 Toolformer (disabled) 12.7 5.9 Toolformer 16.3 27.3 OPT (66B) 14.5 1.3 GPT-3 (175B) 15.5 0.8
Table 7: temporal datasets
Only the calendar tool lifts DATESET scores From Table 7 0 10 20 30 DATESET score (%) GPT-J 3.9% Toolformer (disabled) 5.9% Toolformer 27.3% OPT (66B) 1.3% GPT-3 (175B) 0.8%
Hover, tap or focus a bar to read its exact value.
Show as table Model DATESET (%) GPT-J 3.9 Toolformer (disabled) 5.9 Toolformer 27.3 OPT (66B) 1.3 GPT-3 (175B) 0.8
Result Toolformer wins on both. On TEMPLAMA, gains come from Wikipedia search and Q&A, not the calendar. The calendar is used in only 0.2% of TEMPLAMA examples. The best route is calendar first, then the Q&A tool. Chains are not supported. On DATESET the gain is fully from the calendar, used in 54.8% of examples. 4.3 Language modeling Show in original The check They check that finetuning on calls does not hurt perplexity. Perplexity is how surprised a model is by text. Lower is better. Datasets: WikiText and 10,000 random CCNet documents not used in training. Model WikiText CCNet GPT-J 9.9 10.6 GPT-J + CC 10.3 10.5 Toolformer (disabled) 10.3 10.5
Table 8: perplexity
Result Finetuning on CCNet helps a CCNet subset a little. It hurts WikiText a little, since GPT-J's pretraining data is closer to WikiText. Training on text with calls adds no perplexity cost with calls off. Perplexity with calls on cannot be computed. It needs a sum over all possible calls. 4.4 Scaling laws Show in original Setup They apply the method to four GPT-2 models: 124M , 355M , 775M and 1.6B parameters. They use three tools: Q&A, calculator and Wikipedia search. Result Tool use appears only at around 775M parameters. Smaller models do the same with or without tools. The exception is Wikipedia search on Q&A tests, which is easy to use. Models improve without calls as they grow, and use calls better too. A large gap with and without calls remains even for the biggest model. 5 Analysis Show in original Decoding strategy Table 9 varies k, the number of top tokens that may start a call. Higher k means more calls. At k = 1: 40.3% and 8.5% . At k = 10: 98.1% and 100% . On T-REx, k = 1 already improves results. On WebQS, calls start only when k grows. k T-REx: All T-REx: AC / NC WebQS: All 0 34.9 none / 34.9 18.9 1 47.8 53.0 / 44.3 19.3 3 52.9 58.0 / 29.0 26.3 10 53.5 54.0 / 22.5 26.3
Table 9: results by k, as All, then API-call subset / no-call subset
AC is where the model calls an API. NC is where it does not. WebQS AC / NC at k = 1 is 17.1 / 19.9.
Calibration At k = 1 the model seems calibrated. It calls APIs on examples it would do badly on. Its no-call scores (44.3 and 19.9 ) beat the no-call average (34.9 and 18.9 ). This calibration is lost at higher k. Data quality Table 10 shows sample calls sorted by the loss gain. High scores are usually useful calls. Low scores add nothing. Top example: Wikipedia search about the Flodden Window, score 5.49 . A calendar call scored 2.11 . A Q&A call on the Nile scored 2.08 . A calculator call on Venus scored 1.59 . Noise There are exceptions: a "Fast train success" call gave nothing useful but cut perplexity. Some noise helps. It stops the model from blindly trusting every result. 7 Limitations Show in original Chains and interaction It cannot chain tools, because calls are made independently. There are no chained examples in the finetuning data. It cannot use a tool interactively, such as browsing search results. Wording and efficiency It is sensitive to the exact wording of the input. It is sample-inefficient for some tools. More than a million documents give only a few thousand useful calculator calls. Repeating the method may help. Cost It ignores the computational cost of a call when deciding to call. 8 Conclusion Show in original Summary Toolformer learns, self-supervised, to use tools through simple API calls. It finetunes on sampled calls filtered by whether they cut perplexity. A 6.7B GPT-J gets much better zero-shot results. It beats a much larger GPT-3 on several tasks. App A Appendix A: API details Show in original Defaults Defaults: sampling threshold 0.05 and filter threshold 1.0 . Keep the top k = 5 positions. Sample up to m = 5 calls each. Rare tools For calculator and translation, few texts qualify. So sampling threshold 0.0 , k = 20 , m = 10 . Filter threshold 0.5 . A.1 Implementation 1 Question answering: Atlas-large to build the data, the larger Atlas-xxl at inference.Calculator: a Python script with +, -, * and /. Invalid equations return nothing.Heuristics: three numbers within 100 tokens, or words like "equals" before a number. Texts matching only the three-number rule keep a random 1% . A.1 Implementation 2 Calendar: the date is taken from the document URL. About 18% of documents have one.Translation: keep paragraphs with a non-English chunk between English text.Chunks are 10 tokens. fastText confidence must exceed 0.8 . Drop calls whose input comes only after the call. A.2 Prompts A.2 lists the prompt for each tool. Each asks the model to add API calls to text, with a few examples. App B Appendix B: training details Show in original Settings Up to 25k examples per API. Max sequence length 1,024 . Effective batch size 128 . Training uses DeepSpeed ZeRO-3 on 8 NVIDIA A100 40 GB GPUs with BF16. Up to 2k steps. Perplexity is checked every 500 steps on 1,000 CCNet examples. Pick the best checkpoint. App C Appendix C: zero-shot prompts Show in original Prompts LAMA and TEMPLAMA: "Please complete the following text so that it is factually correct". Math: the context and question followed by "The answer is". Question answering: prefix "Answer the following question:". MLQA: a prompt that answers in English from the paragraph. App D Appendix D: DATESET Show in original Construction They pick 500 random current dates. For each, a past or future date within a four-year range fills the templates. Example: "How many days ago was August 14, 2020?". If called, the calendar returns the presumed date. Example: "Today is Sunday, November 20, 2020". Table 11 lists the templates, including US federal holidays. Words to know API: A way for a program to call a service.Calibrated: Stated confidence matches how often the answer is right.Filtering threshold: The minimum loss gain a call needs to be kept.Hallucination: When a model states made-up facts as if true.Perplexity: A score of how surprised a model is by text. Lower is better.Self-supervised: Learning from signals the data already has, with no human labels.Zero-shot: Doing a task with instructions only, with no examples.Ask AI about this paper Stuck on a word or an idea? Ask in plain words. The answer comes from the write-up on this page.
Ask AI about this paper Explain it like I am new to AI How would a finance team use this? What are the limits?
Jump to