Built-in Tools with Llama 3.1
In this tutorial you will learn how to use “Built-In Tools” as introduced by the Llama 3.1 family of models.
Introduction
Function calling is a feature give LLMs the ability to consider using external code functions to respond to a user query. When the LLM triggers the use of a tool, it sends back a tool message to the application with the name and the parameters of the function to be used. The backend application then uses this inforamtion to execute the function locally. OctoAI models support function calling already, as described in this documentation page.
A new type of Functions
The release of the Llama 3.1 family of models introduced the concept of “Built-in Tools”. The models have enhanced support for a set of functions by default, without extra prompting or fine-tuning. In order to support these, the model was trained using a set of special tags. Using Llama built-in tools is easy with OctoAI. These are supported through our standard tool API, so you don’t need to worry about any low level implementation details.
Let’s take a look in the next section at how to use them. Each section will contain snippets of code that you can copy and test in your environment.
Built-In Tools
These are the built-in tools available in Llama 3.1 models, with their respective code tool name:
- Brave Search:
brave_search
- Used to perform web searches.
- Wolfram Alpha:
wolfram_alpha
- Used to perform complex mathematical calculations.
- Code Interpeter:
code_interpreter
- Used to evaluate the generated Python code.
Built-in support only means that the models are better trained at triggering the use of these functions. The functions still need to be implemented locally. In the following sections we cover how you can trigger each of the Llama 3.1 Built-in tools.
Brave Search Tool
The Brave Search tool gets triggered by the model when the response benefits from a web search of a given query. We will mock the function so we can get up and running quickly.
Using the Brave Search Tool
The following snippet of code shows how to handle a chat interaction that uses the Brave Search tool:
As you can see, we don’t have to specify the parameters of the function, because this is a function with Built-in support. This also means that custom functions can not use the brave_search
identifier.
You can expect a final response similar to this:
Wolfram Alpha Tool
The Wolfram Alpha tool gets triggered by the model when the response benefits from querying the Wolfram Alpha API. Let’s mock the function so we can get up and running quickly.
Using the Wolfram Alpha Tool
The following snippet of code shows how to handle a chat interaction that uses the Wolfram Alpha tool:
Similarly to the Brave Search tool, we don’t specify the parameters. Also custom functions can not use the wolfram_alpha
identifier.
You can expect a final response similar to this:
Code Interpreter Tool
The Code Interpreter tool gets triggered by the model when the response requires executing a snippet of Python code generated by the model itself.
Using the Code Interpreter Tool
The following snippet of code shows how to handle a chat interaction that uses the Code Interpreter tool:
Similarly to the Wolfram Alpha tool, one does not require to define the parameters of the function. Also, custom functions can not use the code_interpreter
identifier.
From this request you can expect the model to generate the following code:
With our mocked function you can expect the final response to be like this:
Implementation Notes
Care must be taken in order to handle the possiblity of the model calling the code_interpreter
at any point if any of the other buil-in tools are active. This is expected behavior and your implementation needs to handle this case.
You are in charge of doing the final implementation of these functions. This provides interesting opportunities to create new and innovative experiences. Still, we will be providing examples of implementations of these functions for the default cases in our Text-Gen Cookbook repository soon.
Conclusion
In this tutorial we have seen how to use Llama 3.1’s Built-in Tools. You can easily take advantage of them using OctoAI’s convenient API, wihtout having to worry about low-level implementation or cumbersome tool definitions.
For more examples and reference designs take a look at our Text-Gen Cookbook repository in GitHub, or for more inspiration browse through our demo pages.