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

2023 · NeurIPS

Toolformer: Language Models Can Teach Themselves to Use Tools

Problem

Framing

Large LMs still hallucinate facts, miss recent information, and fail at arithmetic or time-sensitive queries. The paper closes this gap by teaching the LM to insert API calls into its own text stream, then keeping only calls that lower next-token loss. A 6.7B GPT-J variant then beats larger models on several zero-shot tasks, including 40.4 on ASDiv.

Currently Used Methods

Direct antecedents

Proposed Method

Architecture

Toolformer keeps the base LM architecture fixed and augments training text with inline tool calls of the form e(c)=<API> ac(ic) </API>e(c)=\texttt{<API>}\ a_c(i_c)\ \texttt{</API>} or e(c,r)=<API> ac(ic) r </API>e(c,r)=\texttt{<API>}\ a_c(i_c)\ \to r\ \texttt{</API>}. The tool set spans question answering, Wikipedia search, calculator, machine translation, and calendar APIs. Inference runs normal decoding until the model emits \to, then executes the chosen API and resumes generation.

Pipeline figure: sample candidate API calls, execute them, keep only loss-reducing calls, then interleave accepted calls into the LM training text.

Loss / Objective

Candidate calls survive only if their returned result improves prediction of future tokens.

Li(z)=j=inwjilogpM(xjz,x1:j1)L_i(z)=-\sum_{j=i}^{n} w_{j-i}\, \log p_M(x_j\mid z, x_{1:j-1}) Li+=Li(e(ci,ri))L_i^{+}=L_i\big(e(c_i,r_i)\big) Li=min(Li(ϵ),Li(e(ci,ϵ)))L_i^{-}=\min\Big(L_i(\epsilon),\,L_i\big(e(c_i,\epsilon)\big)\Big) LiLi+τfL_i^{-}-L_i^{+} \geq \tau_f

Sampling Rule / Algorithm

The LM first proposes where a tool call should start, then samples call strings at those positions.

pi=pM(<API>P(x),x1:i1)p_i=p_M(\texttt{<API>}\mid P(x),x_{1:i-1}) I={ipi>τs}I=\{\, i \mid p_i>\tau_s \,\}

For each iIi\in I, it samples up to mm calls and keeps at most top-kk positions.

Training Procedure

Evaluation

Datasets

Metrics

Headline results

Results table

Table 2: Number of examples with API calls in CC^{\ast} for different filtering thresholds τf\tau_f

APIτf=0.5\tau_f = 0.5τf=1.0\tau_f = 1.0τf=2.0\tau_f = 2.0
Question Answering51,98718,5265,135
Wikipedia Search207,24160,97413,944
Calculator3,680994138
Calendar61,81120,5873,007
Machine Translation3,1561,034229

Ablations

Method Strengths and Weaknesses

Strengths

Weaknesses

Suggestions from the authors

Links

Prior Papers

Further Papers