ai.si
Field notes

From AI to SI

How Can a Language Model Learn When to Call a Tool?

Bottom line

Toolformer kept a call when its result helped predict the original passage beyond what the call alone supplied. A correct calculation can still fail that test, or answer a different question from the one the user asked.

Examples feed a model, its outputs pass through a check, and selected material loops back to the examples.

In a learning loop, follow the information that changes the next attempt and identify what checks it.

Given some text and candidate interfaces, one can generate many possible tool calls and retain those that, when executed, return information which helps the model complete the original text. With just a few hand-crafted examples per desired tool, Toolformer was able to auto-generate large numbers of instances of text augmented with appropriate calls. The criterion was that the returned information should reduce loss over completing the original passage. An easy-to-predict continuation can reward an irrelevant call, while a useful check may fail to reduce the loss. Schick et al., Toolformer

For example, continuing the description of stock on a warehouse shelf, "There were 18 bottles per crate. Seven crates held 126 bottles." It would be useful to have access to a calculator at some point in this text. But there are many points at which one may want to insert training examples to teach the model to use the calculator, and equally many at which one would not want to, such as after years mentioned in dates. Generating an example to call the calculator after every numerical value would be one extreme; relying entirely on human annotation of suitable places would be very laborious. So Toolformer generated examples and selected on the basis of reducing loss to complete the rest of this passage.

Providing examples of tool calls

A model can write a calculation in ordinary language without actually invoking the calculator. So initially, examples were provided of how to initiate a call, including specifying the name of the tool to be used and parameters. The process described above finds points at which there is a reasonable probability that the model would begin to make such a call. It generates a number of examples, makes the corresponding tool calls, and so gets back results. Note the difference between generating text corresponding to a tool call, and actually making that tool call and receiving a result back. Producing, say, a sum to be evaluated, does not mean that the operation was executed. Toolformer, Sections 2 and 3

So a fair amount of information is provided, other than the model reducing its loss on future predictions. There is a decision that at least one calculator is useful, code to interface with it, and examples of suitable sums. Large amounts of natural language text are assumed available, providing the rest of the passage. All the model is then used for, initially, is to generate further examples of places in the text where a tool call would be appropriate.

So for the example above there may be many sums the model generates. 18 x 7, 18+7, how many days in 1 week, and so on. These are possible requests, although a question in prose may require conversion into a calculator expression. Presumably only one would give an answer helpful in completing the next sentence. Of course, at the time of generating training instances, the remainder of the sentence or passage is available to assess this. At inference the model has no completed passage to score.

Selecting useful tool calls

So the process is to assess the loss to predict the remainder of the original passage, if the generated tool call and the result returned are given to the model. Loss is calculated with certain weights applied to various terms. A lower weighted loss means the model predicted the observed continuation tokens better under the specified weights. Loss is also assessed to predict the continuation i) with no tool call and ii) with the generated tool call, but no information returned. The minimum of these latter two losses is compared with the loss including information from the tool. Toolformer, filtering equations

As an illustration, assume losses of 2.4 with no call, 2.0 with the call alone and 1.3 with the call and its result. Then the reduction due to information from the tool would be taken as 2.0-1.3=0.7. If this needed to reach at least 0.5 (arbitrary units) to accept an instance for training, it would be included. Comparing with 2.4 would have given 1.1, over-estimating the value of the information returned by the tool, since merely generating the sum would help predict the answer. On the other hand, assume it was quite easy to predict the continuation once the model had generated the sum, giving a loss of 1.4, and including the answer reduced this only to 1.3. Then this would not pass the test with a cut-off of 0.5. There may well be nothing wrong with the answer returned by the tool. It just may not add much to the model's ability to predict the continuation. The test measures improved prediction of the original text. Correctness of the tool output would need to be checked separately.

Note that, for this test, all the information about the generated tool call and result returned is prepended to the passage. The authors feel that this is preferable, as inserting text into the middle of a passage may unduly affect a model which has not been trained on such insertions. Once instances have been selected in this way, the tool call and answer are inserted into the passage at the appropriate point for training the model. So any implementation which inserted them somewhere else when assessing loss would be performing a different experiment. Toolformer, filtering and fine-tuning

What happens when the trained model receives a new question?

So once the model is trained, it generates words as normal, until it generates code showing that it is expecting information back from a tool. Control is passed to appropriate software, which invokes the relevant API, passes the answer back to the language model, which continues generating. So there are a number of things that all have to go right for the model to give a good answer to a question requiring use of, say, a calculator. Generate an appropriate sum with the right parameters; the calculator evaluates it correctly; the language model interprets and uses the answer appropriately. Toolformer, inference procedure

If the user wanted to know how many bottles remained if one was taken from each of the 7 crates, generating 18 x 7 would give a "correct" answer back from the calculator. That would be the wrong answer to the user's question, which required 17 × 7, or 119 unopened bottles. So seeing that the model generated a sum and got back a "successful" answer of 126 only shows that the calculator worked. It would mask the error by the language model. It is therefore worth checking each stage; for example, running with and without access to tools; checking the sum generated, the answer returned by the tool, and the final answer given, including units. There is no point blaming the accuracy of answers returned by the tool if it is never invoked, or expecting a better tool to solve the problem if the units are wrong.

Problems with selecting on reduction in loss

Of course, the original text may be in error. Continuing the example, if it went on to say there were 127 bottles in 7 crates, a correct answer from the calculator would make it harder to predict this continuation. So a tool call may be rejected although it would highlight an error in the original. This is a contrived example, but it could easily happen. There is nothing in what is being optimized to encourage correcting errors.

Similarly, a call to a search engine may return words which occur later in the original passage, so reducing loss. This does not mean that they are correct or relevant to a user's question. A model very good at generating well-known facts could do very well by this measure and give wrong answers to a user. Obviously further applications of the same test would not help. Some ground truth, whether answers to questions or observable results of actions, is required.

There may also be very different times taken, and costs incurred, making calls to various tools. So a small reduction in loss may be worth incurring the small delay of an internal calculation, but not worth a long delay to access some other service. The cut-off level will determine which candidate examples are retained for training. But other considerations, such as maximum acceptable delay or cost per query, will need to be assessed by making the relevant calls under realistic conditions. It is worth analyzing the number and type of useful and unnecessary calls made, as well as calls that should have been made, and the delays and costs. An overall good figure may conceal problems with frequent queries.

Assessment of ability to use tools

The authors tested tools for calculation, question answering, search, translation and calendar information. Toolformer, tools and evaluations Each will have different types of error. A calculator will give a "correct" answer to a wrong sum. A search engine may be given an appropriate query and fail to return a suitable document. A translator may give a reasonable translation missing some significant aspect of the meaning. So good performance overall would not be sufficient to show the model can make appropriate use of each.

Similarly, being able to generate an appropriate single call is not sufficient to ensure a series of related calls can be made. For example, the user may want prices quoted in two different lists, and the percentage difference. Appropriate calls to a search engine would first be required. The model would then need to use the appropriate returned answers in generating a further sum, and be aware of differences of units, currency, and so on. So again the full range of actions required should be tested, and each step analyzed to find where an error first arises.

It would also be informative to prevent access to one or more tools. Would the model recognize that it could not complete the required calculation? Make alternative calls? Make up an answer? Obviously the process of generating training instances assumes that answers can be returned from tools, so says nothing about behavior if they are unavailable. These should be assessed separately from other types of error.

So for the original example, it would be worthwhile to change the number of crates, have various numbers of bottles removed, have other incidental numbers mentioned. Check when the model decides to invoke a calculator, and if the sum is appropriate to answer the question. So checking both making unnecessary calls and failing to make a call. As described, Toolformer can train a model to some extent to make these decisions. How well it does so for any particular set of requirements will depend on analysis of the calls made, information returned, and final answer.

References

All field notes