SlowGuess/NeurIPS_2023_OCR huggingface dataset:
Shibo Hao $^{1}$ , Tianyang Liu $^{1}$ , Zhen Wang $^{1,2}$ , Zhiting Hu $^{1}$ $^{1}$ UC San Diego,  $^{2}$ Mohamed bin Zayed University of Artificial Intelligence {s5hao, ti1040, zhw085, zhh019}@ucsd.edu
Augmenting large language models (LLMs) with external tools has emerged as a promising approach to solving complex problems. However, traditional methods, which fine-tune LLMs with tool demonstration data, can be both costly and restricted to a predefined set of tools. Recent in-context learning paradigm alleviates these issues, but the limited context length only allows for a few shots of demonstrations, leading to suboptimal understandings of the tools. Moreover, when there are numerous tools to choose from, in-context learning could completely fail to work. In this paper, we propose an alternative approach, ToolkenGPT, which combines the benefits of both sides. Our approach represents each tool as a token ("toolken") and learns an embedding for it, enabling tool calls in the same way as generating a regular word token. Once a toolken is triggered, the LLM is prompted to complete arguments for the tool to execute. ToolkenGPT offers the flexibility to plug in an arbitrary number of tools by expanding the set of toolkens on the fly. In addition, it improves tool use by allowing extensive demonstration data for learning the toolken embeddings. In diverse domains, including numerical reasoning, knowledge-based question answering, and embodied plan generation, our approach effectively augments LLMs with tools and substantially outperforms various latest baselines. ToolkenGPT demonstrates the promising ability to use relevant tools from a large tool set in complex scenarios.<sup>1</sup>
Large Language Models (LLMs) [5, 9, 62, 47] have established themselves as powerful tools for diverse real-world applications, ranging from writing assistance to automated customer support [2, 6, 14]. As these models continue to evolve, there is a growing interest in their potential to interact with the real world and enhance their functionality through integration with other tools, such as the calculator, databases, etc [50, 61, 56, 53]. The capability of these models to master and control a wide array of tools not only serves as an indicator of their intelligence, but also signals a promising path to overcome some of their fundamental weaknesses. These include updating the latest world knowledge [46], reducing their hallucinations [55, 58], and executing symbolic operations [12, 17, 48], etc. However, the rapid emergence of new tools, such as advanced software libraries, novel APIs, or domain-specific utilities [39, 36, 29], introduces additional richness and complexity to the task of tool learning for LLMs. This continuous evolution accentuates the importance of empowering LLMs with the ability to adapt and master massive new tools swiftly.
Recent advancements in LLMs have witnessed two primary lines of research approaches for tool integration with LLMs [45, 68, 53] (Table 1). The first paradigm involves fine-tuning LLMs to learn specific tools [50]. For example, there are enormous efforts to integrate the retrieval tool into
$^{1}$ Code is available at https://github.com/Ber666/ToolkenGPT
Table 1: Comparison of different tool learning paradigms. Plug-&-Play means the LLMs can be equipped and unequipped with a tool flexibly. Note that it doesn't indicate zero-shot tool learning.
Tool Learning Paradigms	Frozen LMs	Massive Tools	Plug-&-Play	Ability to Use Extensive Data
Fine-tuning [e.g., 56, 50]	×	×	×	✓
In-context learning [e.g., 69, 53, 7]	✓	×	✓	×
ToolkenGPT (Ours)	✓	✓	✓	✓
LLMs [18, 35, 58, 3] and the recent Toolformer [56] fine-tuned GPT-J to learn five tools. While this method could yield promising results, it is computationally expensive and lacks the adaptability to new tools. The second approach relies on in-context learning [69, 49, 53], where LLMs learn how to use the tool through in-context demonstrations provided in the prompt. This method allows LLMs to handle newly introduced tools and drives successful applications like LangChain [7] and ChatGPT Plugin  $^{2}$ . However, in-context learning comes with its own unique limitations. Specifically, it struggles with the inherent limitation of context length, making it impossible to demonstrate massive tools in the context. Also, mastering new tools simply via few-shot examples could be challenging. For example, even the latest models like GPT-4 face difficulties when handling unusual tools [6].
In this paper, we introduce ToolkenGPT, an alternative solution that enables LLMs to master massive tools without the need for any LLM fine-tuning, while still allowing for quick adaptation to new tools. The key idea of ToolkenGPT is to represent each tool as a new token ("toolken") to augment the vocabulary. Specifically, each tool is associated with an embedding inserted into the LLM head like a regular word token embedding. During generation, once a toolken is predicted, the LLM temporarily switches into a special mode (through prompting) to produce input arguments for the tool to execute, and inject the outputs back into the generation (see Figure 1). This approach offers an efficient way for LLMs to master tools by only learning the lightweight toolken embeddings. Consequently, ToolkenGPT combines the strengths of both fine-tuning and in-context learning paradigms while avoiding their limitations (Table 1): Compared to in-context learning that can only accommodate a small number of tools and few-shot demonstrations, ToolkenGPT allows massive tools (by simply inserting respective toolkens in the vocabulary) and can use extensive demonstration data for learning toolken embeddings; In contrast to LLM fine-tuning, the tool embeddings not only requires minimal training cost, but also provide a convenient means for plugging in arbitrary new tools on the fly by expanding the toolken vocabulary.
We demonstrate the flexibility and effectiveness of our ToolkenGPT in leveraging numerous external tools for solving a diverse set of problems, spanning from numerical reasoning to knowledge-based question answering and embodied plan generation. In complex numerical reasoning problems that involve a number of mathematical tools (numerical operations such as finding greatest common divisor), we show that ToolkenGPT can effectively utilize these tools during the reasoning process, which outperforms some of latest popular approaches, such as Chain-of-Thought [65] and ReAct [69]. For knowledge-based question answering, ToolkenGPT accommodates a substantial number of relation APIs (over 200) from the knowledge base, thereby facilitating factual predictions. Furthermore, we apply our framework to task planning for embodied agents, where an agent interacts with an environment using tools, namely the actions and objects. The findings illustrate that our method offers better grounding by learning toolken embeddings for 58 grounded actions and objects than previous in-context learning and specialized decoding methods.
Fine-tuning LLMs to use tools. Early research relied heavily on fine-tuning to augment LMs with tools. In these works, LMs were mostly fine-tuned to use one or a few tools in a specific domain. For example, the retriever has been a crucial tool for augmenting LLMs with external knowledge sources [70]. The prominent works in this line include REALM [18], RAG [35], and RETRO [3]. More recently, WebGPT [46] fine-tuned GPT-3 on human web search behaviors to learn how to use the web browser. With the advancements in LLMs, there has also been growing interest in tuning
2https://openai.com/blog/chatgpt-plugins
these models on a collection of general tools, including the QA model, calculator, translator, etc. Example works include TALM [50] and Toolformer [56]. However, LLM fine-tuning is costly and these tuned LLMs struggle to generalize to emergent or updated tools. ToolkenGPT learns lightweight toolken embeddings for new tools, without any gradient calculation for the parameters of LLMs. This enables efficient adaption to new tools and maintains a minimal GPU memory overhead for training toolken embeddings, at a cost similar to LLM inference.
In-context learning for tools. LLMs exhibit a strong in-context learning ability [5], which becomes a prevalent method to use tools by showing tool descriptions and demonstrations in context [45, 53]. Building on this idea, reasoning chains can be incorporated to tackle more complex problems [69, 32, 49]. This paradigm has given rise to popular industry products such as ChatGPT plugins and Langchain [7], along with many successful applications in important research topics. For instance, a code interpreter can effectively address the LLM's shortcomings in symbolic operations [8, 17, 21, 44, 67, 40]. Furthermore, by calling "tools" that have an effect on the virtual or physical world, the LLM is capable of guiding embodied agents to accomplish various household tasks [25, 4, 26, 59, 27]. Recent attempts to utilize LLMs as a controller to coordinate multiple neural models also achieve promising progress in multimodal reasoning tasks [57, 43]. Nevertheless, all methods based on in-context learning suffer from inferior performance in complex scenarios, where the tools are unfamiliar or numerous. One concurrent work, Li et al. [36] propose to retrieve the tools based on the text embedding of their documents, which may mitigate that issue. However, ToolkenGPT is fundamentally different from their method, in that the toolken embeddings can encode the implicit semantics of tools from extensive demonstrations, which can never be inferred from the surface text (A concrete example is shown in Figure 3). Also, note that ToolkenGPT is compatible with the recent advanced prompting techniques, e.g., Chain-of-Thought (CoT) [65], to improve the LLMs performance further.
Efficient tuning of large language models. Adapting pre-trained frozen LLMs efficiently to new tasks is an active research area, leading to a surge of interest in parameter-efficient fine-tuning (PEFT) methods [31, 38, 11, 42, 41]. The idea is to only fine-tune a small subset of parameters of the LLM while freezing most of its parameters, which bears similarity to our toolken embedding method. Which part of parameters to tune is the key to PEFT methods; for instance, Adapters [22] insert trainable layers, BitFit [71] tunes the bias parameters, prompt tuning [34, 64] appends parameters to the input embedding layer, and LoRA [23] learns low-rank matrices within specific dense layers, etc. However, existing PEFT methods have not proven suitable for efficient tool learning, and utilizing these methods on tool demonstrations may not efficiently capture the desired tool knowledge as ToolkenGPT does. To the best of our knowledge, we are the first to explore efficient tuning methods for predicting tools as tokens for tool learning of massive tools.
3 ToolkenGPT for Mastering Massive Tools
In this section, we present ToolkenGPT, which enables LLMs to learn and use massive tools for complex problem-solving without the need for heavily fine-tuning the LLM. We begin by introducing the background and notations of language modeling for tool use. Typically, LLMs model the probability of a sequence of word tokens  $s = (t_1, t_2, \dots, t_n)$  as  $P(s) = \sum_{i}^{n} P(t_i \mid t_{<i})$ , where each word token comes from the vocabulary of the LLM, i.e.  $t_i \in \mathcal{V}$  and  $t_{<i}$  denotes the partial word token sequence before  $i$ -th step. In practice, the user often sets the prefix of a sequence (referred to as the prompt) to steer LLMs to generate desired contents, e.g., answering a question. Taking a step deeper, the distribution of the next token is predicted as  $P(t_i | t_{<i}) = \text{softmax}(W_\nu \cdot h_{i-1})$ , where  $h_{i-1} \in \mathbb{R}^d$  is the last hidden state of the current context and  $W_\nu \in \mathbb{R}^{|\mathcal{V}| \times d}$  is the embedding matrix for word tokens (also known as language model head).
Given a set of useful tools  $\mathcal{T} = \{\tau_1, \tau_2, \ldots\}$ , our goal is to enable LLMs to call a subset of these tools for solving the complex problem. Our flexible formulation allows tools to play a role by either returning some results that can help LLMs with text generation (e.g. calculation) or affecting the real-world environment (e.g. robot action). To call a tool during generation, the LLM first needs to select a tool and then input the arguments. In the running examples shown in Figure1, during the answer generation process ("reasoning mode"), a math operator square is selected as the tool, and an operand 16 is generated as the argument in the "tool mode". Once the external tool receives the call, it executes the tool and returns the result 256, back to the "reasoning mode".
Figure 1: Overview of ToolkenGPT framework. Toolken embeddings are appended to the language model head like regular word tokens. In the "reasoning mode" for solving the problem, the LLM generates text as usual, except that any plugged-in toolkens are also considered for the next token generation. Once a toolken is predicted, (1) the LLM switch to the "tool mode", which provides a few demonstrations of the same tool to complete the arguments. Then, (2) the tool call is executed, and (3) the result is sent back to the text to continue the reasoning mode until the final answer is generated.
The core idea of ToolkenGPT is explicitly formulating tools as tokens (called "toolkens"). Each toolken is parameterized as a toolken embedding vector, and we denote a set of toolken embeddings as a matrix, i.e.  $W_{\tau} \in \mathbb{R}^{|\mathcal{T}| \times d}$ . Assuming we have trained toolken embeddings (to be described in Section 3.2), we first give an overview of our framework by introducing how it works in inference. As shown in Figure 1, the LLM is in the reasoning mode by default, generating the next token. Our framework allows the LLM to consider word tokens and toolkens uniformly. Specifically, the tool embedding matrix is concatenated with  $W_{\nu}$ . Therefore, the LLM predicts the next token with the probability as follows:
$$
P \left(t _ {i} \mid t _ {<   i}\right) = \operatorname {s o f t m a x} \left(\left[ W _ {\nu}; W _ {\tau} \right] \cdot h _ {i - 1}\right) \tag {1}
$$
where the next token can be either a word token or a toolken, i.e.  $t_i \in \mathcal{V} \cup \mathcal{T}$ , and  $[;]$  is the concatenation operation. As we can see, our formulation of tools as toolken embeddings naturally allows for the fast adaption of new tools by expanding the toolken embedding matrix easily.
To execute a tool, the LLM switches into the "tool mode" once its toolken is predicted as the next token (as shown in the "mode switch" in Figure 1), which aims to generate the arguments for the tool. Specifically, the LLM pauses the generation and appends the current generated context to another prompt. The prompt in tool mode consists of in-context demonstrations for the predicted tool, showing how to generate the tool arguments by quoting the tool calls in a special syntax of [tool](arguments). Then the LLM can follow the pattern in demonstrations to complete the arguments of the current tool call. Contrasting previous methods [69, 53] that fully rely on in-context learning for tool learning, our framework only leaves the easy work of completing arguments to in-context learning. Besides, there would be abundant context space for extensive demonstrations of a single specified tool. This design shares similarities with the classic divide-and-conquer methods [33, 32, 13]. Finally, the arguments are sent to the specified tool for execution, and the returned value is sent back to the text in the reasoning mode.
3.2 Learning Toolken Embeddings
Our framework keeps the original LLM parameters frozen and introduces a minimal additional training overhead with the toolken embeddings,  $W_{\tau}$ . This embedding matrix contains the only parameters to optimize, but unlike other efficient LLM tuning methods, e.g., prompt tuning [34, 64] or prefix tuning [38], it does not require the gradients flowing through the major body of LLM parameters, leading to much stable and efficient training. Therefore, the tuning of toolken embeddings
maintains nearly the same GPU memory as LLM inference. Whenever a new tool is added, the toolkit embedding can be conveniently expanded and then, subsequent training on tool demonstration data involving the new tool gradually refines its embedding. Moreover, unlike in-context learning methods that only digest a few examples as training signals, ToolkenGPT is capable of tuning toolkit embeddings from massive demonstrations.
Drawing parallels to how infants learn a new tool through demonstrations from adults [15], in this paper, we primarily focus on learning toolken embeddings with tool demonstrations, which can be either in-domain training data or synthetic data generated by LLMs (see Section 4.1 and Section 4.2). We first describe the format of training data and the training objective and we use the same example from Figure 1 to showcase how it can be used for training. Specifically, "the area is 256 square feet..." can be tokenized into a word token sequence  $s = (\text{"the", "area", "is", "2", "5", "square", "feet", ...})$ . To indicate when to predict the toolkens, we need a parallel sequence mixed with word tokens and toolkens, i.e.  $s' = (\text{"the", "area", "is", "[square]", "[N/A]", "[N/A]", "square", "feet", ...})$ . The subsequence of ("2", "5", "6") in  $s$  is where the returned tool results should fill in, and we choose the corresponding first token in  $s'$  as the toolken for the tool call with the following tokens are filled with [N/A], indicating neglect in loss calculation. Thus, given a dataset composed of paired sequences  $\mathcal{D} = \{(s, s')\}$ , the training objective of ToolkenGPT is:
$$
\mathcal {L} \left(W _ {\tau}\right) = \sum_ {\left(s, s ^ {\prime}\right) \in \mathcal {D}} \sum_ {i = 1} ^ {N} - \log P \left(t _ {i} ^ {\prime} \mid t _ {<   i}\right) \mathbb {1} _ {t _ {i} ^ {\prime} \neq [ N / A ]} \tag {2}
$$
where  $P(t_i'|t_{<i})$  is defined in Eq.(1), and  $\mathbb{1}_{t_i' \neq [\mathbb{N}/\mathbb{A}]}$  is the indicator function signaling we ignore the [N/A] tokens during the training. Thus, our training process is largely consistent with the inference in the reasoning mode. That is, to call a tool, the only job for the LLM is to predict a toolken at the beginning, and then the returned value will be filled back to the text. Here, [N/A] is introduced to skip the generation of the returned value of a tool call.
There are two primary ways to get the paired data. First, some datasets provide ground truth tool calls along with natural language sequences, e.g. the facts in KB supporting the answer to a question (Section 4.2), or the calculation trace for solving a math problem (Section 4.1). To use the data for supervised learning, we preprocess them to get the paired data required for training as described in the above paragraph. Second, we explore synthesizing tool demonstrations with LLMs, sharing a similar idea to self-instruct [63]. An intuitive interpretation of this process is to distill the knowledge inside LLM to the new toolken embeddings. Specifically, we can prompt LLMs with the tool document and a few demonstrations with a special syntax indicating tool calling, e.g., The capital of U.S. is  $\langle \text{capital} \rangle$  ("U.S.") = "Washington D.C." Conditioned on that, the LLMs can generate some new use cases that utilizes the given tool and quote the tool call with the same syntax. We can then easily locate the tool calls and process the data into the paired data for training.
In this section, we apply ToolkenGPT to three distinct applications characterized by meaningful tool-use scenarios: arithmetic tools for numerical reasoning, database APIs for knowledge-based question answering, and robot actions for embodied plan generation. We focus on how methods can accurately call the tools and how successfully they can solve the tasks. Our experiments show that ToolkenGPT can efficiently master massive tools while leveraging them to solve complex problems with improved performance, consistently better than advanced prompting techniques.
4.1 Numerical Reasoning
LLMs often struggle with mathematical tasks since the models are inherently designed for probabilistic estimation rather than symbolic operations. In this section, we aim to assess the tool-learning capabilities of ToolkenGPT, compared with in-context tool learning (e.g., ReAct [69]). We first demonstrate that ToolkenGPT consistently matches or outperforms the performance of in-context learning with the availability of four basic arithmetic functions  $(+, -, \times, \div)$ . Moreover, to benchmark the tool-handling capability in more complex math problems, we include more available tools, i.e., an expanded (13) set of functions, and create a set of synthetic data. The results show that ToolkenGPT significantly outperforms baselines by training only on the synthetic data. Note that our focus is not to reach a state-of-the-art accuracy; Rather, the experiment is designed to evaluate the tool learning ability in the setting where certain tools are available.
Table 2: Results on the GSM8K-XL and FuncQA datasets. The numbers in parentheses indicate how many available tools are available. For GSM8K-XL and FuncQA one dataset, accuracy is evaluated based on an exact match (float numbers rounded to two decimals). In FuncQA multi, we allow a margin of error of  $0.1\%$  to account for potential errors at each step of multi-hop reasoning.
Method	GSM8K-XL (4)	FuncQA (13)
One-Hop	Multi-Hops
0-shot ChatGPT	0.17	0.55	0.09
CoT [65]	0.18	0.20	0.03
ReAct [69]	0.32	0.57	0.06
ToolkenGPT (Ours)	0.33	0.73	0.15
Datasets. To evaluate the tool-learning proficiency in numerical reasoning comprehensively, we curate two new test datasets: (1) GSM8K-XL, an enhanced version of the existing GSM8K [10] dataset. GSM8K is a dataset of linguistically diverse grade school math word problems, involving performing a sequence of calculations using 4 basic arithmetic operations  $(+, -, \times, \div)$  to reach the final answer. In the original GSM8K dataset, the numbers for calculations are typically small, which might be less challenging for the recent powerful LLMs [10, 6]. So in the test set, we magnify the numbers to increase the computational difficulty for LLMs, which results in the GSM8K-XL dataset, featuring 568 test cases with much larger numbers. (2) FuncQA is a synthetic dataset we created to increase the complexity of math problems involving more arithmetic tools, which serves as a much more challenging benchmark to test the model's tool-learning capabilities. Specifically, This dataset requires at least 13 operators (e.g., power, sqrt,  $1\mathrm{cm}$ ) to solve, and it is challenging for both humans and LLMs to solve without an external calculator. Furthermore, FuncQA is categorized into two subsets: 68 one-hop questions ( $\mathrm{FuncQA_{one}}$ ) solvable with just one operation, and 60 multi-hop questions ( $\mathrm{FuncQA_{multi}}$ ) requiring a few reasoning steps.
To train the toolkit embeddings used in GSM8K-XL, we preprocess the original training set of GSM8K which has the calculation annotation as described in Section 3.2. We get 6,054 examples, of which 1,000 were allocated for validation, and 5,054 for the training data. For the FuncQA dataset, we prompt ChatGPT to generate some one-hop QA patterns for each operator, and then randomly assign values to the patterns. This process yields 47 training data points and 3 validation data points for each operator, resulting in a total of 611 samples for training and 39 samples for validation.